iTextg(for android) 不会呈现 table 并忽略字体设置

iTextg(for android) wont render table and ignores font settings

我的问题有两个方面。

首先,当我创建一个 Paragraph 对象、添加一些文本和一个自定义 Font 对象时,Font 对象被完全忽略并且对文本没有影响。

其次,当我创建一个 PdfTable 时,添加一行只是为了测试,它根本没有被渲染。

顺便说一下,此代码的 90% 来自 here and here 的教程,所有这些都得到了积极的反馈。这是完整的代码:

PdfPTable table;
private void createPdf() throws FileNotFoundException, DocumentException {

    Font bfBold12 = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD, new BaseColor(0, 0, 0));
    Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 25, Font.BOLD, new BaseColor(0, 0, 0));

    Font bf12 = new Font(Font.FontFamily.TIMES_ROMAN, 12);
    File pdfFolder = new File(Environment.getExternalStorageDirectory()+ "/pdfdemo");
    if (!pdfFolder.exists()) {
        pdfFolder.mkdirs();
        //Log.i(LOG_TAG, "Pdf Directory created");
    }

    //Create time stamp
    Date date = new Date() ;
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(date);

    myFile = new File(pdfFolder, "testPDF.pdf");

    OutputStream output = new FileOutputStream(myFile);

    //Step 1
    Document document = new Document();
    document.setPageSize(PageSize.LETTER);

    //Step 2
    PdfWriter.getInstance(document, output);

    //Step 3
    document.open();

    Paragraph top = new Paragraph("Quotation");
    top.setAlignment(Element.ALIGN_CENTER);
    top.setFont(titleFont);//completely ignored

    document.add(top);

    Chunk glue = new Chunk(new VerticalPositionMark());

    Paragraph p = new Paragraph("Text to the left");
    p.add(new Chunk(glue));
    p.add("Text to the right");

    document.add(p);

    //specify column widths
    float[] columnWidths = {1.5f, 6f, 2f, 2f, 2f, 2f};
    //create PDF table with the given widths
    table = new PdfPTable(columnWidths);
    // set table width a percentage of the page width
    table.setWidthPercentage(90f);

    insertCell("Item No.", Element.ALIGN_CENTER, 1, bfBold12);
    insertCell("Description", Element.ALIGN_CENTER, 1, bfBold12);
    insertCell("Qty", Element.ALIGN_CENTER, 1, bfBold12);
    insertCell("Discount(%)", Element.ALIGN_CENTER, 1, bfBold12);
    insertCell("Unit Price", Element.ALIGN_CENTER, 1, bfBold12);
    insertCell("Line Total", Element.ALIGN_CENTER, 1, bfBold12);

    table.setHeaderRows(1);

    document.add(table);

    document.close();

}


private void insertCell( String text, int align, int colspan, Font font){

    //create a new cell with the specified Text and Font
    PdfPCell cell = new PdfPCell(new Phrase(text.trim(), font));
    //set the cell alignment
    cell.setHorizontalAlignment(align);
    //set the cell column span in case you want to merge two or more cells
    cell.setColspan(colspan);
    //in case there is no text and you wan to create an empty row
    if(text.trim().equalsIgnoreCase("")){
        cell.setMinimumHeight(10f);
    }

    //add the call to the table
    table.addCell(cell);      
}

这是输出:

Paragraph top = new Paragraph("Quotation");
top.setAlignment(Element.ALIGN_CENTER);
top.setFont(titleFont);//completely ignored

这完全被忽略了,因为你没有在设置字体后向段落添加文本。该字体不适用于现有内容,但适用于以后添加的内容(无字体)。

table.setHeaderRows(1);

由于您只为一行添加单元格并声明一个 header 行,因此 table 只有 header 没有内容。因此,它是空的,根本不会被绘制。