Aspose.words for java convert docx to pdf

Hello, I have a problem. In the zip file below, there is a doc file (eg: new .docx) containing an axial gradient pattern. When it is converted to pdf, the pattern becomes a path description (eg: new-path.pdf), Does your company have a way to convert the axial gradient pattern into a picture in pdf (eg: new-image.pdf)?
Thanks.
My file:zip file

@serendipity.zhq Unfortunately, there is no built-in way to achieve this. However, you can render the shape to image and replace the original shape. The simplified code can look like this:

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
        
Shape original = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
ByteArrayOutputStream os = new ByteArrayOutputStream();
original.getShapeRenderer().save(os, new ImageSaveOptions(SaveFormat.PNG));

// It might be required to set additional shape options        
builder.moveTo(original);
builder.insertImage(os.toByteArray());
original.remove();
        
doc.updatePageLayout();
        
doc.save("C:\\Temp\\out.pdf");

Thanks for the reply, I tested the above code and found that the generated words are jagged and not suitable for use.

@serendipity.zhq You can increase the image resolution and enable anti aliassing in ImageSaveOptions to make the image quality better. The provided code was simplified for demonstration purposes.

ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.PNG);
opt.setResolution(300);
opt.setUseAntiAliasing(true);
original.getShapeRenderer().save(os, opt);


The screenshot is a document generated after using the above two codes, and the jaggedness of the words is too obvious.

@serendipity.zhq On my side the output looks much better then yours: out.pdf (26.0 KB)

Here is my code:

Document doc = new Document("C:\\Temp\\in.docx");
DocumentBuilder builder = new DocumentBuilder(doc);

Shape original = (Shape)doc.getChild(NodeType.SHAPE, 0, true);
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageSaveOptions opt = new ImageSaveOptions(SaveFormat.PNG);
opt.setResolution(300);
opt.setUseAntiAliasing(true);
original.getShapeRenderer().save(os, opt);

// It might be required to set additional shape options
builder.moveTo(original);
builder.insertImage(os.toByteArray());
original.remove();

doc.updatePageLayout();

doc.save("C:\\Temp\\out.pdf");

Thanks.Now the picture is displayed normally.

1 Like