<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jen</title>
    <description>The latest articles on DEV Community by Jen (@jenll).</description>
    <link>https://dev.to/jenll</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3826848%2Fc566bd52-364f-45f1-b4ca-91b1ef8efd21.png</url>
      <title>DEV Community: Jen</title>
      <link>https://dev.to/jenll</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jenll"/>
    <language>en</language>
    <item>
      <title>Best .NET PDF Libraries in 2026: Free vs Paid (What Actually Works)</title>
      <dc:creator>Jen</dc:creator>
      <pubDate>Mon, 27 Apr 2026 03:38:54 +0000</pubDate>
      <link>https://dev.to/jenll/best-net-pdf-libraries-in-2026-free-vs-paid-what-actually-works-5127</link>
      <guid>https://dev.to/jenll/best-net-pdf-libraries-in-2026-free-vs-paid-what-actually-works-5127</guid>
      <description>&lt;p&gt;It usually starts with something simple:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"We just need to export a PDF."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sounds straightforward. Until it isn't.&lt;/p&gt;

&lt;p&gt;At first, everything seems fine. You pick a library, write a few lines of code, and get a PDF output. Done.&lt;/p&gt;

&lt;p&gt;Then the real problems begin.&lt;/p&gt;

&lt;p&gt;Not in development — but the moment you deploy.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The layout looks completely different from your HTML.&lt;/li&gt;
&lt;li&gt;Fonts are missing or rendering incorrectly.&lt;/li&gt;
&lt;li&gt;It works on your machine… but fails in Docker or Linux.&lt;/li&gt;
&lt;li&gt;Performance drops when processing larger files.&lt;/li&gt;
&lt;li&gt;And suddenly, licensing becomes a concern you didn't plan for.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What felt like a quick task turns into a series of unexpected trade-offs.&lt;/p&gt;

&lt;p&gt;The truth is, choosing a PDF library in .NET isn't about features — it's about what actually works in production.&lt;/p&gt;

&lt;p&gt;In this article, we'll take a practical look at some of the most popular .NET PDF libraries in 2026, compare free and paid options, and — more importantly — help you figure out which one actually fits your use case.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Real Problem — Why Choosing a PDF Library Is Harder Than It Looks
&lt;/h2&gt;

&lt;p&gt;On the surface, most PDF libraries look similar: create documents, add text, insert images, maybe even support HTML rendering.&lt;/p&gt;

&lt;p&gt;But in practice, the differences start to matter very quickly.&lt;/p&gt;

&lt;p&gt;The challenge isn't generating a PDF — it's generating the &lt;em&gt;right&lt;/em&gt; PDF, under real-world conditions.&lt;/p&gt;

&lt;p&gt;Here are a few trade-offs developers often run into:&lt;/p&gt;

&lt;h3&gt;
  
  
  Feature Complexity vs. Simplicity
&lt;/h3&gt;

&lt;p&gt;Some libraries give you full control over every element — layout, positioning, rendering.&lt;br&gt;
That's powerful, but it also means more code, more setup, and a steeper learning curve.&lt;/p&gt;

&lt;p&gt;Others are much easier to use, but become limiting once your requirements grow beyond simple documents.&lt;/p&gt;

&lt;h3&gt;
  
  
  HTML Rendering vs. Manual Layout
&lt;/h3&gt;

&lt;p&gt;If your PDFs are based on existing HTML (invoices, reports, web pages), HTML-to-PDF support becomes critical.&lt;/p&gt;

&lt;p&gt;But not all libraries handle this well.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some don't support HTML at all.&lt;/li&gt;
&lt;li&gt;Some only support partial or inconsistent rendering.&lt;/li&gt;
&lt;li&gt;Others rely on embedded browsers, which introduces performance and deployment considerations.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Local Development vs. Production Environments
&lt;/h3&gt;

&lt;p&gt;This is where a lot of library choices fall apart in practice. &lt;/p&gt;

&lt;p&gt;Common issues include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Missing system dependencies in &lt;a href="https://learn.microsoft.com/en-us/dotnet/core/docker/introduction" rel="noopener noreferrer"&gt;Linux or Docker&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Font rendering differences.&lt;/li&gt;
&lt;li&gt;Headless environment limitations.&lt;/li&gt;
&lt;li&gt;Unexpected crashes under load.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc3mgrzv9l4i0z8d9nhw0.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc3mgrzv9l4i0z8d9nhw0.webp" alt="Local Development vs. Server Environments" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Free vs. Paid — More Than Just Cost
&lt;/h3&gt;

&lt;p&gt;At first glance, the decision seems simple: use a free library if possible.&lt;/p&gt;

&lt;p&gt;But "free" often comes with trade-offs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limited features.&lt;/li&gt;
&lt;li&gt;More time spent on implementation.&lt;/li&gt;
&lt;li&gt;Licensing restrictions (especially for commercial use).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the other hand, paid libraries usually offer more complete solutions — but introduce cost and vendor dependency.&lt;/p&gt;

&lt;p&gt;All of this leads to one important conclusion:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There is no universally "best" PDF library in .NET — only the one that best fits your specific use case.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Free vs Paid — What's the Actual Difference?
&lt;/h2&gt;

&lt;p&gt;At some point, almost every developer asks the same question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Can I just use a free library?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The answer is: &lt;strong&gt;yes — but it depends on what you need.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The difference between free and paid PDF libraries isn't just about cost. It's about &lt;strong&gt;where you spend your effort&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  🟢 Free / Open-Source Libraries
&lt;/h3&gt;

&lt;p&gt;Free libraries are often the first choice — and for good reason.&lt;/p&gt;

&lt;p&gt;They're accessible, flexible, and backed by active communities.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What they do well:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No upfront cost.&lt;/li&gt;
&lt;li&gt;Full control over implementation.&lt;/li&gt;
&lt;li&gt;Suitable for simple or well-defined use cases.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Where they get challenging:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Limited or no HTML-to-PDF support.&lt;/li&gt;
&lt;li&gt;More manual work (layout, pagination, styling).&lt;/li&gt;
&lt;li&gt;Inconsistent behavior across environments.&lt;/li&gt;
&lt;li&gt;Licensing constraints (e.g., AGPL in some cases).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In practice, they work best when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You're generating structured documents (reports, invoices).&lt;/li&gt;
&lt;li&gt;You don't rely heavily on HTML rendering.&lt;/li&gt;
&lt;li&gt;You're okay investing more development time.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🔵 Paid / Commercial Libraries
&lt;/h3&gt;

&lt;p&gt;Commercial libraries take a different approach: they aim to &lt;strong&gt;solve the entire problem out of the box&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Instead of building everything yourself, you get a more complete toolkit.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What they do well:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reliable HTML-to-PDF rendering.&lt;/li&gt;
&lt;li&gt;Better handling of fonts, layouts, and pagination.&lt;/li&gt;
&lt;li&gt;Consistent behavior across environments (Windows, Linux, Docker).&lt;/li&gt;
&lt;li&gt;Dedicated support and documentation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Trade-offs to consider:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Licensing cost.&lt;/li&gt;
&lt;li&gt;Vendor lock-in.&lt;/li&gt;
&lt;li&gt;Sometimes heavier dependencies.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They're typically a better fit when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need production-ready stability.&lt;/li&gt;
&lt;li&gt;You're building backend services or automation pipelines.&lt;/li&gt;
&lt;li&gt;Time-to-market matters more than implementation flexibility.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The Real Trade-Off
&lt;/h3&gt;

&lt;p&gt;This is where most comparisons miss the point.&lt;/p&gt;

&lt;p&gt;It's not:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Free vs Paid&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It's actually:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Time vs Money&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In most real-world projects, you don't really choose a library — you choose your trade-offs.&lt;/p&gt;

&lt;p&gt;Free libraries often save money upfront, but require more engineering effort.&lt;/p&gt;

&lt;p&gt;Paid libraries cost more initially, but can significantly reduce development time and risk.&lt;/p&gt;

&lt;p&gt;If you're building something quick or highly customized, a free library might be enough.&lt;/p&gt;

&lt;p&gt;But if you're building something that needs to &lt;strong&gt;run reliably in production&lt;/strong&gt;, the decision becomes less about cost — and more about stability.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqevsboalyybu2fuxjkje.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqevsboalyybu2fuxjkje.png" alt="Free vs Paid — What's the Actual Difference" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Library Breakdown — What Actually Works
&lt;/h2&gt;

&lt;p&gt;Enough context. Let's get into the libraries themselves.&lt;/p&gt;

&lt;p&gt;Before we dive into specific libraries, one important note: what follows isn't a rehash of each library's feature page. It's an honest assessment of where each one shines, where it struggles, and what kind of project it's actually suited for.&lt;/p&gt;

&lt;h3&gt;
  
  
  🟢 Open-Source Options
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;QuestPDF&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.questpdf.com/" rel="noopener noreferrer"&gt;QuestPDF&lt;/a&gt; is probably the most exciting thing to happen to .NET PDF generation in recent years. It takes a code-first, Fluent API approach — you describe your document structure in C#, and it renders it precisely and consistently.&lt;/p&gt;

&lt;p&gt;The results are clean, the API is genuinely pleasant to work with, and it handles things like pagination and repeating headers without the usual pain. For structured documents — invoices, financial reports, delivery notes — it's hard to beat &lt;a href="https://github.com/QuestPDF/QuestPDF" rel="noopener noreferrer"&gt;at any price&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The limitation is equally clear: &lt;strong&gt;QuestPDF doesn't do HTML rendering.&lt;/strong&gt; If your documents are driven by Razor templates or existing HTML/CSS, this isn't your tool. You'll be rebuilding your layout in C# from scratch, which is a significant investment depending on complexity.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;👍 Beautiful Fluent API, precise layout control, great for structured docs.&lt;/li&gt;
&lt;li&gt;👎 No HTML support, requires code-based layout for everything.&lt;/li&gt;
&lt;li&gt;✅ Best for: Invoices, reports, any document with a predictable structure.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;PDFsharp&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.pdfsharp.com/" rel="noopener noreferrer"&gt;PDFsharp&lt;/a&gt; is the "old reliable" of the .NET open-source PDF world. It's been around for years, it's lightweight, and it does what it says.&lt;/p&gt;

&lt;p&gt;That said, it's showing its age. The API is lower-level than modern alternatives, documentation is sparse, and complex layout work requires a lot of manual effort. It's not a library you'd choose for a serious production system in 2026 — but for simple one-off PDFs or quick prototypes, it gets the job done without ceremony.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;👍 Lightweight, no frills, easy to get started.&lt;/li&gt;
&lt;li&gt;👎 Low-level API, limited features, not actively developed.&lt;/li&gt;
&lt;li&gt;✅ Best for: Simple PDF generation, quick prototypes, low-complexity needs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;iText 7 (AGPL)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://itextpdf.com/" rel="noopener noreferrer"&gt;iText&lt;/a&gt; is the most powerful open-source PDF library in the .NET ecosystem — and also the most misunderstood.&lt;/p&gt;

&lt;p&gt;The functionality is exceptional. Fine-grained control over PDF structure, solid form handling, digital signatures, PDF/A compliance — if you need to do something with a PDF, iText can probably do it. The API has a learning curve, but it rewards the investment.&lt;/p&gt;

&lt;p&gt;The real issue is the license. iText 7 is released under &lt;a href="https://itextpdf.com/how-buy/AGPLv3-license" rel="noopener noreferrer"&gt;AGPL&lt;/a&gt;, which requires any software using it to also be open-sourced under AGPL. For commercial applications, that's almost certainly not acceptable — which means you'd need their commercial license, at which point it's no longer a "free" option.&lt;/p&gt;

&lt;p&gt;Use it for open-source projects freely. For everything else, price out the commercial license before you build anything on top of it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;👍 Extremely powerful, precise PDF control, handles complex requirements.&lt;/li&gt;
&lt;li&gt;👎 AGPL license is a serious constraint for commercial projects.&lt;/li&gt;
&lt;li&gt;✅ Best for: Open-source projects, or commercial projects with budget for the paid license.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🔵 Commercial Options
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;IronPDF&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If your primary use case is converting HTML to PDF, &lt;a href="https://ironpdf.com/" rel="noopener noreferrer"&gt;IronPDF&lt;/a&gt; is hard to ignore. It's &lt;a href="https://ironpdf.com/features/" rel="noopener noreferrer"&gt;built on Chromium&lt;/a&gt;, which means what you get out is very close to what a real browser would render — CSS, web fonts, JavaScript-rendered content, all of it.&lt;/p&gt;

&lt;p&gt;For teams that maintain their document templates as HTML and want pixel-accurate output, that's a genuinely compelling proposition.&lt;/p&gt;

&lt;p&gt;The tradeoff is resource usage. Chromium is not a lightweight dependency. Cold start times are noticeable, memory footprint is higher than other options, and in high-throughput scenarios you'll need to think carefully about how you manage instances. It's not a dealbreaker, but it's something to test against your actual load before committing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;👍 Best-in-class HTML rendering, near-perfect browser fidelity.&lt;/li&gt;
&lt;li&gt;👎 High resource usage, slower cold starts, heavier infrastructure footprint.&lt;/li&gt;
&lt;li&gt;✅ Best for: HTML-heavy documents, web page to PDF conversion, template-driven output.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Aspose.PDF&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://products.aspose.com/pdf/net/" rel="noopener noreferrer"&gt;Aspose&lt;/a&gt; is the enterprise choice — and it earns that label in both directions.&lt;/p&gt;

&lt;p&gt;The feature set is genuinely comprehensive: PDF generation, editing, conversion to and from dozens of formats, OCR, digital signatures, PDF/A and PDF/UA compliance, redaction. If you have a complex document processing requirement, Aspose almost certainly supports it. The library is mature, well-maintained, and the documentation is thorough.&lt;/p&gt;

&lt;p&gt;The cost reflects that. Aspose is among the most expensive options in this space, and the API can feel heavy for simpler use cases. If you're building a straightforward report generator, it's more library than you need.&lt;/p&gt;

&lt;p&gt;But for large-scale systems where document processing is a core part of the product — legal platforms, financial systems, document management pipelines — the investment tends to pay off. You're not going to hit a wall with Aspose.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;👍 Most complete feature set, enterprise-grade reliability, extensive format support.&lt;/li&gt;
&lt;li&gt;👎 Highest price point, API complexity, more than most projects need.&lt;/li&gt;
&lt;li&gt;✅ Best for: Enterprise systems, complex document workflows, organizations with serious compliance requirements.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Spire.PDF&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html" rel="noopener noreferrer"&gt;Spire.PDF&lt;/a&gt; sits in an interesting position: it's not the flashiest option, and it doesn't get talked about as much as IronPDF or Aspose — but in practice, it quietly handles a lot of real-world scenarios well.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.e-iceblue.com/Tutorials/Spire.PDF/Spire.PDF-Program-Guide/Convert-HTML-to-PDF-Customize-HTML-to-PDF-Conversion-by-Yourself.html" rel="noopener noreferrer"&gt;HTML to PDF conversion&lt;/a&gt; is stable and doesn't require a full browser engine. The API is straightforward without being simplistic. It runs cleanly on Linux and inside Docker containers without needing special configuration — which is exactly where some browser-based solutions start to show friction, usually at the worst possible time: during deployment.&lt;/p&gt;

&lt;p&gt;It won't win any design awards for its API, but for backend automation — generating documents server-side, processing PDFs in bulk, integrating into pipelines — it's a practical, reliable choice that won't surprise you in production.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;👍 Stable HTML to PDF, clean Linux/Docker support, straightforward API.&lt;/li&gt;
&lt;li&gt;👎 Less community presence, not as well known as larger alternatives.&lt;/li&gt;
&lt;li&gt;✅ Best for: Server-side automation, backend document generation, Docker-based deployments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc50zxnv64wlelccsqo4g.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc50zxnv64wlelccsqo4g.png" alt="Library Breakdown — What Actually Works" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparison Table
&lt;/h2&gt;

