This was forwarded to me by one of my coworkers...I wonder if anyone has
some experience and possible insight onthe issue here...

Below is the content of what we are currently trying to do with iText.



*PROBLEM:* Read an existing PDF, populate form field values and change form
fields to Read-Only.

*SOLUTION:* According to several references, it looks like it should be
possible to use the setFieldProperty method of AcroFields Class.

*RESULTS:* Currently, form field values are updated correctly, but we are
not able to change the Read-only property.



*REPORTING ENVIRONMENT*

ColdFusion: 7.x (using JVM 1.4x)

iText: 2.1.7

Acrobat PDF:  v6+ Files



*CODE REFERENCES*

Read-Only Example:
http://www.itextpdf.com/examples/index.php?page=example&id=161

AcroFields Reference:
http://tom.tharrisx.homedns.org/sm3rdpartyref/itext-docs-2.0.4/com/lowagie/text/pdf/AcroFields.html



*ColdFusion Source Code*

<cfscript>

//----------------------------------------------------------------------------------

// FILENAME: iText_demo.cfm

// PURPOSE: Populate form fields and make a field Read Only using iText
v2.1.7 library.

//

// CREATED: 03/05/2010

//----------------------------------------------------------------------------------



fullPathToInputFile = expandpath("testTemplate.pdf");  // full path to PDF
you want to modify



// full path to the PDF we will output. Using creatUUID() to create

// a unique file name so we can delete it afterwards

tempFilename = "#createUUID()#.pdf";

fullPathToOutputFile = expandpath(tempFilename);



ipaths = arrayNew(1);

arrayAppend(ipaths,"C:\CFusionMX7\jlib\iText-2.1.7.jar");

myloader = createObject("component", "javaloader.JavaLoader").init(ipaths);



// Step 1: Read in the source pdf

reader = myloader.create("com.lowagie.text.pdf.PdfReader").init(
fullPathToInputFile );

PageSize = myloader.create("com.lowagie.text.PageSize");

document =
myloader.create("com.lowagie.text.Document").init(PageSize.LETTER);



// Step 2: Create an output stream for the destination file

outStream = createObject("java", "java.io.FileOutputStream").init(
fullPathToOutputFile );



// Step 3: Open a stamper for generating the new pdf file

stamper = myloader.create("com.lowagie.text.pdf.PdfStamper").init( reader,
outStream );



PdfAnnotation = myloader.create("com.lowagie.text.pdf.PdfAnnotation");

TextField = myloader.create("com.lowagie.text.pdf.TextField");

AcroFields = myloader.create("com.lowagie.text.pdf.AcroFields");



pdfForm = stamper.getAcroFields(); // get the form fields



// Set value of PDF form fields

bRet = pdfForm.setField("text1", "Some text for 'text1' field");

WriteOutput("setField - bRet: #bRet#,
value[#pdfForm.getField('text1')#]<br>");

// Results: bRet is consistently “YES” and field is updated correctly…



bRet = pdfForm.setField("text2", "Some text for 'text2' field");

WriteOutput("setField - bRet: #bRet#,
value[#pdfForm.getField('text2')#]<br>");

// Results: bRet is consistently “YES” and field is updated correctly…



bRet = pdfForm.setField("text3", "Some text for 'text3' field");

WriteOutput("setField - bRet: #bRet#,
value[#pdfForm.getField('text3')#]<br>");

// Results: bRet is consistently “YES” and field is updated correctly…



WriteOutput("PdfAnnotation.FLAGS_PRINT: #PdfAnnotation.FLAGS_PRINT#<br>");

WriteOutput("PdfAnnotation.FLAGS_READONLY:
#PdfAnnotation.FLAGS_READONLY#<br>");



// 68 = FLAGS_PRINT | FLAGS_READONLY

bRet = pdfForm.setFieldProperty("text1", "flags", 68, JavaCast("null", ""));

WriteOutput("setFieldProperty of text1 to Read Only - bRet: #bRet#");



// Results: bRet is consistently “NO” and no property change occurs.





/*

com.lowagie.text.pdf.PdfAnnotation

public static final int    FLAGS_HIDDEN 2

public static final int    FLAGS_INVISIBLE      1

public static final int    FLAGS_LOCKED 128

public static final int    FLAGS_NOROTATE       16

public static final int    FLAGS_NOVIEW 32

public static final int    FLAGS_NOZOOM 8

public static final int    FLAGS_PRINT   4

public static final int    FLAGS_READONLY       64

public static final int    FLAGS_TOGGLENOVIEW   256

public static final int    MARKUP_HIGHLIGHT     0

public static final int    MARKUP_SQUIGGLY      3

public static final int    MARKUP_STRIKEOUT     2

public static final int    MARKUP_UNDERLINE     1

*/



stamper.close();     // close the stamper and output our new PDF

reader.close();            // close the reader

</

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Want to reach the ColdFusion community with something they want? Let them know 
on the House of Fusion mailing lists
Archive: 
http://www.houseoffusion.com/groups/cf-talk/message.cfm/messageid:331446
Subscription: http://www.houseoffusion.com/groups/cf-talk/subscribe.cfm
Unsubscribe: http://www.houseoffusion.com/groups/cf-talk/unsubscribe.cfm

Reply via email to