aspose-words / Aspose.Words-for-Java

Aspose.Words for Java examples, plugins and showcases
https://products.aspose.com/words/java
MIT License
401 stars 206 forks source link

Aspose.word function is missing #86

Closed SnowflyW closed 2 years ago

SnowflyW commented 2 years ago

Aspose.word function is missing, and the picture set cannot output multiple pictures in the same table cell

SnowflyW commented 2 years ago

Suppose that there are two data sets, and each data corresponds to multiple pictures, the picture corresponding to the data cannot be achieved

AlexNosk commented 2 years ago

@SnowflyW Unfortunately, your request is not quite clear. Could you please elaborate your requirements in more details and attach your input document, sample data and the expected output? We will investigate and provide you more information.

SnowflyW commented 2 years ago

@AlexNosk Hello, alexnosk My problem is that in the merge area, the data record is a list, and each data also contains multiple pictures. As a result, the pictures of each row of data are the same and repeated

image image

SnowflyW commented 2 years ago

@AlexNosk Supplement:

The data source of non picture fields in the merge area is bound through datatable, and the picture circulates the following code in the following way:

builder. moveToMergeField(fieldName, false, true);
builder. insertImage((BufferedImage) bufferedImage);

Then through the code, the field name of the dynamic control field (imglist1....imglist (n)), but the picture from 1... N lines is still repeated.

The first data record is a picture The second data record is two pictures

AlexNosk commented 2 years ago

@SnowflyW I think in your case you can use IFieldMergingCallback to achieve that you need. For example here is a simple template example: image And here is a code:

Document doc = new Document("C:\\Temp\\in.docx");
doc.getMailMerge().setFieldMergingCallback(new MyFieldMergingCallback());
doc.getMailMerge().executeWithRegions(dt);
doc.save("C:\\Temp\\out.docx");
private static class MyFieldMergingCallback implements IFieldMergingCallback
{
    @Override
    public void fieldMerging(FieldMergingArgs args) throws Exception {

        if(args.getFieldName().equals("ImgList"))
        {
            DocumentBuilder builder = new DocumentBuilder(args.getDocument());
            builder.moveToField(args.getField(), true);
            // And here insert images you need to insert.
            builder.insertImage("C:\\Temp\\" + args.getFieldValue());
            // If required you can insert more images here
            // .....
            // Since we already inserted data, set field value to empty string.
            args.setFieldValue("");
        }
    }

    @Override
    public void imageFieldMerging(ImageFieldMergingArgs args) throws Exception {
        // Do nothing.
        // however, you can use this method to insert image dynamically
        // if field name in the template is in format "Image:FieldName".
    }
}

The result of executing is the following: image

For your convenience here is input and output documents: in.docx out.docx

SnowflyW commented 2 years ago

@AlexNosk Thanks, brother My English is not good. I use translation software I also tried the image:mergefield method later, and it does not support outputting multiple pictures. It will only overwrite the previous pictures on the original field In the callback event, loop builder.insert to try

SnowflyW commented 2 years ago

@AlexNosk I refer to the official documents and cases, so I always use the imagefieldmerging method in the ifieldmergegcallback interface, and replace it with fieldmerging method to achieve the effect I want. Thank you again, brother

The fieldmerging method corresponds to the merge field in the merge area of the word template. The imagefieldmerging method corresponds to the < < image:xxxx > > field in the word template.

When using fieldmerging method, in special scenarios, you need to specify the field type of the next datatable column.

AlexNosk commented 2 years ago

@SnowflyW It looks like you managed to achieve what you need using IFieldMergingCallback. So I am closing the issue. Please feel free to open another one in case of any issues or post your question in our support forum https://forum.aspose.com/c/words/8

SnowflyW commented 2 years ago

@AlexNosk OK 3Q