&lt;p&gt;If you just want the quick answer, here's how they compare:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Library&lt;/th&gt;
&lt;th&gt;HTML to PDF&lt;/th&gt;
&lt;th&gt;License&lt;/th&gt;
&lt;th&gt;Ease&lt;/th&gt;
&lt;th&gt;Performance&lt;/th&gt;
&lt;th&gt;Deployment&lt;/th&gt;
&lt;th&gt;Best For&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;QuestPDF&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;MIT&lt;/td&gt;
&lt;td&gt;⭐⭐⭐⭐&lt;/td&gt;
&lt;td&gt;⭐⭐⭐⭐&lt;/td&gt;
&lt;td&gt;⭐⭐⭐⭐&lt;/td&gt;
&lt;td&gt;Structured reports, invoices&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PDFsharp&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;MIT&lt;/td&gt;
&lt;td&gt;⭐⭐⭐&lt;/td&gt;
&lt;td&gt;⭐⭐⭐&lt;/td&gt;
&lt;td&gt;⭐⭐⭐⭐&lt;/td&gt;
&lt;td&gt;Simple PDF generation&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;iText&lt;/td&gt;
&lt;td&gt;⚠️ Partial&lt;/td&gt;
&lt;td&gt;AGPL / Paid&lt;/td&gt;
&lt;td&gt;⭐⭐&lt;/td&gt;
&lt;td&gt;⭐⭐⭐⭐&lt;/td&gt;
&lt;td&gt;⭐⭐⭐&lt;/td&gt;
&lt;td&gt;Advanced PDF control&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;IronPDF&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;Commercial&lt;/td&gt;
&lt;td&gt;⭐⭐⭐⭐&lt;/td&gt;
&lt;td&gt;⭐⭐⭐&lt;/td&gt;
&lt;td&gt;⭐⭐&lt;/td&gt;
&lt;td&gt;HTML to PDF (web content)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Aspose&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;Commercial&lt;/td&gt;
&lt;td&gt;⭐⭐⭐&lt;/td&gt;
&lt;td&gt;⭐⭐⭐⭐&lt;/td&gt;
&lt;td&gt;⭐⭐⭐⭐&lt;/td&gt;
&lt;td&gt;Enterprise-level solutions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Spire.PDF&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;Commercial&lt;/td&gt;
&lt;td&gt;⭐⭐⭐⭐&lt;/td&gt;
&lt;td&gt;⭐⭐⭐⭐&lt;/td&gt;
&lt;td&gt;⭐⭐⭐⭐&lt;/td&gt;
&lt;td&gt;Automation, backend services&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;A few notes on the table:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;HTML to PDF&lt;/strong&gt; — ⚠️ on iText means partial support exists but isn't a core strength. Don't build an HTML-driven workflow around it without testing first. ⚠️ on IronPDF for Linux/Docker means it works, but requires more setup than other options — Chromium dependencies need to be explicitly handled in your container.&lt;/p&gt;

&lt;h2&gt;
  
  
  So… Which One Should You Choose?
&lt;/h2&gt;

&lt;p&gt;If you don’t want to overthink it, here's the short version:&lt;/p&gt;

&lt;h3&gt;
  
  
  👉 You need to generate structured documents — invoices, reports, statements
&lt;/h3&gt;

&lt;p&gt;Go with &lt;strong&gt;QuestPDF&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Clean API&lt;/li&gt;
&lt;li&gt;Great layout control&lt;/li&gt;
&lt;li&gt;No need for HTML&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  👉 Your documents are HTML-based and you need accurate rendering
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Go with &lt;strong&gt;IronPDF&lt;/strong&gt; if output fidelity is the priority and you can absorb the resource overhead.&lt;/li&gt;
&lt;li&gt;If you're running in a lightweight backend environment and need something more predictable, &lt;strong&gt;Spire.PDF&lt;/strong&gt; is worth evaluating — the rendering isn't browser-identical, but it's consistent and far less demanding on infrastructure.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  👉 You need complete control over PDF internals — signatures, compliance, low-level manipulation
&lt;/h3&gt;

&lt;p&gt;Go with &lt;strong&gt;iText&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Powerful but complex&lt;/li&gt;
&lt;li&gt;Best for advanced scenarios&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  👉 You're building backend automation — bulk generation, scheduled jobs, document pipelines
&lt;/h3&gt;

&lt;p&gt;Go with a &lt;strong&gt;commercial library (Spire, IronPDF, Aspose)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;More stable across environments&lt;/li&gt;
&lt;li&gt;Less time spent debugging edge cases&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  👉 You just need something simple and fast
&lt;/h3&gt;

&lt;p&gt;Go with &lt;strong&gt;PDFsharp&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Lightweight&lt;/li&gt;
&lt;li&gt;Easy to set up&lt;/li&gt;
&lt;li&gt;Good enough for basic tasks&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Real-World Advice — What the Docs Won't Tell You
&lt;/h2&gt;

&lt;p&gt;By this point, the differences between libraries should be clearer. But in practice, a few factors matter more than anything listed in a feature comparison.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Don't choose based on features alone
&lt;/h3&gt;

&lt;p&gt;Most libraries &lt;em&gt;claim&lt;/em&gt; to support similar things.&lt;br&gt;
The real difference shows up when you run them in production.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Test in your actual environment early
&lt;/h3&gt;

&lt;p&gt;Especially if you're deploying to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Linux&lt;/li&gt;
&lt;li&gt;Docker&lt;/li&gt;
&lt;li&gt;Cloud environments&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What works locally may not work there.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Pay attention to licensing
&lt;/h3&gt;

&lt;p&gt;This is often overlooked - until it becomes a problem.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some "free" libraries have strict licenses (like AGPL)&lt;/li&gt;
&lt;li&gt;Commercial use may require purchasing a license&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Start with a small proof of concept (POC)
&lt;/h3&gt;

&lt;p&gt;Before committing to a library, try:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Rendering a real document&lt;/li&gt;
&lt;li&gt;Testing performance&lt;/li&gt;
&lt;li&gt;Checking output consistency&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A few hours of testing can save days of refactoring later.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. Think long-term, not just quick wins
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The wrong library usually doesn't fail immediately — it fails when you scale.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffvls96nyfi9dmwntynlp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffvls96nyfi9dmwntynlp.png" alt="Real-World Advice — What the Docs Won't Tell You" width="800" height="1067"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Closing Thoughts
&lt;/h2&gt;

&lt;p&gt;There's no perfect PDF library for .NET. There's only the right one for what you're actually building.&lt;/p&gt;

&lt;p&gt;If this article has a single takeaway, it's this: &lt;strong&gt;start with your constraints, not the feature list.&lt;/strong&gt; Your deployment environment, your license requirements, your document structure, your team's capacity — those filters will do most of the work for you. By the time you've answered those questions honestly, the shortlist usually writes itself.&lt;/p&gt;

&lt;p&gt;If you're working on document generation or automation in .NET, taking the time to choose the right library early can save you a lot of trouble later.&lt;/p&gt;

&lt;p&gt;And in the end, the best PDF library isn't the most powerful one — it's the one that keeps working when everything else starts to break.&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>productivity</category>
      <category>pdf</category>
    </item>
    <item>
      <title>Why Microsoft Office Interop Fails for PDF Generation in .NET (And What to Use Instead)</title>
      <dc:creator>Jen</dc:creator>
      <pubDate>Tue, 21 Apr 2026 01:59:09 +0000</pubDate>
      <link>https://dev.to/jenll/why-microsoft-office-interop-fails-for-pdf-generation-in-net-and-what-to-use-instead-5bc6</link>
      <guid>https://dev.to/jenll/why-microsoft-office-interop-fails-for-pdf-generation-in-net-and-what-to-use-instead-5bc6</guid>
      <description>&lt;p&gt;Microsoft Office Interop is still widely used for PDF generation in .NET because it feels like a quick win. It works on a local machine, requires minimal code, and produces accurate results by leveraging Microsoft Office itself.&lt;/p&gt;

&lt;p&gt;The problems start in production. Applications begin to hang, background processes like &lt;code&gt;WINWORD.EXE&lt;/code&gt; accumulate, and stability degrades under load—not because of application logic, but because Interop was built for desktop automation rather than server-side workloads.&lt;/p&gt;

