Help with special chars in Word and PDF modules

Hi all,

I have a Word doc I am using as a template with text delimited by “[[” and “]]” to create pseudo-fields, which I am filling in with data from a DB via the Word module.

Once the document is created and filled with data I am converting it to PDF, with the PDF module.

Here is my problem. I have 3 address fields which may or may not be all populated. I have put them all on the same line to avoid nasty white space, e.g.

[[Addr1]][[Addr2]][[Addr3]]
[[City]], [[State]] [[Zip]]

If the value of addr2 is not nothing, then I am inserting a vbCrLf and then the info for addr2. Same with addr3.

The issue I am having is, I am getting square boxes in the Word and PDF doc, depending on which piece I use (either vbCr, vbLf, or vbCrLf). If I use vbLf, the Word doc does not reflect the CR, but the PDF looks great. If I use the vbCr, the Word doc looks great but the PDF has boxes in it.

There must be some way around this, right?

Any help would be greatly appreciated.

Cheers,

Christopher Burns

Hi Christopher,
We’re looking into the issue, thanks. But first, why aren’t you using merge fields + MergeField event, but using pseudo-fields?

Hi Dmitry,

Good question. I was not aware of the merge fields option, however, we already have templates set up with the pseudo fields. Also, if we use merge fields, it seems that if we have multiple fields of the same name, we need to enumerate them (i.e. , etc.). Is that right?

Cheers,

Chris

If you mean repeatable data, then you only need to place into the document its repeatable part. For example, if you need to fill a table with addresses, simply create merge fields for one row and mark beginning and end of the row with two special merge fields. Then run the Document.MailMerge.ExecuteWithRegions method to perform mail merge.
The MergeField event allows to control populating the fields. You can manually set a text to insert depending on some conditions.
Refer here to obtain more info:
https://docs.aspose.com/words/net/mail-merge-and-reporting/

Regarding your first question, use of the ControlChar class would not cause appearance of any garbage like the square boxes. Please have a look at this example demonstrating use of this class and the merge fields as applied to your task. Note how easily it can be accomplished using Aspose.Word. The example was written in C#, but I can rewrite it for you if you need it in VB.

using System;
using System.Data;
using Aspose.Pdf;
using Aspose.Word;
namespace ConsoleApplication123
{
    ///
    /// Summary description for Class1.
    ///
    class Class1
    {
        ///
        /// The main entry point for the application.
        ///
        [STAThread]
        static void Main(string[] args)
        {
            Document doc = new Document("addresses.doc");
            DataTable table = new DataTable("AddressTable");
            table.Columns.Add("Addr1", typeof(string));
            table.Columns.Add("Addr2", typeof(string));
            table.Columns.Add("Addr3", typeof(string));
            table.Columns.Add("City", typeof(string));
            table.Columns.Add("State", typeof(string));
            table.Columns.Add("Zip", typeof(string));
            table.Rows.Add(new object[] {"Dmitry_Address1", "Dmitry_Address2",
null, "Dmitry_City", "Dmitry_State", "Dmitry_Zip"});
            table.Rows.Add(new object[] {"Chris_Address1", null, null,
"Chris_Address3", "Chris_State", "Chris_Zip"});
            doc.MailMerge.MergeField += new MergeFieldEventHandler(
            MailMerge_MergeField);
            doc.MailMerge.ExecuteWithRegions(table);
            doc.Save("temp.xml", SaveFormat.FormatAsposePdf);
            Pdf pdf = new Pdf();
            pdf.BindXML("temp.xml", null);
            pdf.Save("addresses.pdf");
        }
        static void MailMerge_MergeField(object sender, MergeFieldEventArgs e)
        {
            if ((e.FieldName == "Addr2" || e.FieldName == "Addr3") &&
            e.FieldValue != DBNull.Value)
                e.Text = ControlChar.CrLf + e.FieldValue;
        }
    }
}