New Line in Word Document Text Boxes

Hello,

I have a word document (.docx) which has text boxes in it. Within each text box is a text placeholder which I replace (for example [CUSTOMER] gets replaced with the customer name). The code I have used works fine but when I have to use line breaks I just cannot figure out a way to add a line break. I have tried \v and environment.newline both of which result in a unhandled exception hexadecimal value 0x0b is an invalid character. Could anybody help and advise me how I can insert a line break into a textbox within the Word Document correctly?

public Boolean CreateJobTicket(String sTemplateFile, String sJobTicketFile, DataRow rowJobTicket, DataTable dtJobTicketQuantities, DataTable dtJobTicketMaterials, DataTable dtJobTicketTasks)
{
// classes called within this method
CommonConversions clsConv = new CommonConversions();
ConfigurationHandling clsConf = new ConfigurationHandling();
ErrorHandling clsError = new ErrorHandling();

// create the temp word document file
try
{
File.Copy(System.Configuration.ConfigurationManager.AppSettings["templatesFolder"].ToString() + sTemplateFile, sJobTicketFile, true);
}
catch (Exception ex)
{
clsError.RecordErrorDetail(clsConf.ReturnUserURN(), "JobTicketDocument.JobTicket.CreateJobTicket", "The attempt to create the copy word document failed with the error " + ex.Message.ToString());
return false;
}

// open the temporary word document for editing
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(sJobTicketFile, true))
{
var vWordDocument = wordDoc.MainDocumentPart.Document;

// loop through the word document text frames and replace the placeholder text items with the text from the
// datasources used for the job ticket
foreach (var vWordDocText in vWordDocument.Descendants())
{
if (vWordDocText.Text.Contains("[PROCESS]"))
{
vWordDocText.Text = vWordDocText.Text.Replace("[PROCESS]", ReturnJobTicketTasks(dtJobTicketTasks, 0));
}
}
}

return true;
}


public String ReturnJobTicketTasks(DataTable dtJobTicketTasks, Int16 iType)
{
String sJobTicketTasks = "";
foreach(DataRow rowTask in dtJobTicketTasks.Rows)
{
switch (iType)
{
case 0:
sJobTicketTasks = sJobTicketTasks + rowTask["TASK_NAME"].ToString();

if (Convert.ToInt32(rowTask["ESTIMATED_TIME"].ToString()) != 0)
{
if (Convert.ToInt32(rowTask["ESTIMATED_TIME"].ToString()) < 10)
{
sJobTicketTasks = sJobTicketTasks + " (" + rowTask["ESTIMATED_TIME"].ToString() + " min)";
}
else
{
sJobTicketTasks = sJobTicketTasks + " (" + rowTask["ESTIMATED_TIME"].ToString() + " mins)";
}

}
break;
case 1:
sJobTicketTasks = sJobTicketTasks + rowTask["PROCESS_NOTES"].ToString();
break;
}

sJobTicketTasks = sJobTicketTasks + "\n\r";
}

return sJobTicketTasks;
}

3 Replies

MJ Mohanaselvam Jothi Syncfusion Team January 17, 2018 11:57 AM UTC

Hi Gary,

Thank you for your update.

Yes, it is possible to replace text with line break (“\v”) and new line (“\r\n”) in the Word document using Essential DocIO. For your reference we have prepared a sample to replace a text placeholder in the textbox with line break (“\v”) and
Environment.NewLine using DocIO.

In this sample, we have done the following to meet your requirement:
1. Loads an template Word document using DocIO.
2. Iterates the textboxes in the Word document.
3. Replaces the text inside textbox with line break (“\v”) and
Environment.NewLine using Replace API.
4. Saves the Word document.
5. Please find the template Word document in “Data” folder of the below sample application.

Please find the sample from the below location:
http://www.syncfusion.com/downloads/support/forum/135483/ze/GenerateWord-1462601818.zip

If the text placeholders are unique in the whole Word document, we suggest you to use find and replace APIs of
Word document class to replace all the entries of given string in the Word document instead of iterating the document elements (textbox, paragraph and others). Kindly refer the below UG documentation link to know more about working with find and replace in Word document using Essential DocIO:
https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace#replacing-the-search-results

Please let us know if you have any other questions

Regards,
Mohanaselvam J
 



GH Gary Hall January 17, 2018 05:25 PM UTC

Hello Mohanaselvam J,

Many thanks for the sample code, it worked exactly as required! 

I recollect trying to use the Find/Replace API first when I was using the same principle for another project but I could never get the instances of text found which I put down to them being inside text boxes so the API was not searching through them which is why I used the iterate through every object method. 

Once I get this project fnished I will try the Find/Replace API again. 

Again though Thanks Very Much for the quick response and the very helpful sample.




MJ Mohanaselvam Jothi Syncfusion Team January 18, 2018 05:33 AM UTC

Hi Gary,

Thank you for your update.

We are glad to know that our solution has resolved your requirement. As mentioned in your previous update to get the instances of text found in Word document, we suggest you to use
FindAll API of DocIO which returns all the entries of particular text in the Word document. For your reference, we have prepared a sample to meet your requirement.

In this sample we have done the following things:
1.Loads an template Word document using DocIO.
2.Finds all the entries of particular text using
FindAll API.
3.Gets the found text as
WTextRange instance.
4.Modifies the text value with replacement text.
5.Saves the Word document.

Please find the sample from the below location and let us know if this helps you:
http://www.syncfusion.com/downloads/support/forum/135483/ze/GenerateWord1427905918.zip

Kindly refer the below UG documentation link to know more about finding contents in a Word document using Essential DocIO:
https://help.syncfusion.com/file-formats/docio/working-with-find-and-replace#finding-contents-in-a-word-document

Please let us know if you have any other questions.

Regards,
Mohanaselvam J
 


Loader.
Up arrow icon