![]() |
|
#1
|
|||
|
|||
|
Hi,
I have word template document having some merge fields. One merge field have multiple values which i want to show each value with line break. For example: Value1 Value2 Value3 Here is my code, when document is created and download values are not shows with line break they shows with whitespace between them. Code:
private void setupDocument()
{
try
{
string documentLanguage = language.Equals("English") ? "English" : "Swedish";
docName = "~/Media/bookingDocs/" + documentLanguage + "_Doc_" + bookingID + ".docx";
string generatedFile = HttpContext.Current.Server.MapPath(docName);
File.Copy(getTemplateFilePath(), generatedFile, true);
Dictionary<string, string> keyValues = new Dictionary<string, string>();
keyValues.Add("MainRoomName", doc.MainRoom);
keyValues.Add("BookingStartEndTime", doc.BookingTime);
keyValues.Add("TotalGuest", doc.TotalGuest);
keyValues.Add("BreakfastServingTime", doc.BreakfastServingTime);
keyValues.Add("LunchServingTime", doc.LunchServingTime);
keyValues.Add("CoffeServingTime", doc.CoffeServingTime);
keyValues.Add("CompanyName", doc.CompanyName);
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(generatedFile, true))
{
var mergeFields = wordDoc.MainDocumentPart.RootElement.Descendants<FieldCode>();
foreach (KeyValuePair<string, string> keys in keyValues)
{
ReplaceMergeFieldWithText(mergeFields, keys.Key, keys.Value);
}
// update lunch room merge field
ReplaceMergeFieldLunchRooms(mergeFields, "LunchRoomName", doc.ListOfLunchRooms);
// update mergeField for specialFood
ReplaceMergeFieldSpecialFood(mergeFields, "BookingSpecialFoodList", doc.ListOfSpecialFood);
wordDoc.MainDocumentPart.Document.Save();
}
}
catch (Exception exc)
{
log.Error(userName + ": Exception in preparePrintDocument setupDocument: " + exc.Message);
log.Error(userName + ": Exception in preparePrintDocument setupDocument: " + exc.StackTrace);
throw new Exception(exc.Message);
}
}
private void ReplaceMergeFieldLunchRooms(IEnumerable<FieldCode> fields, string mergeFieldName, List<System.Web.UI.WebControls.ListItem> lunchRoomList)
{
try
{
var field = fields
.Where(f => f.InnerText.Contains(mergeFieldName))
.FirstOrDefault();
if (field != null)
{
// Get the Run that contains our FieldCode
// Then get the parent container of this Run
Run rFldCode = (Run)field.Parent;
// Get the three (3) other Runs that make up our merge field
Run rBegin = rFldCode.PreviousSibling<Run>();
Run rSep = rFldCode.NextSibling<Run>();
Run rText = rSep.NextSibling<Run>();
Run rEnd = rText.NextSibling<Run>();
// Get the Run that holds the Text element for our merge field
// Get the Text element and replace the text content
Text t = rText.GetFirstChild<Text>();
System.Text.StringBuilder sb = new StringBuilder();
if (lunchRoomList.Count > 0)
{
foreach (System.Web.UI.WebControls.ListItem item in lunchRoomList)
{
sb.Append(item.Text);
sb.Append(Environment.NewLine);
}
}
t.Text = sb.ToString();
// Remove all the four (4) Runs for our merge field
rFldCode.Remove();
rBegin.Remove();
rSep.Remove();
rEnd.Remove();
}
}
catch (Exception exc)
{
log.Error(userName + ": Exception in preparePrintDocument ReplaceMergeFieldWithText: " + exc.Message);
log.Error(userName + ": Exception in preparePrintDocument ReplaceMergeFieldWithText: " + exc.StackTrace);
throw new Exception(exc.Message);
}
}
|
| Tags |
| line breaking, mergefield |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Find / Replace hard Carriage Return with Line break.
|
GreenBoy | Word | 2 | 03-11-2018 02:32 AM |
Remove Carriage Returns in Address Block Merge field
|
alan100 | Mail Merge | 5 | 12-12-2017 08:32 PM |
| Adding a carriage return at end of each line, and then a blank space, after text entry complete | CEMurphy4 | Word VBA | 1 | 12-02-2015 11:53 PM |
| How to automatically insert a line after a Column Break | YooperNC | Word | 3 | 01-29-2015 07:16 AM |
Mail merge line break
|
camlad | Mail Merge | 3 | 06-29-2011 05:01 PM |