Page 1 of 1

Add SVG to Document

PostPosted: Wed Jul 01, 2020 4:56 am
by jhuettinger
Hi there,

is it possible to add a SVG image to a word document using docx4j without converting it into a png?

If I insert one via Word 2016, it automatically generates a png image in the document.docx/word/media folder. The document.xml looks like this:

Code: Select all
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:cx="http://schemas.microsoft.com/office/drawing/2014/chartex" xmlns:cx1="http://schemas.microsoft.com/office/drawing/2015/9/8/chartex" xmlns:cx2="http://schemas.microsoft.com/office/drawing/2015/10/21/chartex" xmlns:cx3="http://schemas.microsoft.com/office/drawing/2016/5/9/chartex" xmlns:cx4="http://schemas.microsoft.com/office/drawing/2016/5/10/chartex" xmlns:cx5="http://schemas.microsoft.com/office/drawing/2016/5/11/chartex" xmlns:cx6="http://schemas.microsoft.com/office/drawing/2016/5/12/chartex" xmlns:cx7="http://schemas.microsoft.com/office/drawing/2016/5/13/chartex" xmlns:cx8="http://schemas.microsoft.com/office/drawing/2016/5/14/chartex" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:aink="http://schemas.microsoft.com/office/drawing/2016/ink" xmlns:am3d="http://schemas.microsoft.com/office/drawing/2017/model3d" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:w16cex="http://schemas.microsoft.com/office/word/2018/wordml/cex" xmlns:w16cid="http://schemas.microsoft.com/office/word/2016/wordml/cid" xmlns:w16="http://schemas.microsoft.com/office/word/2018/wordml" xmlns:w16se="http://schemas.microsoft.com/office/word/2015/wordml/symex" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 w16se w16cid w16 w16cex wp14">
   <w:body>
      <w:p w14:paraId="521172BB" w14:textId="78124700" w:rsidR="007438CF" w:rsidRDefault="00AB26D8">
         <w:r>
            <w:rPr>
               <w:noProof/>
            </w:rPr>
            <w:drawing>
               <wp:inline distT="0" distB="0" distL="0" distR="0" wp14:anchorId="703ACC19" wp14:editId="1AB4DA17">
                  <wp:extent cx="1552575" cy="514350"/>
                  <wp:effectExtent l="0" t="0" r="9525" b="0"/>
                  <wp:docPr id="1" name="Grafik 1"/>
                  <wp:cNvGraphicFramePr>
                     <a:graphicFrameLocks xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" noChangeAspect="1"/>
                  </wp:cNvGraphicFramePr>
                  <a:graphic xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main">
                     <a:graphicData uri="http://schemas.openxmlformats.org/drawingml/2006/picture">
                        <pic:pic xmlns:pic="http://schemas.openxmlformats.org/drawingml/2006/picture">
                           <pic:nvPicPr>
                              <pic:cNvPr id="1" name=""/>
                              <pic:cNvPicPr/>
                           </pic:nvPicPr>
                           <pic:blipFill>
                              <a:blip r:embed="rId4">   <----------- REMOVE r:embed="rId4"
                                 <a:extLst>
                                    <a:ext uri="{96DAC541-7B7A-43D3-8B79-37D633B846F1}">
                                       <asvg:svgBlip xmlns:asvg="http://schemas.microsoft.com/office/drawing/2016/SVG/main" r:embed="rId5"/>
                                    </a:ext>
                                 </a:extLst>
                              </a:blip>
                              <a:stretch>
                                 <a:fillRect/>
                              </a:stretch>
                           </pic:blipFill>
                           <pic:spPr>
                              <a:xfrm>
                                 <a:off x="0" y="0"/>
                                 <a:ext cx="1552575" cy="514350"/>
                              </a:xfrm>
                              <a:prstGeom prst="rect">
                                 <a:avLst/>
                              </a:prstGeom>
                           </pic:spPr>
                        </pic:pic>
                     </a:graphicData>
                  </a:graphic>
               </wp:inline>
            </w:drawing>
         </w:r>
      </w:p>
      <w:sectPr w:rsidR="007438CF">
         <w:pgSz w:w="11906" w:h="16838"/>
         <w:pgMar w:top="1417" w:right="1417" w:bottom="1134" w:left="1417" w:header="708" w:footer="708" w:gutter="0"/>
         <w:cols w:space="708"/>
         <w:docGrid w:linePitch="360"/>
      </w:sectPr>
   </w:body>
</w:document>


If I remove
Code: Select all
r:embed="rId4"
and delete the png in document.docx/word/media, Word still renders the SVG correctly and doesn't add the png file again.

So is there a way to do this programmatically with docx4j without needing to convert the SVG?
If not, is it possible to provide the fallback png with batik instead of ImageMagick? How would I do this in this case?

Thank you,
Johannes

Re: Add SVG to Document

PostPosted: Wed Mar 10, 2021 1:14 am
by bono57
Hi everybody,

I have similar problem.
I heard it is possible to export svg in word 2016 but I don't manage to do it. It seems to me docx4j doesn't support it....
Is it an image Part we have to create?
Thank you

Re: Add SVG to Document

PostPosted: Fri Mar 12, 2021 8:45 pm
by jason
The image parts extend BinaryPart, but SVG is XML, so it is desirable that it can be treated as an XML part.

