iText 8.0.5 在 Java 中管理法语重音

问题描述 投票:0回答:1

我正在使用 iTextPdf 8.0.5 生成 PDF。 我对 é、à、â、ç 等法语字符有疑问。 例如“défunt”这个词。 我要么得到“dfunt”,要么“d©funt”,要么“défunt”

我尝试使用标准字体、使用本地 ttf 字体、使用 UTF8、使用此页面中的其他编码: https://api.itextpdf.com/iText/java/latest/com/itextpdf/io/font/PdfEncodings.html

但是没有任何作用。

这是我的代码(我删除了一些内容以保留带有强调的单元格)

import com.itextpdf.io.font.constants.StandardFonts;
import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.font.PdfFontFactory.EmbeddingStrategy; 
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfVersion;
import com.itextpdf.kernel.pdf.WriterProperties;
import com.itextpdf.kernel.colors.ColorConstants;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.element.Text;
import com.itextpdf.layout.element.Table;
import com.itextpdf.layout.properties.UnitValue;
import com.itextpdf.layout.element.Cell;
import com.itextpdf.layout.properties.TextAlignment;
import com.itextpdf.layout.borders.SolidBorder; 


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class EtiquettesServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        String fontPath = "E:/Tomcat 9.0/webapps/Thanact/calibri.ttf"; 
        
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=etiquettes.pdf");
        
        try {

            PdfWriter writer = new PdfWriter(response.getOutputStream(), new WriterProperties().setPdfVersion(PdfVersion.PDF_2_0));
//          writer.setPdfVersion(PdfVersion.PDF_2_0);

            PdfDocument pdfDoc = new PdfDocument(writer);

            Document document = new Document(pdfDoc);

            PdfFont font = PdfFontFactory.createFont(fontPath, "UTF-8");

            Table table = new Table(UnitValue.createPercentArray(4)).useAllAvailableWidth(); // 4 colonnes

            Text    label = new Text("Nom défunt : ").setBold();
            Text    value = new Text("MyValue");                    
            cell.add(new Paragraph().add(label).add(value).setFont(font).setFontSize(8));
            
            table.addCell(cell);
            
            document.add(table);
            document.close();

        } catch (Exception e) {
            e.printStackTrace();
            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Erreur lors de la génération du PDF : " + e.getMessage());
            System.out.println(e.getMessage());
        }
    }
}

我在这里找到了一些信息: pdfcell itext 中的 UTF-8 字符

但看起来像是 iText 7 的

感谢您的帮助

java itext
1个回答
0
投票

改变

PdfFont font = PdfFontFactory.createFont(fontPath, "UTF-8");

import com.itextpdf.io.font.PdfEncodings;
....
PdfFont font = PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H);

001.png

我的测试环境

  • Linux
  • LANG = en_US.UTF-8
  • 字体 calibri.ttf(Windows 11)

DocumentWithFrench.java

package com.example;

import com.itextpdf.io.font.PdfEncodings;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.Document;


import java.io.FileNotFoundException;

public class DocumentWithFrench {
    public static void main(String[] args) throws FileNotFoundException {
        //String fontPath = "E:/Tomcat 9.0/webapps/Thanact/calibri.ttf";
        String fontPath = "fonts/calibri.ttf";
        String  frenchString = "Nom défunt (é, à, â, ç): ";
        PdfWriter writer = new PdfWriter("etiquettes.pdf");
        PdfDocument pdf = new PdfDocument(writer);

        try (Document document = new Document(pdf)) {
            PdfFont fontUTF8      = PdfFontFactory.createFont(fontPath, "UTF-8");
            PdfFont fontIDENTITY_H    = PdfFontFactory.createFont(fontPath, PdfEncodings.IDENTITY_H);
            //Text    label = new Text("Nom défunt : ").setBold();
            //Text    value = new Text("MyValue");
            Paragraph paragraph = new Paragraph()
                    .add(new Paragraph("Hello 1 >>>> "))
                    .add(new Paragraph(frenchString).setFont(fontUTF8).setFontSize(20) //FAIL
                    .add(new Paragraph("Hello 2 >>>> "))
                    .add(new Paragraph(frenchString).setFont(fontIDENTITY_H).setFontSize(20))
                    );
            document.add(paragraph);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.