Mailmerge SVG field from database

If anyone can help me with the following problem, I’ll be very grateful.

I’m using Aspose.Words successfully to merge several fields & images from a database into a word document. Using the HTML5 canvas, I’m now also storing SVG images into the database as a text-field. Can anyone show me how to insert the SVG - text as an image during a mail-merge?

I’m using the IFieldMergingCallback interface to handle image fields. The following routine automatically rescales the image if necessary to fit in the table cell in the word document. I’m not sure how to handle SVG files as I have them in string format in the database, and I need a stream to create an image object.

I’m using Aspose.Words version 11.5.0.0

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e)
{

    var builder = new DocumentBuilder(e.Document);
    var image = Image.FromStream(new MemoryStream((byte[]) e.FieldValue));
    var ratio = (double) image.Width/image.Height;

    // Move builder to merge field (merge field is automatically removed).
    if (builder.MoveToMergeField(e.FieldName)) {
    // No image resize by default.
    // (Setting size to negative values makes image to be inserted without resizing.)
    double width = -1;

    // Check if the image is inserted into table cell.
    var cell = (Aspose.Words.Tables.Cell)builder.CurrentParagraph.GetAncestor(typeof(Aspose.Words.Tables.Cell));

    if (cell != null) {
        // Set the cell properties so that the inserted image could occupy the cell space exactly.

        cell.CellFormat.LeftPadding = 0;
        cell.CellFormat.RightPadding = 0;
        cell.CellFormat.TopPadding = 0;
        cell.CellFormat.BottomPadding = 0;
        cell.CellFormat.WrapText = false;
        cell.CellFormat.FitText = true;

        // Get cell dimensions.
        width = cell.CellFormat.Width;

    }

    // Check if the image is inserted into a textbox.
    var shape = (Shape)builder.CurrentParagraph.GetAncestor(typeof(Shape));

    if ((shape != null) && (shape.ShapeType == ShapeType.TextBox)) {
        // Set the textbox properties so that the inserted image could occupy the textbox space exactly.
        shape.TextBox.InternalMarginTop = 0;
        shape.TextBox.InternalMarginLeft = 0;
        shape.TextBox.InternalMarginBottom = 0;
        shape.TextBox.InternalMarginRight = 0;

        // Get cell dimensions.
        width = shape.Width;

        }

        var height = width/ratio;

        // Insert image with or without rescaling, based on the previously done analysis.
        builder.InsertImage((byte[])e.FieldValue, width, height);
    }
}

Hi there,

Thanks for your inquiry. We are working over your query and will get back to you asap.

Many thanks in advance!

the idea is off course to keep the vector format when saving the merged word-document to a pdf file.

Hi there,

Thanks for your patience. In your case, I suggest you please read the SVG text from database and convert it to byte array. Once you have byte array, you can insert SVG into document. Please check the following code snippet for your kind reference.

DocumentBuilder.InsertImage Method (Byte[]) inserts an image from a byte array into the document. The image is inserted inline and at 100% scale.

DocumentBuilder builder = new DocumentBuilder();
byte[] data = Encoding.ASCII.GetBytes(File.ReadAllText(MyDir + "in.svg"));
builder.InsertImage(data);
builder.Document.Save(MyDir + "Out.docx");

After testing, this doesn’t seem to work. Could this be because of the older version of Aspose.Words?

I’m assigning the byte array to a mergefield:

fieldValue = Encoding.ASCII.GetBytes("…");

And then intercept the IFieldMergingCallback as proposed:

void IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e){
    var builder = new DocumentBuilder(e.Document);
    builder.InsertImage((byte[])e.FieldValue);
}

But when saving the document, I get the following error:

