iTextSharp PDF: Handling Central and Eastern European Characters
This guide addresses the common problem of incorrect display of Central and Eastern European characters (e.g., "Č," "Ć") in PDFs generated using iTextSharp.
The Issue: Generating PDFs with these characters often results in rendering errors.
Root Causes: Several factors can cause this:
The Solution: A Step-by-Step Approach
Accurate Code Page Selection: Use the correct code page for the specific language and character set (e.g., "Cp1250" for Czech).
Unicode Escape Sequences: Instead of using literal special characters, employ Unicode escape sequences for consistent encoding handling.
Glyph-Supporting Font: Select a font (like Arial.ttf or FreeSans.ttf) that includes the required glyphs.
Font Embedding: Ensure the font is embedded in the PDF (set the "embedded" parameter to true
) to guarantee correct display even if the font isn't available on the recipient's system.
Font Encoding Definition: Explicitly define the encoding used for glyph interpretation. Options include specifying the code page or using Unicode for horizontal writing (e.g., "Cp1250," BaseFont.IDENTITY_H
).
Code Example (Illustrative):
<code class="language-csharp">using iTextSharp.text; using iTextSharp.text.pdf; public class CEECharacterExample { public void CreatePdf(string destination) { Document document = new Document(); PdfWriter.GetInstance(document, new FileOutputStream(destination)); document.Open(); // Font with glyph support and code page Font fontCp1250 = FontFactory.GetFont("resources/fonts/FreeSans.ttf", "Cp1250", true); // Font with Unicode encoding Font fontUnicode = FontFactory.GetFont("resources/fonts/FreeSans.ttf", BaseFont.IDENTITY_H, true); // Paragraphs using different encodings Paragraph paragraphCp1250 = new Paragraph("Testing characters: \u010c, \u0106, \u0160, \u017d, \u0110", fontCp1250); Paragraph paragraphUnicode = new Paragraph("Testing characters: \u010c, \u0106, \u0160, \u017d, \u0110", fontUnicode); // Add paragraphs to the document document.Add(paragraphCp1250); document.Add(paragraphUnicode); document.Close(); } }</code>
By implementing these steps, you should resolve the character display issues in your iTextSharp PDFs. Remember to replace "resources/fonts/FreeSans.ttf"
with the actual path to your font file.
The above is the detailed content of How to Display Central and Eastern European Characters Correctly in iTextSharp PDFs?. For more information, please follow other related articles on the PHP Chinese website!