Hello, and thank you for taking time to analyze this problem.
I am trying to create a pdf file where multiple images of documents are 
translated onto the pages.  I am having a problem where setting the header with 
document.setHeader() multiple times is causing the header to become distorted 
after calling the method more than once, shrinking and automatically moving the 
alignment to the left corner of the document.  I am not sure why, and after 
looking through the forums and documentation I did not come across potential 
problems that would make this occur.  I have also tried moving where the header 
is set to muliple places, getting the same effect.  If you can offer any 
insight, it would be highly appreciated.
Below is the code that I am using to generate the pdf.  To clarify some points 
that might not be apparent, the array docURLs contains the locations of tiff 
images to be converted into the pdf; there is also a loop for masking certain 
parts of the document that contain sensitive information.  There are 
potentially multiple pages to each document, which is taken care of in another 
loop.
Thank you.

import java.util.StringTokenizer;
import java.io.FileOutputStream;
import com.lowagie.text.Document;import com.lowagie.text.Element;import 
com.lowagie.text.Font;import com.lowagie.text.HeaderFooter;import 
com.lowagie.text.Image;//import com.lowagie.text.Paragraph;import 
com.lowagie.text.Phrase;
import com.lowagie.text.pdf.BaseFont;import 
com.lowagie.text.pdf.PdfContentByte;//import 
com.lowagie.text.pdf.PdfPCell;import com.lowagie.text.pdf.PdfWriter;import 
com.lowagie.text.pdf.RandomAccessFileOrArray;import 
com.lowagie.text.pdf.codec.TiffImage;


//import com.lowagie.text.ExceptionConverter;import 
com.lowagie.text.Rectangle;//import com.lowagie.text.pdf.PdfPTable;import 
com.lowagie.text.pdf.PdfPageEventHelper;



