/// <summary> /// Insert OLE Object into the Word document /// </summary> /// <returns>Return the created Word document as stream</returns> public MemoryStream InsertOLEObject(string documentType) { //Data folder path is resolved from requested page physical path. string basePath = _hostingEnvironment.WebRootPath; FileStream fileStream; WordDocument oleSource; if (documentType == "DOC") { fileStream = new FileStream(basePath + @"/DocIO/OleTemplate.doc", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //Open an existing word document oleSource = new WordDocument(fileStream, FormatType.Doc); } else { fileStream = new FileStream(basePath + @"/DocIO/OleTemplate.docx", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); //Open an existing word document oleSource = new WordDocument(fileStream, FormatType.Docx); } fileStream.Dispose(); fileStream = null; WordDocument dest = new WordDocument(); dest.EnsureMinimal(); // Get OLE object from source document WOleObject oleObject = oleSource.LastParagraph.Items[0] as WOleObject; WPicture pic = oleObject.OlePicture.Clone() as WPicture; dest.LastParagraph.AppendText("OLE Object Demo"); dest.LastParagraph.ApplyStyle(BuiltinStyle.Heading1); dest.LastParagraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; dest.Sections[0].AddParagraph(); dest.LastParagraph.AppendText("Adobe PDF object Inserted"); dest.LastParagraph.ApplyStyle(BuiltinStyle.Heading2); dest.Sections[0].AddParagraph(); //AppendOLE object to the destination document dest.LastParagraph.AppendOleObject(oleObject.Container, pic, OleLinkType.Embed); FormatType formatType = FormatType.Docx; //Save as .doc format if (documentType == "WordDoc") { formatType = FormatType.Doc; } //Save as .xml format else if (documentType == "WordML") { formatType = FormatType.WordML; } //Save the document as a stream and retrun the stream using (MemoryStream stream = new MemoryStream()) { //Save the created Word document to MemoryStream dest.Save(stream, formatType); dest.Close(); stream.Position = 0; return(stream); } }
public ActionResult InsertOLEObject(string Group1) { if (Group1 == null) { return(View()); } //Data folder path is resolved from requested page physical path. string dataPath = ResolveApplicationDataPath("", "App_Data\\DocIO"); WordDocument oleSource; if (Group1 == ".doc") { //Open an existing word document oleSource = new WordDocument(Path.Combine(dataPath, "OleTemplate.doc")); } else { //Open an existing word document oleSource = new WordDocument(Path.Combine(dataPath, "OleTemplate.docx")); } WordDocument dest = new WordDocument(); dest.EnsureMinimal(); // Get OLE object from source document WOleObject oleObject = oleSource.LastParagraph.Items[0] as WOleObject; WPicture pic = oleObject.OlePicture.Clone() as WPicture; dest.LastParagraph.AppendText("OLE Object Demo"); dest.LastParagraph.ApplyStyle(BuiltinStyle.Heading1); dest.LastParagraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; dest.Sections[0].AddParagraph(); dest.LastParagraph.AppendText("Adobe PDF object Inserted"); dest.LastParagraph.ApplyStyle(BuiltinStyle.Heading2); dest.Sections[0].AddParagraph(); // AppendOLE object to the destination document dest.LastParagraph.AppendOleObject(oleObject.Container, pic, OleLinkType.Embed); //Save as .doc format if (Group1 == "WordDoc") { return(dest.ExportAsActionResult("Sample.doc", FormatType.Doc, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } //Save as .docx format else if (Group1 == "WordDocx") { return(dest.ExportAsActionResult("Sample.docx", FormatType.Docx, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } // Save as WordML(.xml) format else if (Group1 == "WordML") { return(dest.ExportAsActionResult("Sample.xml", FormatType.WordML, HttpContext.ApplicationInstance.Response, HttpContentDisposition.Attachment)); } return(View()); }
protected void Button1_Click(object sender, EventArgs e) { //Data folder path is resolved from requested page physical path. string dataPath = new System.IO.DirectoryInfo(Request.PhysicalPath + "..\\..\\..\\App_Data\\DocIO\\").FullName; //Open an existing word document WordDocument oleSource; if (rdButtonDoc.Checked) { oleSource = new WordDocument(Path.Combine(dataPath, "OleTemplate.doc")); } else { oleSource = new WordDocument(Path.Combine(dataPath, "OleTemplate.docx")); } WordDocument dest = new WordDocument(); dest.EnsureMinimal(); // Get OLE object from source document WOleObject oleObject = oleSource.LastParagraph.Items[0] as WOleObject; WPicture pic = oleObject.OlePicture.Clone() as WPicture; dest.LastParagraph.AppendText("OLE Object Demo"); dest.LastParagraph.ApplyStyle(BuiltinStyle.Heading1); dest.LastParagraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; dest.Sections[0].AddParagraph(); dest.LastParagraph.AppendText("Adobe PDF object Inserted"); dest.LastParagraph.ApplyStyle(BuiltinStyle.Heading2); dest.Sections[0].AddParagraph(); // AppendOLE object to the destination document dest.LastParagraph.AppendOleObject(oleObject.Container, pic, OleLinkType.Embed); if (rdButtonDoc.Checked) { //Save as .doc format dest.Save("Sample.doc", FormatType.Doc, Response, HttpContentDisposition.Attachment); } //Save as .docx format else if (rdButtonDocx.Checked) { try { dest.Save("Sample.docx", FormatType.Docx, Response, HttpContentDisposition.Attachment); } catch (Win32Exception ex) { Response.Write("Microsoft Word Viewer or Microsoft Word is not installed in this system"); Console.WriteLine(ex.ToString()); } } //Save as WordML(.xml) format else if (rdButtonWordML.Checked) { try { dest.Save("Sample.xml", FormatType.WordML, Response, HttpContentDisposition.Attachment); } catch (Win32Exception ex) { Response.Write("Microsoft Word Viewer or Microsoft Word is not installed in this system"); Console.WriteLine(ex.ToString()); } } }
private void button1_Click(object sender, EventArgs e) { try { #if NETCORE string TemplatePath = @"..\..\..\..\..\..\..\common\Data\DocIO\"; #else string TemplatePath = @"..\..\..\..\..\..\common\Data\DocIO\"; #endif WordDocument dest = new WordDocument(); dest.EnsureMinimal(); //Set Margin of the section dest.Sections[0].PageSetup.Margins.All = 72; WordDocument oleSource; if (wordDocRadioBtn.Checked) { oleSource = new WordDocument(TemplatePath + "OleTemplate.doc"); } else { oleSource = new WordDocument(TemplatePath + "OleTemplate.docx"); } WOleObject oleObject = null; // Get OLE object from source document for (int i = 0; i < oleSource.LastSection.Paragraphs[4].Items.Count; i++) { if (oleSource.LastSection.Paragraphs[4].Items[i] is WOleObject) { oleObject = oleSource.LastSection.Paragraphs[4].Items[i] as WOleObject; break; } } WPicture pic = oleObject.OlePicture.Clone() as WPicture; dest.LastParagraph.AppendText("OLE Object Demo"); dest.LastParagraph.ApplyStyle(BuiltinStyle.Heading1); dest.LastParagraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; dest.Sections[0].AddParagraph(); dest.LastParagraph.AppendText("MS Excel Object Inserted"); dest.LastParagraph.ApplyStyle(BuiltinStyle.Heading2); dest.Sections[0].AddParagraph(); // AppendOLE object to the destination document oleObject = dest.LastParagraph.AppendOleObject(oleObject.Container, pic, OleLinkType.Embed); oleObject.DisplayAsIcon = checkBoxChoose.Checked; //Save as doc format if (wordDocRadioBtn.Checked) { //Saving the document to disk. dest.Save("Sample.doc"); //Message box confirmation to view the created document. if (MessageBoxAdv.Show("Do you want to view the generated Word document?", "Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer] #if NETCORE System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.doc") { UseShellExecute = true }; process.Start(); #else System.Diagnostics.Process.Start("Sample.doc"); #endif //Exit this.Close(); } } //Save as docx format else if (wordDocxRadioBtn.Checked) { //Saving the document as .docx dest.Save("Sample.docx", FormatType.Docx); //Message box confirmation to view the created document. if (MessageBoxAdv.Show("Do you want to view the generated Word document?", "Document has been created", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes) { try { //Launching the MS Word file using the default Application.[MS Word Or Free WordViewer] #if NETCORE System.Diagnostics.Process process = new System.Diagnostics.Process(); process.StartInfo = new System.Diagnostics.ProcessStartInfo("Sample.docx") { UseShellExecute = true }; process.Start(); #else System.Diagnostics.Process.Start("Sample.docx"); #endif //Exit this.Close(); } catch (Win32Exception ex) { MessageBoxAdv.Show("Microsoft Word Viewer or Microsoft Word is not installed in this system"); Console.WriteLine(ex.ToString()); } } } else { // Exit this.Close(); } } catch (Exception Ex) { MessageBox.Show(Ex.Message); } }
public ActionResult InsertOLEObject(string Group1) { if (Group1 == null) { return(View()); } //Data folder path is resolved from requested page physical path. string basePath = _hostingEnvironment.WebRootPath; string dataPath = basePath + @"/DocIO/OleTemplate.doc"; FileStream fileStream = new FileStream(dataPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); WordDocument oleSource; if (Group1 == ".doc") { //Open an existing word document oleSource = new WordDocument(fileStream, FormatType.Doc); } else { //Open an existing word document oleSource = new WordDocument(fileStream, FormatType.Doc); } fileStream.Dispose(); fileStream = null; WordDocument dest = new WordDocument(); dest.EnsureMinimal(); // Get OLE object from source document WOleObject oleObject = oleSource.LastParagraph.Items[0] as WOleObject; WPicture pic = oleObject.OlePicture.Clone() as WPicture; dest.LastParagraph.AppendText("OLE Object Demo"); dest.LastParagraph.ApplyStyle(BuiltinStyle.Heading1); dest.LastParagraph.ParagraphFormat.HorizontalAlignment = Syncfusion.DocIO.DLS.HorizontalAlignment.Center; dest.Sections[0].AddParagraph(); dest.LastParagraph.AppendText("Adobe PDF object Inserted"); dest.LastParagraph.ApplyStyle(BuiltinStyle.Heading2); dest.Sections[0].AddParagraph(); // AppendOLE object to the destination document dest.LastParagraph.AppendOleObject(oleObject.Container, pic, OleLinkType.Embed); FormatType type = FormatType.Docx; string filename = "Sample.docx"; string contenttype = "application/vnd.ms-word.document.12"; #region Document SaveOption //Save as .doc format if (Group1 == "WordDoc") { type = FormatType.Doc; filename = "Sample.doc"; contenttype = "application/msword"; } //Save as .xml format else if (Group1 == "WordML") { type = FormatType.WordML; filename = "Sample.xml"; contenttype = "application/msword"; } #endregion Document SaveOption MemoryStream ms = new MemoryStream(); dest.Save(ms, type); dest.Close(); ms.Position = 0; return(File(ms, contenttype, filename)); }