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

Java 使用iText将TXT文件转换为PDF(保留格式),java,pdf,itext,Java,Pdf,Itext,我正在尝试使用iText库将.txt文件转换为.pdf文件。 我面临的问题如下: 我在txt文件中有一个清晰的格式,与此类似: TEXT ******************* Other text here * SOME_CODE_HERE_ * Other text ******************* 在输出中,格式设

我正在尝试使用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;
}
代码如下所示:

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

您需要使用等距字体,例如Courier


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


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

它与BaseFont courier=BaseFont.createFont(BaseFont.courier、BaseFont.CP1252、BaseFont.EMBEDDED)一起使用;myfont=新字体(courier),灵感来自您发布的第二个链接。。我以前尝试过身份编码,但没有成功。。谢谢@马吕斯:编码不应该有什么区别;它只是从字节值到字符索引的映射。这绝对与这些字符的宽度无关。