&lt;p&gt;Built for desktop automation, Interop depends on a GUI environment and system states that don’t exist in modern backend architectures. This article explains why it fails—and what to use instead.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is Microsoft Office Interop (And Why It's So Common)
&lt;/h2&gt;

&lt;p&gt;Microsoft Office Interop is a set of .NET assemblies that allow applications to automate Microsoft Office programs such as Word and Excel. In the context of PDF generation, it is often used to open a document and export it directly to PDF using the built-in capabilities of Office.&lt;/p&gt;

&lt;p&gt;Its popularity comes from a few practical advantages. First, it feels familiar—developers are effectively controlling tools they already know. Second, the API surface is relatively straightforward, making it easy to get a working solution with minimal code. And because the output is generated by Microsoft Office itself, the formatting accuracy is typically high.&lt;/p&gt;

&lt;p&gt;This combination makes Interop especially appealing for quick implementations or internal tools. However, the same characteristics that make it convenient during development can become limitations once the application is deployed to a production environment.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Interop Fails in Production
&lt;/h2&gt;

&lt;p&gt;The issues with Microsoft Office Interop are not incidental—they are a direct result of its design.&lt;/p&gt;

&lt;p&gt;The core problems fall into a few categories:&lt;/p&gt;

&lt;h3&gt;
  
  
  Fundamentally Not Designed for Server Environments
&lt;/h3&gt;

&lt;p&gt;Interop doesn't just use Office — it &lt;em&gt;is&lt;/em&gt; Office. Every API call drives a full desktop application running in the background. &lt;a href="https://support.microsoft.com/en-us/topic/considerations-for-server-side-automation-of-office-48bcfe93-8a89-47f1-0bce-017433ad79e2" rel="noopener noreferrer"&gt;Microsoft's own documentation explicitly states&lt;/a&gt; that Office is not designed for unattended server-side execution and is unsupported in that context.&lt;/p&gt;

&lt;p&gt;Server environments don't have desktop sessions, logged-in users, or displays. Interop assumes all three. When those assumptions break, dialog boxes appear with no one to dismiss them, file pickers block indefinitely, and processes stall waiting for input that never comes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Requires Microsoft Office Installation (Hard Dependency)
&lt;/h3&gt;

&lt;p&gt;Every machine that runs your code needs a licensed Office installation — your web server, your build agent, your Docker container. In practice this means two problems: you can't containerize cleanly, and version inconsistencies between Office 2019 and Microsoft 365 mean the same document can render differently across environments.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stability Issues (COM Lifecycle Complexity)
&lt;/h3&gt;

&lt;p&gt;COM objects don't clean themselves up. Every &lt;code&gt;Document&lt;/code&gt; and &lt;code&gt;Application&lt;/code&gt; object needs explicit &lt;code&gt;Marshal.ReleaseComObject()&lt;/code&gt; calls. Miss one, and the Office process keeps running after your code exits.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;finally&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReleaseComObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Quit&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReleaseComObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;GC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Collect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;GC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WaitForPendingFinalizers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even this pattern isn't bulletproof. Under certain error conditions the process still doesn't exit, and orphaned &lt;code&gt;WINWORD.EXE&lt;/code&gt; instances accumulate quietly until the server runs out of memory. Interop is also not thread-safe — in a multi-threaded web server, race conditions are a matter of when, not if.&lt;/p&gt;

&lt;h3&gt;
  
  
  Poor Scalability (Process-Per-Request Model)
&lt;/h3&gt;

&lt;p&gt;Each conversion request starts a new Office instance. Under any real load, this means multiple full Office processes running simultaneously, each consuming hundreds of megabytes. Batch processing is no better — failures become silent and unpredictable once you're iterating over large file sets.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Interop works fine for demos — but breaks under real production workloads.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What a Modern PDF Solution Should Provide
&lt;/h2&gt;

&lt;p&gt;A production-ready PDF solution should meet the expectations of modern backend applications:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No Office dependency&lt;/strong&gt; — a NuGet package, nothing more&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server environment support&lt;/strong&gt; — ASP.NET Core, Docker, Linux, no display required&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Stability under load&lt;/strong&gt; — bounded memory, no process leaks, thread-safe&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Clean .NET API&lt;/strong&gt; — idiomatic C#, no COM artifacts&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Streaming and batch support&lt;/strong&gt; — &lt;code&gt;MemoryStream&lt;/code&gt; for web APIs, stable iteration for batch jobs&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These requirements reflect how applications are built and deployed today—and highlight why Interop struggles in these scenarios.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Landscape: Standalone .NET PDF Libraries
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Library&lt;/th&gt;
&lt;th&gt;Strengths&lt;/th&gt;
&lt;th&gt;Limitations&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;&lt;a href="https://itextpdf.com/how-buy/legal/agpl-gnu-affero-general-public-license" rel="noopener noreferrer"&gt;iText 7&lt;/a&gt;&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Feature-complete, large community&lt;/td&gt;
&lt;td&gt;AGPL — commercial use requires paid license&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;PdfSharp + MigraDoc&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Fully open source&lt;/td&gt;
&lt;td&gt;Weak Word conversion support&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;QuestPDF&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Elegant fluent API for building documents&lt;/td&gt;
&lt;td&gt;Weak support for converting existing Word/Excel files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Spire.Doc / Spire.PDF for .NET&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;No Office dependency, reliable conversion, cross-platform&lt;/td&gt;
&lt;td&gt;Free tier has page limit&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;strong&gt;Puppeteer&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;Flexible HTML→PDF&lt;/td&gt;
&lt;td&gt;Heavy runtime (browser dependency)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Different tools fit different scenarios. For generating PDFs from scratch, libraries like &lt;a href="https://www.questpdf.com" rel="noopener noreferrer"&gt;QuestPDF&lt;/a&gt; offer a clean developer experience. For HTML-based workflows, headless browsers provide flexibility.&lt;/p&gt;

&lt;p&gt;However, when the requirement is &lt;strong&gt;reliable server-side conversion of existing Word or Excel documents&lt;/strong&gt;, the priorities change: no Office dependency, consistent behavior across environments, and stable performance under load. This is where standalone libraries designed specifically for backend processing, like Spire.Doc and Spire.PDF for .NET, become the most practical choice. In practice, different libraries handle different parts of the workflow—for example, Spire.Doc for &lt;a href="https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/How-to-Convert-Word-to-PDF.html" rel="noopener noreferrer"&gt;Word-to-PDF conversion&lt;/a&gt; and Spire.PDF for &lt;a href="https://www.e-iceblue.com/Tutorials/NET/Spire.PDF-for-.NET/Program-Guide/Document-Operation/asp-net-create-pdf.html" rel="noopener noreferrer"&gt;creating or manipulating PDF documents&lt;/a&gt; directly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhg1tvz5xu5hynvu1jnqy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhg1tvz5xu5hynvu1jnqy.png" alt="Spire.PDF for .NET" width="800" height="424"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Practical Examples: Interop vs a Modern Approach
&lt;/h2&gt;

&lt;p&gt;These limitations become obvious when you compare Interop with modern libraries in real-world scenarios.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scenario A: Creating a PDF Report from Scratch
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;While Interop isn't typically used for creating PDFs from scratch, this comparison highlights how different the programming models are.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ❌ Using Interop
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Microsoft&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Office&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Interop&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Application&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Documents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;para&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Paragraphs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;para&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"Quarterly Report"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveAs2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outputPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;WdSaveFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wdFormatPDF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Quit&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReleaseComObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReleaseComObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even for a simple document, you're launching a full Office instance, managing COM object lifecycles manually, and hoping nothing throws before the cleanup code runs.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This approach is error-prone and difficult to maintain in backend code.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  ✅ Using Spire.PDF
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;PdfDocument&lt;/span&gt; &lt;span class="n"&gt;pdf&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;PdfDocument&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;PdfPageBase&lt;/span&gt; &lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;PdfTrueTypeFont&lt;/span&gt; &lt;span class="n"&gt;font&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;PdfTrueTypeFont&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Font&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Arial"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;14f&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="n"&gt;PdfBrush&lt;/span&gt; &lt;span class="n"&gt;brush&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PdfBrushes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Black&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;DrawString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Quarterly Report"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;font&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;brush&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"report.pdf"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Creating PDFs becomes a straightforward, in-process operation.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Scenario B: Converting Existing Documents to PDF
&lt;/h3&gt;

&lt;p&gt;This is where Interop causes the most production pain. The typical implementation looks like this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;❌ Using Interop&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Application&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Visible&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Documents&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inputPath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveAs2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outputPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;WdSaveFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wdFormatPDF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Quit&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReleaseComObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;Marshal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ReleaseComObject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;word&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Locally, this works. In production, it requires Office installed, runs single-threaded, leaks processes under error conditions, and can lock files in ways that require manual cleanup on the server.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Using Spire.Doc&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"report.docx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"report.pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PDF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No Office. No COM cleanup. No process management. The same three lines work identically on Windows, Linux, and inside a Docker container.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F83p0ho44vb7rcit5zayi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F83p0ho44vb7rcit5zayi.png" alt="C#: Convert Word to PDF" width="800" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This is where most Interop-based solutions start to break.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Scenario C: Returning a PDF from an ASP.NET Core Endpoint
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;❌ Using Interop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each request spins up a new Office process. Concurrent requests interfere with each other. There's no clean way to write to a response stream — you're saving to disk and reading it back. Process cleanup in an async context is unreliable.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;✅ Using Spire.Doc with MemoryStream&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;HttpGet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"report"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IActionResult&lt;/span&gt; &lt;span class="nf"&gt;GetReport&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"template.docx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MemoryStream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PDF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Position&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"report.pdf"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The conversion (handled by Spire.Doc) happens entirely in memory. No temp files, no disk I/O, no process lifecycle to manage. This pattern scales horizontally without any changes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw3nlgp1de8np13mmc9hg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw3nlgp1de8np13mmc9hg.png" alt="Spire.Doc for .NET" width="800" height="407"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;This is where standalone libraries truly outperform Interop.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Interop vs Modern Libraries: A Quick Comparison
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;Interop&lt;/th&gt;
&lt;th&gt;Modern Library&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Requires Office&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server / Docker Support&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Thread Safety&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Stability Under Load&lt;/td&gt;
&lt;td&gt;🔴 Low&lt;/td&gt;
&lt;td&gt;🟢 High&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Deployment Complexity&lt;/td&gt;
&lt;td&gt;🔴 High&lt;/td&gt;
&lt;td&gt;🟢 Low&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  When You Might Still Use Interop
&lt;/h2&gt;

&lt;p&gt;Interop isn't universally wrong. It's a reasonable choice for local desktop automation tools, single-user scripts, and scenarios where Office is already present and concurrency isn't a concern. If you need to manipulate macros or preserve highly complex Office-specific formatting, it may still be the only option.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Interop isn't wrong — it's just used in the wrong context.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Conclusion: Move Beyond Interop
&lt;/h2&gt;

&lt;p&gt;Microsoft Office Interop remains convenient for quick solutions, but its reliance on desktop components, lack of scalability, and instability under load make it unsuitable for modern backend systems.&lt;/p&gt;

&lt;p&gt;Today's .NET systems are built to run in cloud environments, containers, and stateless services—where dependencies on GUI-based software simply don't fit. Instead of working around these constraints, it's more effective to adopt tools designed for server-side document processing from the ground up. Standalone libraries—such as Spire.Doc and &lt;a href="https://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html" rel="noopener noreferrer"&gt;Spire.PDF&lt;/a&gt; for .NET—exist precisely to address these challenges.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>productivity</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Highlight Duplicates in Excel: A Developer-Friendly Guide</title>
      <dc:creator>Jen</dc:creator>
      <pubDate>Mon, 13 Apr 2026 09:42:53 +0000</pubDate>
      <link>https://dev.to/jenll/how-to-highlight-duplicates-in-excel-a-developer-friendly-guide-57j8</link>
      <guid>https://dev.to/jenll/how-to-highlight-duplicates-in-excel-a-developer-friendly-guide-57j8</guid>
      <description>&lt;p&gt;If you've ever received a spreadsheet from a product manager and been asked to "just check for duplicates," you know the feeling — it sounds trivial, but the moment you open the file, you realize what you're dealing with: 10,000 rows, multiple columns, no clear definition of what “duplicate” actually means in this context.&lt;/p&gt;

&lt;p&gt;Duplicate detection is one of those tasks that sits at the intersection of Excel power users and backend developers. It's too manual to ignore, yet too tightly coupled to a .xlsx file to fit cleanly into your usual data tooling.&lt;/p&gt;

&lt;p&gt;In this guide, we'll walk through a set of approaches that scale with your needs—from quick UI tricks to automation and developer-friendly solutions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;What Counts as "Duplicates" in Excel?&lt;/li&gt;
&lt;li&gt;Method 1: Conditional Formatting (Built-in UI)&lt;/li&gt;
&lt;li&gt;Method 2: COUNTIF Formula-Driven Conditional Formatting&lt;/li&gt;
&lt;li&gt;Method 3: VBA Macro — Client-Side Automation&lt;/li&gt;
&lt;li&gt;Method 4: Power Query &amp;amp; Dynamic Arrays&lt;/li&gt;
&lt;li&gt;Method 5: Highlight Duplicates in Excel Using C# (Server-Side Automation)&lt;/li&gt;
&lt;li&gt;Method Comparison at a Glance&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What Counts as "Duplicates" in Excel?
&lt;/h2&gt;

&lt;p&gt;Before jumping into solutions, it's worth clarifying what we actually mean by "duplicates," because the definition can vary depending on the scenario.&lt;/p&gt;

&lt;h3&gt;
  
  
  Different Types of Duplicates
&lt;/h3&gt;

&lt;p&gt;In most real-world cases, duplicates fall into one of three categories:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Single-column duplicates&lt;/strong&gt;: the same value appears more than once in a column, such as repeated email addresses&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Full-row duplicates&lt;/strong&gt;: every cell in two or more rows is identical. &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Composite duplicates&lt;/strong&gt;: a combination of columns forms a duplicate key, even if no individual column is duplicated on its own. For example, the same '(user_id, date)' pair appearing twice in a transaction log.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The distinction matters, because Excel’s built-in tools don’t always handle these cases the same way.&lt;/p&gt;

&lt;h3&gt;
  
  
  Common Gotchas
&lt;/h3&gt;

&lt;p&gt;Even when the logic seems straightforward, a few subtle issues can lead to misleading results.&lt;/p&gt;

&lt;p&gt;For example, Excel is &lt;strong&gt;not case-sensitive by default&lt;/strong&gt;, which means &lt;code&gt;Apple&lt;/code&gt; and &lt;code&gt;apple&lt;/code&gt; are treated as the same value. That's convenient in many cases, but it can also hide inconsistencies.&lt;/p&gt;

&lt;p&gt;Hidden characters are another common culprit. A value like &lt;code&gt;"123"&lt;/code&gt; is not the same as &lt;code&gt;"123 "&lt;/code&gt; if there's a trailing space—something that's easy to miss visually.&lt;/p&gt;

&lt;p&gt;You'll also run into &lt;strong&gt;type mismatches&lt;/strong&gt;, where &lt;code&gt;"123"&lt;/code&gt; (text) and &lt;code&gt;123&lt;/code&gt; (number) are treated differently. And yes, even Excel isn’t immune to floating-point quirks—calculations like &lt;code&gt;0.1 + 0.2&lt;/code&gt; may not always behave exactly as expected.&lt;/p&gt;

&lt;p&gt;Taken together, these edge cases are why “finding duplicates” is often more nuanced than it appears.&lt;/p&gt;

&lt;p&gt;&lt;a id="2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 1: Conditional Formatting (Built-in UI)
&lt;/h2&gt;

&lt;p&gt;The fastest way to highlight duplicates — no formulas, no code. Best for one-off checks where you just need to see the problem quickly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How to do it:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Select the column or range you want to check, then follow this path:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvft7mq23d416xy7789sh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvft7mq23d416xy7789sh.png" alt="Select the column or range" width="800" height="556"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://support.microsoft.com/en-us/office/use-conditional-formatting-to-highlight-information-in-excel-fed60dfa-1d3f-4e13-9ecb-f1951ff89d7f" rel="noopener noreferrer"&gt;Home → Conditional Formatting → Highlight Cell Rules → Duplicate Values&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4056qs5nryfan1rczntk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4056qs5nryfan1rczntk.png" alt="Highlight Cell Rules" width="800" height="590"&gt;&lt;/a&gt;&lt;br&gt;
A dialog appears letting you choose a highlight color. Click OK and Excel immediately colors every cell whose value appears more than once in your selection.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffuk6l1x5m0n2iuir8378.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffuk6l1x5m0n2iuir8378.png" alt="Duplicate Values" width="800" height="537"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;One behavior to know:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you select multiple columns at once, Excel checks each cell individually — not each row. So if column A has &lt;code&gt;alice&lt;/code&gt; twice and column B has &lt;code&gt;bob&lt;/code&gt; twice, both get highlighted, but a row where &lt;code&gt;(alice, bob)&lt;/code&gt; appears twice would only be caught if both cells independently repeat. If you need row-level or composite duplicate detection, skip ahead to Method 2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Customizing the format:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The default is a light red fill, but you can set any fill color, font color, or border style. In the same dialog, open the format dropdown and select "Custom Format" to access the full cell formatting options.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Limitations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No case sensitivity control — &lt;code&gt;Alice&lt;/code&gt; and &lt;code&gt;alice&lt;/code&gt; are treated as the same&lt;/li&gt;
&lt;li&gt;Dynamic ranges aren't tracked automatically — if you add rows later, you'll need to reapply or extend the rule manually&lt;/li&gt;
&lt;li&gt;Rules stack: if you have multiple conditional formatting rules on the same range, order matters. Manage them under Home → Conditional Formatting → Manage Rules&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Still, for quick, one-off checks, it's often the most efficient option.&lt;/p&gt;

&lt;p&gt;&lt;a id="3"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Method 2: COUNTIF Formula-Driven Conditional Formatting
&lt;/h2&gt;

&lt;p&gt;When the built-in UI isn't flexible enough — you need case sensitivity, composite keys, or more precise control over which occurrences get flagged — &lt;a href="https://support.microsoft.com/en-us/office/use-the-countif-function-in-microsoft-excel-e0de10c6-f885-4e71-abb4-1f464816df34" rel="noopener noreferrer"&gt;COUNTIF&lt;/a&gt; gives you that control without leaving Excel.&lt;/p&gt;

&lt;p&gt;The idea is simple: instead of letting Excel decide what counts as a duplicate, you write a formula that returns &lt;code&gt;TRUE&lt;/code&gt; or &lt;code&gt;FALSE&lt;/code&gt; for each cell, and conditional formatting applies the highlight based on your logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic setup:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Select your data range, for example &lt;code&gt;B2:B16&lt;/code&gt;, then create a new conditional formatting rule:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Home → Conditional Formatting → New Rule → "Use a formula to determine which cells to format."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjutxvyd19xmeauyzzbvw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjutxvyd19xmeauyzzbvw.png" alt="New Rule" width="800" height="632"&gt;&lt;/a&gt;&lt;br&gt;
Enter this formula:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=COUNTIF($B$2:$B$16, B2) &amp;gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy00yts3uvxjeycljqmij.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy00yts3uvxjeycljqmij.png" alt="Use a formula to determine which cells to format" width="800" height="665"&gt;&lt;/a&gt;&lt;br&gt;
This counts how many times the value in &lt;code&gt;B2&lt;/code&gt; appears across the entire range. If it appears more than once, the cell gets highlighted. The &lt;code&gt;$&lt;/code&gt; signs lock the range while letting the row reference shift as the rule applies to each cell.&lt;/p&gt;

&lt;p&gt;If you're familiar with SQL, this is the spreadsheet equivalent of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;table&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Highlight only the second occurrence onwards:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The basic formula flags every occurrence, including the first. If you want to mark only the duplicates — leaving the original untouched — use this instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=COUNTIF($B$2:B2, B2) &amp;gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key difference is &lt;code&gt;$B$2:B2&lt;/code&gt; — the start of the range is anchored but the end expands as the rule moves down each row. By the time Excel evaluates row 5, the range is &lt;code&gt;$B$2:B5&lt;/code&gt;, so it only counts occurrences up to the current row. The first appearance is never flagged; only repeats are.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Composite duplicate detection:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To check whether a combination of columns forms a duplicate — say &lt;code&gt;(email, signup_date)&lt;/code&gt; — concatenate the values into a single key using &lt;code&gt;&amp;amp;&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=COUNTIFS($A$2:$A$100, A2, $B$2:$B$100, B2) &amp;gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or with string concatenation across more columns:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=COUNTIF($A$2:$A$100 &amp;amp; $B$2:$B$100, A2 &amp;amp; B2) &amp;gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the closest you'll get to a composite primary key check inside native Excel.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Case-sensitive duplicate detection:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;COUNTIF is case-insensitive by default. For case-sensitive matching, use EXACT wrapped in SUMPRODUCT:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=SUMPRODUCT((EXACT($A$2:$A$100, A2)) * 1) &amp;gt; 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This iterates over every value in the range and checks for an exact match, including case, returning a count. More expensive on large datasets, but it works.&lt;/p&gt;

&lt;p&gt;&lt;a id="4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 3: VBA Macro — Client-Side Automation
&lt;/h2&gt;

&lt;p&gt;The three methods above all live inside Excel's UI. They work well for interactive use, but they're hard to trigger programmatically, difficult to version control, and impossible to run without opening Excel manually. If you need duplicate highlighting to happen automatically — on file open, on a button click, or as part of a repeatable workflow — VBA is the natural next step.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Basic implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The core logic is straightforward: iterate over a column, track how many times each value has appeared using a &lt;code&gt;Dictionary&lt;/code&gt;, then go back and color any cell whose value appeared more than once.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sub HighlightDuplicates()

    Dim ws As Worksheet
    Dim rng As Range
    Dim cell As Range
    Dim dict As Object
    Dim highlightColor As Long

    Set ws = ThisWorkbook.Sheets("Sheet1")
    Set rng = ws.Range("A2:A1000")
    highlightColor = RGB(255, 199, 206)

    Set dict = CreateObject("Scripting.Dictionary")

    For Each cell In rng
        If cell.Value &amp;lt;&amp;gt; "" Then
            Dim key As String
            key = Trim(CStr(cell.Value))
            If dict.exists(key) Then
                dict(key) = dict(key) + 1
            Else
                dict.Add key, 1
            End If
        End If
    Next cell

    For Each cell In rng
        If cell.Value &amp;lt;&amp;gt; "" Then
            If dict(CStr(cell.Value)) &amp;gt; 1 Then
                cell.Interior.Color = highlightColor
            Else
                cell.Interior.ColorIndex = xlNone
            End If
        End If
    Next cell

End Sub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first loop builds the frequency map. The second loop applies or removes the highlight based on the count. Splitting into two passes keeps the logic clean and makes it easy to add a "clear all highlights first" step before reapplying.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Making it reusable:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hard-coding the sheet name and range is fine for a one-off script, but if you plan to reuse this across files, parameterize the key variables:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Sub HighlightDuplicates(sheetName As String, columnRange As String, highlightColor As Long)
    Dim ws As Worksheet
    Dim rng As Range
    Set ws = ThisWorkbook.Sheets(sheetName)
    Set rng = ws.Range(columnRange)
    ' ... same logic as above
End Sub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Call it from elsewhere in your project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Call HighlightDuplicates("Sheet1", "A2:A1000", RGB(255, 199, 206))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Triggering automatically on file open:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To run the macro every time the workbook opens, add it to the &lt;code&gt;Workbook_Open&lt;/code&gt; event in &lt;code&gt;ThisWorkbook&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Private Sub Workbook_Open()
    Call HighlightDuplicates("Sheet1", "A2:A1000", RGB(255, 199, 206))
End Sub
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now every time someone opens the file, duplicates are flagged automatically — no manual steps required.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Performance on large datasets:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;VBA loops are slow by default because Excel recalculates and redraws after every change. For datasets over a few thousand rows, disable these before running and re-enable them after:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

' ... your loop here ...

Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On a 50,000-row dataset, this alone can reduce runtime from 30+ seconds to 2–3 seconds.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where VBA falls short:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;VBA runs inside Excel, which means it requires Excel to be installed and open. It can't be triggered from a CI pipeline, a backend service, or a scheduled task on a server. If your use case involves processing Excel files without a desktop environment, the next method is what you need.&lt;/p&gt;

&lt;p&gt;&lt;a id="5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 4: Power Query &amp;amp; Dynamic Arrays
&lt;/h2&gt;

&lt;p&gt;While the previous methods work well within Excel itself, they are harder to integrate into data pipelines or produce clean, structured outputs. If your goal is to output a deduplicated table, feed results into another process, or refresh the analysis automatically when data updates, Power Query and dynamic arrays are better tools.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dynamic arrays (Excel 365 only):&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you're on Microsoft 365, the &lt;code&gt;[UNIQUE()](https://support.microsoft.com/en-us/office/unique-function-c5ab87fd-30a3-4ce9-9d1a-40204fb85e1e)&lt;/code&gt; and &lt;code&gt;[FILTER()](https://support.microsoft.com/en-us/office/filter-function-f4f7cb66-82eb-4767-8f7c-4877ad80c759)&lt;/code&gt; functions let you extract duplicate values directly into a new range — no conditional formatting involved.&lt;/p&gt;

&lt;p&gt;To get a list of values that appear more than once:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=FILTER(A2:A100, COUNTIF(A2:A100, A2:A100) &amp;gt; 1)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;COUNTIF(A2:A100, A2:A100)&lt;/code&gt; returns an array of counts — one per row. &lt;code&gt;FILTER()&lt;/code&gt; then keeps only the rows where that count exceeds 1. The result spills automatically into adjacent cells.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: On very large datasets, this approach may become slow because COUNTIF is evaluated across the entire range for each element.&lt;/p&gt;

&lt;p&gt;To get just the unique values among those duplicates — without repetition:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;=UNIQUE(FILTER(A2:A100, COUNTIF(A2:A100, A2:A100) &amp;gt; 1))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is clean, readable, and updates live as your source data changes. The main constraint is that it requires Excel 365 — it won't work on Excel 2016 or 2019.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Power Query:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Power Query works on Excel 2016 and later, and is better suited for larger datasets or more complex transformations. The approach is to group rows by the columns you care about, count occurrences, then filter for groups with more than one row.&lt;/p&gt;

&lt;p&gt;To open Power Query:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Data → Get Data → From Table/Range&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Select your data range and click OK. In the Power Query editor:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Select the column you want to check for duplicates&lt;/li&gt;
&lt;li&gt;Go to &lt;strong&gt;Home → Group By&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Group by your target column, add a count column using &lt;strong&gt;Count Rows&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Click OK — you now have a summary table with each value and its occurrence count&lt;/li&gt;
&lt;li&gt;Filter the count column to show only rows where count &amp;gt; 1&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Click &lt;strong&gt;Close &amp;amp; Load&lt;/strong&gt; to output the results to a new sheet. Every time you refresh the query (Data → Refresh All), it re-runs against the latest data.&lt;/p&gt;

&lt;p&gt;For composite duplicate detection, select multiple columns before grouping — Power Query will treat the combination as a single key.&lt;/p&gt;

&lt;p&gt;👉 If you're familiar with SQL, you can think of this as a GROUP BY + HAVING COUNT &amp;gt; 1 workflow, but expressed through a visual interface.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to use which:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Dynamic arrays are ideal when you want a live formula that updates automatically and the data fits within a single worksheet. Power Query is the better choice when the dataset is large, the transformation logic is complex, or you need a repeatable process that non-technical users can trigger with a single Refresh click.&lt;/p&gt;

&lt;p&gt;Neither method requires code, but both offer a significant step up in capability — and both output clean data rather than just painting cells.&lt;/p&gt;

&lt;p&gt;&lt;a id="6"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Method 5: Highlight Duplicates in Excel Using C# (Server-Side Automation)
&lt;/h2&gt;

&lt;p&gt;Up to this point, all approaches still depend on Excel as the execution environment. That works for manual workflows, but starts to break down when you need to process files in a backend service, a CI/CD pipeline, or any environment where Microsoft Office isn't available.&lt;/p&gt;

&lt;p&gt;In these scenarios, using a standalone .NET library becomes the most practical approach. Instead of relying on Excel itself, you work directly with the file format in code — making the solution easier to automate, deploy, and scale.&lt;/p&gt;

&lt;p&gt;One option here is &lt;a href="https://www.e-iceblue.com/Introduce/excel-for-net-introduce.html" rel="noopener noreferrer"&gt;Spire.XLS&lt;/a&gt;, a .NET library that &lt;a href="https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Create-Excel-File-in-C-VB.NET.html" rel="noopener noreferrer"&gt;reads and writes Excel files independently of Office&lt;/a&gt;. No COM interop, no Excel process in the background, and no dependency on a desktop environment. You install it via NuGet, point it at a file, and manipulate it like any other object in your codebase.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3t9ff57p6j7h80kp2z8z.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3t9ff57p6j7h80kp2z8z.png" alt="Spire.XLS for .NET" width="800" height="526"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setup:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install the package via &lt;a href="https://www.nuget.org/packages/Spire.XLS" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet add package Spire.XLS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or via the NuGet Package Manager in Visual Studio:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Install-Package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Spire.XLS&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No further configuration needed. The library handles &lt;code&gt;.xlsx&lt;/code&gt; and &lt;code&gt;.xls&lt;/code&gt; formats out of the box.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Core object model:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Before writing the implementation, it helps to understand three objects you'll use throughout:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Workbook&lt;/code&gt; — represents the entire Excel file&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Worksheet&lt;/code&gt; — a single sheet within the workbook&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;CellRange&lt;/code&gt; — a cell or range of cells, with properties for value, style, and formatting&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Basic implementation:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The logic mirrors the VBA approach: build a frequency map, then iterate again to &lt;a href="https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Conditional-Formatting/Highlight-Duplicate-and-Unique-Values-in-Excel-Using-C.html" rel="noopener noreferrer"&gt;highlight duplicates&lt;/a&gt;. The difference is that this runs as a standalone C# program — no Excel required.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Xls&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Collections.Generic&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Drawing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Workbook&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Workbook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"data.xlsx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;Worksheet&lt;/span&gt; &lt;span class="n"&gt;sheet&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Worksheets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;startRow&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;endRow&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LastRow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;targetColumn&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// Build frequency map&lt;/span&gt;
        &lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;valueMap&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Dictionary&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;gt;();&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;startRow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;endRow&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;CellRange&lt;/span&gt; &lt;span class="n"&gt;cell&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;targetColumn&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
            &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;cell&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;Trim&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IsNullOrEmpty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(!&lt;/span&gt;&lt;span class="n"&gt;valueMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ContainsKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
                &lt;span class="n"&gt;valueMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;();&lt;/span&gt;

            &lt;span class="n"&gt;valueMap&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Apply highlighting to duplicate rows&lt;/span&gt;
        &lt;span class="n"&gt;Color&lt;/span&gt; &lt;span class="n"&gt;highlightColor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FromArgb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;255&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;199&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;206&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;valueMap&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                &lt;span class="p"&gt;{&lt;/span&gt;
                    &lt;span class="n"&gt;CellRange&lt;/span&gt; &lt;span class="n"&gt;cell&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;targetColumn&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
                    &lt;span class="n"&gt;cell&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Color&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;highlightColor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"data_highlighted.xlsx"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ExcelVersion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Version2013&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Done. Duplicates highlighted."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjw9gmlqnkjeulaoo5ru8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjw9gmlqnkjeulaoo5ru8.png" alt="Highlight Duplicates in Excel Using C#" width="800" height="569"&gt;&lt;/a&gt;&lt;br&gt;
The &lt;code&gt;Dictionary&amp;lt;string, List&amp;lt;int&amp;gt;&amp;gt;&lt;/code&gt; stores each unique value alongside the row numbers where it appears. After the first pass, any key with more than one row number in its list is a duplicate. The second pass iterates over those row numbers and applies the fill color.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Composite duplicate detection:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To check for duplicates across multiple columns — say columns 1 and 2 together — concatenate the cell values into a composite key:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;$"&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;Trim&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s"&gt;|&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Range&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nf"&gt;Trim&lt;/span&gt;&lt;span class="p"&gt;()}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;|&lt;/code&gt; separator prevents false matches where concatenating two different pairs could produce the same string. For example, &lt;code&gt;("ab", "c")&lt;/code&gt; and &lt;code&gt;("a", "bc")&lt;/code&gt; would both produce &lt;code&gt;"abc"&lt;/code&gt; without a separator, but &lt;code&gt;"ab|c"&lt;/code&gt; and &lt;code&gt;"a|bc"&lt;/code&gt; are distinct.&lt;/p&gt;

&lt;p&gt;Make sure the separator character does not appear in your actual data, or choose a more robust delimiter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Skipping the header row:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;startRow = 2&lt;/code&gt; in the implementation above already handles this — adjust the value to match however many header rows your file has.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Custom highlight colors:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Color.FromArgb(r, g, b)&lt;/code&gt; accepts any RGB value. To match the default Excel duplicate highlight:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="n"&gt;Color&lt;/span&gt; &lt;span class="n"&gt;highlightColor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FromArgb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;255&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;199&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;206&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// light red fill&lt;/span&gt;
&lt;span class="n"&gt;Color&lt;/span&gt; &lt;span class="n"&gt;fontColor&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FromArgb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;156&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;6&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;          &lt;span class="c1"&gt;// dark red text&lt;/span&gt;

&lt;span class="n"&gt;cell&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Color&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;highlightColor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;cell&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Style&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Font&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Color&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fontColor&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Integrating into a .NET service:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because this is plain C#, it slots naturally into any .NET application — an ASP.NET endpoint that processes uploaded files, a background worker that validates reports on a schedule, or a console tool invoked from a CI pipeline:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Example GitHub Actions step&lt;/span&gt;
&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;Validate Excel report&lt;/span&gt;
  &lt;span class="na"&gt;run&lt;/span&gt;&lt;span class="pi"&gt;:&lt;/span&gt; &lt;span class="s"&gt;dotnet run --project ./ExcelValidator -- --file reports/output.xlsx&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The process exits with a non-zero code if duplicates are found, which causes the pipeline step to fail — giving you automatic duplicate detection as a CI gate.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Spire.XLS vs. VBA — when to use which:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;VBA is the right tool when the work stays on a desktop, the file is opened interactively, and the person running it has Excel. Spire.XLS is the right tool when the file is processed on a server, the environment has no Office installation, or you need the logic to be testable, version-controlled, and integrated into a larger codebase.&lt;/p&gt;

&lt;p&gt;&lt;a id="7"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Method Comparison at a Glance
&lt;/h2&gt;

&lt;p&gt;Five methods is a lot to hold in your head. Here's a summary to help you pick the right one for your situation:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Method&lt;/th&gt;
&lt;th&gt;Requires coding&lt;/th&gt;
&lt;th&gt;Needs Office&lt;/th&gt;
&lt;th&gt;Automation&lt;/th&gt;
&lt;th&gt;Best for&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Conditional Formatting&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Manual&lt;/td&gt;
&lt;td&gt;Quick one-off visual check&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;COUNTIF formula&lt;/td&gt;
&lt;td&gt;No (formula)&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Semi-auto&lt;/td&gt;
&lt;td&gt;Flexible logic, stays in Excel&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Power Query / Dynamic arrays&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes (365)&lt;/td&gt;
&lt;td&gt;Auto-refresh&lt;/td&gt;
&lt;td&gt;Periodic reports, clean output&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;VBA macro&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Full auto&lt;/td&gt;
&lt;td&gt;Desktop automation, event-driven&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;C# Spire.XLS&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Full auto&lt;/td&gt;
&lt;td&gt;Server-side, CI/CD, batch processing&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For quick decision-making, here's a simple rule of thumb:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spot-checking a file someone sent you → &lt;strong&gt;Conditional Formatting&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Logic is complex but you're staying in Excel → &lt;strong&gt;COUNTIF formula&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You need a refreshable, clean output table → &lt;strong&gt;Power Query&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Desktop automation, triggered on open or button click → &lt;strong&gt;VBA&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Server-side, no Office, CI/CD pipeline → &lt;strong&gt;C# Spire.XLS&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="8"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Highlighting duplicates in Excel may seem like a small task, but it often reveals deeper issues in how data is structured and validated. What starts as a quick visual check can quickly turn into questions about consistency, edge cases, and reliability. Depending on your workflow, the right approach can range from simple conditional formatting to more controlled, repeatable solutions using formulas, Power Query, or code. As your data grows and processes become more automated, duplicate detection stops being just an Excel trick and becomes part of a broader data quality strategy. Choosing the right method is less about the tools themselves, and more about where your data logic naturally belongs as your workflow evolves.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>productivity</category>
    </item>
    <item>
      <title>How to Export Excel to PDF in .NET (Fast &amp; Accurate Method)</title>
      <dc:creator>Jen</dc:creator>
      <pubDate>Mon, 30 Mar 2026 07:46:31 +0000</pubDate>
      <link>https://dev.to/jenll/how-to-export-excel-to-pdf-in-net-fast-accurate-method-3d8i</link>
      <guid>https://dev.to/jenll/how-to-export-excel-to-pdf-in-net-fast-accurate-method-3d8i</guid>
      <description>&lt;p&gt;If you’ve ever tried exporting Excel to PDF in a backend service, you probably started with &lt;code&gt;Microsoft.Office.Interop.Excel&lt;/code&gt;. And then it broke.&lt;/p&gt;

&lt;p&gt;No Office is installed on the server. Random crashes under load. Processes that refuse to die. It might work on your machine — but not in production. What you really want is something predictable: no Office dependency, no layout surprises, and something that actually survives in a server or container environment.&lt;/p&gt;

&lt;p&gt;So how do you do that cleanly in .NET?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Traditional Approaches Fall Short
&lt;/h2&gt;

&lt;p&gt;The go-to options for &lt;a href="https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Excel-Conversion/NET-Excel-New-method-of-Convert-Excel-to-PDF.html" rel="noopener noreferrer"&gt;Excel-to-PDF conversion in .NET&lt;/a&gt; each come with real trade-offs.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;Microsoft.Office.Interop.Excel&lt;/code&gt;&lt;/strong&gt; requires a full Office installation on the server — something &lt;a href="https://support.microsoft.com/en-us/topic/considerations-for-server-side-automation-of-office-48bcfe93-8a89-47f1-0bce-017433ad79e2" rel="noopener noreferrer"&gt;Microsoft explicitly advises against&lt;/a&gt; for server-side use. Even when it works, it's single-threaded COM automation running in a multi-request environment: slow, fragile, and prone to leaving zombie processes when something goes wrong.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Manual rendering with libraries like iTextSharp&lt;/strong&gt; flips the problem. No Office dependency, but now you're manually reading every cell, recreating styles, handling merged regions, and rebuilding layouts from scratch. The code is brittle, and even after all that work, the output rarely looks like the original.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These approaches either don't scale well or fail to preserve formatting — often both.&lt;/p&gt;

&lt;h2&gt;
  
  
  What We Actually Need
&lt;/h2&gt;

&lt;p&gt;The real challenge isn't just converting Excel to PDF — it's doing it reliably in a backend environment where Office doesn't exist and formatting still has to look right.&lt;/p&gt;

&lt;p&gt;A practical solution needs to check a few boxes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;No Microsoft Office dependency&lt;/strong&gt; — must run on a clean server or Linux container.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Accurate output&lt;/strong&gt; — merged cells, fonts, column widths, and print layout all preserved.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Handles complex files&lt;/strong&gt; — charts, images, and multi-sheet workbooks shouldn't break it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Simple API&lt;/strong&gt; — integration shouldn't require rebuilding your document rendering logic from scratch.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With these requirements in mind, let's look at a practical approach.&lt;/p&gt;

&lt;h2&gt;
  
  
  Getting Started (Minimal Setup)
&lt;/h2&gt;

&lt;p&gt;A practical way to achieve this is by using a dedicated .NET Excel processing library such as &lt;a href="https://www.e-iceblue.com/Introduce/xls-for-net-introduce.html" rel="noopener noreferrer"&gt;Spire.XLS for .NET&lt;/a&gt; — a standalone Excel processing library that handles conversion without any Office dependency.&lt;/p&gt;

&lt;p&gt;Install it via &lt;a href="https://www.nuget.org/packages/Spire.XLS/" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet add package Spire.XLS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or through the Package Manager Console:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;Install-Package Spire.XLS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few things worth noting before we write any code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Works with .NET 6, 7, 8 and .NET Framework.&lt;/li&gt;
&lt;li&gt;Runs on Windows, Linux, and in Docker containers.&lt;/li&gt;
&lt;li&gt;No Microsoft Office installation required.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's all the setup needed. Let's convert something.&lt;/p&gt;

&lt;h2&gt;
  
  
  Basic Example: Export Entire Workbook to PDF
&lt;/h2&gt;

&lt;p&gt;Here's the simplest form of the conversion — load a workbook, export to PDF:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Xls&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Create a Workbook object&lt;/span&gt;
        &lt;span class="n"&gt;Workbook&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Workbook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Load the Excel file&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\Sample.xlsx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Option: Fit each sheet to a page on export&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConverterSetting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SheetFitToPage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// Save as PDF&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\Sample.pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PDF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Release resources&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F14tpa59hfb2zudp00a7v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F14tpa59hfb2zudp00a7v.png" alt="Basic Excel to PDF in C#" width="800" height="425"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's the full conversion. No COM objects, no Office installation, no temp files to clean up. This approach works especially well in background jobs, scheduled tasks, or API-based report generation.&lt;/p&gt;

&lt;p&gt;A few things the library handles automatically:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Merged cells — preserved as-is.&lt;/li&gt;
&lt;li&gt;Fonts and cell styles — carried over to the output.&lt;/li&gt;
&lt;li&gt;Column widths and row heights — no reflow or guesswork.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;SheetFitToPage = true&lt;/code&gt; is worth setting by default. Without it, wide sheets may split across pages in ways that break the layout — we'll cover more layout controls in a later section.&lt;/p&gt;

&lt;p&gt;At this point, you already have a reliable Excel-to-PDF pipeline that works without Office and preserves most formatting out of the box.&lt;/p&gt;

&lt;h2&gt;
  
  
  Advanced Excel to PDF Conversion Scenarios
&lt;/h2&gt;

&lt;p&gt;The basic example works well — but real-world projects rarely stay that simple. Here are three variations you'll likely run into.&lt;/p&gt;

&lt;h3&gt;
  
  
  Export a Specific Worksheet
&lt;/h3&gt;

&lt;p&gt;Useful when your workbook contains multiple sheets and you only need to export one — a department report, a specific template tab:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Xls&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Create a Workbook object&lt;/span&gt;
        &lt;span class="n"&gt;Workbook&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Workbook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Load the Excel file&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\Multi-SheetChart.xlsx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Get the second worksheet (index starts from 0)&lt;/span&gt;
        &lt;span class="n"&gt;Worksheet&lt;/span&gt; &lt;span class="n"&gt;sheet&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Worksheets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="c1"&gt;// Option: Fit the worksheet to a single page (worksheet level)&lt;/span&gt;
        &lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FitToPagesWide&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// Fit width to 1 page&lt;/span&gt;
        &lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FitToPagesTall&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// Height adjusts automatically&lt;/span&gt;

        &lt;span class="c1"&gt;// Save the specific worksheet as PDF&lt;/span&gt;
        &lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToPdf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\Multi-SheetChart_Sheet2.pdf"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Release resources&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1k11pbt1vlgkgn4w62oz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1k11pbt1vlgkgn4w62oz.png" alt="Specific Worksheet to PDF in C#" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Batch Export Multiple Worksheets
&lt;/h3&gt;

&lt;p&gt;When you need each sheet as a separate PDF file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Xls&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.IO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Create a Workbook object and load the Excel file&lt;/span&gt;
        &lt;span class="n"&gt;Workbook&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Workbook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\Multi-SheetChart.xlsx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Get the output directory (same as input file location)&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;outputDir&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetDirectoryName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\Multi-SheetChart.xlsx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Iterate through all worksheets&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Worksheets&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Worksheet&lt;/span&gt; &lt;span class="n"&gt;sheet&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Worksheets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

            &lt;span class="c1"&gt;// Use the worksheet name as the PDF filename&lt;/span&gt;
            &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;fileName&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;$"&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.pdf"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;outputPath&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outputDir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Save the worksheet as PDF&lt;/span&gt;
            &lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToPdf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outputPath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Exported: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt; → &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;fileName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="c1"&gt;// Release resources&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"\nAll worksheets exported successfully!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxsrqjc4zvgbfl0xxgslx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxsrqjc4zvgbfl0xxgslx.png" alt="C#: Batch Export Multiple Worksheets" width="800" height="524"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Each sheet is saved as an individual file named after the sheet tab. Pair this with &lt;code&gt;Directory.CreateDirectory()&lt;/code&gt; to ensure the output folder exists before running.&lt;/p&gt;

&lt;h3&gt;
  
  
  Export a Defined Print Area
&lt;/h3&gt;

&lt;p&gt;If your sheet has a defined print area — say, a template where only rows 10–34 contain actual data — you can export just that range:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Xls&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Create a Workbook object&lt;/span&gt;
        &lt;span class="n"&gt;Workbook&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Workbook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Load the Excel file&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\StyleSheet.xlsx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Get the first worksheet&lt;/span&gt;
        &lt;span class="n"&gt;Worksheet&lt;/span&gt; &lt;span class="n"&gt;sheet&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Worksheets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

        &lt;span class="c1"&gt;// Define the range to export&lt;/span&gt;
        &lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PrintArea&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"A10:H34"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// Optional: Fit the print area to a single page width&lt;/span&gt;
        &lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FitToPagesWide&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// Scale width to 1 page&lt;/span&gt;
        &lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FitToPagesTall&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;    &lt;span class="c1"&gt;// Height automatically adjusts&lt;/span&gt;

        &lt;span class="c1"&gt;// Save the print area as PDF&lt;/span&gt;
        &lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToPdf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\StyleSheet_PrintArea.pdf"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="c1"&gt;// Release resources&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw6if8iu0un0n2c9avb7d.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw6if8iu0un0n2c9avb7d.png" alt="Export a Defined Print Area in C#" width="800" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is particularly useful for template-based reports where the sheet contains helper formulas or reference data outside the visible range that you don't want included in the output.&lt;/p&gt;

&lt;h2&gt;
  
  
  Returning PDF in ASP.NET Core
&lt;/h2&gt;

&lt;p&gt;In a real backend service, you usually don't want to write files to disk — especially in containerized environments where the filesystem may be read-only or ephemeral. Here's how to convert and return the PDF entirely in memory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Microsoft.AspNetCore.Mvc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Xls&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ApiController&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"api/[controller]"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ReportController&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ControllerBase&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;HttpGet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"export"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IActionResult&lt;/span&gt; &lt;span class="nf"&gt;ExportToPdf&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Load the Excel workbook&lt;/span&gt;
        &lt;span class="n"&gt;Workbook&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Workbook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"report.xlsx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Ensure file exists in the correct location&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConverterSetting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SheetFitToPage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// Convert directly to memory — no temp file needed&lt;/span&gt;
        &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;var&lt;/span&gt; &lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MemoryStream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PDF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

        &lt;span class="c1"&gt;// Rewind before reading&lt;/span&gt;
        &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Position&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="c1"&gt;// Return as file download&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToArray&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;  &lt;span class="c1"&gt;// Can be optimized (For large files, consider returning the stream directly instead of ToArray())&lt;/span&gt;
            &lt;span class="s"&gt;"application/pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="s"&gt;"report.pdf"&lt;/span&gt;         &lt;span class="c1"&gt;// triggers download in the browser&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few things worth noting:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;SaveToStream&lt;/code&gt; with &lt;code&gt;FileFormat.PDF&lt;/code&gt; writes directly to the &lt;code&gt;MemoryStream&lt;/code&gt; — no intermediate file on disk.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;stream.Position = 0&lt;/code&gt; rewinds the stream before reading; skipping this returns an empty response.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Content-Disposition&lt;/code&gt; is handled automatically by ASP.NET Core's &lt;code&gt;File()&lt;/code&gt; result when you pass a filename as the third argument.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This pattern works cleanly in Docker containers, Azure App Service, AWS Lambda, or any environment where writing to the local filesystem isn't an option. It also avoids file I/O overhead, which can become a bottleneck under high concurrency.&lt;/p&gt;

&lt;h2&gt;
  
  
  Controlling Layout for Accurate Output
&lt;/h2&gt;

&lt;p&gt;The conversion works — but on complex sheets, the default settings don't always produce clean output. Wide tables get sliced across pages, margins clip content, and landscape data ends up portrait. These three settings fix most of it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Page Size &amp;amp; Orientation
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Xls&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Workbook&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Workbook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"report.xlsx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="n"&gt;Worksheet&lt;/span&gt; &lt;span class="n"&gt;sheet&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Worksheets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Set paper size and orientation&lt;/span&gt;
&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PaperSize&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PaperSizeType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PaperA4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Orientation&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PageOrientationType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Landscape&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToPdf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"report.pdf"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Margins
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Values are in inches&lt;/span&gt;
&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TopMargin&lt;/span&gt;    &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BottomMargin&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;LeftMargin&lt;/span&gt;   &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RightMargin&lt;/span&gt;  &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Tightening the margins gives your content more room to breathe — particularly useful when you're fitting a wide table onto a single page.&lt;/p&gt;

&lt;h3&gt;
  
  
  Scaling (the important one)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Force the sheet to fit within one page wide&lt;/span&gt;
&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FitToPagesWide&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;sheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FitToPagesTall&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 0 = no vertical limit&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;FitToPagesWide = 1&lt;/code&gt; is the single most effective setting for preventing wide sheets from splitting mid-column across pages. Setting &lt;code&gt;FitToPagesTall = 0&lt;/code&gt; lets the content grow vertically as needed — you get a clean single-column layout without squashing everything onto one page.&lt;/p&gt;

&lt;p&gt;These three settings mirror what you'd configure in &lt;a href="https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Document-Operation/How-to-set-Excel-page-margins-before-printing-a-worksheet-in-C.html" rel="noopener noreferrer"&gt;Excel's &lt;strong&gt;Page Layout&lt;/strong&gt; tab before printing&lt;/a&gt; — if the sheet looks right there, it'll look right in the exported PDF.&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Issues &amp;amp; Fixes
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Fonts render as squares or tofu characters
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt; PDF output looks fine on Windows but shows blank boxes where text should be on your CI server or container.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; The Excel file uses fonts that aren't available in the server environment. The library falls back to a missing font and renders nothing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Install the missing fonts in your environment, or point the library to a custom font directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Tell Spire.XLS where to find fonts at runtime&lt;/span&gt;
&lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CustomFontPath&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"/usr/share/fonts/custom"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For Windows-style fonts on Linux, installing &lt;code&gt;ttf-mscorefonts-installer&lt;/code&gt; via apt usually resolves the most common cases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Charts or images missing from the PDF
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt; Your workbook contains charts or embedded images that simply don't appear in the exported PDF.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Chart rendering requires the sheet to be processed in a specific rendering mode. Default settings may skip embedded objects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Make sure you're converting via the workbook-level method rather than the worksheet-level one — workbook-level conversion applies full rendering passes, including charts:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Prefer this for sheets with charts or images&lt;/span&gt;
&lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConverterSetting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SheetFitToPage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"report.pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PDF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Layout errors or crashes on Linux / Docker
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Scenario:&lt;/strong&gt; Everything works locally on Windows, but after deploying to a Linux container you get a &lt;code&gt;DllNotFoundException&lt;/code&gt; or the PDF layout is completely broken.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cause:&lt;/strong&gt; Spire.XLS depends on &lt;code&gt;System.Drawing.Common&lt;/code&gt;, which in turn requires &lt;code&gt;libgdiplus&lt;/code&gt; — a native library that isn't included in the default .NET Linux base image.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Fix:&lt;/strong&gt; Add the following to your Dockerfile:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;RUN &lt;/span&gt;apt-get update &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; apt-get &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; &lt;span class="nt"&gt;--no-install-recommends&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;        libgdiplus &lt;span class="se"&gt;\
&lt;/span&gt;        libc6-dev &lt;span class="se"&gt;\
&lt;/span&gt;        libx11-dev &lt;span class="se"&gt;\
&lt;/span&gt;    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;rm&lt;/span&gt; &lt;span class="nt"&gt;-rf&lt;/span&gt; /var/lib/apt/lists/&lt;span class="k"&gt;*&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is the &lt;a href="https://www.e-iceblue.com/forum/exception-the-type-initializer-for-spire-xls-core-spreadsh-t10260.html" rel="noopener noreferrer"&gt;officially recommended setup&lt;/a&gt; for running Spire.XLS in Linux containers.&lt;/p&gt;

&lt;h2&gt;
  
  
  Wrap-Up
&lt;/h2&gt;

&lt;p&gt;At this point, you’ve got a solid Excel-to-PDF workflow in .NET — no Office dependencies, no fragile COM automation, and no need to rebuild layouts from scratch. It works just as well for background jobs, APIs, or containerized services, and scales from simple exports to more complex reporting scenarios.&lt;/p&gt;

&lt;p&gt;If you're dealing with document generation in your own projects, I’m curious — how are you handling Excel-to-PDF today?&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>csharp</category>
      <category>dotnet</category>
      <category>productivity</category>
    </item>
    <item>
      <title>OpenXML SDK vs Spire.Doc: Which One Should You Use for Word Processing in C#?</title>
      <dc:creator>Jen</dc:creator>
      <pubDate>Mon, 23 Mar 2026 09:32:00 +0000</pubDate>
      <link>https://dev.to/jenll/openxml-sdk-vs-spiredoc-which-one-should-you-use-for-word-processing-in-c-5gkj</link>
      <guid>https://dev.to/jenll/openxml-sdk-vs-spiredoc-which-one-should-you-use-for-word-processing-in-c-5gkj</guid>
      <description>&lt;p&gt;Processing Word documents in C# is a common requirement in many backend systems, such as report generation, document automation, and data extraction. Developers typically choose between the Open XML SDK, which provides low-level access to the &lt;code&gt;.docx&lt;/code&gt; structure, and third-party libraries like Spire.Doc that offer higher-level APIs.&lt;/p&gt;

&lt;p&gt;Each approach comes with trade-offs in terms of development complexity, feature support, and flexibility. In this article, we’ll compare these two options through a practical scenario to help you choose the right solution for your project.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Understanding .docx and Server-Side Constraints&lt;/li&gt;
&lt;li&gt;
Option A: DocumentFormat.OpenXML SDK

&lt;ul&gt;
&lt;li&gt;Example 1: Read All Paragraph Text&lt;/li&gt;
&lt;li&gt;Example 2: Replace Template Placeholders {{name}}&lt;/li&gt;
&lt;li&gt;Example 3: Insert a Formatted Table&lt;/li&gt;
&lt;li&gt;Limitations&lt;/li&gt;
&lt;li&gt;When OpenXML SDK Is the Right Choice&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

Option B: Using Third-Party Libraries (Spire.Doc)

&lt;ul&gt;
&lt;li&gt;Example 1: Read All Paragraph Text&lt;/li&gt;
&lt;li&gt;Example 2: Replace Template Placeholders {{name}}&lt;/li&gt;
&lt;li&gt;Example 3: Insert a Formatted Table&lt;/li&gt;
&lt;li&gt;Example 4: Export to PDF&lt;/li&gt;
&lt;li&gt;Limitations&lt;/li&gt;
&lt;li&gt;When It Makes Sense to Use Third-Party Libraries&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Side-by-Side Comparison&lt;/li&gt;

&lt;li&gt;Performance &amp;amp; Scalability Considerations&lt;/li&gt;

&lt;li&gt;Common Pitfalls and Hidden Traps&lt;/li&gt;

&lt;li&gt;Decision Guide: Which One Should You Choose?&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a id="1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding .docx and Server-Side Constraints
&lt;/h2&gt;

&lt;p&gt;Before choosing a library, it helps to understand what you're actually dealing with.&lt;/p&gt;

&lt;p&gt;A &lt;code&gt;.docx&lt;/code&gt; file is not a binary format. It's a ZIP archive containing a collection of XML files, defined by the ECMA-376 standard — also known as Office Open XML (OOXML). Rename any &lt;code&gt;.docx&lt;/code&gt; to &lt;code&gt;.zip&lt;/code&gt;, extract it, and you'll find a structure like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mydocument.docx (extracted)
├── [Content_Types].xml
├── _rels/
│   └── .rels
└── word/
    ├── document.xml        ← your actual content lives here
    ├── styles.xml          ← paragraph and character styles
    ├── settings.xml
    ├── theme/
    │   └── theme1.xml
    └── _rels/
        └── document.xml.rels
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The main content lives in &lt;code&gt;word/document.xml&lt;/code&gt;. A single paragraph with bold text looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight xml"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;w:p&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;w:r&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;w:rPr&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;w:b/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/w:rPr&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;w:t&amp;gt;&lt;/span&gt;Hello, World!&lt;span class="nt"&gt;&amp;lt;/w:t&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/w:r&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/w:p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;w:p&lt;/code&gt; is a paragraph. &lt;code&gt;w:r&lt;/code&gt; is a run — a contiguous region of text sharing the same formatting. &lt;code&gt;w:rPr&lt;/code&gt; holds the run's properties; &lt;code&gt;w:b&lt;/code&gt; toggles bold. This is the atom of Word's content model, and every library you evaluate is ultimately reading and writing variations of this structure.&lt;/p&gt;

&lt;p&gt;The ECMA-376 specification that defines all of this runs to over 6,000 pages. In practice, you don't need to read it — but you do need to understand that the abstraction level your library provides over this XML is the single most important factor in your day-to-day development experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Server-Side Processing Adds Complexity
&lt;/h3&gt;

&lt;p&gt;Manipulating &lt;code&gt;.docx&lt;/code&gt; files in a desktop context is relatively forgiving. On a server, four constraints change the equation:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No Office installation.&lt;/strong&gt; Microsoft explicitly warns against using Word Automation (COM/Interop) in server environments. That rules out the most "complete" Word-compatible engine unless you license it separately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Cross-platform deployment.&lt;/strong&gt; If your service runs in a Linux container — and most do — any library with a Windows-only dependency is immediately disqualified.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Concurrency.&lt;/strong&gt; A server handling concurrent document generation requests needs thread-safe library behavior. Not all libraries guarantee this, and the ones that don't require per-request instantiation at a minimum.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PDF export.&lt;/strong&gt; "Generate a Word document" almost always means "generate a Word document &lt;em&gt;and&lt;/em&gt; a PDF rendition." The OpenXML SDK has no rendering engine; PDF output requires a separate solution. Some third-party libraries include one.&lt;/p&gt;

&lt;p&gt;These four constraints are what make the OpenXML SDK vs. third-party library decision non-trivial — and why the "just use the free one" instinct doesn't always hold up in production.&lt;/p&gt;

&lt;p&gt;&lt;a id="2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Option A: DocumentFormat.OpenXML SDK
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://learn.microsoft.com/en-us/office/open-xml/open-xml-sdk" rel="noopener noreferrer"&gt;Open XML SDK&lt;/a&gt; is a Microsoft-provided library for working directly with Office Open XML documents such as &lt;code&gt;.docx&lt;/code&gt;, &lt;code&gt;.xlsx&lt;/code&gt;, and &lt;code&gt;.pptx&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Instead of relying on Microsoft Word, it allows you to read and modify documents by interacting with their underlying XML structure. This makes it suitable for server-side environments where installing Office is not an option.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet add package DocumentFormat.OpenXml
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It maps directly to the XML structure described in the previous section — which means it's precise and complete, but also verbose. There is no abstraction between you and the OOXML spec.&lt;/p&gt;

&lt;p&gt;&lt;a id="2-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1: Read All Paragraph Text
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;DocumentFormat.OpenXml.Packaging&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;DocumentFormat.OpenXml.Wordprocessing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;ReadParagraphs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;var&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;WordprocessingDocument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;isEditable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MainDocumentPart&lt;/span&gt;&lt;span class="p"&gt;!.&lt;/span&gt;&lt;span class="n"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;!;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Descendants&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Paragraph&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Descendants&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;().&lt;/span&gt;&lt;span class="nf"&gt;Select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Where&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;!&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IsNullOrWhiteSpace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToList&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is one of the cleaner operations in the SDK. &lt;code&gt;Descendants&amp;lt;T&amp;gt;()&lt;/code&gt; traverses the XML tree generically, and the LINQ chain stays readable.&lt;/p&gt;

&lt;p&gt;&lt;a id="2-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2: Replace Template Placeholders &lt;code&gt;{{name}}&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;A common server-side pattern: fill a pre-authored &lt;code&gt;.docx&lt;/code&gt; template with runtime data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;DocumentFormat.OpenXml.Packaging&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;DocumentFormat.OpenXml.Wordprocessing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;MailMerge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;templatePath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;outputPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;WordprocessingDocument&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;WordprocessingDocument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;templatePath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MainDocumentPart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Descendants&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;())&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{{name}}"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{{name}}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveAs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outputPath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;One important caveat: Word sometimes splits a placeholder like &lt;code&gt;{{name}}&lt;/code&gt; across multiple &lt;code&gt;&amp;lt;w:t&amp;gt;&lt;/code&gt; elements when the user types or edits it — for example, &lt;code&gt;{{&lt;/code&gt;, &lt;code&gt;name&lt;/code&gt;, &lt;code&gt;}}&lt;/code&gt; may each land in a separate &lt;code&gt;Run&lt;/code&gt;. A production-grade implementation needs to consolidate runs within each paragraph before scanning for placeholders. The code above works reliably only for placeholders that were pasted in as a single text node, which is the case for programmatically generated templates.&lt;/p&gt;

&lt;p&gt;&lt;a id="2-3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 3: Insert a Formatted Table
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;DocumentFormat.OpenXml&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;DocumentFormat.OpenXml.Packaging&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;DocumentFormat.OpenXml.Wordprocessing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Linq&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;InsertTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;var&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;WordprocessingDocument&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Open&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;path&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MainDocumentPart&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Body&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Descendants&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;Paragraph&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;()&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FirstOrDefault&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;InnerText&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The table below presents the sales data for key items."&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;InsertAfterSelf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;CreateTable&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
        &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;InsertAfterSelf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Paragraph&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
        &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Save&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="nf"&gt;CreateTable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AppendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;TableProperties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;TableBorders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;TopBorder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Val&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BorderValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Single&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Size&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;8&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;BottomBorder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Val&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BorderValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Single&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Size&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;8&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;LeftBorder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Val&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BorderValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Single&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Size&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;8&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;RightBorder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Val&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BorderValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Single&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Size&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;8&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;InsideHorizontalBorder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Val&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BorderValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Single&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Size&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;8&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;InsideVerticalBorder&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Val&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;BorderValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Single&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Size&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;8&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;)));&lt;/span&gt;

    &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[,]&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Product"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Units Sold"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Revenue ($)"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Laptop"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"1,250"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"1,299,000"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Monitor"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"850"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"765,000"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Wireless Mouse"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"3,200"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"92,800"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;TableRow&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;para&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Paragraph&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;run&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]));&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RunProperties&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;RunProperties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Bold&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
                &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AppendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;TableCell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
                    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;TableCellProperties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Shading&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Val&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ShadingPatternValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Clear&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Fill&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"4472C4"&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
                    &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Paragraph&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Run&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;RunProperties&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;])))&lt;/span&gt;
                &lt;span class="p"&gt;));&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;para&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AppendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;run&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;para&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ParagraphProperties&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ParagraphProperties&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="n"&gt;Justification&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;Val&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;JustificationValues&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Right&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
                &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AppendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;TableCell&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;para&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AppendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is where the verbosity of the SDK becomes apparent. This is where many developers start to feel the friction. The object graph mirrors the XML tree exactly — every border, every shading value, every color is a separate object. This is accurate and fully controllable, but it requires familiarity with the underlying OOXML structure before you can write it confidently.&lt;/p&gt;

