how to rotate pages into landscape and page content should be in portrait iTextpdf

北战南征 提交于 2019-12-12 14:36:47

问题


I'm trying to create a PDF document with more than 2 pages in portrait and others in landscape, I found that both page and text rotates to landscape I need to prevent page content rotation. am using following code

 Document document = new Document(PageSize.A4, 36, 36, 36, 72);
    PdfWriter writer = PdfWriter.getInstance(document, new 
    FileOutputStream(outPutDirectory + indexID + ".pdf"));
    writer.setPageEvent(new Orientation(orientation));
    document.open();
    XMLWorkerHelper.getInstance().parseXHtml(writer,document, new ByteArrayInputStream(parserXHtml(page.getPageContent()).getBytes()))
    document.close();

my expected result should be like this


回答1:


Instead of using a page event, you have to change the page size.

For instance:

Document document = new Document(PageSize.A4);
PdfWriter writer = PdfWriter.getInstance(document, new 
FileOutputStream(outPutDirectory + indexID + ".pdf"));
document.open();
// Add some content in portrait
document.setPageSize(PageSize.A4.rotate());
document.newPage();
// Add some content in landscape
document.close();

Be aware of the fact that the page size only changes on the next page. The order of setPageSize() and newPage() is important.



来源:https://stackoverflow.com/questions/45499384/how-to-rotate-pages-into-landscape-and-page-content-should-be-in-portrait-itextp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!