gpt4 book ai didi

java - 使用 PDDocument 在 java 中创建 PDF 文件会导致 PDF 文件损坏

转载 作者:行者123 更新时间:2023-12-01 19:35:07 26 4
gpt4 key购买 nike

我正在尝试使用 PDDocument 在 Java 中创建临时 PDF 文件。我正在使用以下方法创建临时 PDF 文件。

/* Create a temporary PDF file.*/
private File createPdf(String fileName) throws IOException {
final PDDocument document = new PDDocument();
final File file = File.createTempFile(fileName, ".pdf");
//write it
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write("This is the temporary pdf file content");
bw.close();
document.save(file);
document.close();
return file;
}

这是测试。

@Test
public void testCreateAndMergePdfs() throws IOException {
Collection<File> pdfs = new ArrayList<>(Arrays.asList(createPdf("File1"), createPdf("File2")));
assertFalse(CollectionUtils.isEmpty(pdfs));
PdfPrintPojo pdfPrintPojo = new PdfPrintPojo(pdfs);
File mergedFile = service.createAndMergePDFs(pdfPrintPojo, "Merged");
assertNotNull(mergedFile);
List<File> list = new ArrayList<>(pdfs);
File file1 = list.get(0);
File file2 = list.get(1);
assertTrue(FileUtils.contentEquals(file1, file2));
}

我在这里尝试做的是创建并合并两个 PDF 文件。当我运行测试时,它会在 temp 文件夹中创建两个 PDF 文件,例如 \AppData\Local\Temp\File16375814641476797612.pdf\AppData\Local\Temp\File24102718409195239661.pdf 以及 \AppData\Local\Temp\Merged_merged_3755858389884894769.pdf 中的合并文件。但测试失败于 assertTrue(FileUtils.contentEquals(file1, file2));当我尝试打开 temp 文件夹中的 PDF 文件时,提示 PDF 已损坏。另外,我不知道为什么文件没有保存为 File1File2。谁能帮我这个?

最佳答案

使用Apache PDFBox教程中,我成功创建了一个工作 PDF 文件。方法更改如下。

/* Create a temporary PDF file.*/
private File createPdf(String fileName) throws IOException {
// Create a document and add a page to it
final PDDocument document = new PDDocument();
PDPage page = new PDPage();
document.addPage(page);

// Create a new font object selecting one of the PDF base fonts
PDFont font = PDType1Font.HELVETICA_BOLD;

// Start a new content stream which will "hold" the to be created content
PDPageContentStream contentStream = new PDPageContentStream(document, page);

// Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
contentStream.beginText();
contentStream.setFont(font, 12);
contentStream.newLineAtOffset(100, 700);
contentStream.showText("Hello World");
contentStream.endText();

// Make sure that the content stream is closed:
contentStream.close();

// Save the results and ensure that the document is properly closed:
File file = File.createTempFile(fileName, ".pdf");
document.save(file);
document.close();
return file;
}

对于测试,我采用的方法是使用 PDDocument 加载文件,然后使用 PDFTextStripper 将数据提取为字符串,并在这些字符串上使用断言。

 @Test
public void testCreateAndMergePdfs() throws IOException {
Collection<File> pdfs = new ArrayList<>(Arrays.asList(createPdf("File1"), createPdf("File2")));
assertFalse(CollectionUtils.isEmpty(pdfs));
PdfPrintPojo pdfPrintPojo = new PdfPrintPojo(pdfs);
File mergedFile = service.createAndMergePDFs(pdfPrintPojo, "Merged");
assertNotNull(mergedFile);
List<File> list = new ArrayList<>(pdfs);

/* Load the PDF files and extract data as String. */
PDDocument document1 = PDDocument.load(list.get(0));
PDDocument document2 = PDDocument.load(list.get(1));
PDDocument merged = PDDocument.load(mergedFile);

PDFTextStripper stripper = new PDFTextStripper();
String file1Data = stripper.getText(document1);
String file2Data = stripper.getText(document2);
String mergedData = stripper.getText(merged);

/* Assert that data from file 1 and 2 are equal with each other and merged file. */
assertEquals(file1Data, file2Data);
assertEquals(file1Data + file2Data, mergedData);
}

关于java - 使用 PDDocument 在 java 中创建 PDF 文件会导致 PDF 文件损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58073605/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com