0

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(); 
}
8
  • There's a lot going wrong here. I'd worry about the syntax before the PDFWriter.
    – Ryan Amos
    Aug 22, 2012 at 14:36
  • I tried doing exactly as what they did in the tutorial
    – ALG
    Aug 22, 2012 at 14:38
  • 2
    Please don't use a field called document in a class called document. Actually, please don't call your class document but instead learn the Java naming conventions and stick to them. Also, you need to put your code in a method! (And yes, the tutorial does use \ when lines are wrapped...) Aug 22, 2012 at 15:00
  • 1
    This documentation from IBM (apart from having its examples wrongly formatted) refers to a mighty old version of iText. You might want to refer to the actual iText official documentation, and use the latest iText build available... Aug 22, 2012 at 15:01

2 Answers 2

2

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 \.

1

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();
        }
    }
}
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.