用 Java 生成 PDF

发布于 2024-11-02 02:55:52 字数 838 浏览 0 评论 0原文

我正在尝试用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我家小可爱 2024-11-09 02:55:52

这里有很多错误:

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

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

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

    FileReader reader = null;
    尝试 {
        reader = new FileReader("文件.txt"); //打开文件
    
        //读取或写入文件
    
    } catch (IOException ex) {
        //警告用户,记录错误,...
    } 最后 {
        if (reader!= null) reader.close(); //完成后总是关闭文件
    }
    
  • 最后的if有一个地方。正确的代码是:

    if (Desktop.isDesktopSupported()) {
        桌面 dtop = Desktop.getDesktop();
        if (dtop.isSupported(Desktop.Action.OPEN)) {
            dtop.open(临时文件);
        }
    }
    

    此外,请注意,我调用直接传递文件的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:

    pfile.close();
    
  • 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:

    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
    }
    
  • The final if has a bad place. The correct code would be:

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

    Also, notice that I call the open method passing the file directly.
    There's no need to make a copy of it.

魂牵梦绕锁你心扉 2024-11-09 02:55:52

要创建 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.

著墨染雨君画夕 2024-11-09 02:55:52

写入文件后,必须使用 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.

维持三分热 2024-11-09 02:55:52

我不熟悉以这种方式从桌面打开文件,但在写入文件后关闭 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.

睫毛上残留的泪 2024-11-09 02:55:52
    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;
    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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文