【问题标题】:Convert TXT file to PDF using iText (keep formatting)使用 iText 将 TXT 文件转换为 PDF(保持格式化)
【发布时间】:2015-01-11 14:24:33
【问题描述】:

我正在尝试使用 iText 库将 .txt 文件转换为 .pdf 文件。 我面临的问题如下:

我在 txt 文件中有一个清晰的格式,类似这样:

TEXT                                   *******************
Other text here                        * SOME_CODE_HERE_ *
Other text                             *******************

在输出中,格式消失了,看起来像这样:

TEXT           ******************
Other text here         * SOME_CODE_HERE_ *
Other text          ******************

代码如下所示:

public static boolean convertTextToPDF(File file) throws Exception {

    BufferedReader br = null;

    try {

        Document pdfDoc = new Document(PageSize.A4);
        String output_file = file.getName().replace(".txt", ".pdf");
        System.out.println("## writing to: " + output_file);
        PdfWriter.getInstance(pdfDoc, new FileOutputStream(output_file)).setPdfVersion(PdfWriter.VERSION_1_7);;

        pdfDoc.open();

        Font myfont = new Font();
        myfont.setStyle(Font.NORMAL);
        myfont.setSize(11);

        pdfDoc.add(new Paragraph("\n"));

        if (file.exists()) {

            br = new BufferedReader(new FileReader(file));
            String strLine;

            while ((strLine = br.readLine()) != null) {
                Paragraph para = new Paragraph(strLine + "\n", myfont);
                para.setAlignment(Element.ALIGN_JUSTIFIED);
                pdfDoc.add(para);
            }
        } else {
            System.out.println("no such file exists!");
            return false;
        }
        pdfDoc.close();
    }

    catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (br != null) 
            br.close();
    }

    return true;
}

我也尝试使用 IDENTITY_H 创建 BaseFont,但它不起作用。 我想这是关于编码或类似的东西。 你怎么看?我的解决方案用完了...

谢谢

乐: 正如 Alan 和 iText 页面上的教程所建议的那样,我将这部分与我现有的代码一起使用,它工作正常。

        BaseFont courier = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.EMBEDDED);
        Font myfont = new Font(courier);

【问题讨论】:

    标签: java pdf itext


    【解决方案1】:

    【讨论】:

    • 它适用于 BaseFont courier = BaseFont.createFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.EMBEDDED); myfont = new Font(courier),灵感来自您发布的第二个链接。我之前尝试过使用 IDENTITY_H 编码但没有成功。谢谢!!
    • @Marius:编码不应该有区别;它只是从字节值到字符索引的映射。它与这些字符的 width 完全无关。
    【解决方案2】:

    我知道这是旧的,但我在将文本文件转换为 pdf 时遇到了同样的问题,我使用了这个(我在 vb.net 中写了这个):

     Dim pdfDoc As Document = New Document(PageSize.A4) 
     Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDoc, New FileStream(pdfFoldername & "\" & "name of file", FileMode.Create))
     pdfDoc.Open()
     Dim courier As BaseFont = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, BaseFont.EMBEDDED)
     Dim myfont As iTextSharp.text.Font = New iTextSharp.text.Font(courier, 10)
     Dim para As Paragraph = New Paragraph(page, myfont)
     pdfDoc.Add(para)
    

    上述答案和更新代码的不同之处在于我使用了“10”作为我的字体大小。这使得 PDF 看起来与文本文件中的格式相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-23
      • 2011-01-25
      • 1970-01-01
      • 2020-01-08
      • 1970-01-01
      • 2014-06-26
      • 2017-09-13
      • 2020-09-04
      相关资源
      最近更新 更多