如何用iTextSharp导出含希腊字母的PDF?求助技术专家
Hey there, I see you're stuck getting Greek characters to show up in your iTextSharp-generated PDFs—let's sort this out! The core issue is that the default font iTextSharp uses doesn’t include Greek glyphs, so we need to explicitly use a font that supports Unicode (including Greek) and embed it in the PDF so it renders properly on any machine.
Here's how to adjust your code step by step:
1. Use a Unicode-Supported Font
First, pick a font that includes Greek characters. Arial Unicode MS is a safe bet (it’s included with most Windows systems), or you can use an open-source alternative like Noto Sans Greek if you prefer. We’ll load this font as a BaseFont with the correct Unicode encoding.
2. Revised Code with Greek Font Support
Update your existing code to create and apply the custom font to all text elements. Here’s the fixed version:
int.TryParse(Settings.Default["Invoice_counter"].ToString(), out counter); counter++; Settings.Default["Invoice_counter"] = counter; Settings.Default.Save(); PdfWriter wri = PdfWriter.GetInstance(doc, new FileStream(save.FileName, FileMode.Create)); doc.Open(); // Load a font that supports Greek characters (Arial Unicode MS example) BaseFont greekFontBase = BaseFont.CreateFont( @"C:\Windows\Fonts\ARIALUNI.TTF", // Path to system font BaseFont.IDENTITY_H, // Unicode encoding BaseFont.EMBEDDED // Embed font in PDF for universal display ); Font greekFont = new Font(greekFontBase, 12); // Adjust font size as needed PdfPTable table = new PdfPTable(2); table.DefaultCell.Border = 0; // Apply Greek font to your text Chunk Chunk no = new Chunk("Α.Φ.Τ: ..." + Environment.NewLine + Environment.NewLine + "Φ.Π.Α: ..." + Environment.NewLine + Environment.NewLine + "No: " + counter, greekFont); Paragraph invoice = new Paragraph("Invoice", greekFont); // Apply font here too if needed iTextSharp.text.Image PNG = iTextSharp.text.Image.GetInstance("logo.png"); PNG.ScalePercent(25f); table.AddCell(PNG); table.AddCell(new Phrase(no)); // Don't use ToString()—it loses font formatting! doc.Add(table); doc.Add(invoice); doc.Close();
Key Fixes Explained:
- Font Encoding & Embedding:
BaseFont.IDENTITY_Hensures full Unicode support, andBaseFont.EMBEDDEDmakes sure the font is included in the PDF (so Greek characters show up even on devices without the font installed). - Proper Text Element Handling: We pass the
greekFontdirectly toChunkandParagraphconstructors. Avoid usingno.ToString()—this converts the formatted Chunk to plain text and loses all font information. - Portability Tip: If deploying to other machines, embed the font file in your project (set its "Copy to Output Directory" property to "Copy if newer") and use a relative path instead of the system font path.
For Full Greek DataGridView Export
When you’re ready to export your entire Greek DataGridView, apply the same greekFont to every cell in your PDF table. For each cell value from the grid, create a Phrase or Chunk with the greekFont and add it to the PDF table cell. This ensures all Greek text from the grid renders correctly.
内容的提问来源于stack exchange,提问作者Antreas Antreou

