Hi,
Please suggest me how to edit and save word document content using .net.
And also save word document data to pdf.
Thanks,
Ajay
Hi,
Please suggest me how to edit and save word document content using .net.
And also save word document data to pdf.
Thanks,
Ajay
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jay PankhaniyaRank #798 · 2.9K reputationPosted May 8, 2023, 9:36 AM EDT
Over here i have shared example from the code which i have in my windows form application.
using Word = Microsoft.Office.Interop.Word; private void btnEditDocument_Click(object sender, EventArgs e) { Word.Application wordApp = new Word.Application(); Word.Document doc = wordApp.Documents.Open(@"C:\Path\To\Document.docx"); Word.Range range = doc.Content; range.Text = "Hello, World!"; doc.Save(); doc.Close(); wordApp.Quit(); }Vishal YelveRank #178 · 13.6K reputationPosted May 10, 2023, 7:08 AM EDT
Hi Ajay,
Use DocumentFormat.OpenXml
To edit word document
using (WordprocessingDocument doc = WordprocessingDocument.Create ("C:\\Users\\Administrator\\Desktop\\Test\\WordTest.docx", DocumentFormat.OpenXml.WordprocessingDocumentType.Document)) { // Add a main document part. MainDocumentPart mainPart = doc.AddMainDocumentPart(); // Create the document structure and add some text. mainPart.Document = new Document(); Body body = mainPart.Document.AppendChild(new Body()); Paragraph para = body.AppendChild(new Paragraph()); Run run = para.AppendChild(new Run()); // String msg contains the text, "Hello, Word!" run.AppendChild(new Text("New text in document")); }To Save word file to pdf use Microsoft.Office.Interop.Word
protected void SaveAsPDF_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.Application(); Document document = null; application.Visible = false; // open docx file document = application.Documents.Open("C:\\Users\\Administrator\\Desktop\\Test\\WordTest.docx"); // convert to pdf file document.ExportAsFixedFormat("C:\\Users\\Administrator\\Desktop\\Test\\WordTest.pdf", WdExportFormat.wdExportFormatPDF); document.Close(); }Leon DRank #851 · 2.6K reputationPosted May 10, 2023, 11:35 PM EDT
Hi Ajay,
You can use Spire.Doc to edit Word document as well as convert it to PDF.
https://www.e-iceblue.com/Introduce/word-for-net-introduce.html
Code example for creating a Word document:
https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/Create-Write-and-Save-Word-in-C-and-VB.NET.html
using Spire.Doc; using Spire.Doc.Documents; using System.Drawing; namespace CreateWordDocument { class Program { static void Main(string[] args) { //Create a Document object Document doc = new Document(); //Add a section Section section = doc.AddSection(); //Set the page margins section.PageSetup.Margins.All = 40f; //Add a paragraph as title Paragraph titleParagraph = section.AddParagraph(); titleParagraph.AppendText("Introduction of Spire.Doc for .NET"); //Add two paragraphs as body Paragraph bodyParagraph_1 = section.AddParagraph(); bodyParagraph_1.AppendText("Spire.Doc for .NET is a professional Word.NET library specifically designed " + "for developers to create, read, write, convert, compare and print Word documents on any.NET platform " + "(.NET Framework, .NET Core, .NET Standard, Xamarin & Mono Android) with fast and high-quality performance."); Paragraph bodyParagraph_2 = section.AddParagraph(); bodyParagraph_2.AppendText("As an independent Word .NET API, Spire.Doc for .NET doesn't need Microsoft Word to " + "be installed on neither the development nor target systems. However, it can incorporate Microsoft Word " + "document creation capabilities into any developers' .NET applications."); //Create a style for title paragraph ParagraphStyle style1 = new ParagraphStyle(doc); style1.Name = "titleStyle"; style1.CharacterFormat.Bold = true; style1.CharacterFormat.TextColor = Color.Purple; style1.CharacterFormat.FontName = "Times New Roman"; style1.CharacterFormat.FontSize = 12; doc.Styles.Add(style1); titleParagraph.ApplyStyle("titleStyle"); //Create a style for body paragraphs ParagraphStyle style2 = new ParagraphStyle(doc); style2.Name = "paraStyle"; style2.CharacterFormat.FontName = "Times New Roman"; style2.CharacterFormat.FontSize = 12; doc.Styles.Add(style2); bodyParagraph_1.ApplyStyle("paraStyle"); bodyParagraph_2.ApplyStyle("paraStyle"); //Set the horizontal alignment of paragraphs titleParagraph.Format.HorizontalAlignment = HorizontalAlignment.Center; bodyParagraph_1.Format.HorizontalAlignment = HorizontalAlignment.Justify; bodyParagraph_2.Format.HorizontalAlignment = HorizontalAlignment.Justify; //Set the first line indent bodyParagraph_1.Format.FirstLineIndent = 30; bodyParagraph_2.Format.FirstLineIndent = 30; //Set the after spacing titleParagraph.Format.AfterSpacing = 10; bodyParagraph_1.Format.AfterSpacing = 10; //Save to file doc.SaveToFile("WordDocument.docx", FileFormat.Docx2013); } } }Code example for converting a Word document to PDF:
https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/How-to-Convert-Word-to-PDF.html
using Spire.Doc; namespace ToPDF { class Program { static void Main(string[] args) { //Create a Document object Document document = new Document(); //Load a sample Word document document.LoadFromFile(@"C:\Users\Administrator\Desktop\Test.docx"); //Save the document to PDF document.SaveToFile("ToPDF.pdf", FileFormat.PDF); } } }