/// <summary>
        /// Creates a new instance of the WordprocessingDocument class from the spcified package.
        /// </summary>
        /// <param name="package">The specified OpenXml package</param>
        /// <param name="type">The type of the WordprocessingDocument.</param>
        /// <param name="autoSave">Whether to auto save the created document.</param>
        /// <returns>A new instance of WordprocessingDocument.</returns>
        /// <exception cref="ArgumentNullException">Thrown when "package" is null reference.</exception>
        /// <exception cref="IOException">Thrown when "package" is not opened with Write access.</exception>
        public static WordprocessingDocument Create(Package package, WordprocessingDocumentType type, bool autoSave)
        {
            WordprocessingDocument doc = new WordprocessingDocument();

            doc.DocumentType          = type;
            doc.OpenSettings          = new OpenSettings();
            doc.OpenSettings.AutoSave = autoSave;
            doc.MainPartContentType   = MainPartContentTypes[type];
            doc.CreateCore(package);
            return(doc);
        }
        private static MemoryStream CreateOtherWordprocessingDocument()
        {
            var stream = new MemoryStream();
            const WordprocessingDocumentType type = WordprocessingDocumentType.Document;

            using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(stream, type))
            {
                MainDocumentPart mainDocumentPart = wordDocument.AddMainDocumentPart();
                mainDocumentPart.Document = CreateOtherDocument();
            }

            return(stream);
        }
        private static MemoryStream CreateWordprocessingDocument(IEnumerable <string> runTexts)
        {
            var stream = new MemoryStream();
            const WordprocessingDocumentType type = WordprocessingDocumentType.Document;

            using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(stream, type))
            {
                MainDocumentPart mainDocumentPart = wordDocument.AddMainDocumentPart();
                mainDocumentPart.PutXDocument(new XDocument(CreateDocument(runTexts)));
            }

            return(stream);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Creates a <see cref="WordprocessingDocument"/> for on a <see cref="MemoryStream"/>
        /// testing purposes, using the given <paramref name="document"/> as the w:document
        /// root element of the main document part.
        /// </summary>
        /// <param name="document">The w:document root element.</param>
        /// <returns>The <see cref="MemoryStream"/> containing the <see cref="WordprocessingDocument"/>.</returns>
        private static MemoryStream CreateWordprocessingDocument(XElement document)
        {
            var stream = new MemoryStream();
            const WordprocessingDocumentType type = WordprocessingDocumentType.Document;

            using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(stream, type))
            {
                MainDocumentPart part = wordDocument.AddMainDocumentPart();
                part.PutXDocument(new XDocument(document));
            }

            return(stream);
        }
 private static void CreateMacroEnabledWordDocument(string path, int size)
 {
     const WordprocessingDocumentType type = WordprocessingDocumentType.MacroEnabledDocument;
     using WordprocessingDocument wordDocument = WordprocessingDocument.Create(path, type);
     // Create a main document part with an empty document.
     MainDocumentPart mainDocumentPart = wordDocument.AddMainDocumentPart();
     WriteRootElement(mainDocumentPart,
         new XElement(W + "document",
             new XElement(W + "body",
                 new XElement(W + "p"))));
     // Create a custom XML part with the desired size in MB.
     CustomXmlPart customXmlPart = mainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
     WriteRootElement(customXmlPart, CreatePartRootElement(size));
 }
        /// <summary>
        /// Creates a new instance of the WordprocessingDocument class from the specified file.
        /// </summary>
        /// <param name="path">The path and file name of the target WordprocessingDocument.</param>
        /// <param name="type">The type of the WordprocessingDocument.</param>
        /// <param name="autoSave">Whether to auto save the created document.</param>
        /// <returns>A new instance of WordprocessingDocument.</returns>
        /// <exception cref="ArgumentNullException">Thrown when "path" is null reference.</exception>
        public static WordprocessingDocument Create(string path, WordprocessingDocumentType type, bool autoSave)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path));
            }
            WordprocessingDocument doc = new WordprocessingDocument();

            doc.DocumentType          = type;
            doc.OpenSettings          = new OpenSettings();
            doc.OpenSettings.AutoSave = autoSave;
            doc.MainPartContentType   = MainPartContentTypes[type];
            doc.CreateCore(path);
            return(doc);
        }
        private void UpdateDocumentTypeFromContentType()
        {
            if (this.MainPartContentType == null)
            {
                throw new InvalidOperationException();
            }

            foreach (KeyValuePair <WordprocessingDocumentType, string> types in MainPartContentTypes)
            {
                if (types.Value == this.MainPartContentType)
                {
                    this.DocumentType = types.Key;
                }
            }
        }
