使用 iText 7 库将文本文件转换为 PDF 的问题

Problems to convert text file to PDF using iText 7 library

我需要将使用 "blanks"(旧大型机文件)格式化的简单文本文件转换为 PDF 格式。我正在使用 iText 7 库和 Java 1.8。通常它可以工作,但使用空格的格式不会显示在生成的 PDF 中,因此缩进不会显示在 PDF 中。

我使用的是 COURIER 字体,因此空白应该与字符的大小相同。

这是我正在使用的 java 代码:

import com.itextpdf.io.font.FontConstants;
import com.itextpdf.kernel.font.PdfFont;
import com.itextpdf.kernel.font.PdfFontFactory;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;
import com.itextpdf.layout.property.TextAlignment;
import com.itextpdf.test.annotations.WrapToTest;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

@WrapToTest
public class Text2Pdf {

    // public static final String DEST = "results/text2pdf.pdf";

    public static void main(String[] args) throws IOException {

        // Check for the mandatory 1st argument -> Source File
        if (args.length != 1) {
            System.out.println("\nUsage:");
            System.out.println("Text2Pdf AbsolutePath2TextFile");
            System.out.println("\nExample:");
            System.out.println("Text2Pdf C:\Users\USER1\Desktop\HelloWorld.txt");
            System.out.println(
                    "\nResulting .pdf file (HelloWorld.pdf) will be written to Text2PdfResults directory in the current working directory.");
            System.exit(1);
        }

        // Check if given Source File exist ...
        File sourceFile = new File(args[0]);

        // Get the Source and Destination File Name
        String sourceFileName = sourceFile.getName();
        String destFileName = sourceFileName + ".pdf";
        String destFileNameDir = "Text2PdfResults/" + destFileName;

        if (!sourceFile.exists()) {
            System.out.println("\nERROR:");
            System.out.println("Given source text file " + sourceFile.getAbsolutePath() + " doesn't exist!");
            System.exit(2);
        }

        File destFile = new File(destFileNameDir);
        destFile.getParentFile().mkdirs();
        new Text2Pdf().createPdf(sourceFile, destFileNameDir);
    }

    public void createPdf(File sourceFile, String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        Document document = new Document(pdf);
        document.setTextAlignment(TextAlignment.LEFT);
        document.setFontSize((float) 8.0);
        document.setLeftMargin((float) 40.0);
        document.setRightMargin((float) 40.0);

        //BufferedReader br = new BufferedReader(new FileReader(sourceFile));
        BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(sourceFile), "UTF8"));
        String line;
        PdfFont normal = PdfFontFactory.createFont(FontConstants.COURIER);
        while ((line = br.readLine()) != null) {
            document.add(new Paragraph(line).setFont(normal));
        }
        document.close();
        br.close();
    }

这是我要转换的纯文本文件:

Test Customer                                                         28.06.2019
12345678901234567890123456789012345678901234567890123456789012345678901234567890
         10        20        30        40        50        60        70       79
         This section starts with 10 blanks                      right alignment
---------1. Test Nr. 1
         2. Test Nr. 2         
--------------------
    Section starts with 5 blanks ...
This is the last line.

结果输出如下:

Test Customer 28.06.2019
12345678901234567890123456789012345678901234567890123456789012345678901234567890
10 20 30 40 50 60 70 79
This section starts with 10 blanks right alignment
---------1. Test Nr. 1
2. Test Nr. 2
--------------------
Section starts with 5 blanks ...
This is the last line.

谁能帮助理解为什么生成的 PDF 中没有显示空白?

此致,

拉尔夫

嗨,mkl,

感谢您的回复。我添加了以下代码,它在空格方面看起来好多了:-)

while ((line = br.readLine()) != null) {
            line = line.replace("\u0020", "\u00A0");
            document.add(new Paragraph(line).setFont(normal));
        }

我唯一的问题是每行后面的 CR。你知道为什么每行后面都有一行吗?

此致,

拉尔夫

更新

我在 document.add 代码之前添加了一个 System.out.println(line);,结果输出没有显示任何额外的行:

Test Customer                                                         28.06.2019
12345678901234567890123456789012345678901234567890123456789012345678901234567890
         10        20        30        40        50        60        70       79
         This section starts with 10 blanks                      right alignment
---------1. Test Nr. 1
         2. Test Nr. 2         
--------------------
    Section starts with 5 blanks ...
This is the last line.

注意:如果我使用 System.out.print(line); 我只会得到一行 - 没有 \n.

似乎是 iText document.add 会生成这些额外的行!!??

此致,

拉尔夫

我已将代码更改为:

public void createPdf(File sourceFile, String dest) throws IOException {
        PdfDocument pdf = new PdfDocument(new PdfWriter(dest));

        Document document = new Document(pdf);
        document.setTextAlignment(TextAlignment.LEFT);
        document.setFontSize((float) 8.0);
        document.setLeftMargin((float) 40.0);
        document.setRightMargin((float) 40.0);

        //BufferedReader br = new BufferedReader(new FileReader(sourceFile));
        BufferedReader br = new BufferedReader( new InputStreamReader( new FileInputStream(sourceFile), "UTF8"));
        String line;
        PdfFont normal = PdfFontFactory.createFont(FontConstants.COURIER);
        Paragraph para = new Paragraph();
        para.setFont(normal);
        while ((line = br.readLine()) != null) {
            line = line.replace("\u0020", "\u00A0");
            para.add(line + "\n");
            //document.add(new Paragraph(line).setFont(normal));    
        }
        document.add(para);
        document.close();
        br.close();
    }

现在看起来符合预期。我认为这与wile循环中的"document.add(new Paragraph) ..."有关,这似乎是为每个新段落添加一个NL。

此致,

拉尔夫