Adobe Reader 无法显示 iText 添加的 pdf 的 unicode 字体

Adobe Reader can't display unicode font of pdf added with iText

我想用 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();

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

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

谢谢

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

您的示例文件声称是 PDF 1.2 文件。 Adobe Reader 在 PDF 1.2 文件中遇到复合字体时似乎表现不同。

您可以通过修补 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);

结果: