Можно ли визуализировать большую строку HTML на нескольких страницах в Itext 7 - PullRequest
0 голосов
/ 03 марта 2020

Вот часть кода, которую я использую:

String cleanedContent = "<p style="margin-top: 0em; margin-bottom: 0em;">&nbsp;</p><p style="margin-top: 0em; margin-bottom: 0em;">Indeed, while the Fed moved the funds rate range another 25 basis points higher at its September meeting, in what was widely anticipated as the next step in the Fed’s normalization path, and another such move higher at the December meeting is quite likely, we think the market is over-anticipating the number of policy moves in 2019. We do believe that the Fed’s rate-hiking path will be influenced by the U.S. economy leveling off next year, by global economic conditions (including the U.S. dollar), by tariffs and trade impacts, and by how much inflation actually accelerates in the year ahead. The fact is that the new economy evolving today is replete with technology-driven goods-sector disinflation and that will mean that the neutral rate of interest, and the desire to go much above it, will likely be muted. So, in our view, the Fed will probably only tighten a couple of times, or so, next year, versus the consensus expectation of three or four hikes in 2019, which we think could be excessive.</p><p style="margin-top: 0em; margin-bottom: 0em;">&nbsp;</p><p style="margin-top: 0em; margin-bottom: 0em;">&nbsp;</p>"


//To convert to list of elements
List<IElement> elements = HtmlConverter.convertToElements(cleanedContent,converterProperties);
//Then creating a paragraph from all elements:

for (IElement element : elements) {
    //SolidBorder border = new SolidBorder(2f);//Border.NO_BORDER;
    com.itextpdf.layout.element.Paragraph paragraphTemp = new Paragraph();
    UnitValue val = new UnitValue(1,0.0f);
    element.setProperty(43, val);
    element.setProperty(44, val);
    element.setProperty(45, val);
    element.setProperty(46, val);
    String[] fontFamily = element.getProperty(20);
    if(fontFamily!=null && fontFamily.length>0 && fontFamily[0]!=null &&  !("").equalsIgnoreCase(fontFamily[0])){
        element.setProperty(20,pDFCellEmitter.createRegisteredFont(fontFamily[0], cleanedContent));
    }else{
        element.setProperty(20,pDFCellEmitter.createRegisteredFont(fontFamily[0], cleanedContent));
    }

    paragraph.add((IBlockElement) element);

}

и, наконец, используя этот фрагмент кода, чтобы проверить, достаточно ли прямоугольника для размещения текста, иначе поместите его на следующую страницу, передавая прямоугольник с такими же размерами;

public void fitString(PdfDocument pdfDoc,Paragraph paragraph, int pageNumber, Rectangle area) throws IOException {
    LayoutResult layoutResult;
    ParagraphRenderer renderer = (ParagraphRenderer) paragraph.createRendererSubTree();
    renderer.setParent(new DocumentRenderer(new Document(pdfDoc)));
    while (((layoutResult = renderer.layout(new LayoutContext(new LayoutArea(pageNumber, area))))).getStatus() != LayoutResult.FULL) {
        if (pdfDoc.getNumberOfPages() < pageNumber) {
            pdfDoc.addNewPage();
        }
        layoutResult.getSplitRenderer().draw(new DrawContext(pdfDoc, new PdfCanvas(pdfDoc.getPage(pageNumber++)), false));
        renderer = (ParagraphRenderer) layoutResult.getOverflowRenderer();
    }
    if (pdfDoc.getNumberOfPages() < pageNumber) {
        pdfDoc.addNewPage();
    }
    renderer.draw(new DrawContext(pdfDoc, new PdfCanvas(pdfDoc.getPage(pageNumber)), false));

}

Я получаю это исключение:

java .lang.NullPointerException на com.itextpdf.layout.renderer.AbstractRenderer.isFirstOnRootArea (AbstractRenderer. java: 2083) на com.itextpdf.layout.renderer.AbstractRenderer.isFirstOnRootArea (AbstractRenderer. java: 1076) на com.itextpdf.layout.renderer.LineRenderer.layout (LineRenderer. java: 468) на com.itextrende. .ParagraphRenderer.layout (ParagraphRenderer. java: 209)

Заранее спасибо за помощь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...