itext 7-带有自定义页面的画布

itext 7-带有自定义页面的画布,itext,Itext,在iText 2中,我们可以使用PdfContentByte来设置自定义页面大小,但是在iText 7.1.2中 PdfDocument pdf = new PdfDocument(new PdfWriter(dest)); PdfPage page = pdf.addNewPage(); PdfCanvas pdfCanvas = new PdfCanvas(page); Rectangle rectangle = new Rectangle(0, 0, 2

在iText 2中,我们可以使用PdfContentByte来设置自定义页面大小,但是在iText 7.1.2中

    PdfDocument pdf = new PdfDocument(new PdfWriter(dest));


    PdfPage page = pdf.addNewPage();
    PdfCanvas pdfCanvas = new PdfCanvas(page);
    Rectangle rectangle = new Rectangle(0, 0, 2000, 800);
    pdfCanvas.rectangle(rectangle);
    pdfCanvas.stroke();
    Canvas canvas = new Canvas(pdfCanvas, pdf, rectangle);
    PdfFont font = PdfFontFactory.createFont(FontConstants.TIMES_ROMAN);
    PdfFont bold = PdfFontFactory.createFont(FontConstants.TIMES_BOLD);
    Text title =
        new Text("The Strange Case of Dr. Jekyll and Mr. Hyde").setFont(bold);
    Text author = new Text("Robert Louis Stevenson").setFont(font);
    Paragraph p = new Paragraph().add(title).add(" by ").add(author);
    canvas.add(p);
    canvas.close();
    pdf.close();
即使我们设置了更大的宽度,它也不起作用。还是保持A4大小。如何正确更改页面大小


添加新页面时未指定页面大小,因此使用默认页面大小(A4)。请查看
addPage()
方法的API文档:。如果要获取另一个大小的页面,需要传递
PageSize
参数

如果要将默认页面大小从A4更改为其他大小,还有一种方法


PageSize
类扩展了
矩形
类:

您也可以显示itext 2代码吗?能够使用PdfContentByte设置自定义页面大小听起来像是意外的副作用…我的错,iText 2使用document.setPageSize(矩形..);