System.ArgumentException was caught
HResult=-2147024809
Message=Parameter is not valid.
Source=System.Drawing
StackTrace:
        at System.Drawing.Bitmap..ctor(Stream stream)
        at xf9a9481c3f63a419.x3cd5d648729cd9b6..ctor(Byte[] imageBytes)
        at x011d489fb9df7027.x8e500baeffc6e539.x601e9a2243ca6720(Byte[] x43e181e083db6cdf)
        at x011d489fb9df7027.x8e500baeffc6e539.set_xcc18177a965e0313(Byte[] xbcea506a33cf9111)
        at Aspose.Words.Drawing.ImageData.set_ImageBytes(Byte[] value)
        at Aspose.Words.DocumentBuilder.InsertImage(Byte[] imageBytes, RelativeHorizontalPosition horzPos, Double left, RelativeVerticalPosition vertPos, Double top, Double width, Double height, WrapType wrapType)
        at Aspose.Words.DocumentBuilder.InsertImage(Byte[] imageBytes, Double width, Double height)
        at Aspose.Words.DocumentBuilder.InsertImage(Byte[] imageBytes)
        at VetConnect.ApplicationServices.Services.Templates.HandleMergeImageFieldFromBlob.Aspose.Words.Reporting.IFieldMergingCallback.ImageFieldMerging(ImageFieldMergingArgs e) in c:\Projects\2010.vetworks.vetconnect.program\app\VetConnect.ApplicationServices\Services\Templates\TemplateFactory.cs:line 1030
        at Aspose.Words.Reporting.MailMerge.x8ac49bb85e2a468f(ImageFieldMergingArgs xfbf34718e704c6bc)
        at xe86f37adaccef1c3.xc5c3f438428cb03b.xbab965d1b5a8be25(x561fa53c007d3597 xe01ae93d9fe5a880, ImageFieldMergingArgs& xfbf34718e704c6bc)
        at xfbd1009a0cbb9842.x561fa53c007d3597.xdd44da4fbab52b81(xc5c3f438428cb03b xa4d52e34b62b5495)
        at xfbd1009a0cbb9842.x561fa53c007d3597.x83bcdf1790545fdb()
        at Aspose.Words.Fields.Field.x42a25ae95099edb8(x5e36356bc92c609b x0f7b23d1c393aed9)
        at Aspose.Words.Fields.Field.x295cb4a1df7a5add(x5e36356bc92c609b x0f7b23d1c393aed9)
        at xfbd1009a0cbb9842.xfedf115fd9c03862.x4e3cfc222c92cda7(Field xe01ae93d9fe5a880, x5e36356bc92c609b x0f7b23d1c393aed9)
        at xfbd1009a0cbb9842.xfedf115fd9c03862.x4e3cfc222c92cda7(Field xe01ae93d9fe5a880)
        at xfbd1009a0cbb9842.xbf9ddf72e1283af9.x18dfca7c5fd2402f()
        at xfbd1009a0cbb9842.xfedf115fd9c03862.xdd6cf0348a23f220(xcf417e2db4fe9ed3 xe00c282e1a49fcfb)
        at xfbd1009a0cbb9842.xfedf115fd9c03862.xdf269951086089ce(x6435b7bbb0879a04 xa942970cc8a85fd4)
        at xe86f37adaccef1c3.xc5c3f438428cb03b.xd01ea9131ba53b87(ArrayList x292b5f49fee42032, xfedf115fd9c03862 xcc5022025f73a533)
        at xe86f37adaccef1c3.xc5c3f438428cb03b.xdeeb682062ef79a5()
        at xe86f37adaccef1c3.xc5c3f438428cb03b.xd5da23b762ce52a2(IMailMergeDataSource xef1769c4fe6ae4ca, Boolean xc9c7b90943167aed)
        at Aspose.Words.Reporting.MailMerge.Execute(IMailMergeDataSource dataSource)
        at Aspose.Words.Reporting.MailMerge.Execute(DataRow row)
        at VetConnect.ApplicationServices.Services.Templates.TemplateFactory.CreateTemplate(MemoryStream docStream, DataSet ds, String extension, Dictionary`2 singleValues) in c:\Projects\2010.vetworks.vetconnect.program\app\VetConnect.ApplicationServices\Services\Templates\TemplateFactory.cs:line 196
InnerException:

In line 14 you’ll see the call:

builder.InsertImage((byte[])e.FieldValue);

Hi there,

Thanks for your inquiry. Insertion of SVG into Word document does not support in older version of Aspose.Wrods. Please use the latest version of Aspose.Words for .NET 14.5.0. If you still face problem, please share your SVG file here for testing. I will investigate the issue and provide you more information.