Search code examples
pdfunicodeitextacrobat

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


I'd like to stamp text to a certain pdf with iText. As this text can be cyrillic I use a unicode font and encoding. The following sample code represents how I do it:

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();

The program works as expected and without errors. But if I want to open the stamped pdf in Acrobat Reader 11 or DC on Windows it says that there are proplems to display content and the stamped text is not there. I use itextsharp 5.5.8.

Any idea how to fix this problem?

Thanks


Solution

  • This is not an issue of iText(Sharp) but a quirk (a feature?) of Adobe Reader.

    Your sample file claims to be a PDF 1.2 file. Adobe Reader seems to behave differently when confronted with composite fonts in PDF 1.2 files.

    You can check this by patching your sampleStamped.pdf, simply replace the first bytes %PDF-1.2 by %PDF-1.3 and open the file in Adobe Reader... no problem anymore.

    Thus, you should make sure that your stamped PDF claims to be at least PDF 1.3. If you stamp your PDF 1.2 file, you can do so by creating the PdfStamper like this:

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

    The result:

    Screenshot of fixed PDF