【问题标题】:Adobe Reader can't display unicode font of pdf added with iTextAdobe Reader 无法显示使用 iText 添加的 pdf 的 unicode 字体
【发布时间】:2016-05-07 01:52:18
【问题描述】:

我想使用 iText 将文本标记为 certain pdf。由于此文本可以是西里尔文,因此我使用 unicode 字体和编码。 以下示例代码代表我是如何做到的:

string inputFile = @"sampleStamped.pdf";
PdfReader reader;
PdfStamper stamper;
FileStream fs;

byte[] binaryPdf = File.ReadAllBytes(inputFile);
reader = new PdfReader(binaryPdf);

fs = new FileStream(inputFile, FileMode.Create, FileAccess.Write);
stamper = new PdfStamper(reader, fs);

BaseFont bf = BaseFont.CreateFont("c:/windows/fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
PdfContentByte cb = stamper.GetOverContent(1);

Phrase p = new Phrase();
p.Font = new Font(bf, 25, Font.NORMAL, BaseColor.BLUE);
p.Add("Sample Text");

ColumnText.ShowTextAligned(cb, PdfContentByte.ALIGN_LEFT, p, 200, 200, 0);

if (stamper != null)
    stamper.Close();

if (fs != null)
    fs.Close();

if (reader != null)
    reader.Close();

程序按预期运行,没有错误。但是,如果我想在 Windows 上的 Acrobat Reader 11 或 DC 中打开 stamped pdf,它会说显示内容存在问题,并且没有标记文本。 我使用 itextsharp 5.5.8。

知道如何解决这个问题吗?

谢谢

【问题讨论】:

    标签: pdf unicode itextsharp itext acrobat


    【解决方案1】:

    这不是 iText(Sharp) 的问题,而是 Adob​​e Reader 的一个怪癖(功能?)。

    您的示例文件声称是 PDF 1.2 文件。面对 PDF 1.2 文件中的复合字体时,Adobe Reader 的行为似乎有所不同。

    您可以通过修补您的 sampleStamped.pdf 来检查这一点,只需将第一个字节 %PDF-1.2 替换为 %PDF-1.3 并在 Adob​​e Reader 中打开文件...没有问题了。

    因此,您应该确保您盖章的 PDF 声称至少是 PDF 1.3。如果您在 PDF 1.2 文件上加盖戳,您可以像这样创建 PdfStamper

    stamper = new PdfStamper(reader, fs, (char)3);
    

    结果:

    【讨论】:

    • 查看 PDF 版本是一个不错的捕获,以及我从未需要使用的重载构造函数 - +1。
    • @kuujinbo :) 实际上我花了很长时间才考虑这里的版本号......
    • 很好的答案!谢谢你。这对我来说很好。 +1 并被接受为答案。
    猜你喜欢
    • 1970-01-01
    • 2012-11-03
    • 2016-11-18
    • 1970-01-01
    • 1970-01-01
    • 2017-01-28
    • 2020-04-08
    • 1970-01-01
    • 2015-11-15
    相关资源
    最近更新 更多