首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iTextg(用于android)不会呈现表并忽略字体设置

iTextg(用于android)不会呈现表并忽略字体设置
EN

Stack Overflow用户
提问于 2016-08-05 22:41:54
回答 1查看 85关注 0票数 0

我的问题是双重的。

首先,在创建段落对象、添加文本和自定义字体对象时,字体对象将被完全忽略,对文本没有任何影响。

其次,当我创建一个PdfTable时,添加一个行只是为了测试,它根本不被呈现。

顺便说一下,90%的代码来自这里这里的教程,它们都有积极的反馈。以下是完整的代码:

代码语言:javascript
复制
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);      
}

,这是输出:

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-06 10:26:33

代码语言:javascript
复制
Paragraph top = new Paragraph("Quotation");
top.setAlignment(Element.ALIGN_CENTER);
top.setFont(titleFont);//completely ignored

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

代码语言:javascript
复制
table.setHeaderRows(1);

当您只为一行添加单元格并声明一个标题行时,该表只具有标题,而没有内容。因此,它是空的,根本不会画出来。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38798673

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档