5

我需要使用 Java 将一些文本写入 PDF 文档。我为此写了一个代码。但我无法打开文件。如果你们有什么想法,请与我分享。

public class WritPDF
{

    public static void main(String[] args)
    {
        Writer writer = null;

        try
            {
                String text = "This is a text file";

                File file = new File("C:/Users/PrinterTest/Hi1.pdf");
                writer = new BufferedWriter(new FileWriter(file));;
                writer.write(text);
            } catch (FileNotFoundException e)
            {
                e.printStackTrace();
            } catch (IOException e)
            {
                e.printStackTrace();
            } finally
            {
                try
                    {
                        if (writer != null)
                            {           
                            }
                    } catch (IOException e)
                    {}
            }
    }

}
4

1 回答 1

8

您的代码正在编写一个扩展名为.pdf. PDF 文件不是纯文本文件。

有几个库可用于在 Java 中处理 PDF 文件,例如iTextApache PDFBox

于 2013-04-11T09:22:39.973 回答