2016-08-30 104 views
0

我使用this example來顯示HTML標題和HTML頁腳。ColumnText和帶有Image Base64標記的HTML

但是,當html有一個圖像元素(base64)不起作用。我想試試another example。但是這並沒有使用ColumnText。

我嘗試添加

htmlContext.setImageProvider(new Base64ImageProvider()); 

TagProcessorFactory htmlTagProcessorFactory = Tags.getHtmlTagProcessorFactory(); 
htmlTagProcessorFactory.removeProcessor(HTML.Tag.IMG); 
htmlTagProcessorFactory.addProcessor(new ImageTagProcessor(), HTML.Tag.IMG); 

但沒有嘗試的成功。

回答

1

我設法讓它工作。但是與表。在這種方式中的Base64圖像顯示在PDF文檔

//CSS 
    CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true); 

    // HTML 
    TagProcessorFactory htmlTagProcessorFactory = Tags.getHtmlTagProcessorFactory(); 
    //htmlTagProcessorFactory.removeProcessor(HTML.Tag.IMG); 
    //htmlTagProcessorFactory.addProcessor(new ImageTagProcessor(), HTML.Tag.IMG); 

    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); 
    htmlContext.setTagFactory(htmlTagProcessorFactory); 
    htmlContext.setImageProvider(new Base64ImageProvider()); 

    // Pipelines 
    ElementList elements = new ElementList(); 
    ElementHandlerPipeline end = new ElementHandlerPipeline(elements, (Pipeline) null); 

    HtmlPipeline htmlPipeline = new HtmlPipeline(htmlContext, end); 
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, htmlPipeline); 

    // XML Worker 
    XMLWorker worker = new XMLWorker(css, true); 
    XMLParser p = new XMLParser(worker); 
    p.parse(new ByteArrayInputStream(html.getBytes())); 

    PdfPTable table = new PdfPTable(1); 

    PdfPCell cell = new PdfPCell(); 
    cell.setPadding(0); 
    cell.setUseBorderPadding(false); 
    cell.setBorder(0); 

    for (Element e : elements) { 
     cell.addElement(e); 
    } 

    table.addCell(cell); 

    // Adding a table at an absolute position 
    table.writeSelectedRows(0, -1, x, y, writer.getDirectContent()); 
0

使用你做同樣的例子中,我得到了它,而不需要表的,只有ColumnText工作。您只需將每個元素添加到ColumnText對象,如下所示。

// step 3 
    document.open(); 
    // step 4 
    PdfContentByte canvas = writer.getDirectContent(); 
    ColumnText ct = new ColumnText(canvas); 
    int side_of_the_page = 0; 
    ct.setSimpleColumn(COLUMNS[side_of_the_page]); 
    ct.setIgnoreSpacingBefore(false); 

    // CSS 
    CSSResolver cssResolver = XMLWorkerHelper.getInstance().getDefaultCssResolver(true); 

    // ELEMENT 
    ElementList elements = new ElementList(); 
    ElementHandlerPipeline end = new ElementHandlerPipeline(elements, (Pipeline) null); 

    // HTML 
    HtmlPipelineContext htmlContext = new HtmlPipelineContext(null); 
    htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory()); 
    htmlContext.setImageProvider(new Base64ImageProvider()); 

    // Pipelines 
    HtmlPipeline html = new HtmlPipeline(htmlContext, end); 
    CssResolverPipeline css = new CssResolverPipeline(cssResolver, html); 

    // XML Worker 
    XMLWorker worker = new XMLWorker(css, true); 
    XMLParser p = new XMLParser(worker); 
    p.parse(new ByteArrayInputStream(HTML.getBytes())); 

    for (Element element : elements) { 
     ct.addElement(element); 
    } 

    // step 5 
    document.close();