我正在使用itextpdf5.3.4
开发用于在pdf中创建表的android应用程序需求。
我在pdf中遇到与表有关的问题。我在绝对位置的一个表中创建25条记录。如果有超过25条记录意味着在新页面中再创建一个表。如果我有两个表,第一个表中的文字变暗了。
下面我粘贴了我的代码:
Document document = new Document();
try {
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/report.pdf");
initializeFonts();
Font hedder_font= new Font(bfBold, 8);
Font content_font = new Font(bf, 8);
Font color_font = new Font(bf, 8);
color_font.setColor(new BaseColor(0,172,237));
PdfWriter docWriter= PdfWriter.getInstance(document, new FileOutputStream(file));
docWriter.setRgbTransparencyBlending(true);
BaseReportBuilder event = new BaseReportBuilder();
docWriter.setPageEvent(event);
document.open();
PdfContentByte cb = docWriter.getDirectContent();
if (list.size() > 0) {
createHeadings(cb,25,620,"Details",10,bfBold);
}
//list all the products sold to the customer
float[] columnWidths = {0.5f,3f, 1f, 1f,1f};
//create PDF table with the given widths
PdfPTable table = new PdfPTable(columnWidths);
// set table width a percentage of the page width
table.setTotalWidth(500f);
PdfPCell cell = new PdfPCell(new Phrase("Sr",hedder_font));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Item ",hedder_font));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Date",hedder_font));
cell.setHorizontalAlignment(Element.ALIGN_LEFT);
table.addCell(cell);
cell = new PdfPCell(new Phrase("id",hedder_font));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
cell = new PdfPCell(new Phrase("Amount",hedder_font));
cell.setHorizontalAlignment(Element.ALIGN_RIGHT);
table.addCell(cell);
table.setHeaderRows(1);
if (list.size() > 25) {
int value =630;
for (int i = 0; i < 25; i++) {
System.out.println("i" + i);
value = value - 15;
String date = list.get(i).getCreated_on();
table.addCell(new PdfPCell(new Phrase(String.valueOf(i+1),content_font)));
table.addCell(new PdfPCell(new Phrase(list.get(i).getItem(),content_font)));
table.addCell(new PdfPCell(new Phrase(date,content_font)));
table.addCell(new PdfPCell(new Phrase(list.get(i).getCategory_name(), content_font)));
table.addCell(new PdfPCell(new Phrase(list.get(i).getAmount(), content_font)));
table.writeSelectedRows(0, -1, document.leftMargin(),610, docWriter.getDirectContent());
}
document.newPage();
//create PDF table with the given widths
PdfPTable table1 = new PdfPTable(columnWidths);
// set table width a percentage of the page width
table1.setSpacingAfter(250f);
table1.setTotalWidth(500f);
for (int i = 25; i < list.size(); i++) {
String date = list.get(i).getCreated_on();
table1.addCell(new PdfPCell(new Phrase(String.valueOf(i+1),content_font)));
table1.addCell(new PdfPCell(new Phrase(list.get(i).getItem(), content_font)));
table1.addCell(new PdfPCell(new Phrase(date,content_font)));
table1.addCell(new PdfPCell(new Phrase(list.get(i).getItemid(), content_font)));
table1.addCell(new PdfPCell(new Phrase(list.get(i).getAmount(), content_font)));
}
//absolute location to print the PDF table from
if (list.size() > 0) {
table1.addCell(new PdfPCell(new Phrase("Total",content_font)));
table1.addCell(new PdfPCell(new Phrase("",content_font)));
table1.addCell(new PdfPCell(new Phrase("",content_font)));
table1.addCell(new PdfPCell(new Phrase("",content_font)));
table1.addCell(new PdfPCell(new Phrase(""+""+total,hedder_font)));
}
document.add(table1);
}
else {
for (int i = 0; i < list.size(); i++) {
System.out.println("i"+i);
String date = list.get(i).getCreated_on();
table.addCell(new PdfPCell(new Phrase(String.valueOf(i+1),content_font)));
table.addCell(new PdfPCell(new Phrase(list.get(i).getItem(),content_font)));
table.addCell(new PdfPCell(new Phrase(date,content_font)));
table.addCell(new PdfPCell(new Phrase(list.get(i).getItemid(), content_font)));
table.addCell(new PdfPCell(new Phrase(list.get(i).getAmount(), content_font)));
}
if (list.size() > 0) {
table.addCell(new PdfPCell(new Phrase("Total",content_font)));
table.addCell(new PdfPCell(new Phrase("",content_font)));
table.addCell(new PdfPCell(new Phrase("",content_font)));
table.addCell(new PdfPCell(new Phrase("",content_font)));
table.addCell(new PdfPCell(new Phrase(""+""+total,hedder_font)));}
table.writeSelectedRows(0, -1, document.leftMargin(),610, docWriter.getDirectContent());
}
System.out.println("docWriter.getVerticalPosition(true)" + docWriter.getVerticalPosition(true));
if (docWriter.getVerticalPosition(true) < 100) {
document.newPage();
}
createHeadings(cb,30,100,"Number of items : " +list.size(),12,bfBold);
createHeadings(cb,495,100,": " +list.size(),11,bfBold);
createHeadings(cb,350,75,"Total :",11,bfBold);
createHeadings(cb,495,75,": " +total,12,bfBold);
document.close();
}
}
catch(Exception e){
e.printStackTrace();
}
private void initializeFonts() {
try {
bfBold = BaseFont.createFont(BaseFont.HELVETICA_BOLD, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
bf =BaseFont.createFont(BaseFont.HELVETICA, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}