【问题标题】:iText PDF Forms: How do you change the tooltip for a field?iText PDF 表单:如何更改字段的工具提示?
【发布时间】:2018-12-25 18:07:16
【问题描述】:

我正在使用已融入 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;

}

【问题讨论】:

    标签: itext pdf-generation


    【解决方案1】:

    我不知道 ColdFusion(语法),但解决方案应该是这样的:

    // 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 (if you already renamed it might have to use the new name)
         Item field = local.acroFields.getFieldItem(local.fieldArray[a]);
         field.getValue(0).put(PdfName.TU, new PdfString("SomeNewTooltip"));
    }
    

    【讨论】:

    • 你是救生员!我不得不稍微调整您的解决方案以使用 CFML,但最终它引导我找到了正确的答案。有关工作代码,请参阅我的原始问题。非常感谢!
    猜你喜欢
    • 2021-08-27
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    • 2017-06-11
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 2020-04-22
    相关资源
    最近更新 更多