4

多くのテーブルを表示するpdfを扱っています。テーブルがページの残りの部分に収まらない場合、iText はテーブルをカットし、残りを次のページに設定します。

表がページの残りの部分に収まらない場合は、新しいページに配置してください。

どうやってやるの?

4

3 に答える 3

3

これにはさまざまな方法があります。次の 3 つのオプションがあります。

  1. テーブルの高さを計算し、残っているスペースと比較します。ドキュメントに追加する前にテーブルの高さを計算すると、setTotalWidth() および setLockedWidth() を使用してテーブルの幅を定義しない限り、高さは常に 0 になります。getVerticalPosition() メソッド (PdfWriter 内) を使用して、ページ上の現在の Y 位置を見つけることができます。
  2. テーブルを ColumnText オブジェクトに追加し、ColumnText オブジェクトを四角形に追加して、シミュレーション モードでページに残すスペースを定義します。ColumnText オブジェクトのコンテンツを完全にレンダリングできなかった場合は、実際にテーブルを追加する前に新しい Page をトリガーします。
  3. SourceForge のトランク (バージョン 5.3.3-SNAPSHOT) から最新の (まだリリースされていない) バージョンを取得する場合、table.keepRowsTogether(0); を使用して中断を回避できます。ここで、0 はテーブルの最初の行のインデックスです。
于 2012-09-10T11:00:44.473 に答える
0
public class XX_RPT_D84_AR_Invoice_Printing_GBS extends PdfPageEventHelper{
        public void onEndPage(PdfWriter writer, Document document) {
            Rectangle rect = writer.getBoxSize("art");
            ColumnText.showTextAligned(writer.getDirectContent(),
                    Element.ALIGN_CENTER, new Phrase(String.format("page %d", 1)),
                    (rect.getLeft() + rect.getRight()) / 2, rect.getBottom() - 18, 0);
        }
        public static void main(String[] args) {
            try {
                OutputStream file = new FileOutputStream(new File("D:\\PDF1.pdf"));
                Document document = new Document();
                PdfWriter writer = PdfWriter.getInstance(document, file);
                BaseFont bf = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED);
                BaseColor color = BaseColor.BLACK;
                PdfContentByte cb = null; 
                Phrase[] header = new Phrase[3];
                header [0] = new Phrase("Genpact India");
                header [1] = new Phrase("(A Private Company with Unlimited Liability)");
                header [2] = new Phrase("Registered Office: Delhi Information Technology Park, Shastri Park, Delhi-110053, India");                
                Font font = new Font(BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.NOT_EMBEDDED),
                         9, Font.NORMAL);
                boolean isPrinted = false;

                   Image image = Image.getInstance ("src/Logo/logo.png");
                   image.scaleAbsolute(120f, 31f);//image width,height   
                   image.setDpi(-10, -5);
                   image.setSpacingBefore(-15);

                   Image sign = Image.getInstance ("src/Logo/sign.png");
                   sign.scaleAbsolute(100f, 50f);//image width,height 
                   sign.setIndentationLeft(10);
                   PdfPTable table = new PdfPTable(new float[]{0.8f,5,1.5f,1.3f,2,2});
                   PdfPCell s_No = new PdfPCell(new Phrase("S No", font));
                   s_No.setBorder(Rectangle.BOX);
                   s_No.setPaddingTop(2);
                   PdfPCell description = new PdfPCell(new Phrase("Description", font));
                   description.setBorder(Rectangle.BOX);
                   description.setPaddingTop(2);
                   PdfPCell ratePerUnit = new PdfPCell(new Phrase("Rate Per Unit", font));
                   ratePerUnit.setBorder(Rectangle.BOX);
                   ratePerUnit.setPaddingTop(2);
                   PdfPCell quantity = new PdfPCell(new Phrase("Quantity", font));
                   quantity.setBorder(Rectangle.BOX);
                   quantity.setPaddingTop(2);
                   PdfPCell inrReferenceAmount = new PdfPCell(new Phrase("INR Reference Amount", font));
                   inrReferenceAmount.setBorder(Rectangle.BOX);
                   inrReferenceAmount.setPaddingTop(2);
                   PdfPCell amountINR = new PdfPCell(new Phrase("Amount INR", font));
                   amountINR.setBorder(Rectangle.BOX);
                   amountINR.setPaddingTop(2);
                   table.addCell(s_No);
                   table.addCell(description);
                   table.addCell(ratePerUnit);
                   table.addCell(quantity);
                   table.addCell(inrReferenceAmount);
                   table.addCell(amountINR);
                   int i = 1;
                   String value = "123";
                   PdfPCell cell = null;
                   int referenceAmount = 0;
                   int amount = 0;
                   PdfPCell staticData1 = new PdfPCell(new Phrase("Please remit funds via telex transfer/SWIFT as follows to HSBC BANK Limited (Bank address:"+
                           "JMD Regent Square Ist Floor, DLF Phase II, MG Road, Gurgaon 122002, Haryana, India) with the following"+
                           "Instructions:", font));
                   staticData1.setPaddingTop(20);
                   staticData1.setColspan(6);
                   staticData1.setBorder(Rectangle.NO_BORDER);
                   PdfPCell staticData2 = new PdfPCell(new Phrase("Ultimate Beneficiary: Genpact India", font));
                   staticData2.setColspan(6);
                   staticData2.setBorder(Rectangle.NO_BORDER);

