VintaSoft Imaging .NET SDK 10.1
Vintasoft.Imaging.Office.OpenXml.Editor Namespace / DocxDocumentEditor Class
Members Example



In This Topic
    DocxDocumentEditor Class
    In This Topic
    Defines the editor of DOCX document.
    Object Model
    DocxDocumentEditor ClassOpenXmlDocumentRootElement ClassDocxDocumentStyles Class
    Syntax
    'Declaration
     
    Public Class DocxDocumentEditor 
       Inherits OpenXmlDocumentEditor
    public class DocxDocumentEditor : OpenXmlDocumentEditor 
    public __gc class DocxDocumentEditor : public OpenXmlDocumentEditor 
    public ref class DocxDocumentEditor : public OpenXmlDocumentEditor 
    Example
    This example shows how to find and replace text in DOCX document (example uses template document FindAndReplaceText_template.docx):
    Public Shared Sub DocxFindAndReplaceTextExample()
        Dim templateFilename As String = "FindAndReplaceText_template.docx"
        Dim outFilename As String = "FindAndReplaceText.docx"
        Dim outPdfFilename As String = "FindAndReplaceText.pdf"
                
        ' create DocxDocumentEditor that allows to edit file "FindAndReplaceText_template.docx"
        Using editor As New Vintasoft.Imaging.Office.OpenXml.Editor.DocxDocumentEditor(templateFilename)
            ' get document body
            Dim documentBody As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement = editor.Body
                
            ' replace first occurrence of text "[field1]" by text "value1"
            documentBody("[field1]") = "value1"
                
            ' replace all occurrences of text "[field2]" by text "value2"
            documentBody.ReplaceText("[field2]", "value2")
                
            ' find text content that corresponds to the text "[field3]"
            Dim field3Content As Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextContent = documentBody.FindText("[field3]")
            ' change text in found text content
            field3Content.Text = "value3"
                
            ' replace text "[multiline_field]" by multiline text
            documentBody("[multiline_field]") = vbLf & "line1" & vbLf & "line2" & vbLf & "line3"
                
            ' save changed document to a DOCX file
            editor.Save(outFilename)
                
            ' export changed document to a PDF document
            editor.Export(outPdfFilename)
        End Using
    End Sub
    public static void DocxFindAndReplaceTextExample()
    {
        string templateFilename = "FindAndReplaceText_template.docx";
        string outFilename = "FindAndReplaceText.docx";
        string outPdfFilename = "FindAndReplaceText.pdf";
                
        // create DocxDocumentEditor that allows to edit file "FindAndReplaceText_template.docx"
        using (Vintasoft.Imaging.Office.OpenXml.Editor.DocxDocumentEditor editor =
            new Vintasoft.Imaging.Office.OpenXml.Editor.DocxDocumentEditor(templateFilename))
        {
            // get document body
            Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentElement documentBody = editor.Body;
                
            // replace first occurrence of text "[field1]" by text "value1"
            documentBody["[field1]"] = "value1";
                
            // replace all occurrences of text "[field2]" by text "value2"
            documentBody.ReplaceText("[field2]", "value2");
                
            // find text content that corresponds to the text "[field3]"
            Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlTextContent field3Content = documentBody.FindText("[field3]");
            // change text in found text content
            field3Content.Text = "value3";
                
            // replace text "[multiline_field]" by multiline text
            documentBody["[multiline_field]"] = "\nline1\nline2\nline3";
                
            // save changed document to a DOCX file
            editor.Save(outFilename);
                
            // export changed document to a PDF document
            editor.Export(outPdfFilename);
        }
    }
    Inheritance Hierarchy

    System.Object
       Vintasoft.Imaging.Office.OpenXml.Editor.OpenXmlDocumentEditor
          Vintasoft.Imaging.Office.OpenXml.Editor.DocxDocumentEditor

    Requirements

    Target Platforms: .NET 5; .NET Core 3.1; .NET Framework 4.8, 4.7, 4.6, 4.5, 4.0, 3.5

    See Also