简体   繁体   中英

PdfByteContent not adding in iText pdf

I'm trying to put text at specified locations in pdf and using code

public static PdfContentByte setContentSize(com.itextpdf.text.Document itextDocument) {
        itextDocument.open();
        PdfContentByte contentByte = getPdfWriter(itextDocument).getDirectContent();
        contentByte.saveState();

        Font font = new Font(FontFamily.TIMES_ROMAN, 12);
        BaseFont baseFont = font.getCalculatedBaseFont(false);

        contentByte.beginText();
        contentByte.setFontAndSize(baseFont, 12);
        contentByte.setTextMatrix(50, 800);

        return contentByte;
    }

       ColumnText ct = new ColumnText(contentByte);
                  ct.setSimpleColumn(new Phrase(new Chunk(text, FontFactory.getFont(FontFactory.HELVETICA, 18, Font.NORMAL))),
                                     46, 190, 530, 36, 25, com.itextpdf.text.Element.ALIGN_LEFT);
                  ct.go(); 

        //contentByte.showTextAligned(PdfContentByte.ALIGN_LEFT, text, 150, 240, 0);
        //Phrase phrase = new Phrase(text, new Font());
      //ColumnText.showTextAligned(contentByte, com.itextpdf.text.Element.ALIGN_LEFT, phrase, 200, 572, 0);

I have tried commented code also but the text is not printing in pdf. Any suggestion will be appreciated.

Please read the documentation before writing code. You will benefit from reading the section "Absolute position of text" in the free ebook "The Best iText Questions on StackOverflow" .

As for your code:

First you try adding text the hard way, but I see a saveState() without a restoreState() and I see a beginText() without an endText . I don't see a showText() anywher, so no text is shown.

Then you try adding text in an easier way, more specifically, a way that doesn't require beginText() , setFontAndSize() , setTextMatrix() , etc... Instead you use ColumnText .

If you combine beginText() with ColumnText , you are introducing a syntax error into your PDF. When using ColumnText , iText will call beginText() in your place, resulting in a nested text block. This is forbidden by ISO-32000-1. See PDFs generated using itextsharp giving error at the time of first print command

You say that the text that you are adding doesn't appear on the page, but we can not check if this is true because you do not provide a SSCCE . Maybe you are adding the text outside the visible area of the page. Maybe you are introducing so many syntax errors (such as the nested text blocks) that the viewer has no clue about what you want to do.

We, on the other hand, have many examples that do work. They are all explained in the answers to these questions:

There are more examples in the free ebook that bundles The Best iText Questions on StackOverflow .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM