# Itext ###### tags: `Java` `Itext` **PdfDocument** addNewPage() - Document.add操作會在第一頁上 - PdfDocument的操作會在所有頁面上 addNewPage(int index); addNewPage(int index,PageSize pageSize); 1. index超過當前pdf的頁數,也就是index=pdf頁數+1 - 通過Document添加的內容 在執行addNewPage(int index)之前 在執行addNewPage(int index)之後 - 通過PdfDocument添加的內容 在執行addNewPage(int index)之前 在執行addNewPage(int index)之後 2. index小於當前pdf的頁數 - 通過Document添加的內容: 在執行addNewPage(int index)之前 在執行addNewPage(int index)之後 - 通過PdfDocument添加的內容: 在執行addNewPage(int index)之前 在執行addNewPage(int index)之後 總結:PdfDocument.addNewPage()後Document操作的頁面會是不同的 **Document** 使用此方法換頁後,Document操作的會在最新的頁面上 document.add(newAreaBreak(AreaBreakType.NEXT_PAGE)); **PdfFont** 建構字型 PdfFontFactory.createFont(String fontProgram, String encoding, boolean embedded) fontProgram : 字型檔位置 encoding : BaseFont.IDENTITY_H(Unicode、水平寫)... embedded : 字型是否生效 ## ElementPropertyContainer 繼承次抽象方法的類可透過設定此方法設定絕對位置 setFixedPosition(float left, float bottom, float width); ## IBlockElement **Table** Table透過建構子傳入參數 new Table(UnitValue.createPercentArray(flaot[])) 可使用百分比分配欄位寬度 Table寬度百分比設定 : setWidth(UnitValue.createPercentValue(int)) Table表頭設定 : addHeaderCell(..) Table欄位設定 : addCell(...) 兩者皆可傳入 Cell、Image、String、BlockElement<T> Cell、BlockElement : 兩者可設定欄位屬性(目前不確定差異..文字模式?混和模式?) 使用畫面最大寬度(同百分比100%) useAllAvailableWidth() **Cell** 透過建構子可設定跨欄、跨行 public Cell(int rowspan, int colspan) 透過add可添加內容( IBlockElement、Image ) 類別方法可設定各種欄位屬性同html 常用 1. setTextAlignment 2. setMargin 3. setPadding 4. setBorder **Paragraph** 與Cell不同的地方是子元素除了可添加IBlockElement還可添加ILeafElement或是字串 其他部分同Cell 此方法可設定絕對位置 setFixedPosition(float left, float bottom, float width) **Div** **List** **LineSeparator** ## ILeafElement **Image** 建構子可傳入ImageData Image可透過ImageDataFactory取得; ImageDataFactory可建立任何形式(URL、byte[]...)來源的圖片 **Text** **Tab** ## Other **Barcode** 1. BarCodeEAN 產生條碼 BarCode類型 : BarcodeEAN BarcodeEAN barcode = new BarcodeEAN(pdfDoc);//建立BarCode物件 barcode.setCodeType(BarcodeEAN.EAN8);//設定BarCode類型 barcode.setCode("12344672");//設定BarCode(根據類型長度、值域不同) //將BarCode轉成圖片放入Cell PdfFormXObject barcodeObject = barcode.createFormXObject(null, null, pdfDoc); Cell cell = new Cell().add(new Image(barcodeObject)); 2. Barcode39 Barcode39 barcode = new Barcode39(pdfDoc);//建立BarCode物件 barcode.setCode("12344672");//設定BarCode值 //將BarCode轉成圖片放入Cell PdfFormXObject barcodeObject = barcode.createFormXObject(null, null, pdfDoc); Cell cell = new Cell().add(new Image(barcodeObject)); 另外還有BarCode128、BarcodeInter25、BarcodePostnet、BarcodePDF417