Поделиться через


MainDocumentPart Class

Defines the MainDocumentPart.

Inheritance Hierarchy

System.Object
  DocumentFormat.OpenXml.Packaging.OpenXmlPartContainer
    DocumentFormat.OpenXml.Packaging.OpenXmlPart
      DocumentFormat.OpenXml.Packaging.MainDocumentPart

Namespace:  DocumentFormat.OpenXml.Packaging
Assembly:  DocumentFormat.OpenXml (in DocumentFormat.OpenXml.dll)

Syntax

'Declaration
Public Class MainDocumentPart _
    Inherits OpenXmlPart
'Usage
Dim instance As MainDocumentPart
public class MainDocumentPart : OpenXmlPart

Examples

The following code example opens a word-processing document, instantiates the document main part, reads the comments from the comment part, and displays them.

using System;
using System.IO;
using DocumentFormat.OpenXml.Packaging;

namespace MainDocumentPartEx
{
    class Program
    {
        // Get the contents of a document part.
        static void Main(string[] args)
        {
            string document = @"C:\Users\Public\Documents\MainDocumentPartEx.docx";
            string comments = null;

            // Open the file read-only.
            using (WordprocessingDocument wordprocessingDocument = 
                WordprocessingDocument.Open(document, false))
            {
                MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
                WordprocessingCommentsPart WordprocessingCommentsPart = 
                    mainPart.WordprocessingCommentsPart;

                // Read the comments using a stream reader.
                using (StreamReader streamReader = 
                    new StreamReader(WordprocessingCommentsPart.GetStream()))
                {
                    comments = streamReader.ReadToEnd();
                }
            }
            Console.WriteLine(comments);
            Console.ReadKey();
        }
    }
}
Imports System.IO
Imports DocumentFormat.OpenXml.Packaging

Module Module1
    ' Get the contents of a document part.
    Sub Main(ByVal args As String())
        Dim document As String = "C:\Users\Public\Documents\MainDocumentPartEx.docx"
        Dim comments As String = Nothing

        ' Open the file read-only.
        Using wordprocessingDocument As WordprocessingDocument = wordprocessingDocument.Open(document, False)
            Dim mainPart As MainDocumentPart = wordprocessingDocument.MainDocumentPart
            Dim WordprocessingCommentsPart As WordprocessingCommentsPart = mainPart.WordprocessingCommentsPart

            ' Read the comments using a stream reader.
            Using streamReader As New StreamReader(WordprocessingCommentsPart.GetStream())
                comments = streamReader.ReadToEnd()
            End Using
        End Using
        Console.WriteLine(comments)
        Console.ReadKey()
    End Sub
End Module

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

MainDocumentPart Members

DocumentFormat.OpenXml.Packaging Namespace