&lt;p&gt;&lt;a id="2-4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Limitations
&lt;/h3&gt;

&lt;p&gt;At first glance, the SDK seems manageable. But as soon as you move beyond simple text operations, complexity increases quickly.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Verbose and Low-Level API
&lt;/h4&gt;

&lt;p&gt;Even basic formatting requires navigating multiple layers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Paragraph&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Run&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;RunProperties&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A small visual change in Word often translates into a surprisingly large amount of code.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Understanding Document Structure Is Required
&lt;/h4&gt;

&lt;p&gt;To do anything non-trivial, you need to understand how Word structures content internally:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How text is split across runs.&lt;/li&gt;
&lt;li&gt;How styles are applied.&lt;/li&gt;
&lt;li&gt;How relationships (&lt;code&gt;rId&lt;/code&gt;) are managed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without this, it's easy to produce documents that &lt;em&gt;look fine in code&lt;/em&gt; but break in Word.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. No Built-in Rendering (e.g., PDF)
&lt;/h4&gt;

&lt;p&gt;The SDK does not support converting Word documents to PDF.&lt;/p&gt;

&lt;p&gt;To achieve this, you typically need to integrate external tools such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;LibreOffice (via command line).&lt;/li&gt;
&lt;li&gt;Other conversion services.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This adds operational complexity, especially in containerized environments.&lt;/p&gt;

