Print Page | Close Window

Overlay one page onto another (stitch PDFs)

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=1203
Printed Date: 22 Nov 24 at 6:44PM
Software Version: Web Wiz Forums 11.01 - http://www.webwizforums.com


Topic: Overlay one page onto another (stitch PDFs)
Posted By: Rowan
Subject: Overlay one page onto another (stitch PDFs)
Date Posted: 07 Sep 09 at 2:35AM
This is a quick and relatively simple way to overlay one page onto another. This is sometimes called stitching, overlaying, watermarking, etc, -- although each of those terms can refer to slightly different tasks as well. 

The benefit of overlaying one page onto another the way I've outlined below is that you don't have to render either of the pages as an image, so in the final output, you'll still be able to select individual elements on the page, including text.

I've used Delphi for this sample, but the syntax is quite straightforward and it shouldn't be difficult to understand.

------------
procedure TForm2.btnOverlayClick(Sender: TObject);
var
  FileA: Integer;
  FileB: Integer;
  CapturedPageID: Integer;
  PageHeight: Double;
  PageWidth: Double;
  Path: AnsiString;
begin
  QP := TQuickPDF0716.Create;
  try
    UnlockResult := QP.UnlockKey('...'); // Insert trial or paid license key here
    if UnlockResult = 1 then
    begin
    // You can only use the DrawCapturedPage function if the captured page is in the same
    // document, so first we'll need to merge the two pages that we wish to overlay together
    // into one document.
    if dlgOpen.Execute then
      begin
         QP.LoadFromFile(dlgOpen.FileName);
         FileA := QP.SelectedDocument();
      end;
      if dlgOpen.Execute then
      begin
         QP.LoadFromFile(dlgOpen.FileName);
         FileB := QP.SelectedDocument();
      end;
      // After merging FileB is automatically deleted, leaving only FileB which is now a combination of FileA and FileB
      QP.SelectDocument(FileA);
      QP.MergeDocument(FileB);

      // Capture the second page in the merged document
      CapturedPageID := QP.CapturePage(2);
      // Now select the first page and retrieve its height and width
      QP.SelectDocument(FileA);
      QP.SelectPage(1);
      PageHeight := QP.PageHeight();
      PageWidth := QP.PageWidth();
      // Draw the captured page onto the currently selected page
      QP.DrawCapturedPage(CapturedPageID, 0, PageHeight, PageWidth, PageHeight);

      // Save the stitched file to disk
    if dlgSave.Execute then
      begin
        Path := dlgSave.FileName;
      end;
      QP.SaveToFile(Path);
    end;
  finally
    QP.Free;
  end;
end;
------------

Obviously if the two pages don't have the same dimensions then you'll need to do some tinkering, but if they do, then this code should work quite well.



Replies:
Posted By: mmolenkp
Date Posted: 26 Feb 13 at 4:24PM
I took this sample for my own code in Delphi 7 and it works as designed BUT not for all cases.
 
I have a multi page word document and I need to place an overlay pdf page on the back ground of some pages.
The first step generate a PDF with microsoft word and use it with this sample, it works correct.
When I use OpenOffice 4.3 or LIbre Office 4.0 and then export as PDF it dont work coreect.
The stitching works on every page, but when I open the resulting file with acrobat I get an error message. 
 
Ik have done some debugging, to isolate the problem:
When I captured the first page of the PDF file generated with Openoffice en then SaveToFile the remaining pages. It gives me a file that get an error by acrobat reader.
 
I Have tried this problem with Lib 0811 an the 0912 the same result.
My Idee is that Microsoft has put more page information on every page and OpenOffice Not.
 
What can I do ?
Thanks for help
Mike
 
 



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