ALG
ALG

Reputation: 93

creating a pdf file through java with a hyperlink

I am trying to create a PDF file using Java. What I have seen is that I must have the iText library so I went and I got it.

This is the piece of code I wrote but it's all full of errors... There is something going wrong here.

//com.lowagie.text.pdf.PdfWriter

public class document  {
    Document document = new Document (PageSize.A4, 50, 50, 50, 50);

    PdfWriter writer = PdfWriter.getInstance(document, \
    new FileOutputStream("C:\ITextTest.pdf"));
    document.open(); 

    document.add(new Paragraph("First page of the document."));
    //document.add(new Paragraph("Some more text on the \ first page with different color and font type.", 
    //FontFactory.getFont(FontFactory.COURIER, 14, Font.BOLD, new Color(255, 150, 200))));

    document.close(); 
}

Upvotes: 0

Views: 1837

Answers (2)

CodeChula
CodeChula

Reputation: 26

Try this:

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileOutputStream;
import java.io.File;
import java.io.FileNotFoundException;

public class MyPDF {
    public static void main(String[] args) {

        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        try {

            PdfWriter.getInstance(document, new FileOutputStream(new File(
                    "Test.pdf")));
            document.open();
            String content = "pdf data...";
            Paragraph paragraph = new Paragraph(content);
            document.add(paragraph);

        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }
}

Upvotes: 1

Kazekage Gaara
Kazekage Gaara

Reputation: 15052

I don't know about the rest,but this for sure is a mistake. Please rectify this :

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\ITextTest.pdf"));
  1. There was an unnecessary \ in there.

  2. You need to escape \ in the file path. So add an extra \.

Upvotes: 2

Related Questions