亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何創建我自己的 PageSize 變量以使用 iTextpdf 創建 PDF 文檔

如何創建我自己的 PageSize 變量以使用 iTextpdf 創建 PDF 文檔

HUWWW 2023-09-06 15:49:55
我想使用 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 在工作時查看更多內容: 

https://img1.sycdn.imooc.com//64f82f5500014bec18250150.jpg

請更換這個

Rectangle pagesize = new Rectangle(148, 350);
Document document = new Document(pdf, pagesize);

有了這個:

PageSize pagesize = new PageSize(148, 350);
Document document = new Document(pdf, pagesize);


查看完整回答
反對 回復 2023-09-06
  • 1 回答
  • 0 關注
  • 125 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號