Пример #1
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();
            }
        }
Пример #2
0
        private static string CreateCustomXmlPart(MainDocumentPart mainDocumentPart, XElement rootElement)
        {
            CustomXmlPart customXmlPart = mainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

            customXmlPart.PutXDocument(new XDocument(rootElement));
            return(CreateCustomXmlPropertiesPart(customXmlPart));
        }
Пример #3
0
        /// <summary>
        /// 创建用户自定义部件
        /// </summary>
        /// <param name="mainDocumentPart"></param>
        /// <param name="xdoc"></param>
        /// <returns></returns>
        public static CustomXmlPart CreateCustomXmlPart(this MainDocumentPart mainDocumentPart, XDocument xdoc)
        {
            CustomXmlPart xmlPart = mainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);

            xmlPart.WriteXml(xdoc);
            return(xmlPart);
        }
 public void AddCustomXMLPart()
 {
     using (WordprocessingDocument CBContract = WordprocessingDocument.Open(strFULLPATH_CB_AGREEMENT, true))
     {
         MainDocumentPart CBContractMain = CBContract.MainDocumentPart;
         CBContractMain.AddCustomXmlPart(CustomXmlPartType.CustomXml);
         // Have to provide XML or face an error.
         CBContractMain.Document.Save();
     }
 }
Пример #5
0
        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();
            }
        }
Пример #6
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);
                }
            }
        }
 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));
 }
Пример #8
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
        }
Пример #9
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");
 }
        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();
            }
        }
Пример #11
0
        /// <summary>
        /// Adds the custom XML part.
        /// </summary>
        /// <param name="mainDocumentPart">The main part.</param>
        /// <param name="rootElementName">Name of the root element.</param>
        /// <returns>
        /// Returns CustomXmlPart
        /// </returns>
        public CustomXmlPart AddCustomXmlPart(MainDocumentPart mainDocumentPart, string rootElementName)
        {
            if (mainDocumentPart == null)
            {
                throw new ArgumentNullException("mainDocumentPart");
            }

            if (string.IsNullOrEmpty(rootElementName))
            {
                throw new ArgumentNullException("rootElementName");
            }

            XName                   rootElementXName        = XName.Get(rootElementName, this.namespaceUri);
            XElement                rootElement             = new XElement(rootElementXName);
            CustomXmlPart           customXmlPart           = mainDocumentPart.AddCustomXmlPart(CustomXmlPartType.CustomXml);
            CustomXmlPropertiesPart customXmlPropertiesPart = customXmlPart.AddNewPart <CustomXmlPropertiesPart>();

            GenerateCustomXmlPropertiesPartContent(customXmlPropertiesPart);
            WriteElementToCustomXmlPart(customXmlPart, rootElement);

            return(customXmlPart);
        }
Пример #12
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);
                    }
                }
            }