Do you own a Debenu Quick PDF Library 12/11/10/9/8/7? Upgrade to Debenu Quick PDF Library 13!

Foxit Quick PDF Library

Frequently Asked Question:

Return to FAQ Index

Simple Code to add a layer to a PDF

Question

Just need the simple c# code using QuickPDF to do the following:

  1. open a PDF
  2. create a new layer (OCG)
  3. in the new layer open another PDF which was created in say InDesign that has colours etc
  4. make the new layer the background
  5. save this as a new PDF

If anyone can help me with this code i will be eternally grateful.

Answer

Here is some sample code that should do what you need. There are two output options possible with this code. Set useBackgroundOCG = true to create a background OCG that contains an image. Set useBackgroundOCG = false to place the image as part the page background.

// Create three new optional content groups
int CapturedPageID;
double PageHeight;
double PageWidth;
int fileA, fileB;
int OCG_Back = 0;
int layer;

// True = create a background OCG layer.  False = Create a fixed background.
bool useBackgroundOCG = true;

QP.NewDocument();
QP.SetOrigin(1);

// Save file to disk with new layers

PageHeight = QP.PageHeight();
PageWidth = QP.PageWidth();

// QP.SaveToFile("layers.pdf");
fileA = QP.SelectedDocument();

// 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.

//QP.LoadFromFile("debenu final tm.pdf");
QP.LoadFromFile("JavaScript.pdf");
fileB = QP.SelectedDocument();

// After merging FileB is automatically deleted, leaving only FileB which is now a combination of FileA and FileB
QP.SelectDocument(fileA);
QP.MergeDocument(fileB);
// QP.SaveToFile("combined.pdf");

// Capture the second page in the merged document
CapturedPageID = QP.CapturePage(2);

QP.SelectDocument(fileA);
QP.SelectPage(1);

// Create a background OCG if useBackgroundOCG = true

if (useBackgroundOCG)
    OCG_Back = QP.NewOptionalContentGroup("Background");

QP.DrawText(100, 100, "Background");

// Draw the captured page onto the currently selected OCG

int ret = QP.DrawCapturedPage(CapturedPageID, 0, 0, PageWidth, PageHeight);

if (useBackgroundOCG)
{
    QP.SetLayerOptional(OCG_Back);
    QP.SetOptionalContentGroupVisible(OCG_Back, 1);
}

// Now add the two OCG's with some text.

int OCG1 = QP.NewOptionalContentGroup("Layer 1");
int OCG2 = QP.NewOptionalContentGroup("Layer 2");

// Select the page that you want the layers to be associated with.

QP.SelectPage(1);

// Add OCG 1

layer = QP.NewLayer();
QP.SelectLayer(layer);
QP.DrawText(180, 100, "Layer 1");
QP.SetLayerOptional(OCG1);
QP.SetOptionalContentGroupVisible(OCG1, 1);

// Add OCG 2

layer = QP.NewLayer();
QP.SelectLayer(layer);
QP.DrawText(260, 100, "Layer 2");
QP.SetLayerOptional(OCG2);
QP.SetOptionalContentGroupVisible(OCG2, 1);

// Save the stitched file to disk
QP.SaveToFile("ocg.pdf");
System.Diagnostics.Process.Start(@"ocg.pdf");

© 2015 Debenu & Foxit. All rights reserved. AboutBuyContactBlogNewsletterSupportFAQProduct UpdatesForum