1

I am trying create a PDF file using iText library. But when i use add function in directly to the document, font features doesn't recognize propery.

Actually font size recognized and space after & before too much but String itself is still default i think.

Strange thing is when i use add function of cell all font features recognized properly.

Here is the self-describing code and screenshot of output pdf;

package samplepdfcreator;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import static com.itextpdf.text.Element.ALIGN_CENTER;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.IOException;


public class NewClass {

    public static final String RESULT = "C:\\Users\\World_Home\\Desktop\\hello.pdf";

    public static void main(String[] args) throws DocumentException, IOException {
        new NewClass().createPdf(RESULT);
    }

    public void createPdf(String filename)throws DocumentException, IOException {

        Document document = new Document();

        //Create PDF at specified filepath above.
        PdfWriter.getInstance(document, new FileOutputStream(RESULT));
        document.setPageSize(PageSize.A4);
        document.setMargins(36, 36, 50, 50);
        document.setMarginMirroring(true);

        //Open the created PDF and begin editing.
        document.open();

        //First Table Adding
        PdfPTable table = new PdfPTable(1);
        PdfPCell cell;
        Font font;
        Paragraph paragraph;

        cell = new PdfPCell();
        cell.setColspan(1);
        font = new Font();
        font.setSize(12);
        font.setStyle("bold");
        paragraph = new Paragraph("Header Inside table1");
        paragraph.setFont(font);
        paragraph.setAlignment(ALIGN_CENTER);
        cell.addElement(paragraph);


        paragraph = new Paragraph("Info in table1");
        paragraph.setAlignment(ALIGN_CENTER);
        cell.addElement(paragraph);

        paragraph = new Paragraph(" ");
        cell.addElement(paragraph);

        table.addCell(cell);
        table.setWidthPercentage(100);
        table.setSpacingAfter((float) 0);
        table.setSpacingBefore((float) 0);

        document.add(table);

        //Adding one blank line
        font = new Font();
        font.setSize(12);
        paragraph = new Paragraph(" ");
        document.add(paragraph);

        //Adding Some String to Document directly 2times **Problem is Here**
        font = new Font();
        font.setStyle("bold");
        font.setSize(50);
        paragraph = new Paragraph("String In Document");
        paragraph.setFont(font);
        paragraph.setAlignment(ALIGN_CENTER);

        document.add(paragraph);
        document.add(paragraph);

        //Adding one blank line
        font = new Font();
        font.setSize(12);
        paragraph = new Paragraph(" ");
        document.add(paragraph);


        //Second Table Adding
        PdfPTable table2 = new PdfPTable(2);

        cell = new PdfPCell();
        cell.setColspan(2);
        font.setSize(12);
        font.setStyle("bold");
        paragraph = new Paragraph("Header Inside table2");
        paragraph.setFont(font);
        paragraph.setAlignment(ALIGN_CENTER);
        cell.addElement(paragraph);

        paragraph = new Paragraph("Info in table2");
        paragraph.setAlignment(ALIGN_CENTER);
        cell.addElement(paragraph);

        paragraph = new Paragraph(" ");
        cell.addElement(paragraph);

        table2.addCell(cell);
        table2.setWidthPercentage(100);
        table2.setSpacingAfter((float) 0);
        table2.setSpacingBefore((float) 0);

        document.add(table2);

        //Close the PDF
        document.close();


    }

}

enter image description here

Black White
  • 700
  • 3
  • 11
  • 31
  • 1
    I always forget that I have the power to close a question immediately. In any case, [this answer](http://itextpdf.com/question/why-cant-i-set-font-phrase) explains what you're doing wrong: `setFont()` performed on a `Phrase` or `Paragraph` doesn't change the font of the `Chunk`s that are already present in the `Phrase` or `Paragraph`. It changes the font for the content that is added as a `String` *after* the font has been set. – Bruno Lowagie Nov 28 '15 at 11:40
  • `paragraph = new Paragraph("String In Document", font);` is working fine. Thanks for little trick. – Black White Nov 28 '15 at 12:38

0 Answers0