Hi,
I'am using itextPDF APIs in my project.The scenario is to add a few paragraphs and then followed by an image and then agian a series of text.
say: For n number of times 1.TEXT CONTENT 2.IMAGE 3.TEXT CONTENT
I added #1 TEXT CONTENT,#2.IMAGE and #3 to a Paragraph,and then to document. I tried with two images a small and one big(which will reqire one page to render on pdf) The small one worked fine, but when tried to add a bigger image the above sequence was not in order.
The text which was added after the image started appearing before the mage and the image slipped on to the next page.This is because the image needs one
whole page so went on to next page,but the text which was below image crept on current page which is not expected.
I tried using adding Paragraph to a Chapter, which worked but always diaplayed the chapter number.When I set chapter.setTriggerNewPage(false) the same
behaviour as detailed above was seen.
I have attached both the source, can I request you to help me in getting this issue resolved.
1. Using Paragraph
public Test3() {
// TODO Auto-generated constructor stub
}
public static Image getImageFromResource(String URI){
Image image = null;
try {
image = Image.getInstance(URI);
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return image;
}
public static void main(String[] args) {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
PdfWriter.getInstance(document , new FileOutputStream("D:\\test\\TestPage.pdf"));
document.open();
Paragraph p = new Paragraph();
p.add("TEXT EXPECTED BEFORE IMAGE ");
Paragraph p1 = new Paragraph();
Image image = getImageFromResource("D:\\test\\Test.jpg");
p1.add(image);
Paragraph p2 = new Paragraph();
p2.add("TEXT EXPECTED AFTER IMAGE ");
document.add(p2);
document.add(p1);
document.add(p);
} catch(DocumentException de) {
System.err.println(de.getMessage());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
2.Using Chapter
public Test3() {
// TODO Auto-generated constructor stub
}
public static Image getImageFromResource(String URI){
Image image = null;
try {
image = Image.getInstance(URI);
} catch (BadElementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return image;
}
public static void main(String[] args) {
Document document = new Document(PageSize.A4, 50, 50, 50, 50);
try {
PdfWriter.getInstance(document , new FileOutputStream("D:\\test\\TestPage.pdf"));
document.open();
Paragraph p = new Paragraph();
p.add("TEXT EXPECTED BEFORE IMAGE ");
Paragraph p1 = new Paragraph();
Image image = getImageFromResource("D:\\test\\Test.jpg");
p1.add(image);
Paragraph p2 = new Paragraph();
p2.add("TEXT EXPECTED AFTER IMAGE ");
for(int i=0;i<10;i++){
Chapter chapter1 = new Chapter(p, 1);
Chapter chapter2 = new Chapter(p1, 2);
Chapter chapter3 = new Chapter(p2, 3);
document.add(chapter1);
document.add(chapter2);
document.add(chapter3);
}
} catch(DocumentException de) {
System.err.println(de.getMessage());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
document.close();
}
Thanks in advance, Kiran