- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我目前正在尝试迭代现有的 PDF 并使用 OnPageEnd
事件在每个页面上标记一些页脚文本,如 iText 文档中所述,Chapter 5: Table, cell, and page events .
当我将新的自定义事件类分配给 PdfCopy 实例时,我收到此异常:
"Operation is not valid due to the current state of the object" at iTextSharp.text.pdf.PdfCopy.set_PageEvent(IPdfPageEvent value)
下面是我为执行操作而编写的代码:
PdfReader pdf = new PdfReader(file.Value);
int pages = pdf.NumberOfPages;
pdf.SelectPages(string.Format("0 - {0}", pages));
using (MemoryStream stream = new MemoryStream())
{
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, stream) { PageEvent = new PdfFooterStamp() };
doc.Open();
for (int x = 0, y = pages; x < y; x++)
{
copy.AddPage(copy.GetImportedPage(pdf, x + 1));
}
doc.Close();
copy.Flush();
copy.Close();
collection[file.Key] = stream.ToArray();
}
这是我的自定义事件类定义:
public class PdfFooterStamp : PdfPageEventHelper
{
public override void OnEndPage(PdfWriter writer, Document document)
{
Rectangle rect = writer.PageSize;
ColumnText.ShowTextAligned(writer.DirectContent,
Element.ALIGN_CENTER, new Phrase("PERSONALISED DOCUMENT"),
(rect.Left + rect.Right) / 2, rect.Bottom - 18, 0);
base.OnEndPage(writer, document);
}
}
有没有人知道可能出了什么问题?
最佳答案
按照@BrunoLowagie 的建议,我选择了前一个选项,从导入的页面创建一个 PageStamp
,并在迭代导入的 PDF 集合时更改它的内容。
You can keep on using
PdfCopy
and usePageStamp
to add the text to each page that is added. Or you can create the PDF in two passes: first create the concatenated PDF in memory withPdfCopy
; then add the footer withPdfStamper
in a second pass.
我之前的尝试没有奏效的原因是,
PdfPageEventHelper
andPdfCopy
are mutually exclusive. You can't define a page event when usingPdfCopy
- @BrunoLowagie
以下代码是首选解决方案的示例,测试证明它可以按预期工作。
Document doc = new Document();
PdfCopy copy = new PdfCopy(doc, stream);
doc.Open();
for (int x = 0, y = pages; x < y; x++)
{
PdfImportedPage import = copy.GetImportedPage(pdf, x + 1);
PageStamp stamp = copy.CreatePageStamp(import);
Rectangle rect = stamp.GetUnderContent().PdfWriter.PageSize;
ColumnText.ShowTextAligned(stamp.GetUnderContent(),
Element.ALIGN_CENTER, new Phrase(User.Identity.Name, font),
(rect.Bottom + rect.Top) / 2, rect.Bottom + 8, 0);
stamp.AlterContents();
copy.AddPage(import);
}
关于c# - 将自定义页面事件分配给 PdfCopy 时抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45436826/
我目前正在尝试迭代现有的 PDF 并使用 OnPageEnd 事件在每个页面上标记一些页脚文本,如 iText 文档中所述,Chapter 5: Table, cell, and page event
我正在尝试使用 ItextSharp 中的 PdfSmartCopy,但我在 C# 中找不到任何相关示例。 我的想法是我有一个包含表单字段的 pdf,并且这些字段将 700kb 添加到 pdf 文档的
我正在使用 iText(特别是 iTextSharp 4.1.6),我想通过组合现有 PDF 中的页面来创建 PDF,同时插入从图像创建的新页面。 我分别使用 PdfCopy 和 PdfWriter
当我使用 pdfcopy 创建 pdf 时,返回到客户端的 pdf 为空或只有 1 行,但是当我在资源管理器上打开它时,它包含我合并的所有数据,我使用 itextsharp 5 这是我进行合并的地方:
我有一个在 Spring Boot 应用程序中使用的模板 pdf 文件。我需要根据每个请求的用户输入更新此模板中的值。另外,在请求中,我将获得多个 pdf 文件,我需要将这些文件与更新的文件(最终 p
我需要将多个 pdf(每个单页)添加到我的主 pdf。这些需要添加到特定页码之后,而不是附加到末尾。 我该怎么办 1:在特定页码处合并pdf 2:pdfCopy.AddDocument 不可用。我已经
我正在研究使用 iText 5.4.5 合并一些输入 PDF 文档的任务。输入文档可能包含也可能不包含 AcroForms,我也想合并表单。 我正在使用找到的示例 pdf 文件 here这是代码示例:
iText 实际操作的第 6 章介绍了如何使用 PdfSmartCopy/PdfCopy 复制页面: public void addDataSheets(PdfCopy copy) t
我想合并两个 PDF 文件(仅选定的页面)并向它们添加自定义页眉和页脚。 因此我不使用 PdfCopy 只是复制页面而不改变它。我使用 PdfWriter。 问题是我不知道如何使用 PdfWriter
我正在使用 iText 一遍又一遍地标记 PDF 表单,并尝试将生成的单页 pdf 编译成一个 pdf,在 RAM 中有许多页面,并在 http 响应中返回它。无论我做什么,都会出现无效的 PDF。如
这个问题已经有答案了: Is it possible to merge several pdfs using iText7 (7 个回答) 已关闭 4 年前。 我正在尝试在 java 项目中连接两个不
在 iText 5.4.4 的发行说明中,它说: From now on you can now merge forms and preserve the tagged PDF structure w
我是一名优秀的程序员,十分优秀!