0

I have a function to place my text in the document into something like a table.

    private static void addCenteredParagraph(Document document, float width, String text) {
    PdfFont timesNewRomanBold = null;
    try {
    timesNewRomanBold = PdfFontFactory.createFont(StandardFonts.TIMES_BOLD);
    } catch (IOException e) {
    LOGGER.error("Failed to create Times New Roman Bold font.");
    LOGGER.error(e);
    }

    List<TabStop> tabStops = new ArrayList<>();

    // Create a TabStop at the middle of the page
    tabStops.add(new TabStop(width / 2, TabAlignment.CENTER));

    // Create a TabStop at the end of the page
    tabStops.add(new TabStop(width, TabAlignment.LEFT));

    Paragraph p = new Paragraph().addTabStops(tabStops).setFontSize(14);
    if (timesNewRomanBold != null) {
        p.setFont(timesNewRomanBold);
    }
    p.add(new Tab()).add(text).add(new Tab());
    document.add(p);
    }

But my problem is it shows empty characters in the exported PDF instead of the letters ő, Ő,ű, Ű.

Times New Roman supports these characters, so I think I need to set it to UTF8, but I couldn't find a workaround on Google to do it.

Can someone please explain how to make these characters appear properly on the pdf?

Tried these, but some of them are deprecated functions, or not applicable to the arguments I'm trying to give them, or I don't use Chunk. Itext PDF writer, Is there any way to allow unicode subscript symbol in the pdf? (Without setTextRise) How can I set encoding for iText when I insert value to placeholder in pdf form?

Edit: I figured out, that timesNewRomanBold is null, even if I set it to HELVETICA or COURIER.

Edit 2: I also tried with ChatGPT, but the program still doesn't show these characters:

    import java.io.IOException;
    
    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.Document;
    import com.itextpdf.layout.element.Paragraph;
    import com.itextpdf.layout.property.TextAlignment;
    import com.itextpdf.layout.property.VerticalAlignment;
    
    public class PDFMaker {
        public static void main(String[] args) {
            try {
                PdfDocument pdfDoc = new PdfDocument(new PdfWriter("output.pdf"));
                Document document = new Document(pdfDoc);
    
                PdfFont font = PdfFontFactory.createFont("C:/Windows/Fonts/arial.ttf", PdfEncodings.UTF8, true);
    
                Paragraph paragraph = new Paragraph("Árvíztűrő tükörfúrógép.");
                paragraph.setFont(font);
                paragraph.setTextAlignment(TextAlignment.CENTER);
                paragraph.setVerticalAlignment(VerticalAlignment.MIDDLE);
                document.add(paragraph);
    
                document.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

1 Answers1

0

I finally made it. I don't know why, but UTF-8 is not working with ő, Ő, ű, Ű characters, but CP1250 does.

    PdfFont font = PdfFontFactory.createFont("C:/Windows/Fonts/arial.ttf", PdfEncodings.CP1250, true);