PDF with multiple paragraphs from Java Itext - java

I have a array of Strings as follows :-
String[] data = {“Sunday”,”Monday”,”Tuesday”,”Wednesday”,”Thursday”,”Friday”,”Saturday”}.
Now I want to write this data strings to a pdf file one below the other like :-
1. Sunday
2. Monday
3. Tuesday
4. Wednesday
5. Thursday
6. Friday
7. Saturday.
I am using itext to achieve this. Below is the code snippet I am using
for(int i= 0; i< data.length;i++)
{
Document document=new Document();
PdfWriter.getInstance(document, new FileOutputStream(directory));
document.open();
document.add(new Paragraph(data[i]));
document.add(Chunk.NEWLINE);
document.close();
}
Problem :-
The pdf file which I get has only :-
Saturday.
Please help.

The problem is, you are creating the document in the loop. Try this:
Document document=new Document();
PdfWriter.getInstance(document, new FileOutputStream(directory));
document.open();
for(int i= 0; i< data.length;i++)
{
document.add(new Paragraph(data[i]));
document.add(Chunk.NEWLINE);
}
document.close();
You might want to handle closing of the stream in case something happens.
With Java 7 or above you can achieve with this:
Document document=new Document();
try (FileOutputStream fos = new FileOutputStream(directory)) {
PdfWriter.getInstance(document, fos);
document.open();
for(int i= 0; i< data.length;i++)
{
document.add(new Paragraph(data[i]));
document.add(Chunk.NEWLINE);
}
//EDIT start
document.close();
//EDIT end
}

You are creating document in a loop and also closing same.
make sure document is open and close only once in its lifetime.
try
{
Document document=new Document();
PdfWriter.getInstance(document, new FileOutputStream(directory));
document.open();
for(int i= 0; i< data.length;i++)
{
document.add(new Paragraph(data[i]));
document.add(Chunk.NEWLINE);
}
}
finally{
document.close();
}

Related

I want to change the Position of Anchor link in my PDF. Is there anyway to do it?

This is my code which is redirecting me from 1 page to another in same PDF
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("PageShifting.pdf"));
document.open();
Anchor anchor = new Anchor("Click here to Redirect at Page no.03");
anchor.setReference("#linkTarget");
Paragraph paragraph = new Paragraph("Hello this is new PDF \n");
paragraph.add(anchor);
paragraph.setPaddingTop(1000);
document.add(paragraph);
document.newPage();
Anchor anchorTarget = new Anchor("This is the target of the link above");
anchorTarget.setName("linkTarget");
Paragraph targetParagraph = new Paragraph();
targetParagraph.setSpacingBefore(50);
Chunk chunk = new Chunk("Contact information");
chunk.setLocalGoto("contact");
document.add(new Paragraph(chunk));
document.newPage();
targetParagraph.add(anchorTarget);
document.add(targetParagraph);
System.out.println("PDF Generated");
document.close();

Printing Multiple JTable in a PDF using itext

I am trying to print two JTable in a PDF file, here is what I tried but is printing only the headers of the column, please how will I add the content of the table.
try{
Document document = new Document();
PdfWriter.getInstance(document,new FileOutputStream("transcript.pdf"));
document.open();
document.add(new Paragraph("UNIVERSITY OF MAIDUGURI"));
document.add(new Paragraph("======================================================================="));
PdfPTable table1 = new PdfPTable(partITable.getColumnCount());
PdfPTable table2 = new PdfPTable(partITable2.getColumnCount());
table1.setSpacingAfter(10);
table1.setSpacingBefore(5);
for(int i=0;i<partITable.getColumnCount();i++){
table1.addCell(partITable.getColumnName(i));
}
for(int rows=0;rows<partITable.getRowCount()-1;rows++){
for(int cols=0;cols<partITable.getColumnCount();cols++){
table1.addCell(partITable.getModel().getValueAt(rows,cols).toString());
}
}
for(int i=0;i<partITable2.getColumnCount();i++){
table2.addCell(partITable2.getColumnName(i));
}
for(int rows=0;rows<partITable2.getRowCount()-1;rows++){
for(int cols=0;cols<partITable2.getColumnCount();cols++){
table2.addCell(partITable2.getModel().getValueAt(rows,cols).toString());
}
}
document.add(table1);
document.add(table2);
document.close();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
You have a logic error. I guess that you have following situation: partITable.getRowCount() = partITable2.getRowCount() = 1. Because you set the upper bound to partITable.getRowCount() - 1 = 0, the condition of the for loop returns false, which implies that the body of the for loop will be never executed.
Summarized you have to set the upper bound to partITable.getRowCount()

Generated pdf file save inside the project folder

i want to save the generated pdf file inside my project folder ,
using this code . I have generated it successfully and want to save
it in my project folder , where the jsp file exist ,Below is my code
<%# page import="java.servlet.*,
javax.servlet.http.*,
java.io.*,
java.util.*,
com.itextpdf.text.pdf.*,
com.itextpdf.text.*,java.sql.*" %><%
response.setContentType("application/pdf"); Document document = new
Document(); try{
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
PdfWriter.getInstance(document, buffer);
document.open();
PdfPTable table = new PdfPTable(2);
table.addCell("1");
table.addCell("2");
table.addCell("3");
table.addCell("4");
table.addCell("5");
table.addCell("6");
document.add(table);
document.close();
DataOutput dataOutput = new DataOutputStream(response.getOutputStream());
byte[] bytes = buffer.toByteArray();
response.setContentLength(bytes.length);
for(int i = 0; i < bytes.length; i++)
{
dataOutput.writeByte(bytes[i]);
}
}catch(DocumentException e){
e.printStackTrace(); }
%>
thanks in advance..
Use getServletContext().getRealPath(..)

How to add header in iText 5.2.1?

I use iText 5.2.1 and I have already created Document object.
like this:
document.addHeader("hello");
Why don't I see the header on the PDF File?
Use Page Events. Many good answers have been provided already on StackOverflow, for example this one, or this other one.
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("FicheClient.pdf"));
document.open();
document.add(new Paragraph("hello World",FontFactory.getFont(FontFactory.TIMES_BOLD,20,Font.BOLD,BaseColor.BLACK)));
document.add(new Paragraph("---------------------------------------"));
document.close();
JOptionPane.showMessageDialog(null, "Raport saved ");
} catch (Exception e) {
}

Making a paragraph in iText pdf not divided in two pages

I'm writing a pdf file with iText and I want the paragraphs divided across two different pages. How can I do this?
It is a lot easier to help if you could provide a more accurate example of the paragraphs and the document you are creating, but as far as I understand it is something like this:
Generate an ArrayList or other weapon of choise in making an iterable list of the paragraphs. Iterate through that list and call newPage() before adding content to page 2
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream([file]));
for (ArrayList<Paragraph> : theParagraph ) {
document.addElement(theParagraph)
document.newPage();
}
document.close();
This will automaticaly add new pages as content is added on the pdf-document, but with less controle over when the pagebreak occurs:
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream([file]));
document.open();
for(int i=0 ; i<100; i++){
document.add(new Paragraph("This is a very important message"));
}
document.close();

Categories