tags:

views:

90

answers:

3

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

A: 

Are you wanting the image to be rendered on a page itself? One solution is to use document.next() to insert a page break before and/or after the image. You can also try wrapping all 3 of your elements into another object such as a Phrases and Chunks in IText.

Brad Gardner
A: 

Hi Brad,

 Thanks for the input.Adding  document.newPage() worked :
       document.add(p2);
       document.add(p1);
       document.newPage();      
       document.add(p);

But Paragraph p will always be on a new page even if the image occupies 1/2 of the page; probably it would be a good idea to place P on the same page. Any thoughts? Yes, I tried putting altogether in a single object. But the text which is supposed to be below image creeps above. here is the code example:

 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 ");             
   Image image =  getImageFromResource("D:\\test\\Test.jpg");                  
   p.add(image);         
   p.add("TEXT EXPECTED AFTER IMAGE ");                
   document.add(p);   

  } catch(DocumentException de) {        
   System.err.println(de.getMessage());        
  } catch (Exception e) {        
    // TODO Auto-generated catch block        
    e.printStackTrace();   

Keeping everything in a single paragraph is what I'm looking for as i need to create a PDF for n number of routes with all possible route permutations and each may have an image at the end of route. So if can put all the elements in one object, which would be great.

TIA, Kiran

kiran
Edit your original question to add more information.
Shaggy Frog
A: 

Thanks Brad got it resolved

have a nice day, Happy Easters

cheers!

kiran
Don't reply to your own question. This is not a forum. If you think Brad helped you solve your problem, you should mark his question has answered and also upvote it.
Shaggy Frog