Back to Blog

    Developer Blog

    Programmatically stamp one page from a PDF onto a different page in a different PDF

    Foxit Quick PDF Library

    Foxit Quick PDF Library lets you programmatically stamp one PDF onto another PDF. This can be done using the CapturePage and DrawCapturedPage functions to overlay a PDF file onto another.

    Here is a small JScript code snippet that uses CapturePage, DrawCapturedPage and a few other Foxit Quick PDF Library functions to complete this task.

    /* Overlay one page from a document onto a different page in a different document */
    
    // Load fileA
    DPL.LoadFromFile("firstPDF.pdf", "");
    fileA = DPL.SelectedDocument();
    
    // Load fileB
    DPL.LoadFromFile("secondPDF.pdf", "");
    fileB = DPL.SelectedDocument();
    
    // Select fileA
    DPL.SelectDocument(fileA);
    
    // Merge fileA with fileB
    DPL.MergeDocument(fileB);
    
    // Set orgin co-ordinates to top left of page
    DPL.SetOrigin(1);
    
    // Select page 1 of fileA
    DPL.SelectPage(1);
    
    // Get page height and width
    xPageHeight = DPL.PageHeight();
    xPageWidth = DPL.PageWidth();
    
    // Capture page 2
    CapturedPageID = DPL.CapturePage(2);
    
    // Create OCG for the background
    OCG1 = DPL.NewOptionalContentGroup("Background");
    
    // Create a new content stream that we can draw onto
    contentstreamPos = DPL.NewContentStream();
    DPL.SelectContentStream(contentstream);
    
    // Draw the captured page onto the current page
    ret = DPL.DrawCapturedPage(CapturedPageID, 0, 0, xPageWidth, xPageHeight);
    
    // Associate the new content stream with the new OCG
    DPL.SetContentStreamOptional(OCG1);
    
    // Make the OCG visible
    DPL.SetOptionalContentGroupVisible(OCG1, 1);
    
    // Save the file
    DPL.SaveToFile("overlay.pdf");
    

    This article refers to a deprecated product. If you are looking for support for Foxit PDF SDK, please click here.