/** *  * */public class PrintHelper extends PdfPageEventHelper  {               
private static String pdfHeader="";     public static final String CLASSNAME    
        = PrintHelper.class.getName();          public static void main(String 
args[])  {               generatePDF();  }           /**     *...@see 
com.lowagie.text.pdf.PdfPageEventHelper#onEndPage(com.lowagie.text.pdf.PdfWriter,
 com.lowagie.text.Document)     *      */    public void onEndPage(PdfWriter 
writer, Document document) {        try {            document.setHeader(new 
HeaderFooter(new Phrase(pdfHeader), false));        }        catch (Exception 
e) {                   }    }
        /**     * Generates the pdf     */      public  static void generatePDF 
() {                            String[] docURLs = new String[2];               
docURLs[0] = "C:/sortedDocs/exampleDoc.TIF";            docURLs[1] = 
"C:/sortedDocs/exampleDoc2.TIF";                           try {                
                           Document document = new Document();                  
   FileOutputStream out = new FileOutputStream("C://Test.pdf");                 
   PdfWriter writer = PdfWriter.getInstance(document, out);                     
   writer.setPageEvent(new PrintHelper());                 int pages = 0;       
           RandomAccessFileOrArray ra = null;
                        //set up the starting page size and header              
        String starturl = docURLs[0];                   try {                   
        ra = new RandomAccessFileOrArray(starturl);                     }       
                catch(Exception e) {                            
e.printStackTrace();                    }                       Image 
startImage = TiffImage.getTiffImage(ra, 1);                       
document.setPageSize(new Rectangle(startImage.getScaledWidth(), 
startImage.getScaledHeight()));                 //set up the header before 
first page loaded                    String custNo = "test"; 
//docURLs[0].getCustNo();                       String jobNo = "test"; 
//docURLs[0].getJobNo();                 String rejected= "A"; 
//docURLs[0].getDocStatus();                      String docRejected="";        
          String role = "";                       if(rejected!=null && 
rejected.trim().equalsIgnoreCase("R"))       docRejected="DOCUMENT REJECTED  "; 
                   else if(rejected!=null && 
rejected.trim().equalsIgnoreCase("A"))  docRejected="DOCUMENT APPROVED  ";      
              BaseFont bf_times = BaseFont.createFont(BaseFont.TIMES_ROMAN, 
"Cp1252", false);                 HeaderFooter header = new HeaderFooter(       
          new Phrase(docRejected +"COPY
 CustNo-"+custNo+" JobNo- "+jobNo, new Font(bf_times,40)), false);              
        header.setAlignment(Element.ALIGN_CENTER);                      
                        document.setHeader(header);                     
document.open();                        PdfContentByte cb = 
writer.getDirectContent();                  for(int l = 0 ; l<docURLs.length 
;l++ )                 {                                                        
       String url1 = docURLs[l];                               String 
coordinates1 = null;                                                            
 int comps = 0;                          try {                                  
 ra = new RandomAccessFileOrArray(url1);                                 comps 
= TiffImage.getNumberOfPages(ra);                                               
                  }                                                             
                                  catch (Throwable e) {                         
  }                                                                             
                  custNo = "test1";                               jobNo = 
"test1";                                role = null;                            
rejected= "A";                                                          
docRejected="";                         if(rejected!=null && 
rejected.trim().equalsIgnoreCase("R"))       docRejected="DOCUMENT REJECTED  "; 
                           else if(rejected!=null && 
rejected.trim().equalsIgnoreCase("A"))  docRejected="DOCUMENT APPROVED  ";      
                                                      pdfHeader = docRejected 
+"COPY CustNo-"+ custNo + " JobNo-"+jobNo;                                      
                                                                if(role==null 
|| role.trim().equalsIgnoreCase(""))                                      
role="01";                                                                      
                        try {                                   String 
coordinates =coordinates1;                                       int locX[] = 
null;                                              int locY[] = null;      
                                        int page[] =  null;                     
                        int scaleX[] =  null;                                   
        int scaleY[] =  null;                                           int 
absX = -10; // Constant relative image positioners                              
            int absY = 0    ;  // Constant relative image positioners           
                                    if(jobNo ==null || 
jobNo.trim().equalsIgnoreCase("") || jobNo.trim().equalsIgnoreCase("null"))  
jobNo="";                                               if(custNo==null || 
custNo.trim().equalsIgnoreCase("") || custNo.trim().equalsIgnoreCase("null")) 
custNo="";                                                                      
                       if(coordinates!=null && 
!coordinates.trim().equalsIgnoreCase("") && 
!coordinates.trim().equalsIgnoreCase("null")){                                  
                                                                            
StringTokenizer t = new StringTokenizer(coordinates,";",false);                 
                                int size = t.countTokens();                     
                                locX = new int[size];                           
                        locY = new int[size];                                   
                page = new int[size];                                           
        scaleX = new int[size];                                                 
scaleY = new int[size];                                                 absX = 
-10;     // Constant relative image positioners                                 
                 absY = 0        ;  // Constant relative image positioners      
                                                 int k = 0;                     
                         
        while(t.hasMoreTokens()) {                                              
                String s = t.nextToken();                                       
                        StringTokenizer n = new StringTokenizer(s,",");         
                                                if(n.hasMoreTokens()){          
                                                        s = n.nextToken();      
                                                                locX[k] = 
Integer.parseInt(s.trim());                                                     
      }                                                               
if(n.hasMoreTokens()){                                                          
        s = n.nextToken();                                                      
                locY[k] = Integer.parseInt(s.trim());                           
                                }                                               
                if(n.hasMoreTokens()){                                          
                        s = n.nextToken();                                      
                                scaleX[k] = Integer.parseInt(s.trim());         
                                                }                               
                                if(n.hasMoreTokens()){                          
                                        s = n.nextToken();                      
                                                scaleY[k] = 
Integer.parseInt(s.trim());                                                     
    }                                                               
if(n.hasMoreTokens()){                                                          
        s = n.nextToken();                                                      
                page[k] = Integer.parseInt(s.trim());                           
                                }                                               
                k++;                                                    }       
                                                }                               
                                                                        Image 
img2 = TiffImage.getTiffImage(ra, 1);                                           
  //document.setPageSize(new Rectangle(img2.getScaledWidth(), 
img2.getScaledHeight()));                                                       
                                    for (int c = 0; c < comps; ++c) {           
                                            try {                               
                            Image img = TiffImage.getTiffImage(ra, c + 1);      
            
                                        if(coordinates!=null && 
!coordinates.trim().equalsIgnoreCase("") && 
!coordinates.trim().equalsIgnoreCase("null") && 
!role.trim().equalsIgnoreCase("02")){                                           
                            byte maskr[] = new byte[16384];                     
                                            for (int w = 0; w < 16384; ++w)     
                                                                    maskr[w] = 
-1;                                                                  for(int 
i=0;i<scaleX.length;i++) {                                                      
                        if(page[i]==c+1){                                       
                                                for(int f =locX[i] 
;f<=locY[i];f++){                                                               
                             for(int s=f*64+scaleX[i] ;s<=f*64+scaleY[i] ;s++){ 
                                                                                
                     maskr[s]=0;                                                
                                             }                                  
                                                     }                          
                                                             Image mask = 
Image.getInstance(512,256, 1, 1, maskr);                                        
                                           mask.makeMask();                     
                                                                   
mask.setInverted(true);                                                         
                        img.setImageMask(mask);                                 
                                        }                                       
                                }                                               
                }                                                               
//img.scalePercent(19);                                                         
if (img != null) {                                                              
        absX=(int) (590-img.getScaledWidth());                                  
        absY=(int) (790-img.getScaledHeight());                                 
                                //img.setAbsolutePosition(absX, absY);          
                                                                
                img.scaleToFit(img.getScaledWidth()-70, 
img.getScaledHeight()-70);                                              
img.setAbsolutePosition(0,0);                                                   
                                                        cb.addImage(img);       
                                                                ++pages;        
                                                                                
                                                        document.newPage();     
                                                                
document.setPageSize(new Rectangle(img2.getScaledWidth(), 
img2.getScaledHeight()));                                                       
      }                                                       }                 
                                      catch (Throwable e) {                     
                                                                                
              }                                               }                 
                                                                              
ra.close();                                     }catch (Exception e) {          
                                                        }                       
          }                                             document.close();       
                out.flush();            }               catch (Exception e) {   
                                }                       }       
}



      
------------------------------------------------------------------------------
Enter the BlackBerry Developer Challenge  
This is your chance to win up to $100,000 in prizes! For a limited time, 
vendors submitting new applications to BlackBerry App World(TM) will have 
the opportunity to enter the BlackBerry Developer Challenge. See full prize 
details at: http://p.sf.net/sfu/blackberry
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Buy the iText book: http://www.1t3xt.com/docs/book.php
Check the site with examples before you ask questions: 
http://www.1t3xt.info/examples/
You can also search the keywords list: http://1t3xt.info/tutorials/keywords/

Reply via email to