&lt;p&gt;&lt;a id="2-5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  When OpenXML SDK Is the Right Choice
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Your service runs on Linux or in a container and you cannot take on a commercial dependency.&lt;/li&gt;
&lt;li&gt;You need zero-cost licensing with no per-server or per-document fees.&lt;/li&gt;
&lt;li&gt;You require precise, low-level control over document structure.&lt;/li&gt;
&lt;li&gt;PDF export is not a requirement, or you are prepared to manage a separate rendering pipeline.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In short, OpenXML gives you maximum control — but that control comes with a significant development cost.&lt;/p&gt;

&lt;p&gt;&lt;a id="3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Option B: Using Third-Party Libraries (Spire.Doc)
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.e-iceblue.com/Introduce/word-for-net-introduce.html" rel="noopener noreferrer"&gt;Spire.Doc&lt;/a&gt; is a commercial .NET library developed by E-iceblue. Unlike the OpenXML SDK, it does not expose the OOXML object model directly — instead, it provides a document-oriented API that abstracts the XML layer entirely. It also ships with its own rendering engine, which means PDF export is a first-class feature rather than an afterthought.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;dotnet add package Spire.Doc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The free tier (&lt;code&gt;Spire.Doc for .NET Free&lt;/code&gt;) is available on &lt;a href="https://www.nuget.org/packages/Spire.Doc/" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt; without registration. It supports most core features but imposes two hard limits: documents are capped at 3 pages, and a watermark is appended beyond that threshold. Production use requires a commercial license.&lt;/p&gt;

