1

我正在使用 C++ 库 PoDoFo ( http://podofo.sourceforge.net/ ),我想要实现的是将 PDF 页面嵌入到新的空白 PDF 文档中。

我正在使用的构造函数的文档在这里: http: //podofo.sourceforge.net/doc/html/classPoDoFo_1_1PdfXObject.html#ad60d2bcfa51676961ce09ad274a6a6df

这是我的代码目前的样子:

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 文档构造 PdfXObject 时抛出 PdfError,可能是我犯了一个错误,因为我是 C++ 新手,或者 PoDoFo 中可能存在错误。

引发的错误具有以下消息:

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

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

4

1 回答 1

1

要将现有页面加载到 XObject 中,请使用以下内容(srcDoc 和 g_outputdoc 是 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);

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

这种方法的好处是所有的引用和资源也被复制了。不好的部分是每次都复制所有引用和所有资源;)即使您在其他页面中嵌入了相同的资源...

于 2015-12-18T09:17:01.060 回答