Copying page 'Elements' from one PDF document to another.

Q: I would like to copy page element from a source PDF (clipart
document) to the target PDF but I am getting a error. Could you please
shed some light on this problem?

The code used to copy the clipart from one PDF to another is as
follows:

pdftron.PDFNet.Initialize();
PDFDoc brokenDoc = new PDFDoc();
Page brokenPage = brokenDoc.PageCreate();
brokenDoc.PagePushBack(brokenPage);

PDFDoc clipartDoc = new PDFDoc("c:\\starburst01.pdf");

// Write clipartDoc's page 1 contents to brokenDoc's page 1
ElementWriter brokenWriter = new ElementWriter();
ElementReader brokenReader = new ElementReader();
ElementBuilder brokenBuilder = new ElementBuilder();
Element group = brokenBuilder.CreateGroupBegin();
Element pdfElement;

brokenWriter.Begin(brokenDoc.GetPage(1));
brokenWriter.WritePlacedElement(group);
brokenReader.Begin(clipartDoc.GetPage(1));

while ((pdfElement = brokenReader.Next()) != null) {
    brokenWriter.WriteElement(pdfElement);
}

brokenReader.End();
brokenWriter.WriteElement(brokenBuilder.CreateGroupEnd());
brokenWriter.End();
clipartDoc.Dispose();
-----
A: The problem is in the way the API is used. In your original code,
you are directly copying page 'Elements' from one document to another,
which is not allowed. You can only copy page elements from one page to
another within the same document. This restriction exists because
graphical elements usually share many resources in common (fonts,
images, color spaces, etc) and copying element from one document to
another would result in excessive resource duplication.

To go around this restriction, you need to first import the source
page(s) in the target document and then copy elements from a temporary
page to the target page. The imported pages do not need to be added to
the document's page sequence and will be discarded when the document
is saved. For example:

//--------------
pdftron.PDFNet.Initialize();
// TODO: PDFNet.SetResourcesPath("D:/pdfnet.res");

PDFDoc doc = new PDFDoc();
Page target_page = doc.PageCreate();
doc.PagePushBack(target_page);

// Write clipart_doc's page 1 contents to doc's page 1
ElementWriter writer = new ElementWriter();
ElementReader reader = new ElementReader();
ElementBuilder builder = new ElementBuilder();
Element group = builder.CreateGroupBegin();
Element element;

PDFDoc clipart_doc = new PDFDoc("D:/starburst01.pdf");
clipart_doc.InitSecurityHandler();

System.Collections.ArrayList pages = new
System.Collections.ArrayList();
pages.Add(clipart_doc.GetPage(1));
System.Collections.ArrayList imported_pages = doc.ImportPages(pages);

reader.Begin((Page)imported_pages[0]);
writer.Begin(doc.GetPage(1));
writer.WritePlacedElement(group);

while ((element = reader.Next()) != null) {
  writer.WriteElement(element);
}

reader.End();
writer.WriteElement(builder.CreateGroupEnd());
writer.End();

// doc.Save("D:/out.pdf",
pdftron.SDF.SDFDoc.SaveOptions.e_remove_unused);
byte[] bytes = new byte[0];
int byteCount = 0;
doc.Save(ref bytes, ref byteCount,
  pdftron.SDF.SDFDoc.SaveOptions.e_remove_unused);

clipart_doc.Dispose();
doc.Dispose();
writer.Dispose();
reader.Dispose();
builder.Dispose();

pdftron.PDFNet.Terminate();
//------------------------------------

Since you are planning to import pages and clipart from one document
to another you may want to consider importing pages as form XObjects.
This approach is illustrated in ImpositionTest sample project (http://
www.pdftron.com/net/samplecode.html#Imposition).