&lt;p&gt;&lt;a id="3-1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 1: Read All Paragraph Text
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Doc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nf"&gt;ReadAllParagraphs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Document&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetText&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Paragraph.Text&lt;/code&gt; property aggregates all runs automatically. No descending into child nodes required.&lt;/p&gt;

&lt;p&gt;&lt;a id="3-2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2: Replace Template Placeholders &lt;code&gt;{{name}}&lt;/code&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Doc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;MailMerge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;templatePath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;outputPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Document&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;templatePath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"{{name}}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outputPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Docx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Document.Replace()&lt;/code&gt; handles run-splitting internally. The problem described in the OpenXML SDK section — where a placeholder like &lt;code&gt;{{name}}&lt;/code&gt; is split across multiple &lt;code&gt;&amp;lt;w:t&amp;gt;&lt;/code&gt; elements — does not surface here. The library normalizes the text representation before searching.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqcylw8wevk1vf72wnil.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqcylw8wevk1vf72wnil.webp" alt="Replace Template Placeholders {{name}}" width="800" height="390"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a id="3-3"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 3: Insert a Formatted Table
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Doc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Doc.Documents&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Document&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"input.docx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;sel&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FindString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"The table below presents the sales data for key items."&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sel&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsOneRange&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;OwnerParagraph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Owner&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt; &lt;span class="p"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ChildObjects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;IndexOf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sel&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetAsOneRange&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="n"&gt;OwnerParagraph&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ChildObjects&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;idx&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;CreateTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
                &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"output.docx"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Docx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="nf"&gt;CreateTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Document&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[,]&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Product"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Units Sold"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Revenue ($)"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Laptop"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"1,250"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"1,299,000"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Monitor"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"850"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"765,000"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="s"&gt;"Wireless Mouse"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"3,200"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"92,800"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;};&lt;/span&gt;

        &lt;span class="n"&gt;Table&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Table&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ResetCells&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="m"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
                &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Rows&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;Cells&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;AddParagraph&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;AppendText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;j&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The structure is comparable in length to the OpenXML SDK version for this particular operation — table construction is inherently row-and-cell iteration regardless of the library. The difference is in the API surface: &lt;code&gt;cell.CellFormat.BackColor&lt;/code&gt; versus constructing a &lt;code&gt;Shading&lt;/code&gt; object with &lt;code&gt;ShadingPatternValues.Clear&lt;/code&gt; and a hex string.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyxp9us4tf5obp68z2als.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyxp9us4tf5obp68z2als.webp" alt="Insert a Formatted Table" width="800" height="389"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a id="3-4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 4: Export to PDF
&lt;/h3&gt;

&lt;p&gt;The OpenXML SDK does not provide a rendering engine, so PDF export requires integrating external tools (e.g., LibreOffice or commercial renderers).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Doc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ExportToPdf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;docxPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;pdfPath&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;docxPath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pdfPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PDF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Spire.Doc uses its own rendering engine to produce the PDF — no LibreOffice, no Word installation, no additional process to manage. Font fidelity and layout accuracy are generally reliable for standard documents; complex layouts with custom fonts may require additional configuration (covered in the pitfalls section).&lt;/p&gt;

&lt;p&gt;&lt;a id="3-5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Limitations
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Higher-Level API
&lt;/h4&gt;

&lt;p&gt;Most operations map directly to how developers think about documents:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Sections.&lt;/li&gt;
&lt;li&gt;Paragraphs.&lt;/li&gt;
&lt;li&gt;Tables.&lt;/li&gt;
&lt;li&gt;Styles.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This reduces both development time and cognitive overhead.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Rich Feature Support
&lt;/h4&gt;

&lt;p&gt;Common real-world requirements are handled natively:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Word → PDF / HTML / Image conversion.&lt;/li&gt;
&lt;li&gt;Complex table layouts.&lt;/li&gt;
&lt;li&gt;Headers, footers, styles.&lt;/li&gt;
&lt;li&gt;Images and formatting.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Faster Development
&lt;/h4&gt;

&lt;p&gt;Tasks that require dozens of lines in OpenXML can often be implemented in just a few lines here.&lt;/p&gt;

&lt;p&gt;👉 This difference becomes more significant as document complexity increases.&lt;/p&gt;

&lt;h3&gt;
  
  
  Trade-offs to Consider
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Licensing
&lt;/h4&gt;

&lt;p&gt;Spire.Doc offers a free version with limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Watermarks added to longer documents.&lt;/li&gt;
&lt;li&gt;Some advanced features are restricted.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For production use, a commercial license is typically required.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Less Low-Level Control
&lt;/h4&gt;

&lt;p&gt;Since the library abstracts away the XML layer, fine-grained control over document internals is more limited compared to OpenXML.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Dependency Size and Runtime Considerations
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Larger package size than OpenXML.&lt;/li&gt;
&lt;li&gt;Cold start impact in serverless or container environments.&lt;/li&gt;
&lt;li&gt;Closed-source — debugging internal issues may be harder.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="3-6"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  When It Makes Sense to Use Third-Party Libraries
&lt;/h3&gt;

&lt;p&gt;This approach is often a better fit when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need &lt;strong&gt;document conversion (e.g., Word → PDF)&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You are dealing with &lt;strong&gt;complex layouts or formatting&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;You want to &lt;strong&gt;reduce development time&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Your project has a &lt;strong&gt;budget for commercial components&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In short, third-party libraries trade low-level control for productivity — and in many real-world applications, that trade-off is worth it.&lt;/p&gt;

&lt;p&gt;&lt;a id="4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Side-by-Side Comparison
&lt;/h2&gt;

&lt;p&gt;The previous two sections walked through identical operations with both libraries. Here is a consolidated view across the dimensions that matter most for a production decision.&lt;/p&gt;

