简体   繁体   中英

adding cell of a table in itext

why is the cell containing paragraph p not being added to table?

package iText;

import java.io.FileOutputStream;
import java.io.IOException;
import com.itextpdf.text.Document;
import com.itextpdf.text.*;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Font.FontFamily;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import java.io.FileOutputStream;

public class NewMain1 {
    public static void main(String[] args) {
        Document document = new Document();

        try {
            PdfWriter.getInstance(document,
                new FileOutputStream("C:\\Users\\om\\Desktop\\pdf\\2.pdf"));

            document.open();

            PdfPTable table = new PdfPTable(3); // 3 columns.
             Font font = new Font(FontFamily.HELVETICA, 22, Font.BOLD, BaseColor.WHITE);
        Paragraph p = new Paragraph("xyz", font);
        table.addCell("abc");    
        table.addCell(p);

            table.addCell("cef");
            table.addCell("ghi");
           // table.completeRow();
            PdfPCell cell1 = new PdfPCell(new Paragraph("Cell 1"));
            PdfPCell cell2 = new PdfPCell(new Paragraph("Cell 2"));
            PdfPCell cell3 = new PdfPCell(new Paragraph("Cell 3"));

            table.addCell(cell1);
            table.addCell(cell2);
            table.addCell(cell3);
cell1.setHorizontalAlignment(Element.ALIGN_LEFT);

            document.add(table);

            document.close();
        } catch(Exception e){

        }
    }
}

ps I'm a newbie at this itext. It would be great if anyone could explain me how to generate pdf forms through itext. Thanks in advance :)

You're create a white font and drawing it on a canvas that is usually displayed as white. It is there, you just can't see it. Try changing the color to something else like BaseColor.BLACK .

EDIT

To answer the question in your comment, one of the overloads for the Document constructor takes a rectangle that defines the default size for pages. iText has a helper class called PageSize that defines many common pages but you are free to use any dimension from 3 x 3 up to 14,400 x 14,400 . For instance, you could say new Document(PageSize.LETTER) or new Document(new RectangleReadOnly(612, 702)) .

Once you know that, you've got your maximum x and y and unless you do something really weird your minimum x and y are both zero. With PDFs, the bottom left corner is the origin point and thus 0 x 0 .

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