使用iText将Excel转换为PDF-Java [英] Convert Excel to PDF - Java using iText

查看:2222
本文介绍了使用iText将Excel转换为PDF-Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经用Apache POI创建了一个excel文件. 现在,我需要将其转换为PDF. 我想使用iText,但是我想将合并的单元格,样式等从excel文件复制到pdf.

I have created an excel file with Apache POI. And now I need convert it to PDF. I want to use iText, but I want copy the merged cell, styles, etc from excel file to pdf.

我该如何实现?因为我在网上找到了代码示例:使用iText和Apache POI的PDF到Excel很好,但是不会复制合并的单元格,样式.

How can I achieve this? Because the code sample I've found on the web: PDF to Excel using iText and Apache POI is good but doesn't copy the merged cells, styles.

有什么想法吗?非常感谢你!

Any ideas? Thank you very much !

推荐答案

如果要使用iText,最好使用iText7.这是iText的最新版本,并且已经完成了大量的错误修正(尤其是对于表格).

If you are going to use iText, it's best to use iText7. This is the latest version of iText, and substantial bugfixing has been done (especially for tables).

话虽如此,当我看示例时,似乎该示例只是遍历所有单元格并将它们添加到iText Table对象中.

That having been said, when I look at the example, it seems like the example just iterates over all the cells and adds them to an iText Table object.

使用iText7可以完成完全相同的操作.您创建具有适当列数的表,然后向其中添加单元格.

The exact same thing can be done with iText7. You create a table with the proper amount of columns, and then add cells to it.

您的问题(就我而言)归结为如何为iText表格单元设置样式".

Your question (as far as I'm concerned) comes down to "how can I style iText table cells".

这是一段仅生成表的示例代码.我已经指出了可以在哪里设置表格内容的样式.

This is a piece of sample code that just generates a table. I have indicated where you can style the content of the table.

public static void main(String[] args) throws Exception {
    File file = new File(DEST);
    file.getParentFile().mkdirs();
    manipulatePdf(DEST);
}

public static void manipulatePdf(String dest) throws Exception {
    PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
    Document doc = new Document(pdfDoc);

    Table table = new Table(8);
    for (int i = 0; i < 16; i++) {
        Paragraph para = new Paragraph("hi");
        // now you can use methods like:
        // para.setFont()
        // para.setFontColor()
        // para.setFontSize()
        table.addCell(para);
    }
    doc.add(table);

    doc.close();
}

http://developers中查看更多示例. itextpdf.com/content/itext-7-examples/itext-7-tables

这篇关于使用iText将Excel转换为PDF-Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