Hello,

as far as I know I have read nearly everything what I found about setting a 
defaultvalue in a textfield (AcroFields), but some misunderstanding was 
preventing my success for some days. Here is my proposal for others to use if 
necessary and I hope it is not wrong.

It is known that PdfStamper.getAcroFields().setField(...) sets only the value 
(PdfName.V) and not the default value (PdfName.DV). While using merged, I have 
created a PDF with no changed value or default value. If I use 
AcroFields.getFieldItem.widgets and AcroFields.getFieldItem.merged both fields 
(PdfName.V and PdfName.DV)  are written, but after opening the PDF the value is 
not seen, because the appearance stream was not changed. So I use the 
AcroFields.setField(...) after the PdfDictionary(Widgets).put(PdfName.DV, new 
PdfString(...)) and then value and appearance are okay too.

Only one question remains: do I have to flatten the form? Where do I find an 
explanation for this issue.

I want to thank all the people who created and maintain iText for this 
wonderful product: specially B.Lowagie and P.Soares.

My source code working:

import ...

public class testPDFInst {
    
     public testPDFInst() {
    }
    
     public static void main(String[] args) {
        int iChangedFields = 0;
        int iFoundFields = 0;
        File oSourceFile = new File("demoform.pdf");
        File oTargetFile = new File("newform.pdf");
        StringBuffer instLogBuf = new StringBuffer();
        try {
            PdfReader oPdfReader = new PdfReader(oSourceFile.getAbsolutePath());
            PdfStamper oPdfStamp = new PdfStamper(oPdfReader, new 
FileOutputStream(oTargetFile));
            AcroFields oAcroFields = oPdfStamp.getAcroFields();
            AcroFields.Item oAcroItem = null;
 
            PdfDictionary oPdfDictMerged = null;
            PdfDictionary oPdfDictWidgets = null;
            
            iFoundFields = oAcroFields.getFields().size();
            
            String sKey = "formtextfield";
            String sOldValue = "";
            String sOldDefaultValue = "";
            String sNewContentString = "Changing the content through iText";
            
            oAcroItem = oAcroFields.getFieldItem(sKey);
            if (oAcroItem != null) {
                oPdfDictMerged = (PdfDictionary) oAcroItem.merged.get(0);
                oPdfDictWidgets = (PdfDictionary) oAcroItem.widgets.get(0);
                
                PdfString oPdfDefaultValue = (PdfString) 
oPdfStamp.getReader().getPdfObject(oPdfDictMerged.get(PdfName.DV));
                PdfString oPdfValue = (PdfString) 
oPdfStamp.getReader().getPdfObject(oPdfDictMerged.get(PdfName.V));
                
                if (oPdfDefaultValue != null) {
                    sOldDefaultValue = oPdfDefaultValue.toString();
                }
                if (oPdfValue != null) {
                    sOldValue = oPdfValue.toString();
                }
                              
                PdfName oPdfType = (PdfName) 
PdfReader.getPdfObject(oPdfDictMerged.get(PdfName.FT));
                if (PdfName.TX.equals(oPdfType)) {
                    
                // sets the default value
                oPdfDictMerged.put(PdfName.DV, new 
PdfString(sNewContentString));
                    oPdfDictWidgets.put(PdfName.DV, new 
PdfString(sNewContentString));
                    
                    // sets the value and refreshes the appearance
                    oAcroFields.setField(sKey, sNewContentString + 
"_plus_a_difference");
                    
                    instLogBuf.append("Fieldname: '" + sKey + "'");
                    instLogBuf.append("\n\tFound value:             '" + 
sOldValue + "'");
                    instLogBuf.append("\n\tFound DefaultValue:      '" + 
sOldDefaultValue + "'");
                    instLogBuf.append("\n\tUse new content:         '" + 
sNewContentString+"'");
                    instLogBuf.append("\n\n");
                    iChangedFields++;
                }
            }
                
            oPdfStamp.close();
            oPdfReader.close();
            
        } catch (DocumentException doe) {
            doe.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        // end processing file
        instLogBuf.append("\nNumber of fields modified: " + iChangedFields);
        instLogBuf.append("\nNumber of fields found in document: " + 
iFoundFields);
        
        System.out.println(instLogBuf.toString());

    }
}

Short explanation of the working:There is a PDF with one formfield named 
'formtextfield': it has a defaultvalue 'no DefaultContent' and a value 'but a 
certain Value'. The value should be 'Changing the content through 
iText_plus_a_difference' and the defaultvalue 'Changing the content through 
iText'. 






-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
iText-questions mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/itext-questions
Buy the iText book: http://itext.ugent.be/itext-in-action/

Reply via email to