How to export inserted SVG as SVG in media folder using Java

If I set the transparency manually in MS Word, it creates XML like this:

<v:shape id="TextBox 100048" … fillcolor="yellow" …>
  <v:fill opacity="0.75" />
    <v:textbox inset="1pt,1pt,1pt,1pt">
    ...

Aspose lib doesn’t create a <v:fill> element with the opacity attribute regardless which opacity has been assigned.
Maybe there is another option to configure or was this attribute not yet available in Word 2006?
It looks like currently Aspose.Word simply writes fillcolor="none" if opacity == 0 and ignores any other opacity value.

The second problem of Aspose is the missing support of opacity between 0 and 1:

  • do aspose already support the features or
  • is there a roadmap when they will be supported

Expected code:

Shape shape = ...
shape.getFill().setOpacity(0.5);

I discovered that the SVGs are embedded as EMF files into the Word document. I approved that Word itself supports embedding SVGs but maybe this is a feature of a younger version than 2006,
or what can I overide to change implementation for support svg elements like there:

I need answers to my questions before purchasing a license

@aolo23

To ensure a timely and accurate response, please attach the following resources here for testing:

  • Your input Word document.
  • Please attach the output Word file that shows the undesired behavior.
  • Please attach the expected output Word file that shows the desired behavior.
  • Please create a simple Java application ( source code without compilation errors ) that helps us to reproduce your problem on our end and attach it here for testing.

As soon as you get these pieces of information ready, we will start investigation into your issue and provide you more information. Thanks for your cooperation.

PS: To attach these resources, please zip and upload them.

Java Code:

import com.aspose.words.*;
import com.aspose.words.Font;
import com.aspose.words.Shape;

import java.awt.*;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class AsposeBugExample {

    public static void main(String[] args) {

        try{
            // Create empty docx
            com.aspose.words.Document doc = new Document();
            doc.removeAllChildren();
            Section section = new Section(doc);
            doc.appendChild(section);
            DocumentBuilder documentBuilder = new DocumentBuilder(doc);


            //Page setup
            PageSetup ps = documentBuilder.getPageSetup();
            ps.setPageWidth(595.275634765625);
            ps.setPageHeight(841.8897705078125);
            ps.setOrientation(Orientation.PORTRAIT);
            ps.setTopMargin(17.00787353515625);
            ps.setBottomMargin(28.346435546875);
            ps.setLeftMargin(17.00787353515625);
            ps.setRightMargin(28.34649658203125);

            // Create shape
            Shape shape = documentBuilder.insertShape(ShapeType.TEXT_BOX, 91.1971853661725, 17.253521446167955);
            shape.setWrapType(WrapType.NONE);
            shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
            shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

            shape.setFillColor(new Color(255,255,255));
            shape.getFill().setOpacity(0.6); // <-->  Main problem area
            shape.setStrokeColor(new Color(128,128,128));

            TextBox textBox = shape.getTextBox();
            textBox.setInternalMarginRight(2.0);
            textBox.setInternalMarginLeft(2.0);
            textBox.setInternalMarginTop(2.0);
            textBox.setInternalMarginBottom(2.0);

            shape.setStroked(false);
            shape.setTop(-2.7033287709153546E-6);
            shape.setTop(85.03934304545245);

            // append styled title
            Run run = new Run(documentBuilder.getDocument(), "X: 30 mm");
            Font font = run.getFont();
            font.setName("Dialog");
            font.setSize(12);
            font.setBold(true);
            font.setItalic(false);
            font.setColor(new Color(237,45,45));

            Paragraph paragraph = shape.getLastParagraph();
            paragraph.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
            paragraph.appendChild(run);

            // save docx file
            DateFormat dateFormat = new SimpleDateFormat("_HH_mm_ss");
            Date currentDate = new Date();
            Calendar c = Calendar.getInstance();
            c.setTime(currentDate);
            final String outPath = System.getProperty("user.dir") + File.separator + "aspose_word" + dateFormat.format(currentDate) + ".docx";
            documentBuilder.getDocument().save(outPath);
        }
        catch (Exception ex){
            ex.printStackTrace();
        }
    }
}

After run code You will get result document.
In zip You can see expected file: expected_word_opacity_60%.docx.

I would still ask for info on adding SVG photos to MSWord WITHOUT converting to EMF file.
There is some road map that shows future support that would implement this use case (I mean RGBA support for SVG photos, EMF doesn’t support RGBA) ?

Expected document steps: Click on box --> TextBox --> Advanced Tools --> Colors and Lines —> Transparency set 60% --> OK

[image]

reproduce files.zip (22.5 KB)

@aolo23

We suggest you please create Shape node as shown below instead of calling DocumentBuilder.insertShape method. Hope this helps you.

Shape shape =  new Shape(doc, ShapeType.TEXT_BOX);
shape.setWidth(91.1971853661725);
shape.setHeight(17.253521446167955);
shape.setWrapType(WrapType.NONE);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

documentBuilder.insertNode(shape); 

Unfortunately, this requirement is not clear to us. Could you please share some more detail about this requirement? We will then provide you more information on it.

@aolo23

We have noticed that Fill.Opacity is not set when Shape is inserted by InsertShape method. We have logged this issue as WORDSNET-21097 in our issue tracking system. You will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

