【问题标题】:images padding when converting html to pdf using itextpdf and XMLWorker使用 itextpdf 和 XMLWorker 将 html 转换为 pdf 时的图像填充
【发布时间】:2014-11-03 10:58:12
【问题描述】:

我正在使用 XMLWorker 5.5.3 和 itextpdf 5.5.3 将 html 转换为 pdf。 我的 html 包含包含包含图像的单元格的表格。 即我的代码的一部分:

我的问题是某些单元格添加了填充 > 0,因此包含图像的 td 大于应有的大小。 有没有办法为图像定义样式,所以不会有填充和边距? 我正在查看css Demo of Itextpdf 并没有找到解决方案,任何帮助/技巧/ideas 都会有所帮助。 谢谢 塔米

【问题讨论】:

    标签: itextpdf xmlworker


    【解决方案1】:

    我找到了解决方案: 当我创建表格时,我定义了列宽:

     PdfPTable table = new PdfPTable(vColWidth.length);
     table.setWidthPercentage(100);
     table.setWidths(vColWidth);
    

    在创建我定义的 Image 对象时:

    RegularImg = Image.getInstance(imgsrc); 
    RegularImg.setSpacingAfter(0);
    RegularImg.setSpacingBefore(0);
    

    定义 td 时:

    cell = new PdfPCell(RegularImg);
    if(colSpan!="") 
        cell.setColspan(Integer.parseInt(colSpan));
    if(rowSpan!= "") 
        cell.setRowspan(Integer.parseInt(rowSpan));
    if(cellHight!="") 
        cell.setFixedHeight(Float.parseFloat(cellHight));
    
    table.addCell(cell);
    

    这个解决方案工作正常。如果表不是那么大或与 colSpan , rowSpan 复杂。 在复杂的情况下,我建议人们将图片表格转换为 Canvas ,而不是使用表格。只需将每个图像放在文档中的特定位置即可。 (性能要快得多。

    【讨论】:

      猜你喜欢
      • 2013-09-24
      • 2013-06-28
      • 2013-01-26
      • 2021-01-15
      • 2018-02-15
      • 2014-08-28
      • 2015-05-20
      • 2019-12-08
      • 1970-01-01
      相关资源
      最近更新 更多