使用iTextSharp填充PDF时无法修改字体的问题
Hey there, I’ve run into this exact font issue with PDF form filling before—let’s get it sorted out! The problem here is that just using SetFieldProperty to set "textfont" doesn’t work on its own; you need to properly load and register the font first with iText’s API.
Here’s what’s going wrong:
When you set the "textfont" property directly, iText doesn’t recognize the font unless it’s been loaded into the AcroFields context. Your code snippet cuts off at Ba..., but I’m guessing you tried passing a font object without properly initializing or registering it first.
Step-by-Step Solution:
Load your custom font correctly
First, you need to load the TrueType/OpenType font file usingBaseFont.CreateFont. Make sure the path is correct, and use the right encoding for non-ASCII characters (like Chinese) if needed.Register the font with AcroFields
UseAddSubstitutionFontto make the font available for the form fields, or target individual fields directly after loading.
Corrected Code Example:
var filename = ConfigurationManager.AppSettings["Pdf"]; var reader = new PdfReader(AppDomain.CurrentDomain.BaseDirectory + filename); var ms = new MemoryStream(); var stamper = new PdfStamper(reader, ms); try { // Load your custom font (update the path to your actual font file) BaseFont customFont = BaseFont.CreateFont( @"C:\Windows\Fonts\simsun.ttc,1", // Example: SimSun (宋体) BaseFont.IDENTITY_H, BaseFont.EMBEDDED // Embed the font so it displays correctly on any device ); // Register the font globally for all fields (optional but helpful) stamper.AcroFields.AddSubstitutionFont(customFont); // Iterate through fields to set properties foreach (var key in reader.AcroFields.Fields.Keys) { // Set font size (this part was working for you) stamper.AcroFields.SetFieldProperty(key, "textsize", 8.0f, null); // Set the custom font for the field stamper.AcroFields.SetFieldProperty(key, "textfont", customFont, null); // Don't forget to set the field value if you haven't already! // stamper.AcroFields.SetField(key, "Your input text here"); } } catch (DocumentException ex) { // Handle font loading errors (e.g., wrong path, corrupted font file) Console.WriteLine($"Font loading error: {ex.Message}"); } finally { // Always clean up resources stamper.Close(); reader.Close(); } // Use the MemoryStream as needed (save to file, return as response, etc.)
Key Notes:
- Font Path & Encoding: Double-check the font file path. For languages with non-Latin characters, use
BaseFont.IDENTITY_Hto ensure proper rendering. - Embed the Font: Using
BaseFont.EMBEDDEDensures the font is included in the PDF, so recipients don’t need the font installed on their device. - Error Handling: Wrap font loading in a try-catch to catch issues like missing font files or unsupported font formats.
If you still have trouble, verify that the original PDF form fields aren’t set to use a "locked" font (some forms enforce specific fonts), but since you can modify the font size, this is unlikely.
内容的提问来源于stack exchange,提问作者jonpfl

