我PdfPTable
的 PDF 的页脚有一个。页脚应包含三个句子,一个在另一个之下。此页脚下方还应有条形码图像。在我当前的实现中,我是通过以下方式进行的:
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 种变体
- 将图像直接添加到页脚表。
- 将图像添加到 PDfPCell,然后将其添加到页脚表。
- 将图像添加到 PDFPTable,然后将其添加到页脚表。
但是,在上述所有 3 种情况下,我得到了意想不到的结果,我的条码被拉伸得超过了我要求的宽度。见下图。
如果有人可以帮助我了解如何更改我的最后一个单元格宽度以匹配我的原始实现,那就太好了。
提前致谢!