We have a 700px by 250px SVG image that we are inserting into a Word document. The size of the inserted image is different between Mac and Windows versions of Word. We are using the latest version of Aspose Words for Java (18.4). The Mac version displays correctly when inserted using pixels, and the Windows version displays correctly when inserted using points. Class DocumentBuilder.insertImage API documentation says the units is in points. The code is below.
public static final String THE_IMAGE = "<svg xmlns:xlink=\"http://www.w3.org/1999/xlink\" version=\"1.1\" style=\"font-family:arial;font-size:12px;\" xmlns=\"http://www.w3.org/2000/svg\" width=\"700\" height=\"250\"><desc>Created with Highcharts 4.2.6</desc><defs><clipPath id=\"highcharts-166\"><rect x=\"0\" y=\"0\" width=\"608\" height=\"163\"></rect></clipPath></defs><rect x=\"0\" y=\"0\" width=\"700\" height=\"250\" fill=\"rgb(255,255,255)\" fill-opacity=\"0\" class=\" highcharts-background\"></rect><g class=\"highcharts-grid\"></g><g class=\"highcharts-grid\"><path fill=\"none\" d=\"M 82 202.5 L 690 202.5\" stroke=\"#D8D8D8\" stroke-width=\"1\" opacity=\"1\"></path><path fill=\"none\" d=\"M 82 148.5 L 690 148.5\" stroke=\"#D8D8D8\" stroke-width=\"1\" opacity=\"1\"></path><path fill=\"none\" d=\"M 82 93.5 L 690 93.5\" stroke=\"#D8D8D8\" stroke-width=\"1\" opacity=\"1\"></path><path fill=\"none\" d=\"M 82 38.5 L 690 38.5\" stroke=\"#D8D8D8\" stroke-width=\"1\" opacity=\"1\"></path></g><g class=\"highcharts-axis\"></g><g class=\"highcharts-axis\"></g><g class=\"highcharts-series-group\"><g class=\"highcharts-series highcharts-series-0\" transform=\"translate(82,39) scale(1 1)\" clip-path=\"url(#highcharts-166)\"><rect x=\"28\" y=\"137\" width=\"30\" height=\"27\" fill=\"#12699E\" rx=\"0\" ry=\"0\"></rect><rect x=\"150\" y=\"87\" width=\"30\" height=\"77\" fill=\"#12699E\" rx=\"0\" ry=\"0\"></rect><rect x=\"271\" y=\"54\" width=\"30\" height=\"110\" fill=\"#12699E\" rx=\"0\" ry=\"0\"></rect><rect x=\"393\" y=\"54\" width=\"30\" height=\"110\" fill=\"#12699E\" rx=\"0\" ry=\"0\"></rect><rect x=\"514\" y=\"54\" width=\"30\" height=\"110\" fill=\"#12699E\" rx=\"0\" ry=\"0\"></rect></g><g class=\"highcharts-markers highcharts-series-0\" transform=\"translate(82,39) scale(1 1)\" clip-path=\"none\"></g><g class=\"highcharts-series highcharts-series-1\" transform=\"translate(82,39) scale(1 1)\" clip-path=\"url(#highcharts-166)\"><rect x=\"64\" y=\"130\" width=\"30\" height=\"34\" fill=\"#00BEEA\" rx=\"0\" ry=\"0\"></rect><rect x=\"186\" y=\"70\" width=\"30\" height=\"94\" fill=\"#00BEEA\" rx=\"0\" ry=\"0\"></rect><rect x=\"308\" y=\"29\" width=\"30\" height=\"135\" fill=\"#00BEEA\" rx=\"0\" ry=\"0\"></rect><rect x=\"429\" y=\"29\" width=\"30\" height=\"135\" fill=\"#00BEEA\" rx=\"0\" ry=\"0\"></rect><rect x=\"551\" y=\"29\" width=\"30\" height=\"135\" fill=\"#00BEEA\" rx=\"0\" ry=\"0\"></rect></g><g class=\"highcharts-markers highcharts-series-1\" transform=\"translate(82,39) scale(1 1)\" clip-path=\"none\"></g></g><text x=\"345\" text-anchor=\"middle\" class=\"highcharts-title\" style=\"color:#666;font-size:12px;font-weight:bold;fill:#666;width:646px;\" y=\"19\"><tspan>Realized Measurable Cash Flow Impact Starting Year 1</tspan></text><g class=\"highcharts-legend\" transform=\"translate(251,225)\"><g><g><g class=\"highcharts-legend-item\" transform=\"translate(8,3)\"><text x=\"21\" style=\"color:#333333;font-size:12px;font-weight:bold;cursor:pointer;fill:#333333;\" text-anchor=\"start\" y=\"15\">Conservative</text><rect x=\"0\" y=\"4\" width=\"16\" height=\"12\" fill=\"#12699E\"></rect></g><g class=\"highcharts-legend-item\" transform=\"translate(124.6875,3)\"><text x=\"21\" y=\"15\" style=\"color:#333333;font-size:12px;font-weight:bold;cursor:pointer;fill:#333333;\" text-anchor=\"start\">Likely</text><rect x=\"0\" y=\"4\" width=\"16\" height=\"12\" fill=\"#00BEEA\"></rect></g></g></g></g><g class=\"highcharts-axis-labels highcharts-xaxis-labels\"><text x=\"142.8\" style=\"color:#666;cursor:default;font-size:11px;fill:#666;width:112px;text-overflow:clip;\" text-anchor=\"middle\" transform=\"translate(0,0)\" y=\"212\" opacity=\"1\"><tspan>Year 1</tspan></text><text x=\"264.4\" style=\"color:#666;cursor:default;font-size:11px;fill:#666;width:112px;text-overflow:clip;\" text-anchor=\"middle\" transform=\"translate(0,0)\" y=\"212\" opacity=\"1\"><tspan>Year 2</tspan></text><text x=\"386\" style=\"color:#666;cursor:default;font-size:11px;fill:#666;width:112px;text-overflow:clip;\" text-anchor=\"middle\" transform=\"translate(0,0)\" y=\"212\" opacity=\"1\"><tspan>Year 3</tspan></text><text x=\"507.59999999999997\" style=\"color:#666;cursor:default;font-size:11px;fill:#666;width:112px;text-overflow:clip;\" text-anchor=\"middle\" transform=\"translate(0,0)\" y=\"212\" opacity=\"1\"><tspan>Year 4</tspan></text><text x=\"629.1999999999999\" style=\"color:#666;cursor:default;font-size:11px;fill:#666;width:112px;text-overflow:clip;\" text-anchor=\"middle\" transform=\"translate(0,0)\" y=\"212\" opacity=\"1\"><tspan>Year 5</tspan></text></g><g class=\"highcharts-axis-labels highcharts-yaxis-labels\"><text x=\"67\" style=\"color:#666;cursor:default;font-size:11px;fill:#666;width:221px;text-overflow:clip;\" text-anchor=\"end\" transform=\"translate(0,0)\" y=\"207\" opacity=\"1\"><tspan>$0</tspan></text><text x=\"67\" style=\"color:#666;cursor:default;font-size:11px;fill:#666;width:221px;text-overflow:clip;\" text-anchor=\"end\" transform=\"translate(0,0)\" y=\"153\" opacity=\"1\"><tspan>$200,000,000</tspan></text><text x=\"67\" style=\"color:#666;cursor:default;font-size:11px;fill:#666;width:221px;text-overflow:clip;\" text-anchor=\"end\" transform=\"translate(0,0)\" y=\"98\" opacity=\"1\"><tspan>$400,000,000</tspan></text><text x=\"67\" style=\"color:#666;cursor:default;font-size:11px;fill:#666;width:221px;text-overflow:clip;\" text-anchor=\"end\" transform=\"translate(0,0)\" y=\"44\" opacity=\"1\"><tspan>$600,000,000</tspan></text></g><g class=\"highcharts-tooltip\" style=\"cursor:default;padding:0;pointer-events:none;white-space:nowrap;\" transform=\"translate(0,-9000000000)\"><path fill=\"none\" d=\"M 3.5 0.5 L 13.5 0.5 C 16.5 0.5 16.5 0.5 16.5 3.5 L 16.5 13.5 C 16.5 16.5 16.5 16.5 13.5 16.5 L 3.5 16.5 C 0.5 16.5 0.5 16.5 0.5 13.5 L 0.5 3.5 C 0.5 0.5 0.5 0.5 3.5 0.5\" stroke=\"black\" stroke-opacity=\"0.049999999999999996\" stroke-width=\"5\" transform=\"translate(1, 1)\"></path><path fill=\"none\" d=\"M 3.5 0.5 L 13.5 0.5 C 16.5 0.5 16.5 0.5 16.5 3.5 L 16.5 13.5 C 16.5 16.5 16.5 16.5 13.5 16.5 L 3.5 16.5 C 0.5 16.5 0.5 16.5 0.5 13.5 L 0.5 3.5 C 0.5 0.5 0.5 0.5 3.5 0.5\" stroke=\"black\" stroke-opacity=\"0.09999999999999999\" stroke-width=\"3\" transform=\"translate(1, 1)\"></path><path fill=\"none\" d=\"M 3.5 0.5 L 13.5 0.5 C 16.5 0.5 16.5 0.5 16.5 3.5 L 16.5 13.5 C 16.5 16.5 16.5 16.5 13.5 16.5 L 3.5 16.5 C 0.5 16.5 0.5 16.5 0.5 13.5 L 0.5 3.5 C 0.5 0.5 0.5 0.5 3.5 0.5\" stroke=\"black\" stroke-opacity=\"0.15\" stroke-width=\"1\" transform=\"translate(1, 1)\"></path><path fill=\"rgb(249, 249, 249)\" fill-opacity=\" .85\" d=\"M 3.5 0.5 L 13.5 0.5 C 16.5 0.5 16.5 0.5 16.5 3.5 L 16.5 13.5 C 16.5 16.5 16.5 16.5 13.5 16.5 L 3.5 16.5 C 0.5 16.5 0.5 16.5 0.5 13.5 L 0.5 3.5 C 0.5 0.5 0.5 0.5 3.5 0.5\"></path><text x=\"8\" style=\"font-size:12px;color:#333333;fill:#333333;\" y=\"20\"></text></g></svg>";
public static void main(String[] args) {
System.out.println("Start.");
try {
double shapeWidthPixels = 700;
double shapeHeightPixels = 250;
double shapeWidthPoints = com.aspose.words.ConvertUtil.pixelToPoint(shapeWidthPixels);
double shapeHeightPoints = com.aspose.words.ConvertUtil.pixelToPoint(shapeHeightPixels);
com.aspose.words.Document document = new com.aspose.words.Document();
com.aspose.words.DocumentBuilder builder = new com.aspose.words.DocumentBuilder(document);
builder.insertImage(THE_IMAGE.getBytes(), shapeWidthPoints, shapeHeightPoints); // Renders correct size on PC and too small on Mac.
builder.insertImage(THE_IMAGE.getBytes(), shapeWidthPixels, shapeHeightPixels); // Renders too big on PC and correctly on Mac.
builder.insertImage(THE_IMAGE.getBytes()); // Renders too small on PC and very small on Mac.
document.save("c:\\users\\myuser\\desktop\\out.docx", SaveFormat.DOCX);
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Done.");
}
How can we get the image to be the correct size across both platforms? Thank you.