0

My PDF is not displayed properly with Adobe Reader. It is fine with other PDF readers so this must be a syntax issue, as I've heard that Adobe Reader is more strict with PDF syntax. The fonts seem to be twice as big as they should be but the horizontal spacing is correct, this makes the fonts overlap with each other.

This is my C# code (font creation code is at the end of this post).

Font officialUseFont = EmbeddedResources.CreateDesignFont(webform);
PdfContentByte officialUseCanvas = _stamper.GetOverContent(3);
ColumnText.ShowTextAligned(officialUseCanvas, Element.ALIGN_CENTER, new Phrase(webform.Text, officialUseFont), posX, posY, 0);

I'm using iTextSharp 5.4.2.0 with runtime v2.0.50727.PDF Font Properties

I must have embedded some fonts because the Cyrillic alphabet and Chinese alphabets were not working before but they work now. The form fields which exist in the PDF are populated with Cyrillic characters without any problems, it's only the canvas which causes the issue.enter image description here

public Font CreateDesignFont(IForm webform)
{
    var baseFont = GetBaseFont(fontNamespace.Length, selectedFontName);
    return new Font(baseFont, webform.FontSize);
}
    private static BaseFont GetBaseFont(int fontNamespaceLength, string selectedFontName)
    {

        byte[] fontBuffer;
        using (var stream = (Assembly.GetExecutingAssembly().GetManifestResourceStream(selectedFontName)))
        {
            fontBuffer = new byte[stream.Length];
            stream.Read(fontBuffer, 0, fontBuffer.Length);
        }
        var fontfile = selectedFontName.Substring(fontNamespaceLength);
        var customFont = BaseFont.CreateFont(fontfile, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, BaseFont.CACHED, fontBuffer, null);
        return customFont;
    }
fireydude
  • 1,181
  • 10
  • 23

2 Answers2

0

The code snippet you're providing isn't sufficient. The problem originates in whatever happens when you do EmbeddedResources.CreateDesignFont(webform);

If the font isn't shown in Adobe Reader, you didn't embed the font. Maybe you think you do, but judging by the behavior of the PDF viewers, you didn't.

Can you provide a screen shot of the Document Properties, more specifically the "Fonts" tab?

UPDATE

I tried writing my own code snippet, and I wasn't able to reproduce the problem. So I took another look at your code, and I saw that you're caching the font, but you've already used ArialMT using the WINANSI encoding to fill out the fields on page 1. IMO (I don't have the time to check) that's incompatible with using the same font from cache using IDENTITY_H. If you don't cache the font (why would you? you're passing the fontBuffer! no need to store the font in a cache if you're already caching the font bytes yourself), your problem will probably be solved.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • I am using Times New Roman with IDENTITY_H encoding. The post has been updated with additional information. – fireydude Aug 14 '13 at 14:22
  • I have stepped through the iTextSharp source code for this but still can't find the problem. The embedded flag has been set so it should be embedded. – fireydude Aug 15 '13 at 10:36
0

The problem stemmed from the fact I was using .otf fonts. When I changed to .ttf the problem disappeared.

fireydude
  • 1,181
  • 10
  • 23