我需要转换简单的文本文件,其中使用“空白”(旧的大型机文件)将其格式化为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会生成这些额外的行!! ??
此致
拉尔夫
答案 0 :(得分:0)
我已将代码更改为:
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。
此致
拉尔夫