コード例 #1
0
        private void AddOrReplaceCustomxmlPath()
        {
            CheckIfTypedSettingIsNull();

            using (WordprocessingDocument document = WordprocessingDocument.Open(DocumentFilePath, true))
            {
                string        dataStoreItemId = TypedSetting.DataStoreItemId;
                CustomXmlPart xmlPart         = GetCustomXmlPart(document, dataStoreItemId);

                if (xmlPart != null)
                {
                    FileStream fs = new FileStream(TypedSetting.XmlPartFilePath, FileMode.Open);
                    xmlPart.FeedData(fs);
                }
                else
                {
                    xmlPart = document.MainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
                    FileStream fs = new FileStream(TypedSetting.XmlPartFilePath, FileMode.Open);
                    xmlPart.FeedData(fs);

                    CustomXmlPropertiesPart xmlPropertiesPart = xmlPart.AddNewPart <CustomXmlPropertiesPart>(TypedSetting.ContentTypeId);
                    DataStoreItem           dataStoreItem     = new DataStoreItem();
                    dataStoreItem.ItemId            = dataStoreItemId;
                    xmlPropertiesPart.DataStoreItem = dataStoreItem;
                }
            }
        }
コード例 #2
0
        private void btnGenerate_Click(object sender, EventArgs e)
        {
            string outFile = @"C:\Users\Christopher\Desktop\BookData\TestReportBetaEND.docx";

            OpenFileDialog OFD = new OpenFileDialog();

            OFD.Multiselect = false;
            OFD.Title       = "Open Word Document";
            OFD.Filter      = "Word Document|*.docx;*.domx";
            OFD.ShowDialog();
            string docPath = OFD.FileName;

            OFD.Title  = "Opne Xml Document";
            OFD.Filter = "Xml Document|*.xml";
            OFD.ShowDialog();
            string xmlPath = OFD.FileName;

            // convert template to document
            File.Copy(docPath, outFile);
            using (WordprocessingDocument doc = WordprocessingDocument.Open(outFile, true))
            {
                MainDocumentPart mdp = doc.MainDocumentPart;
                if (mdp.CustomXmlParts != null)
                {
                    mdp.DeleteParts <CustomXmlPart>(mdp.CustomXmlParts);
                }
                CustomXmlPart cxp = mdp.AddCustomXmlPart(CustomXmlPartType.CustomXml);
                FileStream    fs  = new FileStream(xmlPath, FileMode.Open);
                cxp.FeedData(fs);
                mdp.Document.Save();
            }
        }
コード例 #3
0
ファイル: WordDocument.cs プロジェクト: mrsunil/bp2
        public virtual void AddCustomXmlPart <T>(T metadata)
        {
            // AddCustomXmlPart
            // https://msdn.microsoft.com/en-us/library/office/bb456489.aspx?f=255&MSPPError=-2147217396
            using (MemoryStream memoryStream = new MemoryStream())
            {
                memoryStream.Write(_file, 0, _file.Length);
                using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(memoryStream, true))
                {
                    MainDocumentPart mainPart = wordDoc.MainDocumentPart;

                    // Try to find existing cutom XML part
                    CustomXmlPart myXmlPart = null;
                    foreach (var xmlPart in mainPart.CustomXmlParts)
                    {
                        var exists = false;
                        using (XmlTextReader xReader = new XmlTextReader(xmlPart.GetStream(FileMode.Open, FileAccess.Read)))
                        {
                            xReader.MoveToContent();
                            exists = xReader.NamespaceURI.Equals(AtlasNamespace, StringComparison.InvariantCultureIgnoreCase);
                        }

                        if (exists)
                        {
                            myXmlPart = xmlPart;
                            break;
                        }
                    }

                    // If there is no cutom XML part, we create it
                    if (myXmlPart == null)
                    {
                        myXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml, "atlas");
                    }

                    MemoryStream memoryStream2 = new MemoryStream();
                    {
                        StreamWriter stringwriter = new StreamWriter(memoryStream2);
                        {
                            XmlSerializer serializer = new XmlSerializer(metadata.GetType(), AtlasNamespace);
                            serializer.Serialize(stringwriter, metadata);
                            stringwriter.Flush();
                            memoryStream2.Seek(0, SeekOrigin.Begin);
                            myXmlPart.FeedData(memoryStream2);
                        }
                    }

                    memoryStream2.Seek(0, SeekOrigin.Begin);
                }

                memoryStream.Seek(0, SeekOrigin.Begin);
                _file = memoryStream.ToArray();
            }
        }
