How do I merge two PDFs by merging corresponding pages together?

Q: I have 2 PDF, both with N pages. I want to generate a 3rd PDF in
which the 1st page has the background of the 1st page of the 1st PDF
and the foreground is the 1st page of the 2nd PDF, and so on for the N
pages. Is it possible with PDFNet SDK (http://www.pdftron.com/pdfnet)?
-------------------
A: With PDFNet it is fairly easy to place one PDF page on top or
behing another page. The code would look along the following lines
(assuming you are using C# - other languages are similar):

PDFNet.Initialzie();

using (PDFDoc doc1 = new PDFDoc("in1.pdf")) {
doc1.InitSecurityHandler();

// Import pages from doc2 into doc1.
ArrayList imported_pages = null;
using (PDFDoc doc2 = new PDFDoc("in2.pdf")) {
   doc2.InitSecurityHandler();
   ArrayList import_list = new ArrayList();
   for (PageIterator itr = doc2.GetPageIterator(); itr.HasNext();
itr.Next())
  import_list.Add(itr.Current());
   imported_pages = doc1.ImportPages(import_list);
}

// Place imported pages onto existing pages in doc1
using (ElementBuilder b = new ElementBuilder()) {
  using (ElementWriter w = new ElementWriter()) {
   PageIterator itr = doc1.GetPageIterator();
   for (int i=0; i<imported_pages.Count && itr.HasNext(); ++i,
itr.Next()) {
    w.Begin(itr.Current(), ElementWriter.WriteMode.e_overlay);
    Page pg = (Page)imported_pages[i];
    Element element = b.CreateForm(pg);
    element.GetGState().SetTransform(pg.GetPageWidth(), 0, 0,
pg.GetPageHeight(), 0, 0);
    writer.WritePlacedElement(element);
    w.End();
   }
  }
}
doc.Save("out.pdf", 0);
}

For a relevant code you may want to take a look at ImpositionTest
sample project:
http://www.pdftron.com/pdfnet/samplecode.html#Imposition

Q: Is it possible to use your PDFTron PDFNet SDK (http://
www.pdftron.com/pdfnet) product in order to take certain pages from
one PDF-file and certain pages from another PDF-file and super impose
one page over the other whilst still retaining the layer information?
In other words not have the resulting PDF-file have just one layer
with the old layer data converted into illustrator group objects
(layers from original files put into one single layer).

If this is possible, could you perhaps show an example of how I can
test this out in Java or C? (or refer me to some online material that
might explain this)


A: You can use PDFNet to implement the required functionality. For a

relevant code you may want to take a look at ImpositionTest sample

project: http://www.pdftron.com/pdfnet/samplecode.html#Imposition as
well as LayersTest.

In case you are working with OCG-s depending on your merging
requirements you may need to configure the OCG array in document’s OCG
configuration (i.e. pdfdoc.GetOCGConfig()).

Since I’m a VB programmer can u send me the sample in VB?
A: The best starting point is ImpositionTest sample (http://
www.pdftron.com/pdfnet/samplecode.html). There is also a VB version of
this sample: http://www.pdftron.com/pdfnet/samplecode/ImpositionTest.vb

The main difference is that you would:

a) open an existing document instead of a new one:

Dim new_doc As PDFDoc = New PDFDoc
→ Dim new_doc As PDFDoc = New PDFDoc(“background.pdf”)

b) write to an existing page instead of the new one:

Dim new_page As Page = new_doc.PageCreate(media_box)
writer.Begin(new_page)
→ writer.Begin(new_doc.GetPage(i+1),
ElementWriter.WriteMode.e_overlay)

c) comment out the code which is placing the second page on the same
destination page.

’ Place the second page
i = i + 1
’ If i < imported_pages.Count Then
’ src_page = imported_pages(i)
’ …
’ i = i + 1
'End If