gpt4 book ai didi

c++ - 使用 PoDoFo 构建 PdfXObject

转载 作者:行者123 更新时间:2023-11-30 05:34:35 26 4
gpt4 key购买 nike

我正在使用 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);

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

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

关于c++ - 使用 PoDoFo 构建 PdfXObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34167450/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com