Print Page | Close Window

Merge two PDFs and merge outlines (bookmarks)

Printed From: Debenu Quick PDF Library - PDF SDK Community Forum
Category: For Users of the Library
Forum Name: Sample Code
Forum Description: Share Debenu Quick PDF Library sample code with other forum members
URL: http://www.quickpdf.org/forum/forum_posts.asp?TID=1824
Printed Date: 28 Apr 24 at 8:38PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Merge two PDFs and merge outlines (bookmarks)
Posted By: Rowan
Subject: Merge two PDFs and merge outlines (bookmarks)
Date Posted: 10 May 11 at 8:27AM
Here is some Delphi sample code that helps you to merge two PDF files and also merge their respective outlines / bookmarks.

var
 oldpagecount: Integer;

 function GetOutlineIndex(QPL: TQuickPDF; OutlineID: Integer): Integer;
 var
  i: Integer;
 begin
  Result := 0;
  with QPL do
  begin
    for i := 1 to OutlineCount do
    {$IF LibVerNum >= 7.23}
    if CompareOutlines(GetOutlineID(i), OutlineID) = 1 then
    begin
      Result := i;
      Exit;
    end;
    {$IFEND}
  end;
 end;

 procedure MergeOutlines(QPL: TQuickPDF; DocumentID: Integer;
  PageOldCount: Integer);
 type
  TOutline = record
    Title: AnsiString;
    Page: Integer;
    DestPage, DestType: Integer;
    Left, Top, Right, Bottom, Zoom: Double;
    ColorRed, ColorGreen, ColorBlue: Double;
    StyleBold, StyleItalic: Integer;
    JavaScript, OpenFile, WebLink: String;
    Parent: Integer;
  end;
 var
  i: Integer;
  seldoc: Integer;
  outlineoldcount: Integer;
  outlineindex, outlineid: Integer;
  outlines: array of TOutline;
 begin
  with QPL do
  begin
    seldoc := SelectedDocument;
    outlineoldcount := OutlineCount;
    SelectDocument(DocumentID);
    for i := 1 to OutlineCount do
    begin
      SetLength(outlines, i+1);
      outlineid := GetOutlineID(i);
      outlines.Title := OutlineTitle(outlineid);
      outlines.Page := GetOutlinePage(outlineid) +
        PageOldCount;
      outlines.DestPage := GetDestPage(GetOutlineDest(outlineid)) +
        PageOldCount;
      outlines.DestType := GetDestType(GetOutlineDest(outlineid));
      outlines.Left := GetDestValue(GetOutlineDest(outlineid), 1);
      outlines.Top := GetDestValue(GetOutlineDest(outlineid), 2);
      outlines.Right := GetDestValue(GetOutlineDest(outlineid), 3);
      outlines.Bottom := GetDestValue(GetOutlineDest(outlineid), 4);
      outlines.Zoom := GetDestValue(GetOutlineDest(outlineid), 5);
      outlines.ColorRed := GetOutlineColor(outlineid, 0);
      outlines.ColorGreen := GetOutlineColor(outlineid, 1);
      outlines.ColorBlue := GetOutlineColor(outlineid, 2);
      outlines.StyleBold := (GetOutlineStyle(outlineid) and 2) shr 1;
      outlines.StyleItalic := GetOutlineStyle(outlineid) and 1;
      outlines.JavaScript := GetOutlineJavaScript(outlineid);
      outlines.OpenFile := GetOutlineOpenFile(outlineid);
      outlines.WebLink := GetOutlineWebLink(outlineid);
      // searching for parent outline
      outlineindex := GetOutlineIndex(QPL, GetParentOutline(outlineid));
      if outlineindex <> 0 then
        outlines.Parent := outlineindex + outlineoldcount;
    end;

    SelectDocument(seldoc);
    for i := 1 to Length(outlines) - 1 do
    begin
      outlineid := NewOutline(GetOutlineID(outlines.Parent),
        outlines.Title, outlines.Page, 0);
      SetOutlineDestinationFull(outlineid, outlines.DestPage,
        Round(outlines.Zoom), outlines.DestType, outlines.Left,
        outlines.Top, outlines.Right, outlines.Bottom);
      SetOutlineColor(outlineid, outlines.ColorRed,
        outlines.ColorGreen, outlines.ColorBlue);
      SetOutlineStyle(outlineid, outlines.StyleItalic,
        outlines.StyleBold);
      if outlines.JavaScript <> '' then
        SetOutlineJavaScript(outlineid, outlines.JavaScript);
      if outlines.OpenFile <> '' then
        SetOutlineOpenFile(outlineid, outlines.OpenFile);
      if outlines.WebLink <> '' then
        SetOutlineWebLink(outlineid, outlines.WebLink);
    end;
  end;
 end;

begin

// create and unlock PDFLibrary here and load some PDF to merge to...

 with PDFLibrary do
 begin
  with TOpenDialog.Create(nil) do
  begin
    if not Execute then
      Exit;
    LoadFromFile(FileName); // to merge
    LoadFromFile(FileName); // to copy outlines from
    Free;
  end;

  SelectDocument(GetDocumentID(DocumentCount - 2));
  oldpagecount := PageCount;
  MergeDocument(GetDocumentID(DocumentCount - 1));
  SelectDocument(GetDocumentID(DocumentCount - 1));
  MergeOutlines(PDFLibrary, GetDocumentID(DocumentCount), oldpagecount);
 end;

// here output PDF can be saved or rendered...

end;



Replies:
Posted By: isyscorp
Date Posted: 30 Aug 12 at 1:42AM
Hi, thank you for this. Been trying to make it to work but no luck. Kindly help me please make this to work, all i need is to combine 2 or more PDF files into ONE PDF File.

Thanks,
Vid


Posted By: Ingo
Date Posted: 30 Aug 12 at 9:03PM
Hi Vid!

You should post your code here and we'll see if we can help.
BTW: Please post problems at "i need help..." ;-)

Cheers and welcome here,
Ingo



Posted By: isyscorp
Date Posted: 31 Aug 12 at 12:43AM
Hi Ingo, i am trying Rowan's code, but i receive this error message:

"Record, object or class rwquired" at line:

 outlines.Title := OutlineTitle(outlineid);

Thanks for your help,
Vid



Print Page | Close Window

Forum Software by Web Wiz Forums® version 11.01 - http://www.webwizforums.com
Copyright ©2001-2014 Web Wiz Ltd. - http://www.webwiz.co.uk