Overlay one PDF onto another PDF programmatically
Foxit Quick PDF Library makes it easy to copy a page from one PDF and draw that page onto one or more pages in a different PDF. This is very useful for when you have a template such as a letterhead that needs to be applied to all pages in a PDF before it is sent to a client. Whether the captured page is drawn overlay or underlay can be controlled by changing the content stream parts order using the MoveContentStream function.
In the C# sample code below we demonstrate how to overlay a letterhead template onto multiple pages in a PDF using Foxit Quick PDF Library. There’s also a Delphi sample code section below the C#.
/* Copy a page from one PDF and overlay it on all pages in a different PDF */
// Load the PDF that you want to apply the letterhead to (fileA)
DPL.LoadFromFile("Input.pdf", "");
int fileA = DPL.SelectedDocument();
// Load the letterhead template file (fileB)
DPL.LoadFromFile("Template.pdf", "");
int fileB = DPL.SelectedDocument();
// We'll now need to temporarily merge these two PDF files together
// Select fileA
DPL.SelectDocument(fileA);
// Merge fileA with fileB
DPL.MergeDocument(fileB);
// Count total number of pages in merged PDF
int pageCount = DPL.PageCount();
// Capture the last page in fileA which is 
// now the page with the letterhead from fileB
// Once a page has been captured it is automatically
// removed from the PDF
int CapturedPageID = DPL.CapturePage(pageCount);
// Set drawing origin co-ordinates to top left of page
DPL.SetOrigin(1);
// Iterate through each page in the PDF
for (int i = 1; i < pageCount; i++)
{
    // Select page 1 of fileA
    DPL.SelectPage(i);
    // Normalize page to make sure that the content
    // streams are in the state we want them to be
    DPL.NormalizePage(1);
    // Get page height and width
    double xPageHeight = DPL.PageHeight();
    double xPageWidth = DPL.PageWidth();
    // Create a new content stream that we can draw onto
    int contentstreamPos = DPL.NewContentStream();
    DPL.SelectContentStream(contentstreamPos);
    // Draw the captured template page onto the current page
    int ret = DPL.DrawCapturedPage(CapturedPageID, 0, 0, xPageWidth, xPageHeight);
    // Move the new content stream to the background
    // so that it does not cover the other content of the page
    int totalContentStreams = DPL.ContentStreamCount();
    int moveContentResult = DPL.MoveContentStream(contentstreamPos, 1); 
}
// Save the file
DPL.SaveToFile("Output.pdf");
Here is some sample code from Delphi, it’s slightly different but the core features are the same:
procedure TForm2.btnOverlayClick(Sender: TObject);
var
  FileA: Integer;
  FileB: Integer;
  CapturedPageID: Integer;
  PageHeight: Double;
  PageWidth: Double;
  Path: AnsiString;
begin
  DPL := TDebenuPDFLibrary1113.Create;
  try
    UnlockResult := DPL.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
         DPL.LoadFromFile(dlgOpen.FileName);
         FileA := DPL.SelectedDocument();
      end;
      if dlgOpen.Execute then
      begin
         DPL.LoadFromFile(dlgOpen.FileName);
         FileB := DPL.SelectedDocument();
      end;
      // After merging FileB is automatically deleted, leaving only FileB which is now a combination of FileA and FileB
      DPL.SelectDocument(FileA);
      DPL.MergeDocument(FileB);
      // Capture the second page in the merged document
      CapturedPageID := DPL.CapturePage(2);
      // Now select the first page and retrieve its height and width
      DPL.SelectDocument(FileA);
      DPL.SelectPage(1);
      PageHeight := DPL.PageHeight();
      PageWidth := DPL.PageWidth();
      // Draw the captured page onto the currently selected page
      DPL.DrawCapturedPage(CapturedPageID, 0, PageHeight, PageWidth, PageHeight);
      // Save the stitched file to disk
    if dlgSave.Execute then
      begin
        Path := dlgSave.FileName;
      end;
      DPL.SaveToFile(Path);
    end;
  finally
    DPL.Free;
  end;
end;
This article refers to a deprecated product. If you are looking for support for Foxit PDF SDK, please click here.
Updated on May 16, 2022