Exemplo n.º 8
0
        public static WordprocessingDocument Create(Package package, WordprocessingDocumentType type, bool autoSave)
        {
            var doc = new WordprocessingDocument
            {
                DocumentType = type,
                OpenSettings = new OpenSettings {
                    AutoSave = autoSave
                },
                MainPartContentType = MainPartContentTypes[type],
            };

            doc.CreateCore(package);

            return(doc);
        }
        private static MemoryStream CreateWordprocessingDocument()
        {
            var stream = new MemoryStream();
            const WordprocessingDocumentType type = WordprocessingDocumentType.Document;

            using WordprocessingDocument wordDocument = WordprocessingDocument.Create(stream, type);
            MainDocumentPart mainDocumentPart = wordDocument.AddMainDocumentPart();

            mainDocumentPart.Document =
                new Document(
                    new Body(
                        new Paragraph(
                            new Run(
                                new Text(ToReplace))),
                        new Paragraph(
                            new Run(
                                new Text("to-")),
                            new Run(
                                new Text("replace")))));
            return(stream);
        }
        /// <summary>
        /// Changes the document type.
        /// </summary>
        /// <param name="newType">The new type of the document.</param>
        /// <remarks>The MainDocumentPart will be changed.</remarks>
        public void ChangeDocumentType(WordprocessingDocumentType newType)
        {
            ThrowIfObjectDisposed();

            if (newType == DocumentType)
            {
                // same type, just return
                return;
            }

            if (FileOpenAccess == FileAccess.Read)
            {
                throw new IOException(ExceptionMessages.PackageAccessModeIsReadonly);
            }

            WordprocessingDocumentType oldType = DocumentType;

            DocumentType        = newType;
            MainPartContentType = MainPartContentTypes[newType];

            if (MainDocumentPart == null)
            {
                return;
            }

            try
            {
                ChangeDocumentTypeInternal <MainDocumentPart>();
            }
            catch (OpenXmlPackageException e)
            {
                if (e.Message == ExceptionMessages.CannotChangeDocumentType)
                {
                    DocumentType        = oldType;
                    MainPartContentType = MainPartContentTypes[oldType];
                }

                throw;
            }
        }
Exemplo n.º 11
0
        public void AddNewPart_NewMainDocumentPart_SuccessfullyAdded()
        {
            const string path = "Document.docx";
            const WordprocessingDocumentType type = WordprocessingDocumentType.Document;

            using WordprocessingDocument wordDocument = WordprocessingDocument.Create(path, type);

            // Create minimum main document part.
            MainDocumentPart mainDocumentPart = wordDocument.AddMainDocumentPart();

            mainDocumentPart.Document = new Document(new Body(new Paragraph()));

            // Create empty style definitions part.
            var styleDefinitionsPart = mainDocumentPart.AddNewPart <StyleDefinitionsPart>();

            styleDefinitionsPart.Styles = new Styles();

            // Create empty numbering definitions part.
            var numberingDefinitionsPart = mainDocumentPart.AddNewPart <NumberingDefinitionsPart>();

            numberingDefinitionsPart.Numbering = new Numbering();
        }
 /// <summary>
 /// Creates a new instance of the WordprocessingDocument class from the spcified package.
 /// </summary>
 /// <param name="package">The specified OpenXml package.</param>
 /// <param name="type">The type of the WordprocessingDocument.</param>
 /// <returns>A new instance of WordprocessingDocument.</returns>
 /// <exception cref="ArgumentNullException">Thrown when "package" is null reference.</exception>
 /// <exception cref="IOException">Thrown when "package" is not opened with Write access.</exception>
 public static WordprocessingDocument Create(Package package, WordprocessingDocumentType type)
 {
     return(Create(package, type, true));
 }
 /// <summary>
 /// Creates a new instance of the WordprocessingDocument class from the IO stream.
 /// </summary>
 /// <param name="stream">The IO stream on which to create the WordprocessingDocument.</param>
 /// <param name="type">The type of the WordprocessingDocument.</param>
 /// <returns>A new instance of WordprocessingDocument.</returns>
 /// <exception cref="ArgumentNullException">Thrown when "stream" is null reference.</exception>
 /// <exception cref="IOException">Thrown when "stream" is not opened with Write access.</exception>
 public static WordprocessingDocument Create(Stream stream, WordprocessingDocumentType type)
 {
     return(Create(stream, type, true));
 }
 /// <summary>
 /// Creates a new instance of the WordprocessingDocument class from the specified file.
 /// </summary>
 /// <param name="path">The path and file name of the target WordprocessingDocument.</param>
 /// <param name="type">The type of the WordprocessingDocument.</param>
 /// <returns>A new instance of WordprocessingDocument.</returns>
 /// <exception cref="ArgumentNullException">Thrown when "path" is null reference.</exception>
 public static WordprocessingDocument Create(string path, WordprocessingDocumentType type)
 {
     return(Create(path, type, true));
 }