c++ - 使用 PoDoFo 构建 PdfXObject

标签 c++ pdf podofo

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

相关文章:

java - 有人可以解释一下 iTexts Canvas 绘制顺序吗?

ios - PDF 未从 App 重新启动 iOS 上的文档文件夹加载

c++ - 仅从 PDF 文件中复制必要的对象

java - C++/Java - int(0 - 1023) 到字节数组(仅限两个字节)

C++:结构可以从类继承吗?

c# - 使用 html5 将 byte[] 显示为 pdf

ios - 将目录从一个 PDF 复制到另一个

c++ - 清除 QSerial 正在使用的 QList

c++ - g++ 6.1 可能的 std::forward 回归 - 错误或预期行为?