@tahir.manzoor

I would like to ask you to test on the previously sent code if it works correctly. Because it doesn’t work. (null pointer)

java.lang.NullPointerException
	at AsposeBugExample.main(AsposeBugExample.java:68)

because shape.getLastParagraph() is null

tmp solution:

            Shape shape =  new Shape(doc, ShapeType.TEXT_BOX);
            shape.setWidth(91.1971853661725);
            shape.setHeight(17.253521446167955);
            shape.setWrapType(WrapType.NONE);
            shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
            shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

            shape.appendChild(new Paragraph(documentBuilder.getDocument()));
            documentBuilder.insertNode(shape);

The manual solution attempt works for the example but not for the example I want.
After changing the method of creating a textbox, both the photos and other elements are minimized (after changing these 2 lines of code)
See zip files.

diffrent_results.zip (56.0 KB)

Unfortunately, this requirement is not clear to us. Could you please share some more detail about this .requirement? We will then provide you more information on it.

Every time when I adds an SVG image to a word document in this way:
Shape imageShape = documentBuilder.insertImage(writer.toString().getBytes(), objWidth, objHeight);
it is converted to EMF format. ( I check this by unpacking docx, and see word/media folder)
However, when using Ms Word 2016, when I manually add an image in svg format, it becomes SVG.

@aolo23

Please use the following working code example. Hope this helps you.

com.aspose.words.Document doc = new Document();
doc.removeAllChildren();
Section section = new Section(doc);
doc.appendChild(section);
DocumentBuilder documentBuilder = new DocumentBuilder(doc);


//Page setup
PageSetup ps = documentBuilder.getPageSetup();
ps.setPageWidth(595.275634765625);
ps.setPageHeight(841.8897705078125);
ps.setOrientation(Orientation.PORTRAIT);
ps.setTopMargin(17.00787353515625);
ps.setBottomMargin(28.346435546875);
ps.setLeftMargin(17.00787353515625);
ps.setRightMargin(28.34649658203125);

// Create shape
Shape shape =  new Shape(doc, ShapeType.TEXT_BOX);//documentBuilder.insertShape(ShapeType.TEXT_BOX, 91.1971853661725, 17.253521446167955);
shape.setWidth(91.1971853661725);
shape.setHeight(17.253521446167955);
shape.setWrapType(WrapType.NONE);
shape.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
shape.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);

shape.setFillColor(new Color(255,255,255));
shape.getFill().setOpacity(0.6); // <-->  Main problem area
shape.setStrokeColor(new Color(128,128,128));

TextBox textBox = shape.getTextBox();
textBox.setInternalMarginRight(2.0);
textBox.setInternalMarginLeft(2.0);
textBox.setInternalMarginTop(2.0);
textBox.setInternalMarginBottom(2.0);

shape.setStroked(false);
shape.setTop(-2.7033287709153546E-6);
shape.setTop(85.03934304545245);

// append styled title
Run run = new Run(documentBuilder.getDocument(), "X: 30 mm");
Font font = run.getFont();
font.setName("Dialog");
font.setSize(12);
font.setBold(true);
font.setItalic(false);
font.setColor(new Color(237,45,45));

shape.appendChild(new Paragraph(doc));

Paragraph paragraph = shape.getLastParagraph();
paragraph.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
paragraph.appendChild(run);

// save docx file
DateFormat dateFormat = new SimpleDateFormat("_HH_mm_ss");
Date currentDate = new Date();
Calendar c = Calendar.getInstance();
c.setTime(currentDate);

documentBuilder.insertNode(shape);

documentBuilder.getDocument().save(MyDir + "20.9.docx");

Please note that when SVG is inserted into the document, it is converted to EMF meta file (that is vector image).

Please share the steps that you are using to insert the SVG into document. Please also share the SVG and expected output document. We will then answer your query according to your requirement.

1 Like

Example svg image:

<svg viewBox="0 0 400 100" xmlns="http://www.w3.org/2000/svg">
    <circle cx="50" cy="50" r="40" />
    <circle cx="150" cy="50" r="40" fill="rgba(0,0,0,0.7)" />
    <circle cx="250" cy="50" r="40" fill="rgba(0,0,0,0.5)" />
    <circle cx="350" cy="50" r="40" style="fill:rgba(0,0,0,0.25)" />
</svg>

I realizes that it doesn’t render correctly by MsWord, maybe there is some way to force MsWord to support rgba colors?

Steps to get saved svg image in word (version 16):
New blank document --> Insert --> Pictures --> and just choose the photo I gave above.
After it when I unpack *.docx file in media folder I have *.png, and *.svg version of the same image.
Using aspose lib I have only one emf file.

@aolo23

When SVG is inserted into the document, it is converted to EMF meta file (that is vector image). We have logged new feature as WORDSNET-21102 in our issue tracking system to export SVG in media folder. You will be notified via this forum thread once there is an update available on this feature.

We apologize for your inconvenience.

1 Like

The issues you have found earlier (filed as WORDSNET-21097) have been fixed in this Aspose.Words for .NET 20.10 update and this Aspose.Words for Java 20.10 update.

The issues you have found earlier have been fixed in this Aspose.Words for .NET 20.12 update and this Aspose.Words for Java 20.12 update.

1 Like