Hi everybody,
i'm trying to use the class PdfCopyFields for copying PDFs with fields.

There are the same fields on different pages of a PDF and the fields 
have attached javascript (a coloured border is shown, when the focus 
is in the field). Therefore i'm using kid-fields with method setPlaceInPage().

When copying the PDF with PdfCopyFields the fields are copied correctly, but
the javascript ist lost. In fact, the javascript is attached to the field, 
but not to the field-kids any more.

Does anybody have an idea?

Thanks,
Peter

Here is some sourcecode for reproducing the problem:


import java.io.FileOutputStream;
import java.io.IOException;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.PageSize;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfAction;
import com.lowagie.text.pdf.PdfAnnotation;
import com.lowagie.text.pdf.PdfBorderDictionary;
import com.lowagie.text.pdf.PdfCopyFields;
import com.lowagie.text.pdf.PdfFormField;
import com.lowagie.text.pdf.PdfReader;
import com.lowagie.text.pdf.PdfWriter;


public class CopyWithFields
{

    public static void main(String[] args)
    {
        Document document = new Document(PageSize.A4, 50, 50, 50, 50);
        try 
        {
            PdfFormField aFormField, aKidField;
            String strFieldName;
            String strFile1 = "c:\\form_text.pdf";
            String strFocusColor;

            // creation of the different writers
            PdfWriter writer = PdfWriter.getInstance(document, new 
FileOutputStream(strFile1));
            document.open();

            strFieldName = "field1";
            Rectangle aRect = new Rectangle(100, 400, 100 + 300, 400 + 50);

            // create the field and it's first kid on page 1
            aFormField = createTextField(writer, strFieldName, aRect);
            PdfFormField aKidField1 = createKidField(writer, aFormField, 
strFieldName, aRect);
            aKidField1.setPlaceInPage(1);

            // create the second kid on page 2
            Rectangle aRect2= new Rectangle(100, 600, 100 + 100, 600 + 50);
            PdfFormField aKidField2 = createKidField(writer, aFormField, 
strFieldName, aRect2);
            aKidField2.setPlaceInPage(2);

            document.newPage();
            document.add(new Paragraph("Hello Fields Page 1"));

            writer.addAnnotation(aFormField);

            document.newPage();
            document.add(new Paragraph("Hello Fields Page 2"));

            document.close();
            
            // copy the PDF with PdfCopyFields
            PdfCopyFields copyFields = new PdfCopyFields(new 
FileOutputStream("c:\\form_text_copy.pdf"));
            PdfReader reader = new PdfReader(strFile1);
            copyFields.addDocument(reader);
            copyFields.close();
            
            System.out.println("finished.");
        }
        catch (Exception de) {
            de.printStackTrace();
        }
    }
    
    private static PdfFormField createTextField(PdfWriter writer, String 
strFieldName, Rectangle aRect) throws DocumentException, IOException
    {
        PdfFormField aFormField;
        aFormField = PdfFormField.createTextField(writer, false, false, 100);
        aFormField.setFieldName(strFieldName);
        aFormField.setUserName("Comment");
        
        return aFormField;
    }

    private static PdfFormField createKidField(PdfWriter writer, PdfFormField 
aParentField, String strFieldName, 
                                        Rectangle aRect) throws 
DocumentException, IOException
    {
        PdfFormField aKidField;
        
        float fontSize = 10;
        BaseFont helv = BaseFont.createFont("Helvetica", "winansi", false);
        
        aKidField = PdfFormField.createTextField(writer, false, false, 100);
        aKidField.setWidget(aRect, PdfAnnotation.HIGHLIGHT_INVERT);
      
        PdfAction aActionGetFocus = PdfAction.javaScript("var f = 
this.getField(\"" + strFieldName + "\"); f.strokeColor = color.blue;", writer);
        PdfAction aActionLooseFocus = PdfAction.javaScript("var f = 
this.getField(\"" + strFieldName + "\"); f.strokeColor = color.transparent;", 
writer);
        
        aKidField.setAdditionalActions(PdfAnnotation.AA_FOCUS, aActionGetFocus);
        aKidField.setAdditionalActions(PdfAnnotation.AA_BLUR, 
aActionLooseFocus);
        
        aKidField.setBorderStyle(new PdfBorderDictionary(1, 
PdfBorderDictionary.STYLE_SOLID));

        aParentField.addKid(aKidField);
        
        return aKidField;
    }
    
}



-- 


Echte DSL-Flatrate dauerhaft für 0,- Euro*!
"Feel free" mit GMX DSL! http://www.gmx.net/de/go/dsl


-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions

Reply via email to