使用PoDoFo构建PdfXObject

Constructing PdfXObject using PoDoFo

本文关键字:PdfXObject 构建 PoDoFo 使用      更新时间:2023-10-16

我正在使用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文档中的正确方法是什么?

要将现有页面加载到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);

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

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