0

PdfPTablePDF のフッターに があります。フッターには、上下に 3 つの文を含める必要があります。また、バーコード画像がこのフッターの下にあるはずです。私の現在の実装では、次のようにしています。

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);

私の現在の実装

新しい要件に従って、バーコードとフッターが 2 つの異なるページに分割されないようになりました。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つのバリエーションを試しました

  1. 画像をフッター テーブルに直接追加します。
  2. 画像を PDfPCell に追加してから、フッター テーブルに追加します。
  3. 画像を PDFPTable に追加してから、フッター テーブルに追加します。

しかし、上記の 3 つのケースすべてで、バーコードが必要な幅を超えて引き伸ばされるという予期しない結果が得られます。下の画像を参照してください。 変更後の PDF

元の実装に合わせて最後のセル幅を変更する方法について、誰かが私を助けてくれると助かります。

前もって感謝します!

4

0 に答える 0