How to Generate and Convert Word Files with Spire.DocSpire.Doc is a powerful .NET library for creating, editing, converting and saving Word documents programmatically. Whether you need to generate reports, automate mail merges, convert documents to PDF, or export Word content to HTML or images, Spire.Doc offers a comprehensive API that works with .NET Framework, .NET Core, and .NET 5/6/7+. This article walks through core concepts, practical examples in C#, common workflows, best practices, and troubleshooting tips so you can start building document automation solutions quickly.
What Spire.Doc Does and When to Use It
Spire.Doc lets you:
- Create Word documents from scratch.
- Load existing Word files (.doc, .docx, .rtf, .xml).
- Edit content programmatically (text, formatting, tables, images, headers/footers).
- Perform mail merge operations.
- Convert Word documents to PDF, HTML, images (PNG/JPEG), or XPS.
- Extract text, metadata, and structured content.
Use Spire.Doc when you need server-side document generation/conversion, automated report creation, or bulk processing of Word documents without requiring Microsoft Office installed on the server.
Getting Started
Prerequisites:
- Visual Studio (or another C# IDE) and .NET SDK installed.
- A Spire.Doc license or the free version for small-scale/dev use.
- Add the Spire.Doc NuGet package to your project:
dotnet add package Spire.Doc
Or use the NuGet package manager in Visual Studio to install “Spire.Doc”.
Basic program structure:
- Create or load a Document object.
- Modify document elements (sections, paragraphs, tables).
- Save or convert using appropriate SaveToFile / SaveToStream methods.
Core Concepts and Objects
- Document: top-level object representing a Word file.
- Section: a document can contain multiple sections; each holds body content.
- Paragraph: unit of text within sections; supports runs and formatting.
- DocumentBuilder (or direct API methods): helps insert text, format, and add elements.
- Table, Row, Cell: for structured tabular content.
- Header/Footer: repeated content per page or section.
- Section Breaks and Page Setup: control paper size, margins, orientation.
Example 1 — Create a Simple Word Document (C#)
This minimal example builds a document with a title and a paragraph, then saves it as a .docx file.
using Spire.Doc; using Spire.Doc.Documents; using Spire.Doc.Fields; class Program { static void Main() { Document doc = new Document(); Section section = doc.AddSection(); // Title Paragraph title = section.AddParagraph(); TextRange titleText = title.AppendText("Monthly Sales Report"); titleText.CharacterFormat.Bold = true; title.Format.HorizontalAlignment = HorizontalAlignment.Center; title.ApplyStyle(BuiltinStyle.Title); // Body paragraph Paragraph para = section.AddParagraph(); para.AppendText("This report summarizes monthly sales performance across regions."); para.Format.FirstLineIndent = 20; // Save doc.SaveToFile("MonthlySalesReport.docx", FileFormat.Docx); } }
Example 2 — Load, Edit, and Save
Open an existing document, replace text, and save to a new format.
using Spire.Doc; var doc = new Document(); doc.LoadFromFile("Template.docx"); // Replace placeholder text doc.Replace("{{CustomerName}}", "Acme Corp", true, true); // Add a footer foreach (Section section in doc.Sections) { var footer = section.HeadersFooters.Footer; var p = footer.AddParagraph(); p.AppendText("Confidential — For internal use only."); } // Save as DOCX doc.SaveToFile("Updated_Template.docx", FileFormat.Docx);
Example 3 — Mail Merge
Automate personalized document creation using data from a DataTable, CSV, or database.
using Spire.Doc; using System.Data; Document doc = new Document(); doc.LoadFromFile("InvoiceTemplate.docx"); // Example DataTable DataTable table = new DataTable(); table.Columns.Add("InvoiceNo"); table.Columns.Add("Customer"); table.Rows.Add("INV-1001", "Acme Corp"); table.Rows.Add("INV-1002", "Beta LLC"); // Perform mail merge and save each result foreach (DataRow row in table.Rows) { Document tmp = doc.Clone(); tmp.MailMerge.MergeGroup(row); tmp.SaveToFile($"Invoice_{row["InvoiceNo"]}.docx", FileFormat.Docx); }
Note: Spire.Doc supports merging with various data sources (DataTable, DataSet, arrays, custom objects).
Example 4 — Convert Word to PDF and Images
Converting Word documents to PDF or images is common for archiving, sharing, or displaying in browsers.
Convert to PDF:
using Spire.Doc; var doc = new Document(); doc.LoadFromFile("Report.docx"); doc.SaveToFile("Report.pdf", FileFormat.PDF);
Convert to Images (one image per page):
using Spire.Doc; using System.Drawing.Imaging; var doc = new Document(); doc.LoadFromFile("Report.docx"); // Export each page as PNG for (int i = 0; i < doc.PageCount; i++) { var image = doc.SaveToImages(i, Spire.Doc.Documents.ImageType.Bitmap); image.Save($"Report_Page_{i+1}.png", ImageFormat.Png); }
Important: For high-fidelity PDF conversions, test fonts and embedded images to ensure output matches expectations.
Example 5 — Export to HTML
Save Word content as HTML for web display or email templates.
using Spire.Doc; var doc = new Document(); doc.LoadFromFile("Brochure.docx"); doc.SaveToFile("Brochure.html", FileFormat.Html);
You can control image extraction and CSS generation by post-processing the HTML output.
Working with Tables, Images, and Styles
- Tables: create with doc.Sections[i].AddTable(), set borders, merge cells, and control widths.
- Images: insert via paragraph.AppendPicture(Image) or use document.ImportImage, then position and scale.
- Styles: apply built-in styles (Heading1, Normal) or create custom styles via doc.AddStyle.
Short table example:
Table table = section.AddTable(true); table.ResetCells(3, 3); table.Rows[0].IsHeader = true; table.Rows[0].Height = 20; table[0,0].AddParagraph().AppendText("Product"); table[0,1].AddParagraph().AppendText("Qty"); table[0,2].AddParagraph().AppendText("Price");
Best Practices
- Reuse a Document object for batch operations rather than repeatedly creating heavy objects.
- Dispose images and streams to avoid memory leaks.
- Test conversions with the same fonts available on target servers—embed or substitute fonts if needed.
- Use the latest Spire.Doc version for bug fixes and improved compatibility.
- For high-volume server processing, measure memory and CPU usage; consider queuing and throttling conversions.
Common Pitfalls & Troubleshooting
- Missing fonts: Output PDF/HTML may render differently if fonts on the server differ. Embed fonts where possible.
- Large images: Resize before inserting or convert images to optimized formats to reduce document size.
- Table layout differences: Complex Word layouts may not translate perfectly to HTML or images—simplify templates when possible.
- Licensing: The free/unlicensed version may add evaluation watermarks or have feature limits—use a paid license for production.
Performance Tips
- Use streams (MemoryStream) instead of disk when converting in web apps to minimize I/O.
- Batch conversions during off-peak hours or use a worker queue for large jobs.
- Keep templates simple; complex nested tables and floating elements increase processing time.
When to Choose Alternatives
Spire.Doc is excellent for many use cases, but consider alternatives if:
- You require full Microsoft Word fidelity including macro execution (consider automation on Windows with Word installed).
- You need open-source solutions exclusively (consider Open XML SDK for .docx manipulation, though it’s lower-level).
- You need cross-platform GUI-based editing by end users (use client-side editors).
Comparison (short):
Task | Spire.Doc | Open XML SDK | Word Automation |
---|---|---|---|
Create/modify DOCX programmatically | Good, high-level API | Low-level, more code | Good but requires Office installed |
Convert to PDF | Built-in | Requires additional libraries | High fidelity but server issues |
Mail merge | Built-in | Manual implementation | Supported via Word |
Sample Real-World Workflows
-
Invoice generation service:
- Template with merge fields → Mail merge per customer → Save as PDF → Email attachment.
-
Report archiving pipeline:
- Generate quarterly reports → Convert to PDF and images → Store in object storage with metadata.
-
Document ingestion:
- Load uploaded .docx → Extract text and images → Index content for search → Convert to HTML for preview.
Licensing and Deployment Notes
Spire.Doc has commercial and free editions. The free version is useful for development and small tasks but may add watermarks or limit features. Purchase the appropriate license for production deployment. Confirm licensing terms for server-side use and redistribution.
Conclusion
Spire.Doc provides an accessible, high-level API for generating, editing, and converting Word documents in .NET applications without requiring Microsoft Word. With support for mail merge, format conversions (PDF, HTML, images), and rich content operations (tables, images, styles), it’s a practical choice for server-side document automation. Start with templates, test conversions against your server fonts, and follow the performance tips above to build robust document workflows.
Leave a Reply