在 itext 7 中将 html 转换为 pdf 时,如何继续特定 div 标签的横向方向?

How to continue a landscape orientation for a particular div tag while converting html to pdf in itext 7?

<div isLandscape=false style="page-break-after:always">
<p class="title">
this is the first title in the portrait mode
</p>
<div>
this is the content following the first title in portrait mode
</div>
</div>
<div isLandscape=true style="page-break-after:always">
<p class="title">
this is the first title in the Landscape mode
</p>
<div style="page-break-after:always">
this is the content following the first title in Landscape mode
</div>
<p>
This content which is on the next page should be rendered on a landscape 
page and all the content in this parent div should continue to be in the 
landscape page.
</p>
</div>
<div isLandscape=false style="page-break-after:always">
<p class="title">
this content should be rendered on the portrait page and continue to be on a 
portrait page till the end of the parent div tag.
</p>
</div>

我希望第一个 div 内容在纵向 A4 页面上,下一个内容在横向 A4 页面上 page.This 应该不是通过旋转而是通过实际设置页面大小。

实现此目的的方法之一是解析布局元素而不是直接解析文件或pdfDocument,然后使用页面事件应用页面大小修改。

我在下面做了一个快速示例,每 X 页切换一次方向:

public void createPdfFromHtml(String htmlSource, String pdfDest, String resoureLoc) throws IOException, InterruptedException {
    File pdf = new File(pdfDest);
    pdf.getParentFile().mkdirs();

    //convertToElements takes the string containing the HTML as input
    byte[] bytes = StreamUtil.inputStreamToArray(new FileInputStream(htmlSource));
    String html = new String(bytes);


    PdfWriter writer = new PdfWriter(pdfDest);
    PdfDocument pdfDoc = new PdfDocument(writer);

    Document doc = new Document(pdfDoc,pageSize);


    // Create the page size modifying event handler
    PageSize pageSize = PageSize.A4;
    pageSize = pageSize.rotate();//Start in landscape
    int differentPageSizeInterval = 5;
    PageSizeModifier pageSizeModifier = new PageSizeModifier(doc, differentPageSizeInterval, pageSize);
    //Register it to the pdfDocument and set it to trigger at the start of a page
    pdfDoc.addEventHandler(PdfDocumentEvent.START_PAGE,pageSizeModifier);
    ConverterProperties converterProperties = new ConverterProperties();
    converterProperties.setBaseUri(resoureLoc);
    //Convert the html to elements
    try {
        //parse and return the top level elements of the <body>
        List<IElement> elements = HtmlConverter.convertToElements(html, converterProperties);
        for (IElement ele : elements) {
            //Add the elements to the layout document
            doc.add((BlockElement) ele);
        }
        doc.close();
    } catch (PdfException e) {
        System.out.println(e);
        e.printStackTrace();
    }
}

protected class PageSizeModifier implements IEventHandler {

    Document doc;
    int interval;
    int counter;
    PageSize pageSize;

    public PageSizeModifier(Document doc, int interval,PageSize pageSize) {
        this.doc = doc; //A reference to the layout document must be kept so we can change the margins on the fly
        this.interval = interval;
        this.counter = 1;
        this.pageSize = pageSize;//Start out in landscape
    }
    @Override
    public void handleEvent(Event event) {
        if(counter == interval){
            //Rotate
            pageSize = pageSize.rotate();
            //For the rendering framework, change the default page size
            doc.getPdfDocument().setDefaultPageSize(pageSize);

            //because the page was already created, we need to update the various boxes determining the pagesize
            //By default, only the trim and mediabox will be present
            ((PdfDocumentEvent) event).getPage().setMediaBox(pageSize);
            ((PdfDocumentEvent) event).getPage().setTrimBox(pageSize);

            //Reset the counter
            counter = 1;

        }else{
            counter++;
        }

    }