使用横向格式的iText pdf进行多页打印

时间:2017-10-04 07:08:29

标签: android printing itext pdf-generation

我试过itextPdf_page_orientation但问题是 - 只有第一页是正确的休息所有页面都打印在半个区域。 这是一张图片,可以帮助您了解问题。 enter image description here

我已尝试设置页面大小PdfPage.A4.rotate()并将事件设置为PdfWritter 这是代码snipet。

    @Override
    protected String doInBackground(Void... params) {
    final String PARENT_PATH = 
    Environment.getExternalStorageDirectory().getPath() + "/.GSTInvoice";
    Document document = null;
    try {
        File file = new File(PARENT_PATH);
        if (!file.exists()) {
            file.mkdirs();
        }
        File pdfFile = new File(file, "last_sales_summary");

        document = new Document();
        document.setPageSize(PageSize.A4.rotate());
        event = new RotateEvent();

        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(pdfFile));
        writer.setPageEvent(event);
        document.open();
        event.setOrientation(PdfPage.LANDSCAPE);
        taxList = new ArrayList<>();

        PdfContentByte cb = writer.getDirectContent();
        printPage(document, writer, cb);

        if (document != null && document.isOpen()) {
            document.close();
        }
        return pdfFile.getPath();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (document != null && document.isOpen()) {
            document.close();
        }
    }
    return null;
}


private void printPage(Document document, PdfWriter pdfWriter, PdfContentByte pdfContentByte) throws Exception{


    int noOfPages = getNoOfPages();
    BaseFont latoLight = BaseFont.createFont("assets/Lato-Light.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
    Font light = new Font(latoLight, 8);
    for (int i=1;i<=noOfPages;i++) {
        if (i != 1) {

            document.newPage();
            pdfWriter.setPageSize(PageSize.A4.rotate());
            event.setOrientation(PdfPage.LANDSCAPE);
            document.setPageSize(PageSize.A4.rotate());
        }

        addTopPart(document);

        addMiddleTable(document, i);

        if (noOfPages>1) {
            Paragraph paragraph = new Paragraph(new Phrase("Page " + i + "/" + noOfPages, light));
            paragraph.setAlignment(Element.ALIGN_CENTER);
            paragraph.setSpacingBefore(8f);
            ColumnText.showTextAligned(pdfContentByte, Element.ALIGN_CENTER,
                    paragraph,
                    (document.right() - document.left()) / 2 + document.leftMargin(),
                    document.bottom() - 10, 0);
        }
        event.setOrientation(PdfPage.LANDSCAPE);
    }
}

任何意见都将不胜感激

1 个答案:

答案 0 :(得分:1)

PDF的唯一特点是第一页的页面旋转为0,而第二页的页面旋转为90.

这是由于我在第一条评论中所暗示的:在event.setOrientation(PdfPage.LANDSCAPE)之后执行document.open(),第一页不会被事件监听器轮换,只会跟随所有后续页面。除此之外,所有页面都有一个纵向A4页面的媒体框,其中包含旋转的页面填充内容。

当您指出更改event.setOrientationdocument.open的顺序不会改变行为时,相关的打印管理器似乎有问题。

您可能会尝试删除所有event.setOrientation(PdfPage.LANDSCAPE)次来电;这应该会导致所有页面变得像第一个页面。

或者您可能希望删除所有代码设置并更改轮换,而是使用new Document(new RectangleReadOnly(842,595))实例化文档文档,即根本不进行任何轮换;由于打印管理器似乎没有正确处理旋转,这也可能导致所需的行为。

正如OP在评论中报道的那样,

  

您建议使用RectangleReadOnly(842,595)工作。