iText7中AcroForm替换字体及多语言(含高棉语)渲染问题咨询
Got it, let's tackle this Khmer ligature issue—since iText5's font substitution doesn't handle complex script layouts like Khmer's ligatures, switching to iText7 with the pdfCalligraph plugin is exactly the right move. Here's a step-by-step guide to get your multi-language form filling working correctly:
1. Ensure Dependencies Are Correct
First, make sure you have both the iText7 core library and the pdfCalligraph plugin added to your project. If you're using Maven, your dependencies should look something like this (adjust versions as needed):
<dependency> <groupId>com.itextpdf</groupId> <artifactId>itext7-core</artifactId> <version>7.2.5</version> <type>pom</type> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>pdfcalligraph</artifactId> <version>3.0.3</version> </dependency>
2. Core Implementation Code
The key differences from iText5 are enabling the pdfCalligraph plugin, using PdfFont instead of the old font classes, and setting up a FontProvider to manage your substitution fonts. Here's a complete example:
import com.itextpdf.kernel.font.PdfFont; import com.itextpdf.kernel.font.PdfFontFactory; import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.PdfWriter; import com.itextpdf.kernel.pdf.PdfEncodings; import com.itextpdf.forms.PdfAcroForm; import com.itextpdf.forms.fonts.FontProvider; import com.itextpdf.pdfcalligraph.PdfCalligraph; public class MultiLangFormFiller { public static void main(String[] args) throws Exception { // Load your fonts—make sure the Khmer font supports ligatures (e.g., Khmer OS System) PdfFont khmerFont = PdfFontFactory.createFont("path/to/KhmerOS.ttf", PdfEncodings.IDENTITY_H); PdfFont arialFont = PdfFontFactory.createFont("path/to/arial.ttf", PdfEncodings.IDENTITY_H); // Initialize input/output documents PdfReader reader = new PdfReader("your-input-form.pdf"); PdfWriter writer = new PdfWriter("filled-output.pdf"); PdfDocument pdfDoc = new PdfDocument(reader, writer); // Enable pdfCalligraph to handle complex script rendering PdfCalligraph.addPdfCalligraph(pdfDoc); // Access the AcroForm and set up font provider PdfAcroForm form = PdfAcroForm.getAcroForm(pdfDoc, true); FontProvider fontProvider = new FontProvider(); fontProvider.addFont(arialFont); fontProvider.addFont(khmerFont); form.setFontProvider(fontProvider); // Fill your form fields with multi-language text form.getField("khmer-field").setValue("សួស្តី ពិភពលោក"); // Khmer "Hello World" with ligatures form.getField("chinese-field").setValue("你好世界"); form.getField("korean-field").setValue("안녕하세요"); // Clean up pdfDoc.close(); reader.close(); writer.close(); } }
3. Key Notes for Success
- Use
IDENTITY_HEncoding: This ensures the font can render all Unicode characters, including complex Khmer ligatures, instead of limited legacy encodings. - Pick a Khmer-Friendly Font: Not all fonts support Khmer ligatures—stick to standard options like Khmer OS System, Khmer OS Content, or Noto Sans Khmer for reliable rendering.
- Enable pdfCalligraph: The
PdfCalligraph.addPdfCalligraph(pdfDoc)line is critical—it tells iText7 to apply complex script layout rules (like ligature formation) for languages that require it. - Test with a Proper PDF Reader: Some lightweight readers might not render Khmer ligatures correctly even if the PDF is generated properly—use Adobe Acrobat or a fully standards-compliant reader to verify results.
内容的提问来源于stack exchange,提问作者Dmytro Hryb

