2

I want to create a PDF file from java code. And this is what I have written so far-

public class IOExp  {
    public static void main(String args[]) throws java.io.IOException   {
        java.io.FileOutputStream fout=new java.io.FileOutputStream("MyFile.pdf");

        byte [] arr=("Hello World.\n"+"My name is Prateek Mathur, and I am  a great programmer.\n").getBytes();
        fout.write(arr);
    }
}

MyFile.pdf gets created without any problem, but on opening it, Adobe Reader displays the following message-

'Abode Reader can not open 'MyFile.pdf' because it is either not a supported file
 type or because the file has been damaged (for example, it was sent as an email
 attachment and wasn't correctly decoded.'

I am already familier with iText API, and want to know how to create PDF files manually.

What is the problem with my code, and what changes need to be introduced to successfully create the PDF file??

4

2 回答 2

2

PDF 文件与简单的纯文本文件完全不同。您创建的是纯文本文件。将扩展名更改为.txt,当使用纯文本编辑器打开时,您将看到文字。使用类似PDFBox的库。

于 2013-09-07T12:27:20.507 回答
2

我认为您不能通过简单地创建扩展名为 .pdf 的文件来创建 pdf。创建有效的 pdf 需要特定格式的有效标题和内容字节。您可以使用 Apache PdfBoxItextPdf等框架,而不是重新发明 whee 。

于 2013-09-07T12:21:05.920 回答