&lt;h3&gt;
  
  
  Feature Comparison
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;OpenXML SDK&lt;/th&gt;
&lt;th&gt;Spire.Doc (Free)&lt;/th&gt;
&lt;th&gt;Spire.Doc (Commercial)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Read / write &lt;code&gt;.docx&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Template placeholder replace&lt;/td&gt;
&lt;td&gt;✅ ⚠️&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Table insertion&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mail merge&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PDF export&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅ ⚠️&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HTML export&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Image insertion&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Header / footer editing&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Document encryption&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Page limit&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;td&gt;3 pages&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Watermark on output&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Yes (&amp;gt;3 pages)&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;⚠️ OpenXML SDK placeholder replacement requires manual run consolidation to handle split text nodes.&lt;br&gt;&lt;br&gt;
⚠️ Spire.Doc Free appends a watermark on documents exceeding 3 pages.&lt;/p&gt;
&lt;h3&gt;
  
  
  Technical Characteristics
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Dimension&lt;/th&gt;
&lt;th&gt;OpenXML SDK&lt;/th&gt;
&lt;th&gt;Spire.Doc&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;License&lt;/td&gt;
&lt;td&gt;MIT&lt;/td&gt;
&lt;td&gt;Commercial (free tier available)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;NuGet package size&lt;/td&gt;
&lt;td&gt;~7 MB&lt;/td&gt;
&lt;td&gt;~130 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;.NET support&lt;/td&gt;
&lt;td&gt;.NET 6 / 7 / 8 / Standard 2.0&lt;/td&gt;
&lt;td&gt;.NET 6 / 7 / 8 / Standard 2.0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linux / macOS support&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;td&gt;✅ ⚠️&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Thread safety&lt;/td&gt;
&lt;td&gt;✅ (per-instance)&lt;/td&gt;
&lt;td&gt;❌ (per-request instantiation required)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PDF export&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Source available&lt;/td&gt;
&lt;td&gt;✅ (GitHub)&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Vendor support&lt;/td&gt;
&lt;td&gt;Community / GitHub Issues&lt;/td&gt;
&lt;td&gt;Commercial support included&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Learning curve&lt;/td&gt;
&lt;td&gt;High (OOXML knowledge required)&lt;/td&gt;
&lt;td&gt;Low to medium&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;⚠️ Spire.Doc on Linux requires manual font installation for accurate rendering. Documents using fonts not present on the host system will fall back silently, which can affect PDF layout fidelity.&lt;/p&gt;
&lt;h3&gt;
  
  
  Operational Cost Comparison
&lt;/h3&gt;

&lt;p&gt;This is the dimension developers most commonly overlook during library selection.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Cost factor&lt;/th&gt;
&lt;th&gt;OpenXML SDK&lt;/th&gt;
&lt;th&gt;Spire.Doc&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Library license&lt;/td&gt;
&lt;td&gt;Free&lt;/td&gt;
&lt;td&gt;Free tier / paid commercial&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;PDF rendering&lt;/td&gt;
&lt;td&gt;LibreOffice or equivalent required&lt;/td&gt;
&lt;td&gt;Included&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Infrastructure overhead&lt;/td&gt;
&lt;td&gt;LibreOffice process management&lt;/td&gt;
&lt;td&gt;None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linux font setup&lt;/td&gt;
&lt;td&gt;Not required&lt;/td&gt;
&lt;td&gt;Required for non-Latin fonts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Debug ceiling&lt;/td&gt;
&lt;td&gt;Full source&lt;/td&gt;
&lt;td&gt;Public API only&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The OpenXML SDK's zero license cost is real, but "free" does not account for the engineering time spent implementing PDF export, managing a LibreOffice process in containers, and handling edge cases in the OOXML object model. In a commercial project with a team of more than two developers, that time has a measurable cost.&lt;/p&gt;
&lt;h3&gt;
  
  
  Summary Recommendation
&lt;/h3&gt;

&lt;p&gt;Neither library is categorically better. &lt;/p&gt;

&lt;p&gt;If you are building a service where document processing is peripheral — one feature among many — Spire.Doc's higher-level API reduces the surface area of code you need to own and maintain. If document structure manipulation is central to your product and you need full visibility into the output, the OpenXML SDK gives you that control without any licensing constraints.&lt;/p&gt;

&lt;p&gt;&lt;a id="5"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  6. Performance &amp;amp; Scalability Considerations
&lt;/h2&gt;

&lt;p&gt;When moving from prototypes to production systems, performance and scalability become critical factors — especially in high-load scenarios such as batch processing or document generation APIs.&lt;/p&gt;
&lt;h3&gt;
  
  
  Thread Safety
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;OpenXML SDK&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Open XML SDK is suitable for server-side use, but its document instances are not thread-safe and should not be shared across threads.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each document must be handled within its own scope.&lt;/li&gt;
&lt;li&gt;Avoid sharing document instances across threads.&lt;/li&gt;
&lt;li&gt;Proper use of &lt;code&gt;using&lt;/code&gt; blocks is essential to release resources.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 In practice, it works well in parallel processing as long as each task operates on its own file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Spire.Doc&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Spire.Doc is &lt;strong&gt;not thread-safe by design&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You should create a new &lt;code&gt;Document&lt;/code&gt; instance per request.&lt;/li&gt;
&lt;li&gt;Avoid reusing instances in multi-threaded environments.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 This is a common pattern for most document libraries, but it needs to be explicitly handled in high-concurrency systems.&lt;/p&gt;
&lt;h3&gt;
  
  
  Memory Usage
&lt;/h3&gt;

&lt;p&gt;Memory consumption depends heavily on document size and complexity.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OpenXML SDK&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;More lightweight in terms of dependencies.&lt;/li&gt;
&lt;li&gt;Gives you finer control over how data is processed.&lt;/li&gt;
&lt;li&gt;Can be optimized for streaming scenarios.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Spire.Doc&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Loads more structure into memory due to higher-level abstractions.&lt;/li&gt;
&lt;li&gt;Typically uses more memory for complex documents.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 For small to medium documents, the difference is usually negligible.&lt;br&gt;
👉 For large files (e.g., 10MB+ with images and tables), memory usage becomes a factor worth testing.&lt;/p&gt;
&lt;h3&gt;
  
  
  Performance in Practice
&lt;/h3&gt;

&lt;p&gt;In real-world scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;OpenXML may perform better for &lt;strong&gt;simple, targeted operations&lt;/strong&gt; (e.g., text replacement, metadata updates).&lt;/li&gt;
&lt;li&gt;Spire.Doc often performs better for &lt;strong&gt;complex workflows&lt;/strong&gt;, since it avoids manual XML manipulation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 A key insight:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In most real-world systems, development time and maintainability matter more than raw execution speed.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  Cold Start &amp;amp; Deployment Considerations
&lt;/h3&gt;

&lt;p&gt;For modern deployments (e.g., Docker, serverless):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;OpenXML SDK&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Smaller package size.&lt;/li&gt;
&lt;li&gt;Faster cold start.&lt;/li&gt;
&lt;li&gt;Minimal external dependencies.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Spire.Doc&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Larger DLL size.&lt;/li&gt;
&lt;li&gt;Slightly slower cold start in environments like AWS Lambda or Azure Functions.&lt;/li&gt;
&lt;li&gt;May require additional setup (e.g., fonts in Linux containers).&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use OpenXML when you need &lt;strong&gt;lightweight, controlled processing at scale&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Use Spire.Doc when you need &lt;strong&gt;feature-rich processing with less development overhead&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a id="6"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  7. Common Pitfalls and Hidden Traps
&lt;/h2&gt;

&lt;p&gt;No matter which approach you choose, there are a few non-obvious issues that can cause serious problems in production.&lt;/p&gt;

&lt;p&gt;Here are some of the most common ones.&lt;/p&gt;
&lt;h3&gt;
  
  
  OpenXML SDK Pitfalls
&lt;/h3&gt;
&lt;h4&gt;
  
  
  1. Broken Documents Due to Relationship ID Conflicts
&lt;/h4&gt;

