使用 iTextSharp 生成的 PDF 在关闭时总是提示保存更改。使用非 Acrobat PDF 阅读器查看时有缺失页面
PDF generated with iTextSharp always prompts to save changes when closing. And has missing pages when viewed with non-Acrobat PDF readers
我最近使用 iTextSharp 创建了一个 PDF,方法是从现有 PDF 导入 20 页,然后将动态生成的 link 添加到最后一页的底部。它工作正常......有点。在 windows PC 上的 Acrobat Reader 中查看生成的 PDF 会按预期显示所有内容,但在关闭文档时它总是询问 "Do you want to save changes?"。在带有 PDF Reader 的 Surface Pro 上查看生成的 PDF 显示的文档没有第一页和最后一页。显然在使用 Polaris Office 的移动设备上,第一页和最后一页也丢失了。
我想知道在生成新 PDF 时是否没有正确关闭它,这就是它在关闭时询问 "Do you want to save changes?" 的原因。也许这也是它在某些 PDF reader 应用程序中无法正确显示的原因。
代码如下:
using (var reader = new PdfReader(HostingEnvironment.MapPath("~/app/pdf/OriginalDoc.pdf")))
{
using (
var fileStream =
new FileStream(
HostingEnvironment.MapPath("~/documents/attachments/DocWithLink_" + id + ".pdf"),
FileMode.Create, FileAccess.Write))
{
var document = new Document(reader.GetPageSizeWithRotation(1));
var writer = PdfWriter.GetInstance(document, fileStream);
using (PdfStamper stamper = new PdfStamper(reader, fileStream))
{
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252,
BaseFont.NOT_EMBEDDED);
Font linkFont = FontFactory.GetFont("Arial", 12, Font.UNDERLINE, BaseColor.BLUE);
document.Open();
for (var i = 1; i <= reader.NumberOfPages; i++)
{
document.NewPage();
var importedPage = writer.GetImportedPage(reader, i);
// Copy page of original document to new document.
var contentByte = writer.DirectContent;
contentByte.AddTemplate(importedPage, 0, 0);
if (i == reader.NumberOfPages) // It's the last page so add link.
{
PdfContentByte cb = stamper.GetOverContent(i);
//Create a ColumnText object
var ct = new ColumnText(cb);
//Set the rectangle to write to
ct.SetSimpleColumn(100, 30, 500, 90, 0, PdfContentByte.ALIGN_LEFT);
//Add some text and make it blue so that it looks like a hyperlink
var c = new Chunk("Click here!", linkFont);
var congrats = new Paragraph("Congratulations on reading the eBook! ");
congrats.Alignment = PdfContentByte.ALIGN_LEFT;
c.SetAnchor("http://www.domain.com/pdf/response/" + encryptedId);
//Add the chunk to the ColumnText
congrats.Add(c);
ct.AddElement(congrats);
//Tell the system to process the above commands
ct.Go();
}
}
}
}
}
我看过这些有类似问题的帖子,但 none 似乎提供了我需要的答案:
iTextSharp-generated PDFs cause save dialog when closing
Using iTextSharp to write data to PDF works great, but Acrobat Reader asks 'Do you want to save changes' when closing file
(或者他们指的是内存流而不是写入磁盘等)
我的问题是,如何修改以上内容,以便在 Acrobat Reader 中关闭生成的 PDF 时没有 "Do you want to save changes?" 提示。这个问题的答案可能会解决 Surface Pro 等页面丢失的问题,但如果您知道其他可能导致的原因,我想听听。
非常欢迎任何建议!谢谢!
乍一看(还没有喝多少咖啡)您似乎在三种不同的上下文中使用 PdfReader
,作为 PdfStamper
的来源,作为 Document
以及导入来源。因此,您实质上是将文档导入到您也正在写入的文档中。
为了给您一个快速概览,下面的代码实质上会将 source.pdf
的内容克隆到 dest.pdf
中:
using (var reader = new PdfReader("source.pdf")){
using (var fileStream = new FileStream("dest.pdf", FileMode.Create, FileAccess.Write)){
using (PdfStamper stamper = new PdfStamper(reader, fileStream)){
}
}
}
因为这会为您完成所有克隆,所以您不需要导入页面或任何东西。
然后,如果您唯一想做的就是在最后一页添加一些文本,您可以使用上面的内容并使用 [=19] 向 PdfStamper
请求 PdfContentByte
=] 并告诉它您感兴趣的页码。然后你就可以使用剩下的 ColumnText
逻辑了。
using (var reader = new PdfReader("Source.Pdf")) {
using (var fileStream = new FileStream("Dest.Pdf"), FileMode.Create, FileAccess.Write) {
using (PdfStamper stamper = new PdfStamper(reader, fileStream)) {
//Get a PdfContentByte object
var cb = stamper.GetOverContent(reader.NumberOfPages);
//Create a ColumnText object
var ct = new ColumnText(cb);
//Set the rectangle to write to
ct.SetSimpleColumn(100, 30, 500, 90, 0, PdfContentByte.ALIGN_LEFT);
//Add some text and make it blue so that it looks like a hyperlink
var c = new Chunk("Click here!", linkFont);
var congrats = new Paragraph("Congratulations on reading the eBook! ");
congrats.Alignment = PdfContentByte.ALIGN_LEFT;
c.SetAnchor("http://www.domain.com/pdf/response/" + encryptedId);
//Add the chunk to the ColumnText
congrats.Add(c);
ct.AddElement(congrats);
//Tell the system to process the above commands
ct.Go();
}
}
}
我最近使用 iTextSharp 创建了一个 PDF,方法是从现有 PDF 导入 20 页,然后将动态生成的 link 添加到最后一页的底部。它工作正常......有点。在 windows PC 上的 Acrobat Reader 中查看生成的 PDF 会按预期显示所有内容,但在关闭文档时它总是询问 "Do you want to save changes?"。在带有 PDF Reader 的 Surface Pro 上查看生成的 PDF 显示的文档没有第一页和最后一页。显然在使用 Polaris Office 的移动设备上,第一页和最后一页也丢失了。
我想知道在生成新 PDF 时是否没有正确关闭它,这就是它在关闭时询问 "Do you want to save changes?" 的原因。也许这也是它在某些 PDF reader 应用程序中无法正确显示的原因。
代码如下:
using (var reader = new PdfReader(HostingEnvironment.MapPath("~/app/pdf/OriginalDoc.pdf")))
{
using (
var fileStream =
new FileStream(
HostingEnvironment.MapPath("~/documents/attachments/DocWithLink_" + id + ".pdf"),
FileMode.Create, FileAccess.Write))
{
var document = new Document(reader.GetPageSizeWithRotation(1));
var writer = PdfWriter.GetInstance(document, fileStream);
using (PdfStamper stamper = new PdfStamper(reader, fileStream))
{
var baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252,
BaseFont.NOT_EMBEDDED);
Font linkFont = FontFactory.GetFont("Arial", 12, Font.UNDERLINE, BaseColor.BLUE);
document.Open();
for (var i = 1; i <= reader.NumberOfPages; i++)
{
document.NewPage();
var importedPage = writer.GetImportedPage(reader, i);
// Copy page of original document to new document.
var contentByte = writer.DirectContent;
contentByte.AddTemplate(importedPage, 0, 0);
if (i == reader.NumberOfPages) // It's the last page so add link.
{
PdfContentByte cb = stamper.GetOverContent(i);
//Create a ColumnText object
var ct = new ColumnText(cb);
//Set the rectangle to write to
ct.SetSimpleColumn(100, 30, 500, 90, 0, PdfContentByte.ALIGN_LEFT);
//Add some text and make it blue so that it looks like a hyperlink
var c = new Chunk("Click here!", linkFont);
var congrats = new Paragraph("Congratulations on reading the eBook! ");
congrats.Alignment = PdfContentByte.ALIGN_LEFT;
c.SetAnchor("http://www.domain.com/pdf/response/" + encryptedId);
//Add the chunk to the ColumnText
congrats.Add(c);
ct.AddElement(congrats);
//Tell the system to process the above commands
ct.Go();
}
}
}
}
}
我看过这些有类似问题的帖子,但 none 似乎提供了我需要的答案:
iTextSharp-generated PDFs cause save dialog when closing
Using iTextSharp to write data to PDF works great, but Acrobat Reader asks 'Do you want to save changes' when closing file
(或者他们指的是内存流而不是写入磁盘等)
我的问题是,如何修改以上内容,以便在 Acrobat Reader 中关闭生成的 PDF 时没有 "Do you want to save changes?" 提示。这个问题的答案可能会解决 Surface Pro 等页面丢失的问题,但如果您知道其他可能导致的原因,我想听听。
非常欢迎任何建议!谢谢!
乍一看(还没有喝多少咖啡)您似乎在三种不同的上下文中使用 PdfReader
,作为 PdfStamper
的来源,作为 Document
以及导入来源。因此,您实质上是将文档导入到您也正在写入的文档中。
为了给您一个快速概览,下面的代码实质上会将 source.pdf
的内容克隆到 dest.pdf
中:
using (var reader = new PdfReader("source.pdf")){
using (var fileStream = new FileStream("dest.pdf", FileMode.Create, FileAccess.Write)){
using (PdfStamper stamper = new PdfStamper(reader, fileStream)){
}
}
}
因为这会为您完成所有克隆,所以您不需要导入页面或任何东西。
然后,如果您唯一想做的就是在最后一页添加一些文本,您可以使用上面的内容并使用 [=19] 向 PdfStamper
请求 PdfContentByte
=] 并告诉它您感兴趣的页码。然后你就可以使用剩下的 ColumnText
逻辑了。
using (var reader = new PdfReader("Source.Pdf")) {
using (var fileStream = new FileStream("Dest.Pdf"), FileMode.Create, FileAccess.Write) {
using (PdfStamper stamper = new PdfStamper(reader, fileStream)) {
//Get a PdfContentByte object
var cb = stamper.GetOverContent(reader.NumberOfPages);
//Create a ColumnText object
var ct = new ColumnText(cb);
//Set the rectangle to write to
ct.SetSimpleColumn(100, 30, 500, 90, 0, PdfContentByte.ALIGN_LEFT);
//Add some text and make it blue so that it looks like a hyperlink
var c = new Chunk("Click here!", linkFont);
var congrats = new Paragraph("Congratulations on reading the eBook! ");
congrats.Alignment = PdfContentByte.ALIGN_LEFT;
c.SetAnchor("http://www.domain.com/pdf/response/" + encryptedId);
//Add the chunk to the ColumnText
congrats.Add(c);
ct.AddElement(congrats);
//Tell the system to process the above commands
ct.Go();
}
}
}