Beispiel #1
0
        /// <summary>
        /// Adds a AlternativeFormatImportPart to the HeaderPart
        /// </summary>
        /// <param name="partType">The part type of the AlternativeFormatImportPart</param>
        /// <param name="id">The relationship id</param>
        /// <return>The newly added part</return>
        public AlternativeFormatImportPart AddAlternativeFormatImportPart(AlternativeFormatImportPartType partType, string id)
        {
            var contentType   = AlternativeFormatImportPartTypeInfo.GetContentType(partType);
            var partExtension = AlternativeFormatImportPartTypeInfo.GetTargetExtension(partType);

            OpenXmlPackage.PartExtensionProvider.MakeSurePartExtensionExist(contentType, partExtension);
            return(AddAlternativeFormatImportPart(contentType, id));
        }
        internal static string GetTargetExtension(AlternativeFormatImportPartType imageType)
        {
            switch (imageType)
            {
            //case AlternativeFormatImportPartType.Text:
            //    return ".txt";

            case AlternativeFormatImportPartType.Xhtml:
                return(".xhtml");

            case AlternativeFormatImportPartType.Mht:
                return(".mht");

            case AlternativeFormatImportPartType.Xml:
                return(".xml");

            //case AlternativeFormatImportPartType.TextXml:
            //    return ".xml";

            case AlternativeFormatImportPartType.TextPlain:
                return(".txt");

            case AlternativeFormatImportPartType.WordprocessingML:
                return(".docx");

            case AlternativeFormatImportPartType.OfficeWordMacroEnabled:
                return(".docm");

            case AlternativeFormatImportPartType.OfficeWordTemplate:
                return(".dotx");

            case AlternativeFormatImportPartType.OfficeWordMacroEnabledTemplate:
                return(".dotm");

            case AlternativeFormatImportPartType.Rtf:
                return(".rtf");

            case AlternativeFormatImportPartType.Html:
                return(".htm");


            default:
                return(".dat");
            }
        }
        internal static string GetContentType(AlternativeFormatImportPartType partType)
        {
            switch (partType)
            {
            //case AlternativeFormatImportPartType.Text:
            //    return "application/text/plain";

            case AlternativeFormatImportPartType.Xhtml:
                return("application/xhtml+xml");

            case AlternativeFormatImportPartType.Mht:
                return("message/rfc822");

            case AlternativeFormatImportPartType.Xml:
                return("application/xml");

            //case AlternativeFormatImportPartType.TextXml:
            //    return "text/xml";

            case AlternativeFormatImportPartType.TextPlain:
                return("text/plain");

            case AlternativeFormatImportPartType.WordprocessingML:
                return("application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml");

            case AlternativeFormatImportPartType.OfficeWordMacroEnabled:
                return("application/vnd.ms-word.document.macroEnabled.main+xml");

            case AlternativeFormatImportPartType.OfficeWordTemplate:
                return("application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml");

            case AlternativeFormatImportPartType.OfficeWordMacroEnabledTemplate:
                return("application/vnd.ms-word.template.macroEnabledTemplate.main+xml");

            case AlternativeFormatImportPartType.Rtf:
                return("application/rtf");

            case AlternativeFormatImportPartType.Html:
                return("text/html");

            default:
                throw new ArgumentOutOfRangeException(nameof(partType));
            }
        }
        private static AltChunk CreateAltChunkWithXmlContent(
            WordprocessingDocument wordDocument,
            AlternativeFormatImportPartType contentType,
            string xmlContent)
        {
            string altChunkId = "XmlAltChunkId-" + Guid.NewGuid();
            AlternativeFormatImportPart chunk = wordDocument.MainDocumentPart.AddAlternativeFormatImportPart(
                contentType, altChunkId);

            using (Stream stream = chunk.GetStream(FileMode.Create))
                using (var writer = new StreamWriter(stream))
                {
                    writer.WriteLine(xmlContent);
                }

            return(new AltChunk {
                Id = altChunkId
            });
        }
