我正在使用烘焙到ColdFusion中的iText库重命名PDF表单字段我可以很容易地重命名字段名,但是当您将鼠标悬停在与字段的新名称不匹配的字段上时,生成的pdf会显示一个“工具提示”。
我做了一些研究,看起来工具提示存储在字段字典中的“/tu”键中,可以使用
setUserName()
方法。但是,当我检查代码中的各种对象时,我看不到任何方法可以访问特定pdf字段的该方法。
如何使用itext库设置这个“/tu”键?
这是我目前的代码:
function renameFields( pathToFile ) {
// initialize the PDF file we will be working on
local.pdfService = new pdf();
local.pdfService.setSource( arguments.pathToFile );
local.pdfFile = local.pdfService.read();
// initialize the iText library objects
local.pdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader").init( toBinary( local.pdfFile ) );
local.outputStream = createObject("java", "java.io.ByteArrayOutputStream").init();
local.pdfStamper = createObject("java", "com.lowagie.text.pdf.PdfStamper").init( local.pdfReader, local.outputStream);
// get an instance of the acro fields object
local.acroFields = local.pdfStamper.getAcroFields();
// Get All of the Fields of the PDF
local.allFields = local.acroFields.getFields();
// convert the collection of fields into an array for easy iteration
local.fieldArray = listToArray( structKeyList( local.acroFields.getFields() ) );
// loop through all fields and rename them
for ( var a=1; a < arrayLen( local.fieldArray ); a++ ) {
// do the actual renaming
local.acroFields.renameField( local.fieldArray[a], "field_#a#" );
// update the field tooltip ???
}
// finish up and return the pdf file object
local.pdfStamper.setFormFlattening( false );
local.pdfStamper.close();
local.pdfReader.close();
local.myPdf = local.outputStream.toByteArray();
return local.myPdf;
}
CFML溶液
(更新日期:2018年7月31日)
注意:在这个解决方案中,我首先更改工具提示,然后重命名字段。
function renameFields( pathToFile ) {
// initialize the PDF file we will be working on
local.pdfService = new pdf();
local.pdfService.setSource( arguments.pathToFile );
local.pdfFile = local.pdfService.read();
// initialize the iText library objects
local.pdfReader = createObject("java", "com.lowagie.text.pdf.PdfReader").init( toBinary( local.pdfFile ) );
local.outputStream = createObject("java", "java.io.ByteArrayOutputStream").init();
local.pdfStamper = createObject("java", "com.lowagie.text.pdf.PdfStamper").init( local.pdfReader, local.outputStream);
// get an instance of the acro fields object
local.acroFields = local.pdfStamper.getAcroFields();
// Get All of the Fields of the PDF
local.allFields = local.acroFields.getFields();
// convert the collection of fields into an array for easy iteration
local.fieldArray = listToArray( structKeyList( local.acroFields.getFields() ) );
// loop through all fields and rename them
for ( var a=1; a < arrayLen( local.fieldArray ); a++ ) {
// create the tooltip text
local.newTooltip = createObject( "java", "com.lowagie.text.pdf.PdfString" ).init( "field_#a#" );
// update the field tooltip
local.field = local.acroFields.getFieldItem( local.fieldArray[a] );
local.field.values[ 1 ].put( local.pdfName.TU, local.newTooltip );
// do the actual renaming
local.acroFields.renameField( local.fieldArray[a], "field_#a#" );
}
// finish up and return the pdf file object
local.pdfStamper.setFormFlattening( false );
local.pdfStamper.close();
local.pdfReader.close();
local.myPdf = local.outputStream.toByteArray();
return local.myPdf;
}