コード例 #4
0
        // To add a new document part to a package.
        public static void AddNewPart(string document, string fileName)
        {
            using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
            {
                MainDocumentPart mainPart  = wordDoc.MainDocumentPart;
                CustomXmlPart    myXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

                using (FileStream stream = new FileStream(fileName, FileMode.Open))
                {
                    myXmlPart.FeedData(stream);
                }
            }
        }
コード例 #5
0
        public async Task <byte[]> Convert <T>(T model) where T : IGeneratable
        {
            string templateSource = model.TemplateFile;
            string outputPath     = _docxConverterConfig.Value.OutputDirectory;
            string outputFile     = Path.Combine(
                outputPath,
                model.FileName);

            #region Option #1 - File.Copy()

            // Create a new output folder.
            // If directory already exists, this method does nothing
            Directory.CreateDirectory(outputPath);

            // Make a copy of templateSource then populate with new data (overwrite any existing file)
            File.Copy(templateSource, outputFile, true);

            using (WordprocessingDocument wordDoc =
                       WordprocessingDocument.Open(outputFile, true))
            {
                // Get the main part of the document which contains CustomXMLParts
                MainDocumentPart mainPart = wordDoc.MainDocumentPart;

                // Delete all CustomXMLParts in the document. If needed, only specific CustomXMLParts can be deleted using the CustomXmlParts IEnumerable
                mainPart.DeleteParts <CustomXmlPart>(mainPart.CustomXmlParts);

                // Add new CustomXMLPart with data from new XML
                CustomXmlPart myXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
                using (var stream = SerializeXml(model))
                {
                    myXmlPart.FeedData(stream);
                    wordDoc.Save();
                }
            }

            byte[] byteArray = File.ReadAllBytes(outputFile);

            // Delete the file on disk
            File.Delete(outputFile);

            return(byteArray);

            #endregion
        }
コード例 #6
0
 public static void GenerateDocument(string xmlData, string template, string output)
 {
     if (File.Exists(output))
     {
         File.Delete(output);
     }
     File.Copy(template, output);
     using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(output, true))
     {
         MainDocumentPart mainPart = wordDoc.MainDocumentPart;
         mainPart.DeleteParts(mainPart.CustomXmlParts);
         CustomXmlPart myXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
         using (FileStream stream = new FileStream(xmlData, FileMode.Open))
         {
             myXmlPart.FeedData(stream);
         }
     }
     MessageBox.Show("Document successfully generated.", "Complete");
 }
コード例 #7
0
        static void Main(string[] args)
        {
            string template = @"C:\Users\Christopher\Desktop\BookData\TestReportBeta.docx";
            string outFile  = @"C:\Users\Christopher\Desktop\BookData\TestReportBetaEND.docx";
            string xmlPath  = @"C:\Users\Christopher\Desktop\BookData\TestReport.xml";

            // convert template to document
            File.Copy(template, outFile);
            using (WordprocessingDocument doc = WordprocessingDocument.Open(outFile, true))
            {
                MainDocumentPart mdp = doc.MainDocumentPart;
                if (mdp.CustomXmlParts != null)
                {
                    mdp.DeleteParts <CustomXmlPart>(mdp.CustomXmlParts);
                }
                CustomXmlPart cxp = mdp.AddCustomXmlPart(CustomXmlPartType.CustomXml);
                FileStream    fs  = new FileStream(xmlPath, FileMode.Open);
                cxp.FeedData(fs);
                mdp.Document.Save();
            }
        }
コード例 #8
0
            static void GenerateDocument()
            {
                string rootPath         = @"C:\OfficeDocs";
                string xmlDataFile      = @"test.xml";
                string templateDocument = @"temp.docx";
                string outputDocument   = rootPath + @"\MyGeneratedDocument.docx";

                File.Copy(templateDocument, outputDocument);
                using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(outputDocument, true))
                {
                    //get the main part of the document which contains CustomXMLParts
                    MainDocumentPart mainPart = wordDoc.MainDocumentPart;

                    //delete all CustomXMLParts in the document. If needed only specific CustomXMLParts can be deleted using the CustomXmlParts IEnumerable
                    mainPart.DeleteParts <CustomXmlPart>(mainPart.CustomXmlParts);

                    //add new CustomXMLPart with data from new XML file
                    CustomXmlPart myXmlPart = mainPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
                    using (FileStream stream = new FileStream(xmlDataFile, FileMode.Open))
                    {
                        myXmlPart.FeedData(stream);
                    }
                }
            }