用 Java 生成 PDF
我正在尝试用 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));
}
}
}
I'm trying to write a PDF file in Java to say the words hello neckbeards
but when I run my program, Adobe Reader opens but an error comes up saying:
There was an error opening this document.
The file is already open or in use by another application.
Here's my code:
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));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这里有很多错误:
您正在编写纯文本。 Adobe Reader 将抛出错误,因为该文件不是有效的 PDF!
要编写 PDF,请使用 iText 或 PDFBox 等库。
在写入或读取文件之前,您需要打开从程序到该文件的连接。
因此,当您结束写入/读取文件时,不要忘记关闭连接,以便其他程序(例如 Adobe Reader)也可以读取该文件!要关闭文件,只需执行以下操作:
main
方法不应引发任何异常。相反,如果发生错误,必须被捕获并执行适当的操作(告诉用户,退出,...)。要读/写文件(或任何内容),这是推荐的结构:
最后的
if
有一个坏地方。正确的代码是:此外,请注意,我调用直接传递文件的
open
方法。没有必要复制它。
There are many errors here:
You are writing plain text. Adobe Reader will throw an error as the file is not a valid PDF!
To write PDFs, use libraries like iText or PDFBox.
Before you can write or read a file, you open a connection from your program to the file.
So, when you end writing/reading the file, don't forget to close the connection so that other programs (such as Adobe Reader) can read the file too! To close the file, simply do:
The
main
method shouldn't throw any exception. Instead, if an error occurs, it must be catched and do the appropiate actions (tell the user, exit, ...).To read/write files (or anything), this is the recommended structure:
The final
if
has a bad place. The correct code would be:Also, notice that I call the
open
method passing the file directly.There's no need to make a copy of it.
要创建 PDF 文件,您可以使用 iText 等库。在我看来,您只是创建一个纯文本文件,然后尝试使用 PDF 阅读器打开它。
To create a PDF file you can use a library such as iText. It seems to me that you are simply creating a plain text file and then attempting to open it with a PDF reader.
写入文件后,必须使用 pfile.close() 关闭它;
请注意,您编写的只是一个文本文件,其内容为
helloeckbeards
和扩展名.pdf
。它不是普通意义上的PDF文件,可以使用Adobe Reader等PDF阅读器打开。使用像 iText 这样的库来创建真正的 PDF 文件。
文件必须遵循 PDF 实现(PDF 文件)成为有效的 PDF 文件。正如您所看到的,这比“仅仅”将文本写入文件要复杂得多。
After you write to your file, you have to close it, using
pfile.close()
;Note that what you wrote is just a text file with contents
hello neckbeards
and extension.pdf
. It is not a PDF file in a normal sense that can be opened with a PDF reader like Adobe Reader.Use a library like iText to create real PDF files.
A file must follow the PDF implementation (PDF file) to be a valid PDF file. As you can see, this is much more involved, than "just" writing text to a file.
我不熟悉以这种方式从桌面打开文件,但在写入文件后关闭 FileWriter 是值得的。顺便说一句,我发现 XSLT 是生成 PDF 文档的好方法,因为您可以对输出进行大量控制,并且持续的维护和调整不需要重新编译代码(如果您有营销部门,这很有用)喜欢改变主意的人)。如果您有兴趣,请查看 XSL-FO,Apache FOP 是一个很好的实现。
I'm not familiar with opening files from the desktop in that way, but it's worth closing your FileWriter after writing the file. Incidentally, I've found XSLT to be a good way of producing PDF documents, as you get a great deal of control over the output and ongoing maintenance and adjustments don't involve re-compiling your code (useful if you have a marketing department who like changing their minds). Take a look at XSL-FO if you're interested, Apache FOP is a good implementation.