I am using the C++ library PoDoFo (http://podofo.sourceforge.net/) and what I am trying to achieve is embedding a PDF page into a new blank PDF document.
The documentation for the constructor I am using is here: http://podofo.sourceforge.net/doc/html/classPoDoFo_1_1PdfXObject.html#ad60d2bcfa51676961ce09ad274a6a6df
This is what my code currently looks like:
PoDoFo::PdfMemDocument existingDocument(filename);
PoDoFo::PdfStreamedDocument *newDocument = new PoDoFo::PdfStreamedDocument("new_document.pdf");
PoDoFo::PdfPage *newPage = newDocument->CreatePage(PoDoFo::PdfRect(0.0,0.0,300.0,300.0));
PoDoFo::PdfXObject *XObjectFromPage;
XObjectFromPage = new PoDoFo::PdfXObject(existingDocument, 1, newDocument);
PoDoFo::PdfPainter *painter = new PoDoFo::PdfPainter();
painter->SetPage(newPage);
painter->DrawXObject (50, 50, XObjectFromPage,1);
painter->FinishPage();
newDocument->Close();
When constructing the PdfXObject from an existing PDF document PdfError is thrown, perhaps I have made a mistake because I am new to C++ or there is potentially a bug in PoDoFo.
The error that is thrown has the following message:
PoDoFo encounter an error. Error: 48 ePdfError_ChangeOnImmutable
Error Description: Changing values on immutable objects is not allowed.
Callstack:
What is the correct way to construct a PdfXObject from an existing PDF page and embed it into a new PDF document?
To load an existing page into a XObject use something like this (srcDoc and g_outputdoc are PdfMemDocuments):
PdfPage* srcPage(srcDoc->GetPage(pageNumber));
//create XObject owned by outputDoc with size of srcPage
PdfXObject* xobject = new PdfXObject(srcPage->GetCropBox(), g_outputDoc)));
//fill the xObject with the content of the page + all references and ressources used on this page
g_outputDoc->FillXObjectFromDocumentPage(xobject , *srcDoc, pageNumber, false);
Your embedding part is right. Just draw the object using a pdfPainter :-)
The good part about this method is that all the references and ressources are copied too. The bad part about this is that all the references and all the ressources are copied every time ;) even though you embedd the same ressources with other pages...