[text] testts

Viewer

  1. using DocumentFormat.OpenXml;
  2. using DocumentFormat.OpenXml.Packaging;
  3. using DocumentFormat.OpenXml.Wordprocessing;
  4.  
  5. namespace e2w.common.Utilities.WordHelper
  6. {
  7.     public class WordHelper
  8.     {
  9.         public WordprocessingDocument OpenDocument(string filePath)
  10.         {
  11.             WordprocessingDocument document = WordprocessingDocument.Open(filePath, true);
  12.             
  13.                 return document;
  14.            
  15.         }
  16.  
  17.         public WordprocessingDocument OpenDocumentFromTemplate(string templatePath)
  18.         {
  19.             File.Copy(templatePath, "NewDocument.docx", true);
  20.             var document = WordprocessingDocument.Open("NewDocument.docx", true);
  21.             return document;
  22.         }
  23.  
  24.         public void AddDataToBookmark(WordprocessingDocument document, string bookmarkName, string data)
  25.         {
  26.             var bookmarkStart = FindBookmarkStart(document,bookmarkName);
  27.             if (bookmarkStart != null)
  28.             {
  29.                 var run = new Run(new Text(data));
  30.                 var paragraph = new Paragraph(run);
  31.                 bookmarkStart.Parent.InsertAfter(paragraph, bookmarkStart);
  32.             }
  33.         }
  34.  
  35.         //public void FindAndReplace(string findText, string replaceText)
  36.         //{
  37.         //    IEnumerable<Text> textElements = _body.Descendants<Text>().Where(t => t.Text.Contains(findText));
  38.         //    foreach (Text text in textElements)
  39.         //    {
  40.         //        text.Text = text.Text.Replace(findText, replaceText);
  41.         //    }
  42.         //}
  43.  
  44.         //public void InsertParagraph(string text, bool bold = false, bool italic = false)
  45.         //{
  46.         //    var run = new Run(new Text(text));
  47.         //    if (bold)
  48.         //        run.RunProperties = new RunProperties(new Bold());
  49.         //    if (italic)
  50.         //        run.RunProperties = new RunProperties(new Italic());
  51.         //    var paragraph = new Paragraph(run);
  52.         //    _body.AppendChild(paragraph);
  53.         //}
  54.  
  55.         //public void InsertTable(int rows, int columns)
  56.         //{
  57.         //    var table = new Table();
  58.         //    var tableProperties = new TableProperties(
  59.         //        new TableBorders(
  60.         //            new TopBorder(),
  61.         //            new BottomBorder(),
  62.         //            new LeftBorder(),
  63.         //            new RightBorder(),
  64.         //            new InsideHorizontalBorder(),
  65.         //            new InsideVerticalBorder()
  66.         //        )
  67.         //    );
  68.  
  69.         //    table.AppendChild(tableProperties);
  70.  
  71.         //    for (int i = 0; i < rows; i++)
  72.         //    {
  73.         //        var row = new TableRow();
  74.  
  75.         //        for (int j = 0; j < columns; j++)
  76.         //        {
  77.         //            var cell = new TableCell(new Paragraph(new Run(new Text(""))));
  78.         //            row.AppendChild(cell);
  79.         //        }
  80.  
  81.         //        table.AppendChild(row);
  82.         //    }
  83.  
  84.         //    _body.AppendChild(table);
  85.         //}
  86.  
  87.         //public void PopulateTableData(string[,] data)
  88.         //{
  89.         //    Table table = _body.Elements<Table>().FirstOrDefault();
  90.         //    if (table != null)
  91.         //    {
  92.         //        for (int i = 0; i < table.Elements<TableRow>().Count(); i++)
  93.         //        {
  94.         //            var row = table.Elements<TableRow>().ElementAt(i);
  95.  
  96.         //            for (int j = 0; j < row.Elements<TableCell>().Count(); j++)
  97.         //            {
  98.         //                var cell = row.Elements<TableCell>().ElementAt(j);
  99.         //                cell.Append(new Paragraph(new Run(new Text(data[i, j]))));
  100.         //            }
  101.         //        }
  102.         //    }
  103.         //}
  104.  
  105.         //public void AddChart()
  106.         //{
  107.         //    // Add code here to create and add a chart
  108.         //}
  109.  
  110.         public void SaveDocument(WordprocessingDocument document,string filePath)
  111.         {
  112.             var mainPart = document.MainDocumentPart;
  113.             var body = mainPart.Document.Body;
  114.             mainPart.Document.Save();
  115.             document.Close();
  116.             document.Dispose();
  117.             // Move or rename the document if necessary
  118.             if (filePath != "NewDocument.docx")
  119.             {
  120.                 File.Move("NewDocument.docx", filePath);
  121.             }
  122.         }
  123.  
  124.         private BookmarkStart FindBookmarkStart(WordprocessingDocument document,string bookmarkName)
  125.         {
  126.             var mainPart = document.MainDocumentPart;
  127.             var body = mainPart.Document.Body;
  128.             IEnumerable<BookmarkStart> bookmarkStarts = body.Descendants<BookmarkStart>().Where(b => b.Name == bookmarkName);
  129.             return bookmarkStarts.FirstOrDefault();
  130.         }
  131.     }
  132. }
  133.  
  134.  
  135.  
  136.  
  137. using DocumentFormat.OpenXml.Packaging;
  138. using DocumentFormat.OpenXml.Wordprocessing;
  139. using e2w.common.Utilities.WordHelper;
  140.  
  141. class Program
  142. {
  143.     static void Main(string[] args)
  144.     {
  145.         // Replace "document.docx" with the path to your Word file
  146.         string filePath = @"C:\Users\swapn\Downloads\Test.docx";
  147.  
  148.         WordHelper wordHelper = new WordHelper();
  149.         var document = wordHelper.OpenDocument(filePath);
  150.  
  151.         //var templateDoc = wordHelper.OpenDocumentFromTemplate(filePath);
  152.  
  153.         wordHelper.AddDataToBookmark(document, "testBookmark", "adddatafromcode");
  154.  
  155.         wordHelper.SaveDocument(document, filePath);
  156.  
  157.         Console.ReadLine();
  158.     }
  159. }
  160.  

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: