【问题标题】:Embed pdf content in pdf layer在 pdf 层中嵌入 pdf 内容
【发布时间】:2020-12-11 08:47:45
【问题描述】:

我刚注册。我尝试解决以下情况:

给定一个基本的 pdf(一个简单的单一光栅图像),我想:

  • 创建一个 pdf(最初为空),在其中创建一个图层,在其中嵌入 input_pdf 的光栅图像,并将该图层标记为可见且不可打印。

有工具可以做到吗?

感谢您的提示。

【问题讨论】:

  • 你想用哪种编程语言来实现这个?您使用哪个 pdf 库?
  • 您的问题在 Stack Overflow 上不被接受:“寻求书籍、工具、软件库等方面的建议。这个问题可能会导致基于意见的答案。”
  • 好吧,这个问题是探索性的,这是因为虽然我用python编程,但我完全不清楚是否有一个库可以以这种方式处理层。非常感谢用户 iPDFdev 代码 C#,我将不得不阅读许可条款。我会很感激 python 中的库信息来做到这一点。

标签: pdf embed layer


【解决方案1】:

由于您没有指定您使用的语言或库,这里是 C# 中的解决方案:

// Extract the page content from the source file.
FileStream stream = File.OpenRead("input.pdf");
PDFFile source = new PDFFile(stream);
PDFPageContent pageContent = source.ExtractPageContent(0);
stream.Close();

PDFFixedDocument document = new PDFFixedDocument();
document.OptionalContentProperties = new PDFOptionalContentProperties();
PDFPage page = document.Pages.Add();

// Create an optional content group (layer) for the extracted page content.
PDFOptionalContentGroup ocg = new PDFOptionalContentGroup();
ocg.Name = "Embedded page";
ocg.VisibilityState = PDFOptionalContentGroupVisibilityState.AlwaysVisible;
ocg.PrintState = PDFOptionalContentGroupPrintState.NeverPrint;

// Draw the extracted page content in the layer
page.Canvas.BeginOptionalContentGroup(ocg);
page.Canvas.DrawFormXObject(pageContent, 0, 0, page.Width, page.Height);
page.Canvas.EndOptionalContentGroup();

// Build the display tree for the optional content
PDFOptionalContentDisplayTreeNode ocgNode = new PDFOptionalContentDisplayTreeNode(ocg);
document.OptionalContentProperties.DisplayTree.Nodes.Add(ocgNode);

using (FileStream output = File.Create("EmbedPageAsLayer.pdf"))
{
    document.Save(output); 
}

输出 PDF 文件可在此处获得:https://github.com/o2solutions/pdf4net/blob/master/GettingStarted/EmbedPageAsLayer/EmbedPageAsLayer.pdf

【讨论】:

    猜你喜欢
    • 2015-01-06
    • 2021-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-14
    • 2011-09-28
    • 2012-04-30
    • 2013-05-29
    相关资源
    最近更新 更多