Word Mail合并:主模板?

时间:2015-02-18 10:40:48

标签: xml xslt ms-word mailmerge aspose

目前我们正在使用xslt来生成字母等。数据是xml。我们有80多封信。相同的字母用于2个客户/客户(企业)。页眉/页脚和其他一些部分包含在一些公共基础中,并且对于两个客户端是不同的。因此,如果我想更改页眉/页脚,例如在所有字母中,我只需要在一个地方更改它,所有字母模板都会立即更新。 就像有数字品牌纸一样,打印信件的实际纸张是白色的,所以我可以通过电子邮件轻松发送信件。

还有'块'比如收件人的地址,我们的参考资料'所有字母的90%,相同的签名(图片+名称,有条件地超过多人)都是相同的。

现在(出于商业原因)我们想要使用邮件合并(Aspose),并且我正在调查xslt的一些可能性是否在邮件合并中具有一些等价物。

我们仍在上一步使用XSLT,所以整行如下: '复杂数据xml' - > XSLT->' mergable xml' - >' mailmerge包含一些模板' - > docx / pdf

所以我的问题是:

  • 我可以为数字品牌纸张'?
  • 定义一些父模板
  • 是否有可能包括整块?
  • 是否可以包含背景图片(有条件地)?

其他建议?最佳做法?我似乎找不到合适的搜索关键字。

我不想在以下情况下更改所有邮件合并模板必须包含新签名,或者他们想将其移动到左侧2厘米......

1 个答案:

答案 0 :(得分:1)

是的,即使合并域位于页眉/页脚和注释中,您也可以进行邮件合并。

<强>更新

  

我可以为数字品牌纸张定义一些父模板吗?

是的,模板需要为所有需要在输出字母中分开的块使用MergeField。

  

是否有可能包含整个块?

是的,请参阅示例代码,LetterContent1和LetterContent2字段,整个段落将替换为单个合并字段。

  

是否可以包含背景图片(有条件地)?

是的,请参阅示例代码,我在此示例中添加了背景水印。您还可以添加图像(jpg,png等)。

以下是帮助您入门的示例代码。您可以尝试进行实验和修改以满足您的需求。

Word模板:Download from Google Drive

private void GenerateLetterFromTemplate(String template, String letter, String[] keys, String[] values, String watermarkText)
{
    // Load the template in Aspose.Words
    Aspose.Words.Document document = new Aspose.Words.Document(template);

    // Perform Mail Merge using key/value arrays. Keys should exist as MergeField in the template document
    document.MailMerge.Execute(keys, values);

    // See if watermark needs to be added
    if (watermarkText.Trim().Length > 0)
        InsertWatermarkText(document, watermarkText);

    // Save the document
    document.Save(letter);
}

private static void InsertWatermarkText(Aspose.Words.Document doc, string watermarkText)
{
    // Create a watermark shape. This will be a WordArt shape. 
    // You are free to try other shape types as watermarks.
    Aspose.Words.Drawing.Shape watermark = new Aspose.Words.Drawing.Shape(doc, Aspose.Words.Drawing.ShapeType.TextPlainText);

    // Set up the text of the watermark.
    watermark.TextPath.Text = watermarkText;
    watermark.TextPath.FontFamily = "Arial";
    watermark.Width = 500;
    watermark.Height = 100;
    // Text will be directed from the bottom-left to the top-right corner.
    watermark.Rotation = -40;
    // Remove the following two lines if you need a solid black text.
    watermark.Fill.Color = System.Drawing.Color.Gray; // Try LightGray to get more Word-style watermark
    watermark.StrokeColor = System.Drawing.Color.Gray; // Try LightGray to get more Word-style watermark

    // Place the watermark in the page center.
    watermark.RelativeHorizontalPosition = RelativeHorizontalPosition.Page;
    watermark.RelativeVerticalPosition = RelativeVerticalPosition.Page;
    watermark.WrapType = WrapType.None;
    watermark.VerticalAlignment = Aspose.Words.Drawing.VerticalAlignment.Center;
    watermark.HorizontalAlignment = Aspose.Words.Drawing.HorizontalAlignment.Center;

    // Create a new paragraph and append the watermark to this paragraph.
    Aspose.Words.Paragraph watermarkPara = new Aspose.Words.Paragraph(doc);
    watermarkPara.AppendChild(watermark);

    // Insert the watermark into all headers of each document section.
    foreach (Section sect in doc.Sections)
    {
        // There could be up to three different headers in each section, since we want
        // the watermark to appear on all pages, insert into all headers.
        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderPrimary);
        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderFirst);
        InsertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HeaderEven);
    }
}

private static void InsertWatermarkIntoHeader(Aspose.Words.Paragraph watermarkPara, Section sect, HeaderFooterType headerType)
{
    Aspose.Words.HeaderFooter header = sect.HeadersFooters[headerType];

    if (header == null)
    {
        // There is no header of the specified type in the current section, create it.
        header = new Aspose.Words.HeaderFooter(sect.Document, headerType);
        sect.HeadersFooters.Add(header);
    }

    // Insert a clone of the watermark into the header.
    header.AppendChild(watermarkPara.Clone(true));
}

从程序中调用方法,根据需要更改值。

String template = dataDir + "Letter Template.docx"; // Template
String letter = dataDir + "Dwayne M. Lajoie.docx"; // Letter

String[] keys = new string[] { "SenderCompanyName", "SenderCompanyAddress", "RecipientName", "RecipientAddress",
        "Salutation",
        "LetterContent1",
        "LetterContent2",
        "Closing",
        "SenderName"
    };
String[] values = new String[] { "Sender Corp.", "1138 Post Farm Road, Atlanta, GA 30305", "Dwayne M. Lajoie", "602 Whaley Lane, Milwaukee, WI 53212", 
        "Mr.",
        @"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean porta quam ac tellus mattis, id rutrum nibh tempus. Pellentesque pretium vel erat nec sollicitudin. Vivamus consequat scelerisque fermentum. Ut ullamcorper auctor massa. Fusce sed bibendum sapien. Aenean in placerat metus. In felis velit, hendrerit eget pulvinar auctor, egestas a nisl. Aliquam in urna non nisi placerat ullamcorper ut eu turpis. Phasellus lacus nunc, tincidunt eget consequat at, aliquet quis felis. Quisque tempus ac nisl id tristique. Pellentesque nec tincidunt nulla. Integer congue dui eu tellus varius, at tincidunt magna feugiat. Quisque et arcu elit. Integer tincidunt ipsum urna, a dictum odio vulputate eget. Mauris sagittis risus felis, non dignissim magna facilisis vel.",
        @"Nam egestas dignissim mi id dapibus. Morbi volutpat augue et diam bibendum viverra. Nullam feugiat blandit libero, vitae ultrices risus. Praesent dignissim cursus metus, eget viverra eros scelerisque sit amet. Duis id porttitor nisi, vitae scelerisque sem. Sed vehicula turpis tellus, in pretium dui gravida quis. Ut vel enim ligula. Nulla facilisi.",
        "Have a nice day",
        "David C. Donohue"
    };

String watermarkText = "";

GenerateLetterFromTemplate(template, letter, keys, values, watermarkText);