Hi Bruno (and list),

i have tried to follow your advice but i feel somewhat confused :-)

I have a pdf, letter1.pdf, which contains a field (rtf-enabled). I find the fields position on the canvas, create a column text and add all of my HTML elements which i get from HTMLWorker.parseToList.

The output is a blank copy (with empty fields) of the original pdf (letter1.pdf). What do i miss? Here is a snippet of the code to exemplify further:

>> It would be great if the HtmlParser class could take a
>> "PdfContentByte" object (or a similar reference) where the content is
>> a placeholder (instead of a whole "document" as it is now) to
>> effectively position the html output generated from the parser into
>> this specific location.

>I wouldn't use HtmlParser for that.
>I'd use HtmlWorker to parse an HTML snippet
>into an ArrayList of iText objects, then add all these
>objects to a ColumnText object and position this
>ColumnText so that it fits the placeholder.
>It's all there in iText; there is no need for a feature
>request. You have to specify how you want the
>HTML to be rendered (styles), you have to specify
>the behavior of the ColumnText if the text doesn't
>fit the placeholder, etc...
>There are too many thing that depend on choice
>that have to be made by the developer to turn this
>into a new iText method.



public void readHtml(ByteArrayOutputStream baos) {
 
    //Document document = new Document(PageSize.A4, 80, 50, 30, 65);

    try {        
     
     // baos contains a pdf as a byte array (read in other function)
     PdfReader reader = new PdfReader(/*new FileInputStream("e:/letter1.pdf"));*/baos.toByteArray());
     
     Document document = new Document(reader.getPageSizeWithRotation(1));
         
     PdfDictionary acro = (PdfDictionary) PdfReader.getPdfObject(
          reader.getCatalog().get(PdfName.ACROFORM));
     
     acro.remove(new PdfName("XFA"));  
     FileOutputStream fos = new FileOutputStream("e:/brevout.pdf");
     PdfStamper stp = new PdfStamper(reader, fos );
     stp.setFormFlattening(false);
     AcroFields af = stp.getAcroFields();
         
     HTMLWorker worker = new HTMLWorker(document);  
     
     StyleSheet style = new StyleSheet();
     style.loadStyle("tablecells", "color", "#000000");
     style.loadTagStyle("li", "", "");
     style.loadTagStyle("p", "", "");
     style.loadTagStyle("a", "color", "blue");
     style.loadTagStyle("a", "u", "");
     style.loadStyle("tablecells", "size", "8px");
     style.loadStyle("tablecells", "align", "justify");
     style.loadTagStyle("pre", "size", "8px");
     style.loadTagStyle("table", "bgcolor", "aqua");
     style.loadTagStyle("table", "align", "justify");        
     style.loadStyle("NavBar", "size", "8px");
     
     FileReader fr = new FileReader("e:/HelloWorld.html");
     ArrayList array = HTMLWorker.parseToList(fr,style);  // array of itext objects
         
     float[] position = af.getFieldPositions("text");    
     PdfContentByte cb = stp.getOverContent((int)position[0]);
         
     ColumnText ct = new ColumnText( cb );
     ct.setSimpleColumn(position[1], position[2],position[3], position[4]);
     
     System.out.println(position[1] + "      " + position[2] + "      " + position[3]  + "      " +  position[4]);

     ct.setLeading(0, 1);
         
     for (int idx = 0; idx < array.size(); idx++)
     {    
      ct.addElement( (Element)array.get(idx) );  
     }
     
     ct.go();
     stp.close();    
             
    } catch(Exception e) {
     e.printStackTrace();
     System.err.println(e.getMessage());
    }
   }



kind regards

Daniel

Reply via email to