2017-07-03 17 views
0
<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页面。这应该不是通过旋转,而是通过实际设置页面大小。如何在将itext 7中的html转换为pdf时为特定的div标签继续横向定位?

回答

0

您可以通过解析布局元素而不是直接对文件或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++; 
     } 

    }