我的PDF页脚有一个PdfPTable
。页脚应该包含三个句子,一个在另一个之下。条形码图像也应位于此页脚下方。在我目前的实现中,我是按照以下方式进行的:
PdfPTable FooterNote = new PdfPTable(1);
FooterNote.setTotalWidth(222f);
PdfPCell note1 = new PdfPCell(new Paragraph("MY First Text Here",
font1));
PdfPCell note2 = new PdfPCell(new Paragraph(
"My Second Text Here.", font1));
PdfPCell note3 = new PdfPCell(new Paragraph(
"My Third Text here", font1));
note1.setBorder(Rectangle.NO_BORDER);
note2.setBorder(Rectangle.NO_BORDER);
note3.setBorder(Rectangle.NO_BORDER);
note3.setHorizontalAlignment(Element.ALIGN_CENTER);
note1.setHorizontalAlignment(Element.ALIGN_CENTER);
note2.setHorizontalAlignment(Element.ALIGN_CENTER);
FooterNote.setWidthPercentage(100f);
FooterNote.addCell(note1);
FooterNote.addCell(note2);
FooterNote.addCell(note3);
FooterNote.setHorizontalAlignment(Element.ALIGN_LEFT);
FooterNote.setKeepTogether(true);
PdfContentByte cb = writer.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setCodeType(Barcode.CODE128);
String barcodeNumber = "MY BARCODE NUMBER";
code128.setCode(barcodeNumber);
Image barcodeImage = code128.createImageWithBarcode(cb, null, null);
barcodeImage.setAlignment(Element.ALIGN_CENTER);
code128.setBarHeight(20f);
code128.setBaseline(20f);
code128.setX(10.0f);
code128.setGuardBars(false);
document.add(FooterNote);
document.add(barcodeImage);
现在根据新要求,条形码和页脚不应分成两个不同的页面。为了实现我将条形码添加到我的页脚表而不是直接添加到我的Document
变量。以下是代码:
PdfPTable FooterNote = new PdfPTable(1);
FooterNote.setTotalWidth(222f);
PdfPCell note1 = new PdfPCell(new Paragraph("My First Text Here",
font1));
PdfPCell note2 = new PdfPCell(new Paragraph(
"My Second Text Here.", font1));
PdfPCell note3 = new PdfPCell(new Paragraph(
"My Third Text Here.", font1));
note1.setBorder(Rectangle.NO_BORDER);
note2.setBorder(Rectangle.NO_BORDER);
note3.setBorder(Rectangle.NO_BORDER);
note3.setHorizontalAlignment(Element.ALIGN_CENTER);
note1.setHorizontalAlignment(Element.ALIGN_CENTER);
note2.setHorizontalAlignment(Element.ALIGN_CENTER);
FooterNote.setWidthPercentage(100f);
FooterNote.addCell(note1);
FooterNote.addCell(note2);
FooterNote.addCell(note3);
FooterNote.setHorizontalAlignment(Element.ALIGN_LEFT);
FooterNote.setKeepTogether(true);
PdfContentByte cb = writer.getDirectContent();
Barcode128 code128 = new Barcode128();
code128.setCodeType(Barcode.CODE128);
String barcodeNumber = "MY BARCODE NUMBER";
code128.setCode(barcodeNumber);
Image barcodeImage = code128.createImageWithBarcode(cb, null, null);
barcodeImage.setAlignment(Element.ALIGN_CENTER);
code128.setBarHeight(20f);
code128.setBaseline(20f);
code128.setX(10.0f);
code128.setGuardBars(false);
//FooterNote.addCell(barcodeImage);
//PdfPTable barcodeTable = new PdfPTable(1);
//barcodeTable.setTotalWidth(Utilities.millimetersToPoints(45));
//barcodeTable.setWidthPercentage(60f);
//barcodeTable.setHorizontalAlignment(Element.ALIGN_CENTER);
PdfPCell barcodeCell=new PdfPCell();
//barcodeCell.setImage(barcodeImage);
barcodeCell.addElement(barcodeImage);
barcodeCell.setHorizontalAlignment(Element.ALIGN_CENTER);
barcodeCell.setBorder(Rectangle.NO_BORDER);
//barcodeTable.addCell(barcodeCell);
FooterNote.addCell(barcodeCell);
//FooterNote.addCell(barcodeTable);
document.add(FooterNote);
我在上面的代码中尝试了3种变体