【问题标题】:Why does switching my logo give me a corrupt PDF file?为什么切换我的徽标会给我一个损坏的 PDF 文件?
【发布时间】:2014-12-19 06:40:28
【问题描述】:

我正在使用 iTextSharp 在 ASP.NET C# Windows 控制台应用程序中创建 PDF 文件。

我有这段代码。如果站点是“LMH”,我会得到一个很好的 PDF,我可以用 Adob​​e Reader 打开。如果没有,我会收到错误:处理页面时出错。阅读此文档时出现问题 (114)。

这是我的代码:

string ApplicationPath = System.IO.Directory.GetCurrentDirectory();
PdfPTable table = new PdfPTable(4) { TotalWidth = 800.0F, LockedWidth = true };
float[] widths = new[] { 80.0F, 80.0F, 500.0F, 140.0F };
table.SetWidths(widths);
table.HorizontalAlignment = 0;
table.DefaultCell.Border = 0;

if (ActiveProfile.Site == "LMH")
{
    Image hmsImage = Image.GetInstance(ApplicationPath + "\\" + "HMS Logo.png");
    hmsImage.ScaleToFit(80.0F, 40.0F);

    PdfPCell hmslogo = new PdfPCell(hmsImage);
    hmslogo.Border = 0;
    hmslogo.FixedHeight = 60;
    table.AddCell(hmslogo);
}
else
{
    Image blankImage = Image.GetInstance(ApplicationPath + "\\" + "emptyLogo.png");
    blankImage.ScaleToFit(80.0F, 40.0F);

    PdfPCell emptyCell = new PdfPCell(blankImage);
    emptyCell.Border = 0;
    emptyCell.FixedHeight = 60;
    table.AddCell(emptyCell);
}

还有主干:

System.IO.FileStream file = new System.IO.FileStream(("C:/") + keyPropertyId + ".pdf", System.IO.FileMode.OpenOrCreate);
Document document = new Document(PageSize.A4.Rotate () , 20, 20, 6, 4);

PdfWriter writer = PdfWriter.GetInstance(document, file );

document.AddTitle(_title);
document.Open();
addHeader(document);
addGeneralInfo(document, keyPropertyId);
addAppliances(document, _LGSRobj);
addFaults(document, _LGSRobj);
addAlarms(document, _LGSRobj);
addFinalCheck(document, _LGSRobj);
addSignatures(document, _LGSRobj);
addFooter(document, writer);
document.Close();
writer .Close ();

file.Close();

所有改变的只是一个标志。两个徽标文件具有相同的尺寸。什么可能是错的?顺便提一句。使用 Foxit 可以正常打开文件,但这不是可接受的解决方案。

这是我无法使用 Adob​​e Reader 打开的 PDF:https://dl.dropboxusercontent.com/u/20086858/1003443.pdf

这是 emptyLogo.png 文件:https://dl.dropboxusercontent.com/u/20086858/emptylogo.png

这是有效的标志:https://dl.dropboxusercontent.com/u/20086858/HMS%20Logo.png

这是 pdf 的“好”版本,带有有效的徽标:https://dl.dropboxusercontent.com/u/20086858/1003443-good.pdf

【问题讨论】:

  • 您能分享有问题的损坏的 PDF 吗?
  • 不知道该怎么做。
  • 你能把两张图片都发上来吗?
  • 如何共享 PDF 和图像?
  • Dropbox 是您的朋友,或任何其他文件共享服务。

标签: c# asp.net pdf adobe itextsharp


【解决方案1】:

尽管图像大小非常不同,但“好”和“不太好”的版本具有相同的大小,这是非常令人怀疑的。比较它们会发现两个文件的前 192993 个字节完全不同,但从那里开始只有很小的差异。此外,损坏的 PDF 在此区域中包含 EOF 标记,表示文件以索引 140338 和 192993 结尾,但以下字节看起来根本不像是干净的增量更新。

在第一个表示的文件结尾处剪切文件,140338,得到 OP 想要的文件。

因此:

代码用新数据覆盖现有文件;如果前一个文件较长,则该较长文件的其余部分在最后仍为垃圾,因此会使新文件损坏。

OP 像这样打开流:

new System.IO.FileStream(("C:/") + keyPropertyId + ".pdf", System.IO.FileMode.OpenOrCreate);

FileMode.OpenOrCreate 导致观察到的行为。

FileMode 值记录在 here on MSDN 中,尤其是:

  • OpenOrCreate 指定操作系统应该打开一个文件(如果存在);否则,应创建一个新文件。
  • 创建 指定操作系统应该创建一个新文件。如果文件已经存在,它将被覆盖。 ... FileMode.Create相当于请求如果文件不存在,使用CreateNew;否则,使用截断。

因此,使用

new System.IO.FileStream(("C:/") + keyPropertyId + ".pdf", System.IO.FileMode.Create);

改为。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-06
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    • 1970-01-01
    • 2023-04-10
    • 2021-05-20
    • 1970-01-01
    相关资源
    最近更新 更多