                   PdfPCell staticData3 = new PdfPCell(new Phrase("A)For Inward remittances from out side India : Remit by SWIFT MT103 for further credit to Genpact"+
                            "India, Acct no - 054 021365 001 with HSBC India (SWIFT - HSBCINBB) through HSBC Mumbai's account"+
                            "no.000-04417-2, with HSBC Bank, New York, USA having CHIPS code 302755, SWIFT- MRMDUS33 & ABA Routing"+
                            "No. 021-001-088.", font));
                   staticData3.setColspan(6);
                   staticData3.setBorder(Rectangle.NO_BORDER);

                   PdfPCell staticData4 = new PdfPCell(new Phrase("B)For remittance from within India: Remit to Genpact India HSBC Account No. 054 021365 001, IFSC Code"+
                            "- HSBC0110005.", font));
                   staticData4.setColspan(6);
                   staticData4.setBorder(Rectangle.NO_BORDER);

                   PdfPCell staticData5 = new PdfPCell(new Phrase("You are requested to send the remittances as per the Payment Term mentioned above."+
                            "Please send the payment notification to :collections.helpdesk@genpact.com"+
                            "Incase of any queries contact : genpactbilling.helpdesk@genpact.com", font));
                   staticData5.setColspan(6);
                   staticData5.setBorder(Rectangle.NO_BORDER);
                   PdfPCell staticData6 = new PdfPCell(new Phrase("FOR & ON BEHALF OF", font));
                   staticData6.setPaddingTop(25);
                   staticData6.setColspan(6);
                   staticData6.setBorder(Rectangle.NO_BORDER);
                   PdfPCell staticData7 = new PdfPCell(new Phrase("Genpact India", font));
                   staticData7.setColspan(6);
                   staticData7.setBorder(Rectangle.NO_BORDER);
                   PdfPCell staticData8 = new PdfPCell(new Phrase("(Private Company with Unlimited liability)", font));
                   staticData8.setColspan(6);
                   staticData8.setBorder(Rectangle.NO_BORDER);
                   PdfPCell staticData9 = new PdfPCell(sign);
                   staticData9.setColspan(6);
                   staticData9.setBorder(Rectangle.NO_BORDER);
                   PdfPCell staticData10 = new PdfPCell(new Phrase("Vidut Aggarwal", font));
                   staticData10.setColspan(6);
                   staticData10.setBorder(Rectangle.NO_BORDER);                
                   PdfPCell staticData11 = new PdfPCell(new Phrase("This is a system generated Invoice and does not require physical signature.", font));
                   staticData11.setColspan(6);
                   staticData11.setBorder(Rectangle.NO_BORDER);

                         Chunk chunk0 = new Chunk("                               ");
                      Chunk chunk = new Chunk("                                                                                                                            ");
                      chunk.setUnderline(+1f,-2f);//1st co-ordinate is for line width,2nd is space between  
                      document.open();
                      cb = writer.getDirectContent(); 
                      cb.setLineWidth(1);
                      MyPageEventListener mpel = new MyPageEventListener();
                      mpel.onEndPage(writer, document);
                      image.setAbsolutePosition(20, 795);
                      document.add(image);
                      document.add(chunk0);
                      document.add(chunk);
                      document.add(Chunk.NEWLINE);
                      cb.saveState();
                      cb.beginText();
                      cb.moveText(280, 810);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("INVOICE");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(20, 775);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("Service Tax No : " + "d_ServiceTaxNo");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(320, 788);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("Genpact India");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(316, 775);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("(Private Company with unlimited liability)");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(20, 750);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("PAN : " + "d_PAN");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(318, 760);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("d_AddressLine1");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(20, 725);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("Category of services: ");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(318, 746);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("d_AddressLine2");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(318, 732);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("d_AddressLine3");
                      cb.endText();
                      cb.setColorStroke(color);
                      cb.rectangle(20,715,245,-100);
                      cb.stroke();
                      cb.beginText();
                      cb.moveText(25, 700);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("d_cust_name");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(25, 670);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("d_cust_address");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(318, 715);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("d_pin");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(318, 699);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("Tele : " + "d_Telephone");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(318, 685);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("Fax : " + "d_Fax");
                      cb.endText();
                      cb.setColorStroke(color);
                      cb.rectangle(275,680,300,-80);
                      cb.stroke();
                      cb.setColorStroke(color);
                      cb.rectangle(10,90,575,742);
                      cb.stroke();
                      cb.setColorStroke(color);
                      cb.rectangle(275,680,300,-80);
                      cb.stroke();
                      cb.setColorStroke(color);
                      cb.rectangle(20,610,245,-30);
                      cb.stroke();
                      cb.beginText();
                      cb.moveText(280, 660);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("Invoice Number:  " + "d_Invoice_no.");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(280, 645);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("Invoice Date:  " + "d_Invoice_date");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(280, 626);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("Currency  :   " + "d_Currency");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(280, 605);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("Payment Term : " + "d_Payment_term");
                      cb.endText();
                      cb.beginText();
                      cb.moveText(25, 590);
                      cb.setFontAndSize(bf, 10);
                      cb.showText("Attention :  " + "Finance Dept");
于 2013-03-15T12:33:50.660 に答える