Beispiel #5
0
        /// <summary>
        /// Insert a document on bookmark
        /// </summary>
        /// <param name="bookmark"></param>
        /// <param name="content"></param>
        /// <param name="importType"></param>
        private void AddAltChunkOnBookmark(string bookmark, Stream content, AlternativeFormatImportPartType importType)
        {
            AlternativeFormatImportPart formatImportPart = wdMainDocumentPart.AddAlternativeFormatImportPart(importType);

            formatImportPart.FeedData(content);

            AltChunk altChunk = new AltChunk();

            altChunk.Id = wdMainDocumentPart.GetIdOfPart(formatImportPart);

            var bookmarkElement = FindBookmark(bookmark);

            if (bookmarkElement != null)
            {
                var paragraph = bookmarkElement.Ancestors <Paragraph>().LastOrDefault();
                // without an empty paragraph after altchunk, the docx might be corrupted if the bookmark is inside a table and the html only contains one paragraph
                if (paragraph.Ancestors <Table>().Any())
                {
                    paragraph.InsertAfterSelf(new Paragraph());
                }
                paragraph.InsertAfterSelf(new AltChunk(altChunk));
            }
        }
Beispiel #6
0
 /// <summary>
 /// Adds a AlternativeFormatImportPart to the HeaderPart.
 /// </summary>
 /// <param name="partType">The part type of the AlternativeFormatImportPart.</param>
 /// <param name="id">The relationship id.</param>
 /// <returns>The newly added part.</returns>
  public AlternativeFormatImportPart AddAlternativeFormatImportPart(AlternativeFormatImportPartType partType, string id)
 {
     string contentType = AlternativeFormatImportPartTypeInfo.GetContentType(partType);
     string partExtension = AlternativeFormatImportPartTypeInfo.GetTargetExtension(partType);
     OpenXmlPackage.PartExtensionProvider.MakeSurePartExtensionExist(contentType, partExtension);
 
     return AddAlternativeFormatImportPart(contentType, id);
 }
        internal static string GetTargetExtension(AlternativeFormatImportPartType imageType)
        {
            switch (imageType)
            {
                //case AlternativeFormatImportPartType.Text:
                //    return ".txt";

                case AlternativeFormatImportPartType.Xhtml:
                    return ".xhtml";

                case AlternativeFormatImportPartType.Mht:
                    return ".mht";

                case AlternativeFormatImportPartType.Xml:
                    return ".xml";

                //case AlternativeFormatImportPartType.TextXml:
                //    return ".xml";

                case AlternativeFormatImportPartType.TextPlain:
                    return ".txt";

                case AlternativeFormatImportPartType.WordprocessingML:
                    return ".docx";

                case AlternativeFormatImportPartType.OfficeWordMacroEnabled:
                    return ".docm";

                case AlternativeFormatImportPartType.OfficeWordTemplate:
                    return ".dotx";

                case AlternativeFormatImportPartType.OfficeWordMacroEnabledTemplate:
                    return ".dotm";

                case AlternativeFormatImportPartType.Rtf:
                    return ".rtf";

                case AlternativeFormatImportPartType.Html:
                    return ".htm";

                
                default:
                    return ".dat";
            }
        }
        internal static string GetContentType(AlternativeFormatImportPartType partType)
        {
            switch (partType)
            {
                //case AlternativeFormatImportPartType.Text:
                //    return "application/text/plain";

                case AlternativeFormatImportPartType.Xhtml:
                    return "application/xhtml+xml";

                case AlternativeFormatImportPartType.Mht:
                    return "message/rfc822";

                case AlternativeFormatImportPartType.Xml:
                    return "application/xml";

                //case AlternativeFormatImportPartType.TextXml:
                //    return "text/xml";

                case AlternativeFormatImportPartType.TextPlain:
                    return "text/plain";
                
                case AlternativeFormatImportPartType.WordprocessingML:
                    return "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";

                case AlternativeFormatImportPartType.OfficeWordMacroEnabled:
                    return "application/vnd.ms-word.document.macroEnabled.main+xml";

                case AlternativeFormatImportPartType.OfficeWordTemplate:
                    return "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml";

                case AlternativeFormatImportPartType.OfficeWordMacroEnabledTemplate:
                    return "application/vnd.ms-word.template.macroEnabledTemplate.main+xml";

                case AlternativeFormatImportPartType.Rtf:
                    return "application/rtf";

                case AlternativeFormatImportPartType.Html:
                    return "text/html";

                default:
                    throw new ArgumentOutOfRangeException("partType");
            }
        }
 internal static string GetContentType(AlternativeFormatImportPartType partType)
 => partType switch
 {