我想用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或任何类似的东西。我只想创建我自己的页面大小,但是我不知道我的代码出了什么问题。我能做些什么来解决这个问题?
发布于 2019-09-28 22:54:03
Rectangle和PageSize是不同的类型,虽然PageSize扩展了Rectangle,但文档构造函数需要一个PageSize。在Netbeans中,使用CTRL-空格键可以在工作时查看更多内容:
请把这个换掉
Rectangle pagesize = new Rectangle(148, 350);
Document document = new Document(pdf, pagesize);
有了这个:
PageSize pagesize = new PageSize(148, 350);
Document document = new Document(pdf, pagesize);
https://stackoverflow.com/questions/58138783
复制相似问题