Skip to content

Commit 0bbd7cb

Browse files
Modified sample
1 parent 01237cd commit 0bbd7cb

2 files changed

Lines changed: 18 additions & 24 deletions

File tree

  • Mail-Merge/Insert_Signature_in_Word_Document_through_MailMerge/.NET/Insert_Signature_in_Word_Document_through_MailMerge

Mail-Merge/Insert_Signature_in_Word_Document_through_MailMerge/.NET/Insert_Signature_in_Word_Document_through_MailMerge/Program.cs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,17 @@ class Program
99
static void Main(string[] args)
1010
{
1111
// Load the word document
12-
using (FileStream fileStream = new FileStream(Path.GetFullPath("Data/Template.docx"), FileMode.Open, FileAccess.Read))
12+
using (FileStream fileStream = new FileStream(Path.GetFullPath("../../../Data/Template.docx"), FileMode.Open, FileAccess.Read))
1313
{
1414
using (WordDocument document = new WordDocument(fileStream, FormatType.Docx))
1515
{
16-
string replacementText = "John’s Juice corner was established in the year of 2002 by John. Initially it was started in a small shop. Today Juice corner has over 300 branches over USA. The secret behind this success story is the recipes of John’s Mother Angelica. She has discovered about 500 secret recipes which are all used by John. ";
17-
18-
//Creates the bookmark navigator instance to access the bookmark
19-
BookmarksNavigator bookmarksNavigator = new BookmarksNavigator(document);
20-
//Moves the virtual cursor to the location before the end of the bookmark
21-
bookmarksNavigator.MoveToBookmark("Bkmk");
22-
//Replaces the bookmark content with text
23-
bookmarksNavigator.ReplaceBookmarkContent(replacementText, true);
24-
2516
string[] fieldNames = { "Signature" };
2617
string[] fieldValues = { "signature.gif" };
2718

2819
document.MailMerge.MergeImageField += MailMerge_MergeSignature;
2920
//Execute mail merge in the Word document
3021
document.MailMerge.Execute(fieldNames, fieldValues);
31-
using (FileStream outputStream = new FileStream(Path.GetFullPath("Output/Result.docx"), FileMode.Create, FileAccess.Write))
22+
using (FileStream outputStream = new FileStream(Path.GetFullPath("../../../Output/Result.docx"), FileMode.Create, FileAccess.Write))
3223
{
3324
//Saves the stream as Word file
3425
document.Save(outputStream, FormatType.Docx);
@@ -50,23 +41,26 @@ private static void MailMerge_MergeSignature(object sender, MergeImageFieldEvent
5041
MemoryStream imageStream = new MemoryStream(imageBytes);
5142
args.ImageStream = imageStream;
5243
// Get the picture to be merged
53-
WPicture picture = args.Picture;
44+
WPicture picture = args.Picture;
5445

55-
// Get the text box format
56-
WTextBoxFormat textBoxFormat = (args.CurrentMergeField.OwnerParagraph.OwnerTextBody.Owner as WTextBox).TextBoxFormat;
46+
//Get the text box format
47+
WTextBoxFormat textBoxFormat = (args.CurrentMergeField.OwnerParagraph.OwnerTextBody.Owner as WTextBox).TextBoxFormat;
5748

58-
// Resize width
59-
if (picture.Width != textBoxFormat.Width)
49+
if (textBoxFormat != null)
6050
{
61-
float widthScale = textBoxFormat.Width / picture.Width * 100;
62-
picture.WidthScale = widthScale;
63-
}
51+
// Resize width
52+
if (picture.Width != textBoxFormat.Width)
53+
{
54+
float widthScale = textBoxFormat.Width / picture.Width * 100;
55+
picture.WidthScale = widthScale;
56+
}
6457

65-
// Resize height
66-
if (picture.Height != textBoxFormat.Height)
67-
{
68-
float heightScale = textBoxFormat.Height / picture.Height * 100;
69-
picture.HeightScale = heightScale;
58+
// Resize height
59+
if (picture.Height != textBoxFormat.Height)
60+
{
61+
float heightScale = textBoxFormat.Height / picture.Height * 100;
62+
picture.HeightScale = heightScale;
63+
}
7064
}
7165
}
7266
}

0 commit comments

Comments
 (0)