【问题标题】:Generating a PDF in Java用 Java 生成 PDF
【发布时间】:2011-04-18 15:39:13
【问题描述】:

我正在尝试用 Java 编写一个 PDF 文件来说出 hello neckbeards 的字样,但是当我运行我的程序时,Adobe Reader 会打开但出现错误提示:

There was an error opening this document.
The file is already open or in use by another application.

这是我的代码:

import java.awt.Desktop;
import java.io.*;

public class count10 {

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

        File tempfile = File.createTempFile("report", ".pdf");
        FileWriter pfile = new FileWriter(tempfile);
        pfile.write("hello neckbeards");

        Desktop dtop = null;

        if (Desktop.isDesktopSupported()) {
            dtop = Desktop.getDesktop();
        }
        if (dtop.isSupported(Desktop.Action.OPEN)){
            String path = tempfile.getPath();
            dtop.open(new File(path));
        }
    }
}

【问题讨论】:

  • 正如下面的几个人所提到的,iText 是构建 pdf 文件的好方法。请在进行任何重大开发之前查看 iText 的许可。
  • 哇。这不会是家庭作业吧?
  • @neckbeard69 1) 如果这是家庭作业,请用homework 标记。 2) 当您找到解决您的问题的答案时,请通过检查其绿色勾号接受它,靠近投票区。谢谢!

标签: java pdf io pdf-generation


【解决方案1】:

这里有很多错误:

  • 您正在编写纯文本。 Adobe Reader 将抛出错误,因为该文件不是有效的 PDF!
    要编写 PDF,请使用 iText 或 PDFBox 等库。

  • 在您可以写入或读取文件之前,您打开从程序到文件的连接。
    因此,当您结束写入/读取文件时,不要忘记关闭连接,以便其他程序(例如 Adob​​e Reader)也可以读取该文件!要关闭文件,只需执行以下操作:

    pfile.close();
    
  • main 方法不应抛出任何异常。相反,如果发生错误,必须被捕获并执行适当的操作(告诉用户,退出,...)。 要读/写文件(或任何东西),这是推荐的结构

    FileReader reader = null;
    try {
        reader = new FileReader("file.txt"); //open the file
    
        //read or write the file
    
    } catch (IOException ex) {
        //warn the user, log the error, ...
    } finally {
        if (reader != null) reader.close(); //always close the file when finished
    }
    
  • 最后的if 有一个不好的地方。正确的代码是:

    if (Desktop.isDesktopSupported()) {
        Desktop dtop = Desktop.getDesktop();
        if (dtop.isSupported(Desktop.Action.OPEN)) {
            dtop.open(tempfile);
        }
    }
    

    另外,请注意我调用open 方法直接传递文件。
    没有必要复制它。

【讨论】:

    【解决方案2】:

    要创建 PDF 文件,您可以使用 iText 等库。在我看来,您只是在创建一个纯文本文件,然后尝试使用 PDF 阅读器打开它。

    【讨论】:

      【解决方案3】:

      写入文件后,必须使用pfile.close() 关闭它;

      请注意,您编写的只是一个文本文件,其内容为hello neckbeards,扩展名为.pdf。它不是普通意义上的PDF文件,可以用Adobe Reader等PDF阅读器打开。

      使用像iText 这样的库来创建真正的 PDF 文件。

      文件必须遵循PDF implementation(PDF 文件)才能成为有效的 PDF 文件。如您所见,这比“仅仅”将文本写入文件要复杂得多。

      【讨论】:

        【解决方案4】:

        我不熟悉以这种方式从桌面打开文件,但在写入文件后关闭 FileWriter 是值得的。顺便说一句,我发现 XSLT 是一种生成 PDF 文档的好方法,因为您可以很好地控制输出,并且持续的维护和调整不涉及重新编译您的代码(如果您有营销部门,这很有用喜欢改变主意的人)。感兴趣的可以看看 XSL-FO,Apache FOP 是一个很好的实现。

        【讨论】:

          【解决方案5】:
              Try this code....
              Document document=new Document();
              PdfWriter.getInstance(document,new FileOutputStream("E:/data.pdf"));
              document.open();
              PdfPTable table=new PdfPTable(2);
                         table.addCell("Employee ID");
                         table.addCell("Employee Name");
                         table.addCell("1");
                         table.addCell("Temperary Employee");
                         document.add(table);
                         document.close();
          
          You have to import....
          import com.itextpdf.text.Document;
          import com.itextpdf.text.DocumentException;
          import com.itextpdf.text.pdf.PdfPTable;
          import com.itextpdf.text.pdf.PdfWriter;
          

          【讨论】:

            猜你喜欢
            • 2020-02-06
            • 2012-05-17
            • 2016-06-26
            • 1970-01-01
            • 1970-01-01
            • 2010-11-07
            • 1970-01-01
            • 2015-03-23
            • 1970-01-01
            相关资源
            最近更新 更多