So if we were to have a dedicated SvgPart, which would it extend?

In the absence of that, you can try the following (you'll need to modify it to size your svg appropriately):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
package org.docx4j.samples;

import java.io.File;

import javax.xml.bind.JAXBElement;

import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.contenttype.ContentType;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.DefaultXmlPart;
import org.docx4j.openpackaging.parts.PartName;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.Drawing;

public class ImageAddSVG {

        public static void main(String[] args) throws Exception {
               
                WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.createPackage();
               
                // The image to add
                File file = new File(System.getProperty("user.dir")
                                + "/image1.svg" );
               
                java.io.InputStream is = new java.io.FileInputStream(file );

                DefaultXmlPart imagePart = new DefaultXmlPart(new PartName("/word/media/image1.svg"));
                imagePart.setRelationshipType("http://schemas.openxmlformats.org/officeDocument/2006/relationships/image");
                // <Default ContentType="image/svg+xml" Extension="svg"/>
                imagePart.setContentType(new ContentType("image/svg+xml"));
                imagePart.setDocument(is);
               
               
                Relationship rel = wordMLPackage.getMainDocumentPart().addTargetPart(imagePart);

        org.docx4j.wml.P p = newImage( rel );
                wordMLPackage.getMainDocumentPart().addObject(p);

               
               
                // Now save it
                wordMLPackage.save(new java.io.File(System.getProperty("user.dir") + "/OUT_ImageAddSVG.docx") );
               
        }

        /**
         * Create image, without specifying width
         */

        public static org.docx4j.wml.P newImage(Relationship rel) throws Exception {           
               
                Drawing drawing = createImageInline( rel);
       
        // Now add the inline in w:p/w:r/w:drawing
                org.docx4j.wml.ObjectFactory factory = Context.getWmlObjectFactory();
                org.docx4j.wml.P  p = factory.createP();
                org.docx4j.wml.R  run = factory.createR();             
                p.getContent().add(run);        

        JAXBElement<org.docx4j.wml.Drawing> drawingWrapped = factory.createRDrawing(drawing);
        run.getContent().add( drawingWrapped);        
                               
                return p;
               
        }      
       
        public static Drawing createImageInline(Relationship rel) throws Exception {
               
               
        String ml =
                "<w:drawing " + namespaces + ">"
                        + "<wp:inline distB=\"0\" distL=\"0\" distR=\"0\" distT=\"0\">"
                        + "<wp:extent cx=\"1019175\" cy=\"1581150\"/>"

                        + "<wp:effectExtent b=\"0\" l=\"0\" r=\"9525\" t=\"0\"/>"

                        + "<wp:docPr id=\"2\" name=\"Picture 2\"/>"

                        + "<wp:cNvGraphicFramePr>"
                                + "<a:graphicFrameLocks noChangeAspect=\"true\"/>"

                        + "</wp:cNvGraphicFramePr>"

                        + "<a:graphic>"
                                + "<a:graphicData uri=\"http://schemas.openxmlformats.org/drawingml/2006/picture\">"
                                        + "<pic:pic>"
                                                + "<pic:nvPicPr>"
                                                        + "<pic:cNvPr id=\"1\" name=\"\"/>"

                                                        + "<pic:cNvPicPr/>"

                                                + "</pic:nvPicPr>"

                                                + "<pic:blipFill>"
                                                        + "<a:blip>"
                                                                + "<a:extLst>"
                                                                        + "<a:ext uri=\"{96DAC541-7B7A-43D3-8B79-37D633B846F1}\">"
                                                                                + "<asvg:svgBlip xmlns:asvg=\"http://schemas.microsoft.com/office/drawing/2016/SVG/main\" r:embed=\"" + rel.getId() + "\"/>"
                                                                                                                        + "</a:ext>"
                                                                                                                + "</a:extLst>"
                                                             + "</a:blip>"
                                               

                                                        + "<a:stretch>"
                                                                + "<a:fillRect/>"

                                                        + "</a:stretch>"

                                                + "</pic:blipFill>"

                                                + "<pic:spPr>"
                                                        + "<a:xfrm>"
                                                                + "<a:off x=\"0\" y=\"0\"/>"

                                                                + "<a:ext cx=\"1019175\" cy=\"1581150\"/>"

                                                        + "</a:xfrm>"

                                                        + "<a:prstGeom prst=\"rect\">"
                                                                + "<a:avLst/>"

                                                        + "</a:prstGeom>"

                                                + "</pic:spPr>"

                                        + "</pic:pic>"

                                + "</a:graphicData>"

                        + "</a:graphic>"

                + "</wp:inline>"

        + "</w:drawing>";
       
        Drawing drawing =  (Drawing)org.docx4j.XmlUtils.unmarshalString(ml);
       
                return drawing;        
        }

    final static String namespaces = " xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" "
                + "xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" "
                + "xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" "
                + "xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" "
            + "xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" "
            ;
   
       
}

 
Parsed in 0.029 seconds, using GeSHi 1.0.8.4

Re: Add SVG to Document

PostPosted: Fri Mar 19, 2021 1:35 am
by bono57
Thank you! It works fine to generate docx!
However, the generated docx can't be converted to pdf with docx4j.toPDF function...
Do you know this problem?