C++ 使用PoDoFo构造PdfXObject

C++ 使用PoDoFo构造PdfXObject,c++,pdf,podofo,C++,Pdf,Podofo,我使用C++库PoDoFo(),我正在尝试将PDF页面嵌入到一个新的空白PDF文档中。 我正在使用的构造函数的文档如下: 这就是我的代码当前的样子: PoDoFo::PdfMemDocument existingDocument(filename); PoDoFo::PdfStreamedDocument *newDocument = new PoDoFo::PdfStreamedDocument("new_document.pdf"); PoDoFo::PdfPage *newPage =

我使用C++库PoDoFo(),我正在尝试将PDF页面嵌入到一个新的空白PDF文档中。 我正在使用的构造函数的文档如下:

这就是我的代码当前的样子:

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();

当从现有PDF文档PDFRIOR构建PDFXObjt时,可能我犯了一个错误,因为我是C++新手,或者PodoFo中有一个bug。 引发的错误包含以下消息:

PoDoFo encounter an error. Error: 48 ePdfError_ChangeOnImmutable
    Error Description: Changing values on immutable objects is not allowed.
    Callstack:

从现有PDF页面构造PdfXObject并将其嵌入新PDF文档的正确方法是什么?

要将现有页面加载到XObject中,请使用以下内容(srcDoc和g_outputdoc是PdfMemDocuments):

你的嵌入部分是对的。只需使用pdfPainter绘制对象:-)

这种方法的优点在于所有的引用和资源都被复制了。糟糕的是每次都会复制所有的引用和资源;)即使您在其他页面中嵌入了相同的资源

    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);