java 使用 iText 将 TXT 文件转换为 PDF(保持格式)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26925856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:53:15  来源:igfitidea点击:

Convert TXT file to PDF using iText (keep formatting)

javapdfitext

提问by Marius Manastireanu

I am trying to convert a .txt file into a .pdf file using iText library. The problem that I am facing is the following:

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

I have a clear formatting in the txt file, something similar with this:

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

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

And in the output the formatting is gone and looks like this:

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

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

The code looks like this:

代码如下所示:

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;
}

I also tried to create a BaseFont with IDENTITY_H but it doesn't work. I guess it's about the encoding or something like that. What do you think? I run out of solutions...

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

Thanks

谢谢

LE: As suggested by Alan, and by the tutorial from iText's page, I used this part in addition with my existing code and it works fine.

LE:按照 Alan 的建议以及 iText 页面上的教程,我将这部分与现有代码一起使用,并且运行良好。

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

回答by Pr0x1mo

I know this is old, but i had the same problem in converting text files into pdf's and i used this (i wrote this in vb.net):

我知道这是旧的,但我在将文本文件转换为 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)

The difference to the above answer and updated code is using '10' as my font size. That made the PDF look identical to the formatting from the text file.

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