Rohan Pillai
Rohan Pillai

Reputation: 967

iText PDF Creation Error

I am using iText to create a PDF File from my Java file using NetBeans 8.2 . I have downloaded the iText JAR files from here. My iText version is 7.0.2.

Here is the partial code (I am only including the import files and code related to iText from my Java File):
Attendance.java

import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.Paragraph;

.
.
.
.

try {
    OutputStream output = new FileOutputStream(new File("C:\\Users\\dell\\Desktop\\Attendance.pdf"));
    DefaultListModel model1 = (DefaultListModel)present.getModel();            
    Document doc = new Document();
    PdfWriter.getInstance(doc, output);
    doc.open();
    for (int i = 0; i < 68; i++) {
        doc.add(new Paragraph((String) model1.getElementAt(i)));
    }                      
} catch (FileNotFoundException ex) {
    Logger.getLogger(Attendance.class.getName()).log(Level.SEVERE, null, ex);
}

Problem:

I am getting the following 3 errors:

  1. no suitable constructor found for Document(no arguments)

Document Error

  1. cannot find symbol: getInstance(Document, OutputStream)

getInstance

  1. cannot find symbol: open()

open

I have followed a lot of tutorials like this, yet somehow they did not face the error I am facing even though I have used the same code. If someone has used iText to do such a thing, any advice would be appreciated. Let me know if you need any more information.

Upvotes: 2

Views: 5213

Answers (1)

Micha&#235;l Demey
Micha&#235;l Demey

Reputation: 1577

You're mixing up iText 5 tutorials and iText 7 code. You are using iText 7, but you are looking at an unofficial tutorial that describes how to use iText 5. iText 7 is a complete rewrite of iText 5, and the API is totally different.

Please take a look at the following tutorials on the official web site:

A hello world example can be found here and looks like this:

//Initialize PDF writer
PdfWriter writer = new PdfWriter(dest);
//Initialize PDF document
PdfDocument pdf = new PdfDocument(writer);
// Initialize document
Document document = new Document(pdf);
//Add paragraph to the document
document.add(new Paragraph("Hello World!"));
//Close document
document.close(); 

As you can see, it's completely different from the iText 5 code you are using.

Important: ALWAYS go to the official web site for information! There are many web sites that contain examples that give you the wrong advice, especially regarding digital signatures (outdated examples) and merging PDF's (wrong approach).

As for downloading iText, always start on the download hub page because that contains the links to the most recent versions, including a compatibility matrix for the add-ons.

Upvotes: 5

Related Questions