我想使用 iTextpdf 7 創建 pdf 文檔,但我不想使用任何默認頁面大小。我想設置紙張尺寸的寬度和高度,但是當我嘗試使用Rectangle()類來執行此操作時,它會顯示錯誤,并且我無法創建任何內容。我以前從未使用過這個庫,我不知道如何做好它。這是我編寫的代碼的示例: String url_file= "C:\\Users\\Mike89\\Documents\\PDFJava\\pdfFiles\\SALES\\SALEINVOICE"+id+".pdf"; PdfWriter writer = new PdfWriter(url_file); PdfDocument pdf = new PdfDocument(writer); Rectangle pagesize = new Rectangle(148, 350); Document document = new Document(pdf, pagesize); document.setMargins(2, 2, 2, 2); Table table1 = new Table(UnitValue.createPercentArray(1)).useAllAvailableWidth().setBorder(Border.NO_BORDER); Cell cell1; cell1 = new Cell(); cell1.add(new Paragraph("COMPANY NAME").setTextAlignment(TextAlignment.CENTER).setFontSize(5).setBold()).setBorder(Border.NO_BORDER); cell1.add(new Paragraph("DOCUMENT ID").setTextAlignment(TextAlignment.CENTER).setFontSize(5)).setBorder(Border.NO_BORDER); cell1.add(new Paragraph("COMPANY ADDRESS").setTextAlignment(TextAlignment.CENTER).setFontSize(5)).setBorder(Border.NO_BORDER); cell1.add(new Paragraph("TELEPHONE").setTextAlignment(TextAlignment.CENTER).setFontSize(5)).setBorder(Border.NO_BORDER); table1.addCell(cell1); document.add(table1); document.close();netbeans 向我顯示的錯誤是: incompatible types: Rectangle cannot be converted to PageSize我不想使用 PageSize.A8 或 A9 或 A10 或類似的東西。我只想創建自己的頁面大小,但我無法弄清楚我的代碼有什么問題。我能做什么來解決這個問題?
1 回答

守著一只汪
TA貢獻1872條經驗 獲得超4個贊
Rectangle 和 PageSize 是不同的類型,盡管 PageSize 擴展了 Rectangle,但 Document 構造函數需要一個 PageSize。在 Netbeans 中,使用 CTRL-SPACE 在工作時查看更多內容:
請更換這個
Rectangle pagesize = new Rectangle(148, 350); Document document = new Document(pdf, pagesize);
有了這個:
PageSize pagesize = new PageSize(148, 350); Document document = new Document(pdf, pagesize);
添加回答
舉報
0/150
提交
取消