We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replace FormFields by a image

Hello,

I use Aspose.Words v2.4.2 under Java

I search a solution (through the method below) to replace in a document several FormFields that have the same “result” property by the same image but each time i obtain a NullPointerException or the image do not appear…

Code of the method :

// Search for the FormField occurences of the current bookmark in
// the document FormFields list
List formFieldPositionIndex = new ArrayList();
FormFields formFields = document.getRange().getFormFields();
for (int i = 0; i < formFields.getCount(); i++)
{
    if (!StringUtils.isEmpty(formFields.get(i).getResult())
    && formFields.get(i).getResult().equals(bookmarkReplacementSetting.getName()))
    {
        formFieldPositionIndex.add(i);
    }
}

// Get image file and check existence
File imgFile = new File(ServiceUtils.convertPath(bookmarkReplacementSetting.getValue(),
businessRequestHeader.getJuridictionCode(), businessRequestHeader.getSourceFileRootDirectory()));
if (!imgFile.exists())
{
    throw new PWEException("Image file '" + imgFile.getAbsolutePath() + "’ cannot be found !");
}

// Process all occurrence of the FormField for the current
// bookmark
for (Integer position : formFieldPositionIndex)
{
    // Delete the bookmark if exists
    if (document.getRange().getBookmarks().get(bookmarkReplacementSetting.getName()) != null)
    {
        document.getRange().getBookmarks().get(bookmarkReplacementSetting.getName()).remove();
    }
    // Get current FormField occurence
    FormField formField = formFields.get(position);
    // Move to FormField position
    documentBuilder.moveTo(formField);
    // Move cursor to the end position of the field
    while (documentBuilder.getCurrentNode().getNodeType() != NodeType.FIELD_END)
    {
        documentBuilder.moveTo(documentBuilder.getCurrentNode().getNextSibling());
    }
    if (documentBuilder.getCurrentNode() != null
    && documentBuilder.getCurrentNode().getNextSibling() != null)
    {
        documentBuilder.moveTo(documentBuilder.getCurrentNode().getNextSibling());
    }
    // Delete FormField
    formFields.remove(formField);
    // Insert image and set properties
    ImageIcon imgIcon = new ImageIcon(imgFile.getAbsolutePath());
    Shape shape = documentBuilder.insertImage(imgFile.getAbsolutePath());
    shape.setBehindText(false);
    shape.setWidth(imgIcon.getIconWidth());
    shape.setHeight(imgIcon.getIconHeight());
}

I have also another question : It’s possible with Aspose.Words to disable the FormField Shading property ?

I have put in attachment the input Word document

Thanks in advance for your help…

Dominique

Hi

Thanks for your inquiry.

  1. Please try using the following code:
// Open the document and create DocumentBuilder
Document doc = new Document("C:\\Temp\\sampleP.dot");
DocumentBuilder builder = new DocumentBuilder(doc);
// Let's replace all formfield with value "FAX_MAIL" with iamge
ArrayList fields = new ArrayList();
// Loop through all formfields
for (int ffIndex = 0; ffIndex < doc.getRange().getFormFields().getCount(); ffIndex++)
{
    FormField field = doc.getRange().getFormFields().get(ffIndex);
    // Check whether value of formfield is "FAX_MAIL"
    if (field.getResult().equals("FAX_MAIL"))
    {
        fields.add(field);
    }
}
// Loop through all matched fields
for (int i = 0; i < fields.size(); i++)
{
    FormField field = (FormField)fields.get(i);
    // Search for field start
    Node currentNode = field;
    while (currentNode.getNodeType() != NodeType.FIELD_START)
    {
        currentNode = currentNode.getPreviousSibling();
    }
    // move cursor to fieldStrar
    builder.moveTo(currentNode);
    // Insert image
    builder.insertImage("C:\\Temp\\test.jpg");
    // Now we should remove field
    while (currentNode.getNodeType() != NodeType.FIELD_END)
    {
        currentNode = currentNode.getNextSibling();
        currentNode.getPreviousSibling().remove();
    }
    currentNode.remove();
}
// Save output doc
doc.save("C:\\Temp\\out.doc");

Hope this helps.

  1. No there is no way to disable the FormField Shading. This option is not stored in the document.

Best regards.

Hi,

All is OK !!!

Thanks another time ;o)))

Dominique