How do I overlay two pdf pages in one document?

Q: we’re using your product for a few years and we are very pleased
with it.

Now we have the requirement to overlay two pdf pages in one document.

e.g.
We have a letter paper (with header and footer) and a bill (with
positions, etc.) which has one or more pages. The bill is created by
our system and the letter paper is created by any layout program. Can
we read both documents, overlay the bill (each page) with the letter
paper and save the complete document as new file?

If this is possible please give me a short code snippet how to do
this.

Thanks in advance.
---------------------------
A: As astarting point you may want to take a look at ImpositionTest
sample project (http://www.pdftron.com/pdfnet/
samplecode.html#Imposition).
Besically you can use element_builder.CreateForm(page, src_do) to
create a form xobject from any PDF page. You can then place the form
on another page as if was image element.

To add headers/footers you could use ElementBuilder & ElementWriter to
add dynamic content (or in newer versions of PDFNet you could also use
Stamper utility class to simplify process of adding watermarks to sets
of pages).

Q: I did some testing and get the following error when I wan to place
the read page from the second pdf document.

Message: Objects cannot belong to different documents

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using pdftron;
using pdftron.Common;
using pdftron.SDF;
using pdftron.PDF;
using System.Collections;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public Form1()
    {
      InitializeComponent();
    }

    private void btnMerge_Click(object sender, EventArgs e)
    {
      PDFNet.Initialize();

      string input_path = "c:\\pdfnet\\";
      string output_path = "c:\\";

Console.WriteLine("_______________________________________________");
        Console.WriteLine("Opening the input pdf...");
        PDFDoc in_doc = new PDFDoc(input_path + "Rechnung.pdf");
        in_doc.InitSecurityHandler();

        // Create a list of pages to import from one PDF document to
another.
        ArrayList import_list = new ArrayList();
        for (PageIterator itr = in_doc.GetPageIterator();
itr.HasNext(); itr.Next())
          import_list.Add(itr.Current());

        PDFDoc new_doc = new PDFDoc(); // Create a new document
        ArrayList imported_pages = new_doc.ImportPages(import_list);

        //Briefpapier holen
        PDFDoc overlay_doc = new PDFDoc(input_path +
"Briefpapier.pdf");
        overlay_doc.InitSecurityHandler();

        ArrayList overlay_import_list = new ArrayList();
        for (PageIterator itr = overlay_doc.GetPageIterator();
itr.HasNext(); itr.Next())
          overlay_import_list.Add(itr.Current());

        ArrayList overlay_imported_pages =
overlay_doc.ImportPages(overlay_import_list);

        // Paper dimension for A4 format in points
        Rect media_box = new Rect(0, 0, 597.6, 842.4);
        double mid_point = media_box.Width() / 2;

        ElementBuilder builder = new ElementBuilder();
        ElementWriter writer = new ElementWriter();

        for (int i = 0; i < imported_pages.Count; ++i)
        {

          Page new_page = new_doc.PageCreate(media_box);
          writer.Begin(new_page);

          Page src_page = (Page)imported_pages[i];
          Element element = builder.CreateForm(src_page);

          writer.WritePlacedElement(element);

          Page src_overlay = (Page)overlay_imported_pages[i];
          Element element_overlay = builder.CreateForm(src_overlay);

          writer.WritePlacedElement(element_overlay);

          writer.End();
          new_doc.PagePushBack(new_page);

        }
        // Calling Dispose() on ElementReader/Writer/Builder can
result in increased performance and lower memory consumption.
        writer.Dispose();
        builder.Dispose();

        new_doc.Save(output_path + "a.pdf",
SDFDoc.SaveOptions.e_linearized);
        new_doc.Close();
        in_doc.Close();
    }
  }
}

Whats the reason for this error?
---------------
A: If you are importing a page from another document you need to pass
source document as the second parameter to CreateForm ( for example
builder.CreateForm(src_page, src_doc).

If you are planning the page from the same document you should remove
the source doc parameter.

Also it seems that you imported pages from overlay_doc to itself
(overlay_doc.ImportPages) but you need to import pages in the new_doc.