user2255885
user2255885

Reputation: 333

How to Write PDF using Java

I need to write some text in to a PDF document using Java. I wrote a code for that. But i'm unable to open the file. If you guys have some idea please share with me.

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)
                    {}
            }
    }

}

Upvotes: 4

Views: 26919

Answers (1)

Jesper
Jesper

Reputation: 206776

Your code is writing a plain text file with the extension .pdf. A PDF file is not a plain text file.

There are several libraries available for working with PDF files in Java, for example iText and Apache PDFBox.

Upvotes: 8

Related Questions