&lt;p&gt;When adding images, styles, or other parts, each element is linked using a relationship ID (&lt;code&gt;rId&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Hardcoding or reusing IDs can lead to corrupted files:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Word found unreadable content…”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;👉 &lt;strong&gt;Best practice:&lt;/strong&gt;&lt;br&gt;
Always use built-in methods to generate IDs instead of manually assigning them.&lt;/p&gt;
&lt;h4&gt;
  
  
  2. Text Replacement Isn’t as Simple as It Looks
&lt;/h4&gt;

&lt;p&gt;In Word, text is often split across multiple &lt;code&gt;Run&lt;/code&gt; elements.&lt;/p&gt;

&lt;p&gt;So a placeholder like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight handlebars"&gt;&lt;code&gt;&lt;span class="k"&gt;{{&lt;/span&gt;&lt;span class="nv"&gt;name&lt;/span&gt;&lt;span class="k"&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;may actually be stored as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight handlebars"&gt;&lt;code&gt;&lt;span class="k"&gt;{{&lt;/span&gt;&lt;span class="nv"&gt;na&lt;/span&gt;
&lt;span class="nv"&gt;me&lt;/span&gt;&lt;span class="k"&gt;}}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 This breaks naive string replacement logic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Normalize runs before replacement.&lt;/li&gt;
&lt;li&gt;Or use more robust search strategies.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  3. Strict vs Transitional OOXML Compatibility
&lt;/h4&gt;

&lt;p&gt;Not all &lt;code&gt;.docx&lt;/code&gt; files follow the same OOXML standard.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Some use &lt;strong&gt;Strict&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Others use &lt;strong&gt;Transitional&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 This can lead to unexpected parsing or formatting issues.&lt;/p&gt;

&lt;h3&gt;
  
  
  Spire.Doc Pitfalls
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Watermarks in Production
&lt;/h4&gt;

&lt;p&gt;The free version of Spire.Doc adds watermarks to documents beyond certain limits.&lt;/p&gt;

&lt;p&gt;This can easily slip into production if licensing is not configured properly.&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;Best practice:&lt;/strong&gt;&lt;br&gt;
Load the license at application startup (e.g., via environment variables).&lt;/p&gt;
&lt;h4&gt;
  
  
  2. Font Issues in Linux Environments
&lt;/h4&gt;

&lt;p&gt;When running in Docker or Linux containers, missing fonts can cause:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Garbled text.&lt;/li&gt;
&lt;li&gt;Missing characters.&lt;/li&gt;
&lt;li&gt;Layout inconsistencies.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 &lt;strong&gt;Solution:&lt;/strong&gt;&lt;br&gt;
Install necessary fonts in your container, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;apt-get &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;-y&lt;/span&gt; fonts-wqy-zenhei
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  3. Limited Debugging Visibility
&lt;/h4&gt;

&lt;p&gt;Since the library is closed-source:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Internal processing is not visible&lt;/li&gt;
&lt;li&gt;Debugging rendering or layout issues can be harder&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 You may need to rely on documentation or vendor support.&lt;/p&gt;

&lt;h3&gt;
  
  
  Shared Pitfall: Security Risks (XXE)
&lt;/h3&gt;

&lt;p&gt;Both approaches can be vulnerable when handling user-uploaded documents.&lt;/p&gt;

&lt;p&gt;A common risk is &lt;strong&gt;XML External Entity (XXE) injection&lt;/strong&gt;, which can lead to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Data leakage&lt;/li&gt;
&lt;li&gt;Remote file access&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 &lt;strong&gt;Mitigation strategies:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use secure XML parsing settings (e.g., prohibit DTD processing).&lt;/li&gt;
&lt;li&gt;Validate and sanitize uploaded files.&lt;/li&gt;
&lt;li&gt;Avoid processing untrusted documents directly.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Takeaway
&lt;/h3&gt;

&lt;p&gt;Most issues don’t come from “wrong APIs” — they come from assumptions about how Word documents behave internally.&lt;/p&gt;

&lt;p&gt;Understanding these pitfalls early can save hours of debugging and prevent production incidents.&lt;/p&gt;

&lt;p&gt;&lt;a id="7"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Decision Guide: Which One Should You Choose?
&lt;/h2&gt;

&lt;p&gt;At this point, the choice between the Open XML SDK and a third-party library like Spire.Doc should be clearer — but let’s make it more practical.&lt;/p&gt;

&lt;p&gt;Instead of abstract comparison, here’s a simple decision flow based on real-world needs:&lt;/p&gt;

&lt;h3&gt;
  
  
  Quick Decision Flow
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Do you need PDF or other format conversion?&lt;/strong&gt;&lt;br&gt;
→ Yes → Use a third-party library.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Do you have a budget for commercial components?&lt;/strong&gt;&lt;br&gt;
→ No → Use OpenXML (optionally combined with external tools like LibreOffice).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Is your document logic simple (e.g., text replacement, basic structure)?&lt;/strong&gt;&lt;br&gt;
→ Yes → OpenXML is sufficient.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Do you need to handle complex layouts, tables, or styling?&lt;/strong&gt;&lt;br&gt;
→ Yes → Third-party libraries will save significant time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Are you targeting Linux containers or fully open-source stacks?&lt;/strong&gt;&lt;br&gt;
→ Yes → OpenXML is the safest choice.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Scenario-Based Recommendations
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;Recommended Approach&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Internal tools / scripts&lt;/td&gt;
&lt;td&gt;OpenXML SDK&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Open-source projects&lt;/td&gt;
&lt;td&gt;OpenXML SDK&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Commercial SaaS platforms&lt;/td&gt;
&lt;td&gt;Third-party libraries&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Document-heavy workflows&lt;/td&gt;
&lt;td&gt;Third-party libraries&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;MVP with tight deadlines&lt;/td&gt;
&lt;td&gt;Third-party libraries&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  A Flexible Approach (Best of Both Worlds)
&lt;/h3&gt;

&lt;p&gt;In some projects, you don’t have to commit to a single solution.&lt;/p&gt;

&lt;p&gt;A common strategy is to define an abstraction layer, for example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;IWordProcessor&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ReplaceText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="k"&gt;value&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;InsertTable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;filePath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;object&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;ExportToPdf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;inputPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;outputPath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;👉 Then provide different implementations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;OpenXmlWordProcessor&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;SpireWordProcessor&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This allows you to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Start with one approach&lt;/li&gt;
&lt;li&gt;Switch later if requirements change&lt;/li&gt;
&lt;li&gt;Test performance or cost trade-offs&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Final Thoughts on Choosing
&lt;/h3&gt;

&lt;p&gt;There is no universally “better” solution.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;OpenXML SDK&lt;/strong&gt; is ideal when you need control, flexibility, and a free solution.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Third-party libraries&lt;/strong&gt; are better when you need speed, features, and simpler development.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;👉 The real decision comes down to this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Are you optimizing for control, or for productivity?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  9. Conclusion
&lt;/h2&gt;

&lt;p&gt;Processing Word documents in C# can range from simple text manipulation to complex document generation pipelines.&lt;/p&gt;

&lt;p&gt;The Open XML SDK gives you full control over the document structure, but requires a deeper understanding of OOXML and more development effort. Third-party libraries like Spire.Doc simplify most tasks and accelerate delivery, especially for feature-rich scenarios.&lt;/p&gt;

&lt;p&gt;The difference isn’t just about APIs — it’s about how much complexity you’re willing to manage yourself.&lt;/p&gt;

&lt;p&gt;If you're building production systems, choosing the right approach early can save significant time and effort down the line.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>tutorial</category>
      <category>productivity</category>
    </item>
    <item>
      <title>How to Convert Word to PDF in C# Without Microsoft Office (Fast &amp; Server-Safe)</title>
      <dc:creator>Jen</dc:creator>
      <pubDate>Mon, 16 Mar 2026 09:40:48 +0000</pubDate>
      <link>https://dev.to/jenll/how-to-convert-word-to-pdf-in-c-without-microsoft-office-fast-server-safe-4004</link>
      <guid>https://dev.to/jenll/how-to-convert-word-to-pdf-in-c-without-microsoft-office-fast-server-safe-4004</guid>
      <description>&lt;p&gt;If you've ever tried to convert Word to PDF in C# using &lt;strong&gt;Microsoft Office Interop&lt;/strong&gt;, you probably know the pain.&lt;/p&gt;

&lt;p&gt;COM dependency issues, unstable server-side automation, and the requirement to install the full &lt;strong&gt;Microsoft Office&lt;/strong&gt; suite just to perform document conversion.&lt;/p&gt;

&lt;p&gt;This approach might work on a local machine, but it quickly becomes problematic in production environments:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Office automation is &lt;strong&gt;not recommended for server-side applications&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It introduces &lt;strong&gt;heavy dependencies&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;It often causes &lt;strong&gt;performance and stability issues&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So what's the better way?&lt;/p&gt;

&lt;p&gt;In this guide, we'll show how to convert Word documents to PDF in C# &lt;strong&gt;without installing Microsoft Office&lt;/strong&gt;, using &lt;a href="https://www.e-iceblue.com/Introduce/word-for-net-introduce.html" rel="noopener noreferrer"&gt;&lt;strong&gt;Spire.Doc for .NET&lt;/strong&gt;&lt;/a&gt; - a lightweight .NET library designed for programmatic Word processing.&lt;/p&gt;




&lt;h2&gt;
  
  
  Why Spire.Doc for .NET for Word to PDF Conversion?
&lt;/h2&gt;

&lt;p&gt;Before we write any code, let's address the obvious question: &lt;em&gt;why Spire.Doc specifically?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Unlike Office automation, it allows developers to work with Word documents &lt;strong&gt;directly through a managed .NET API&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here is a quick comparison of common approaches:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;strong&gt;Feature&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Spire.Doc&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Word Interop&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;LibreOffice Headless&lt;/strong&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Office required&lt;/td&gt;
&lt;td&gt;✅ None&lt;/td&gt;
&lt;td&gt;❌ Full install&lt;/td&gt;
&lt;td&gt;✅ None&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Linux / Docker&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes (via CLI)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Conversion fidelity&lt;/td&gt;
&lt;td&gt;✅ High&lt;/td&gt;
&lt;td&gt;✅ High&lt;/td&gt;
&lt;td&gt;⚠️ Variable&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Server-side safe&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;❌ Not recommended&lt;/td&gt;
&lt;td&gt;⚠️ Via subprocess&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Offline / air-gapped&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Latency&lt;/td&gt;
&lt;td&gt;✅ In-process&lt;/td&gt;
&lt;td&gt;🐢 Slow (COM)&lt;/td&gt;
&lt;td&gt;🐢 Process spin-up&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cost model&lt;/td&gt;
&lt;td&gt;One-time license&lt;/td&gt;
&lt;td&gt;Office license&lt;/td&gt;
&lt;td&gt;Free&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The pattern is pretty clear. Word Interop is the go-to for desktop apps where Office is already present - but it's &lt;a href="https://learn.microsoft.com/en-us/office/troubleshoot/office-developer/consider-using-third-party-component" rel="noopener noreferrer"&gt;explicitly not recommended by Microsoft&lt;/a&gt; for server-side use. LibreOffice headless is a solid free option, but spawning a new process per conversion under load creates its own reliability headaches.&lt;/p&gt;

&lt;p&gt;Spire.Doc hits the sweet spot for server-side automation: it's a single NuGet package, fully managed .NET, no external processes, no Office, no network calls.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8018iz2j3yrzhq23ymcq.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8018iz2j3yrzhq23ymcq.webp" alt="Spire.Doc for .NET" width="800" height="472"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This makes it a reliable solution for &lt;strong&gt;modern .NET applications&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;💡Tip:&lt;/strong&gt; Spire.Doc for .NET offers a free version for evaluation and small projects. A commercial license is required for full features and production use.&lt;/p&gt;

&lt;p&gt;🚀 If your .NET application runs in Linux containers, make sure the libgdiplus package is installed. It provides the GDI+ compatibility layer required for document rendering.&lt;/p&gt;




&lt;h2&gt;
  
  
  Setup: Install Spire.Doc
&lt;/h2&gt;

&lt;p&gt;Installation only takes a few seconds using &lt;a href="https://www.nuget.org/packages/Spire.Doc" rel="noopener noreferrer"&gt;NuGet&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;PM&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Install-Package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;Spire.Doc&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once installed, you're ready to start converting Word documents programmatically.&lt;/p&gt;




&lt;h2&gt;
  
  
  Basic Conversion - Word to PDF in C
&lt;/h2&gt;

&lt;p&gt;Let's start with the simplest scenario: converting &lt;strong&gt;a .docx&lt;/strong&gt; file to &lt;strong&gt;PDF&lt;/strong&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Doc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;ConvertWordToPdf&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Document&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="c1"&gt;// Load a Word document&lt;/span&gt;
            &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C:\\Users\\Tommy\\Desktop\\Sample.docx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Save the document to PDF&lt;/span&gt;
            &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"C:\\Users\\Tommy\\Desktop\\Sample.pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PDF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Dispose resources&lt;/span&gt;
            &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it.&lt;/p&gt;

&lt;p&gt;Example output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4w58u9t6dh0qfecnn8i5.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4w58u9t6dh0qfecnn8i5.webp" alt="Convert Word to PDF in C#" width="800" height="434"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The code performs three steps:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Load the Word document into memory.&lt;/li&gt;
&lt;li&gt;Render the document layout.&lt;/li&gt;
&lt;li&gt;Export the rendered content as a PDF file.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Advanced Word to PDF Conversion Settings
&lt;/h2&gt;

&lt;p&gt;The default conversion works well, but production systems often need more control over the output. Spire.Doc provides several options that allow developers to tune PDF generation for different scenarios.&lt;/p&gt;

&lt;h3&gt;
  
  
  Stream-based Conversion - for Web API responses
&lt;/h3&gt;

&lt;p&gt;If you're building an API endpoint that returns a PDF download, writing to disk first is wasteful. Spire.Doc can write directly to any &lt;strong&gt;Stream&lt;/strong&gt;, which means you can pipe the output straight to the HTTP response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Doc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;HttpPost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"convert"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="n"&gt;IActionResult&lt;/span&gt; &lt;span class="nf"&gt;ConvertToPdf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IFormFile&lt;/span&gt; &lt;span class="n"&gt;wordFile&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wordFile&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="p"&gt;||&lt;/span&gt; &lt;span class="n"&gt;wordFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Length&lt;/span&gt; &lt;span class="p"&gt;==&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;BadRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"No file uploaded."&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;var&lt;/span&gt; &lt;span class="n"&gt;inputStream&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;wordFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;OpenReadStream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;var&lt;/span&gt; &lt;span class="n"&gt;outputStream&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MemoryStream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;var&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="c1"&gt;// Load from upload stream directly — no temp file needed&lt;/span&gt;
    &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;inputStream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Docx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;outputStream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PDF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Rewind before returning&lt;/span&gt;
    &lt;span class="n"&gt;outputStream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Position&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;outputStream&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="c1"&gt;//&lt;/span&gt;
        &lt;span class="s"&gt;"application/pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetFileNameWithoutExtension&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;wordFile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;FileName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;".pdf"&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key Points Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;doc.LoadFromStream(inputStream, FileFormat.Docx);&lt;/strong&gt; - Loads Word document directly from upload stream, no temp file needed&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;doc.SaveToStream(outputStream, FileFormat.PDF);&lt;/strong&gt; - Writes converted PDF directly to memory stream&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;outputStream.Position = 0;&lt;/strong&gt; - Resets stream position before returning&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;return File(outputStream, "application/pdf", fileName);&lt;/strong&gt; - Streams PDF directly to HTTP response&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;⚠️ Note on &lt;strong&gt;MemoryStream&lt;/strong&gt; and large files: For documents under ~50 MB this pattern works fine. For larger files in high-throughput scenarios, consider writing to a &lt;strong&gt;FileStream&lt;/strong&gt; on a temp path and streaming that back - holding very large documents in &lt;strong&gt;MemoryStream&lt;/strong&gt; will put pressure on the LOH (Large Object Heap).&lt;/p&gt;

&lt;h3&gt;
  
  
  PDF output options - page size, image quality, font embedding
&lt;/h3&gt;

&lt;p&gt;The default output is good, but Spire.Doc exposes a ToPdfParameterList object that gives you control over the most commonly tuned settings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Doc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Pdf&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;ConvertWordToPdf&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Document&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="k"&gt;try&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// Load a Word document&lt;/span&gt;
                &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\Tommy\Desktop\Sample.docx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

                &lt;span class="c1"&gt;// Create PDF parameter list for output options&lt;/span&gt;
                &lt;span class="n"&gt;ToPdfParameterList&lt;/span&gt; &lt;span class="n"&gt;pdfParams&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;ToPdfParameterList&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

                &lt;span class="c1"&gt;// 1. Set page size&lt;/span&gt;
                &lt;span class="c1"&gt;// Options: A1, A2, A3, A4, A5, Letter, Legal, etc.&lt;/span&gt;
                &lt;span class="n"&gt;pdfParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PdfPageSize&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;PdfPageSize&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;A4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

                &lt;span class="c1"&gt;// 2. Set image quality (0-100, higher = better quality, larger file size)&lt;/span&gt;
                &lt;span class="n"&gt;pdfParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ImageQuality&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;80&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 80% quality, good balance&lt;/span&gt;

                &lt;span class="c1"&gt;// 3. Set font embedding&lt;/span&gt;
                &lt;span class="n"&gt;pdfParams&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;IsEmbeddedAllFonts&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Embed all fonts for consistent display&lt;/span&gt;
                &lt;span class="c1"&gt;// Alternative: pdfParams.IsEmbeddedFonts = true; // Some versions use this&lt;/span&gt;

                &lt;span class="c1"&gt;// Optional: Set PDF conformance level (for long-term archiving)&lt;/span&gt;
                &lt;span class="c1"&gt;// pdfParams.PdfConformanceLevel = PdfConformanceLevel.Pdf_A1B;&lt;/span&gt;

                &lt;span class="c1"&gt;// Optional: Set compression level&lt;/span&gt;
                &lt;span class="c1"&gt;// pdfParams.CompressionLevel = PdfCompressionLevel.Best;&lt;/span&gt;

                &lt;span class="c1"&gt;// Save the document to PDF with custom parameters&lt;/span&gt;
                &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\Tommy\Desktop\Sample.pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pdfParams&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

                &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"PDF created successfully with custom options!"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;ToPdfParameterList&lt;/strong&gt; - Object that configures PDF output settings like page size, image quality, and font embedding.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PdfPageSize.A4&lt;/strong&gt; - Sets the output page dimensions (A4, Letter, Legal, etc.).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ImageQuality = 80&lt;/strong&gt; - Controls image compression (0-100); 80 balances quality and file size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;IsEmbeddedAllFonts&lt;/strong&gt; = true - Embeds all fonts to ensure cross-platform display consistency.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Batch conversion - process an entire folder
&lt;/h3&gt;

&lt;p&gt;For report generation pipelines, scheduled exports, or migration jobs, you'll typically need to process multiple files. Here's a production-ready pattern with basic error isolation so one bad file doesn't abort the whole batch:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Doc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.IO&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;sourceDir&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;@"C:\Users\Tommy\Desktop\WordFiles"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;targetDir&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;@"C:\Users\Tommy\Desktop\PDFFiles"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="n"&gt;Directory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CreateDirectory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;targetDir&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Directory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetFiles&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sourceDir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"*.docx"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;Document&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
            &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;pdfFile&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;targetDir&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
                &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetFileNameWithoutExtension&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="s"&gt;".pdf"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pdfFile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PDF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Convert: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetFileName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Typical use cases include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;document archiving systems.&lt;/li&gt;
&lt;li&gt;automated report generation.&lt;/li&gt;
&lt;li&gt;contract processing pipelines.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Key Points Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Directory.CreateDirectory(targetDir);&lt;/strong&gt; - Ensures output folder exists before saving files.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Directory.GetFiles(sourceDir, "*.docx")&lt;/strong&gt; - Retrieves all Word documents from the source folder for batch processing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Path.Combine(targetDir, ...)&lt;/strong&gt; - Constructs output file path while preserving filename.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Path.GetFileNameWithoutExtension(file) + ".pdf"&lt;/strong&gt; - Generates PDF filename by replacing the original extension.&lt;/li&gt;
&lt;li&gt;Creating a new &lt;strong&gt;Document&lt;/strong&gt; instance per file ensures resources are released and prevents memory buildup during large batch jobs&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Real-World Scenarios: ASP.NET Core Web API
&lt;/h2&gt;

&lt;p&gt;The most common server-side pattern: a client POSTs a &lt;strong&gt;.docx&lt;/strong&gt;, gets a &lt;strong&gt;.pdf&lt;/strong&gt; back in the same response. Here's a complete, production-ready controller:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Microsoft.AspNetCore.Mvc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Doc&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;Route&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"api/[controller]"&lt;/span&gt;&lt;span class="p"&gt;)]&lt;/span&gt;
&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;ApiController&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WordToPdfController&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ControllerBase&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;HttpPost&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="n"&gt;Task&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;IActionResult&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IFormFile&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;var&lt;/span&gt; &lt;span class="n"&gt;ms&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MemoryStream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;file&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CopyToAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Position&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;var&lt;/span&gt; &lt;span class="n"&gt;doc&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Document&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ms&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Docx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;var&lt;/span&gt; &lt;span class="n"&gt;pdf&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;MemoryStream&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="n"&gt;doc&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pdf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FileFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PDF&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Position&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pdf&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"application/pdf"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"output.pdf"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This approach is commonly used in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;document generation platforms.&lt;/li&gt;
&lt;li&gt;SaaS reporting systems.&lt;/li&gt;
&lt;li&gt;automated invoice processing.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Code Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;[Route("api/[controller]")]&lt;/strong&gt; and &lt;strong&gt;[ApiController]&lt;/strong&gt; - Standard ASP.NET Core attributes that define the API endpoint and enable automatic model validation.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;await file.CopyToAsync(ms);&lt;/strong&gt; - Asynchronously copies uploaded file to memory stream for processing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ms.Position = 0;&lt;/strong&gt; and &lt;strong&gt;pdf.Position = 0;&lt;/strong&gt; - Resets stream positions to beginning before reading or returning.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;doc.LoadFromStream(ms, FileFormat.Docx);&lt;/strong&gt; - Loads Word document directly from memory stream without saving to disk.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;doc.SaveToStream(pdf, FileFormat.PDF);&lt;/strong&gt; - Converts and writes PDF to memory stream.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;return File(pdf, "application/pdf", "output.pdf");&lt;/strong&gt; - Streams PDF directly back to client as downloadable file.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Common Pitfalls Developers Hit When Converting Word to PDF
&lt;/h2&gt;

&lt;p&gt;When implementing &lt;a href="https://www.e-iceblue.com/Tutorials/Spire.Doc/Spire.Doc-Program-Guide/How-to-Convert-Word-to-PDF.html" rel="noopener noreferrer"&gt;Word-to-PDF conversion in C#&lt;/a&gt; in production, there are a few common issues developers run into.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Missing Fonts on Linux ServersLinux&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If your application runs on Linux, missing fonts can cause layout problems.&lt;/p&gt;

&lt;p&gt;Install common fonts using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;fontconfig
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You may also need to install additional font packages depending on your document templates.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Large Document Memory Usage&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Large documents with high-resolution images can consume significant memory.&lt;/p&gt;

&lt;p&gt;Possible solutions include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;reducing image resolution.&lt;/li&gt;
&lt;li&gt;streaming processing pipelines.&lt;/li&gt;
&lt;li&gt;batch processing instead of single large jobs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Layout Differences&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Some complex Word features (especially embedded objects) may render slightly differently in PDF.&lt;/p&gt;

&lt;p&gt;Testing with real-world documents is always recommended.&lt;/p&gt;




&lt;h2&gt;
  
  
  Wrap-up
&lt;/h2&gt;

&lt;p&gt;If you're building a C# application that needs Word-to-PDF conversion:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid Microsoft Office Interop for server environments.&lt;/li&gt;
&lt;li&gt;Use a dedicated document library like Spire.Doc for .NET.&lt;/li&gt;
&lt;li&gt;Implement stream-based or batch processing for scalable systems.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With just a few lines of code, you can integrate reliable Word-to-PDF conversion into your .NET applications - without installing Microsoft Office.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>productivity</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
