While working with OpenXML, I need to update an existing word template document with some bullet list. The template and required formatting follow following points:
I am taking part of my solution to explain the code. Below table is the part of my document template which i need to update.
- The document template consists of several tables
- In 1 of its table, we need to insert a row with bullet list content
I am taking part of my solution to explain the code. Below table is the part of my document template which i need to update.
UniqueTableName
|
Please refer following
|
· One
|
·
TwoPlaceHolder:
|
·
RowToRemove
|
Note: Some text here
|
In the above table, the requirement is to add a row under TwoPlaceHolder and with 1 indent and based on certain condition the RowToRemove is to be deleted.
Note : All bullets in above table are as table row of which border style has been set to none. I am jumping directly to required code for this without explaining any OpenXml namespace, assuming the reader knows about them.
Here, the GenerateDocument method takes the path of source and destination files as parameter and the other 2 parameters are for updating the document.
Note: the method updates the document at the given source location only.
private string GenerateDocument(string sourceFile, string destinationFile,
bool isRowDelete, List<string> selectedItems)
{
System.IO.File.Copy(sourceFile, destinationFile, true);
using (var document = WordprocessingDocument.Open(destinationFile,
true))
{
string documentText = null;
using (StreamReader sr = new StreamReader(document.MainDocumentPart.GetStream()))
{
documentText = sr.ReadToEnd();
}
Body documentBody =
document.MainDocumentPart.Document.Body;
foreach (Table documentTable in
documentBody.Descendants<Table>().
Where(tbl
=> tbl.InnerText.Contains("UniqueTableName")))
{
if (selectedItems.Count
> 0)
{
var secondRow
= documentTable.Descendants<TableRow>().Where(tr => tr.InnerText.
Contains("TwoPlaceHolder")).FirstOrDefault();
TableRow
newRow = new TableRow();
TableCell newcell = new TableCell();
foreach
(string
item in selectedItems)
{
//Adding the bulleted list dynamically
Paragraph para = new Paragraph
(new ParagraphProperties(
new NumberingProperties(
new NumberingLevelReference()
{ Val = 1 },
new NumberingId() {
Val = 2 })),
new Run(
new RunProperties(),
new Text(item) { Space = SpaceProcessingModeValues.Preserve
}));
newcell.Append(para);
}
newRow.Append(newcell);
secondRow.InsertAfterSelf(newRow);
}
if (isRowDelete)
{
//removing the row
var
rowForDelete = documentTable.Descendants<TableRow>().Where(tr => tr.InnerText.
Contains("RowToRemove")).FirstOrDefault();
rowForDelete.Remove();
}
}
Regex regexTablename = new Regex("UniqueTableName");
documentText = regexTablename.Replace(documentText, HttpUtility.HtmlEncode("Edited Bullet List"));using (StreamWriter sw = new StreamWriter(document.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(documentText);
}
}
return destinationFile;
}
|
After executing this method, open the document from its source location, you will see the edited table as below:
|
Edited Bullet List
|
|
Premier
Support for Office 365
|
|
· One
|
|
· TwoPlaceHolder:
|
|
o This is first Item
o This is second Item
o This is third Item
o This is forth Item
o This is fifth Item
o This is sixth Item
o This is seventh Item
|
|
Note: Some text here.
|
Result:
- The table title has been replaced by new Title "Edited Bullet List".
- New seven items have been inserted below TwoPlaceHolder row
- The "RowToRemove" has been removed from the table
No comments:
Post a Comment