- using DocumentFormat.OpenXml;
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml.Wordprocessing;
- namespace e2w.common.Utilities.WordHelper
- {
- public class WordHelper
- {
- public WordprocessingDocument OpenDocument(string filePath)
- {
- WordprocessingDocument document = WordprocessingDocument.Open(filePath, true);
- return document;
- }
- public WordprocessingDocument OpenDocumentFromTemplate(string templatePath)
- {
- File.Copy(templatePath, "NewDocument.docx", true);
- var document = WordprocessingDocument.Open("NewDocument.docx", true);
- return document;
- }
- public void AddDataToBookmark(WordprocessingDocument document, string bookmarkName, string data)
- {
- var bookmarkStart = FindBookmarkStart(document,bookmarkName);
- if (bookmarkStart != null)
- {
- var run = new Run(new Text(data));
- var paragraph = new Paragraph(run);
- bookmarkStart.Parent.InsertAfter(paragraph, bookmarkStart);
- }
- }
- //public void FindAndReplace(string findText, string replaceText)
- //{
- // IEnumerable<Text> textElements = _body.Descendants<Text>().Where(t => t.Text.Contains(findText));
- // foreach (Text text in textElements)
- // {
- // text.Text = text.Text.Replace(findText, replaceText);
- // }
- //}
- //public void InsertParagraph(string text, bool bold = false, bool italic = false)
- //{
- // var run = new Run(new Text(text));
- // if (bold)
- // run.RunProperties = new RunProperties(new Bold());
- // if (italic)
- // run.RunProperties = new RunProperties(new Italic());
- // var paragraph = new Paragraph(run);
- // _body.AppendChild(paragraph);
- //}
- //public void InsertTable(int rows, int columns)
- //{
- // var table = new Table();
- // var tableProperties = new TableProperties(
- // new TableBorders(
- // new TopBorder(),
- // new BottomBorder(),
- // new LeftBorder(),
- // new RightBorder(),
- // new InsideHorizontalBorder(),
- // new InsideVerticalBorder()
- // )
- // );
- // table.AppendChild(tableProperties);
- // for (int i = 0; i < rows; i++)
- // {
- // var row = new TableRow();
- // for (int j = 0; j < columns; j++)
- // {
- // var cell = new TableCell(new Paragraph(new Run(new Text(""))));
- // row.AppendChild(cell);
- // }
- // table.AppendChild(row);
- // }
- // _body.AppendChild(table);
- //}
- //public void PopulateTableData(string[,] data)
- //{
- // Table table = _body.Elements<Table>().FirstOrDefault();
- // if (table != null)
- // {
- // for (int i = 0; i < table.Elements<TableRow>().Count(); i++)
- // {
- // var row = table.Elements<TableRow>().ElementAt(i);
- // for (int j = 0; j < row.Elements<TableCell>().Count(); j++)
- // {
- // var cell = row.Elements<TableCell>().ElementAt(j);
- // cell.Append(new Paragraph(new Run(new Text(data[i, j]))));
- // }
- // }
- // }
- //}
- //public void AddChart()
- //{
- // // Add code here to create and add a chart
- //}
- public void SaveDocument(WordprocessingDocument document,string filePath)
- {
- var mainPart = document.MainDocumentPart;
- var body = mainPart.Document.Body;
- mainPart.Document.Save();
- document.Close();
- document.Dispose();
- // Move or rename the document if necessary
- if (filePath != "NewDocument.docx")
- {
- File.Move("NewDocument.docx", filePath);
- }
- }
- private BookmarkStart FindBookmarkStart(WordprocessingDocument document,string bookmarkName)
- {
- var mainPart = document.MainDocumentPart;
- var body = mainPart.Document.Body;
- IEnumerable<BookmarkStart> bookmarkStarts = body.Descendants<BookmarkStart>().Where(b => b.Name == bookmarkName);
- return bookmarkStarts.FirstOrDefault();
- }
- }
- }
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml.Wordprocessing;
- using e2w.common.Utilities.WordHelper;
- class Program
- {
- static void Main(string[] args)
- {
- // Replace "document.docx" with the path to your Word file
- string filePath = @"C:\Users\swapn\Downloads\Test.docx";
- WordHelper wordHelper = new WordHelper();
- var document = wordHelper.OpenDocument(filePath);
- //var templateDoc = wordHelper.OpenDocumentFromTemplate(filePath);
- wordHelper.AddDataToBookmark(document, "testBookmark", "adddatafromcode");
- wordHelper.SaveDocument(document, filePath);
- Console.ReadLine();
- }
- }
[text] testts
Viewer
*** This page was generated with the meta tag "noindex, nofollow". This happened because you selected this option before saving or the system detected it as spam. This means that this page will never get into the search engines and the search bot will not crawl it. There is nothing to worry about, you can still share it with anyone.
Editor
You can edit this paste and save as new:
File Description
- testts
- Paste Code
- 09 Jun-2023
- 5.39 Kb
You can Share it: