我的问题是双重的。
首先,在创建段落对象、添加文本和自定义字体对象时,字体对象将被完全忽略,对文本没有任何影响。
其次,当我创建一个PdfTable时,添加一个行只是为了测试,它根本不被呈现。
顺便说一下,90%的代码来自这里和这里的教程,它们都有积极的反馈。以下是完整的代码:
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);
},这是输出:

发布于 2016-08-06 10:26:33
Paragraph top = new Paragraph("Quotation");
top.setAlignment(Element.ALIGN_CENTER);
top.setFont(titleFont);//completely ignored这完全被忽略,因为您没有在设置字体后向段落添加文本。字体不适用于现有内容,而适用于以后添加的内容(没有字体)。
table.setHeaderRows(1);当您只为一行添加单元格并声明一个标题行时,该表只具有标题,而没有内容。因此,它是空的,根本不会画出来。
https://stackoverflow.com/questions/38798673
复制相似问题