Need to convert Word files to PDF programmatically? With Aspose.Words for .NET any developer can convert Word to PDF format with just a few lines of C# code.
Modern document-processing C# API creates PDF from Word with high speed. Test the quality of Word to PDF conversion right in a browser. Powerful C# library allows converting Word files to many popular formats.
Save Word as PDF in C#
The following example demonstrates how to convert Word to PDF in C#.
Follow the easy steps to turn a Word file into PDF format. Read Word file from the local drive, then simply save it as PDF, specifying the required file format by PDF extension. For both Word reading and PDF writing you can use fully qualified filenames. The output PDF content and formatting will be identical to the original Word document.
Input file
Upload a file you want to convert
Run code
Output format
Select the target format from the list
using Aspose.Words;
var doc = new Document("Input.docx");
doc.Save("Output.pdf");
using Aspose.Words;
var doc = new Document("Input.docx");
doc.Save("Output.pdf");
using Aspose.Words;
var doc = new Document("Input.docx");
for (int page = 0; page < doc.PageCount; page++)
{
var extractedPage = doc.ExtractPages(page, 1);
extractedPage.Save($"Output_{page + 1}.pdf");
}
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
builder.InsertImage("Input.docx");
doc.Save("Output.pdf");
using Aspose.Words;
var doc = new Document();
var builder = new DocumentBuilder(doc);
var shape = builder.InsertImage("Input.docx");
shape.ImageData.Save("Output.pdf");
How to convert Word to PDF
- Install ‘Aspose.Words for .NET’.
- Add a library reference (import the library) to your C# project.
- Open the source Word file in C#.
- Call the ‘Save()’ method, passing an output filename with PDF extension.
- Get the result of Word conversion as PDF.
C# library to convert Word to PDF
There are three alternative options to install «Aspose.Words for .NET» onto your system. Please choose one that resembles your needs and follow the step-by-step instructions:
- Install a NuGet Package. See Documentation
- Install the library using Package Manager Console within Visual Studio IDE
- Install the library manually using Windows Installer
System Requirements
Our product is fully cross-platform and supports all major .NET implementations following ‘.NET Standard 2.0’ specification:
- Microsoft .NET Framework, starting from the earliest 2.0 version, and ending with the latest ‘.NET Framework 4.8’
- .NET Core, starting from the earliest 2.0, and ending with the latest ‘.NET 6’
- Mono >= 2.6.7
As far as .NET code doesn’t depend on the underlying hardware or operating system, but only on a Virtual Machine, you are free to develop any kind of software for Windows, macOS, Android, iOS and Linux. Just make sure you have installed the corresponding version of .NET Framework, .NET Core, Windows Azure, Mono or Xamarin.
We recommend using Microsoft Visual Studio, Xamarin, and MonoDevelop integrated development environments to create C#, F#, VB.NET applications.
Fore more details please refer to Product Documentation.
You can convert Word to many other file formats:
Word to PDF
Powered by
aspose.com
and
aspose.cloud
Choose Word files
or drop Word files
Use password
This password will be applied to all documents
If you need to convert several Word to one PDF use Merger
Convert Word to PDF online
Use Word to PDF converter to export Word documents to PDF format online. It is completely free.
Our online service can convert Word documents of any complexity. Word documents may contain tables and lists, headers and footers, formulas and graphics, stylized text, and so on. Our converter will parse the content of a Word file to the smallest detail and recreate the corresponding elements in the target PDF format.
Word to PDF Converter online
Converting from Word to PDF format and vice versa is one of the most requested operations with office documents. Word document formats are great when you want to let other people make changes to the content. In contrast, the PDF format is an excellent choice when we need to protect the document from being modified. We need both of the unique features that Word and PDF formats provide. PDF and Word document formats complement each other in some cases and are therefore closely linked in today’s office work. Quite often we want to convert an editable Word document to an immutable PDF file. This may be a contract or some financial data that should not be modified.
Convert Word file to PDF online
To convert Word to PDF format, simply drag and drop a Word file into the data upload field, specify the conversion options, click the ‘Convert’ button, and get your output PDF file in seconds. The resulting PDF content, structure, and style will be identical to the original Word document.
Free Word to PDF Converter is based on Aspose software products, which are widely used around the world for programmatic processing of Word and PDF files with high speed and professional quality of the result.
Join the DZone community and get the full member experience.
Join For Free
What’s new in this release?
Earlier, Aspose.Words for .NET version 6.3 supported glossary documents while loading or saving Office Open XML only. But the newer version 6.4 enables exporting Word documents to PDF compliant to PDF/A-1b and Aspose.Words team is also committed to support PDF/A-1a in the next few months as well.
Example: How to export Word to PDF docs compliant to PDF/A-1b?
Document doc = new Document(@»C:TestRendering.doc»);
PdfOptions options = new PdfOptions();
options.Compliance = PdfCompliance.PdfA1b;
doc.SaveToPdf(0, doc.PageCount, @»C:TestRendering Out.pdf», options);
More about Aspose.Words for .NET
Aspose.Words is a word processing component that enables .NET applications to read, write, modify & convert Word documents without using MS Word. Other features include smart tags, document creation, content and formatting manipulation, mail merge abilities, reporting features, DrawingML and support of EPUB, OOXML, DOCX, DOC, WordML, ODT, HTML, XHTML, MHTML, TXT and PDF formats. It supports printing and rendering Word documents to images. Also use Aspose.Words to build applications with Mono.
PDF
Doc (computing)
Opinions expressed by DZone contributors are their own.
Trending
-
The Ultimate API Development Guide: Strategy, Tools, Best Practices
-
A New Era of Data Analytics: Exploring the Innovative World of Data Lakehouse Architectures
-
Understanding How Stream Processing Works
-
The Holy Grail of Agile-DevOps Value Stream Hunting: Actualizing DevOps Transition Purpose
Word to PDF Conversions in C# | Aspose.Words for .NET
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Document doc = new Document(«word.docx«); | |
// Set PDFSaveOption compliance to PDF17 | |
PdfSaveOptions options = new PdfSaveOptions(); | |
options.Compliance = PdfCompliance.Pdf17; | |
// Convert Word to PDF | |
doc.Save(«output.pdf«, options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Document doc = new Document(«word.docx«); | |
PdfSaveOptions options = new PdfSaveOptions(); | |
// Digital signatures details | |
CertificateHolder certHolder = CertificateHolder.Create(«signature.pfx«, «12345«); | |
options.DigitalSignatureDetails = new PdfDigitalSignatureDetails(certHolder, «reason«, «location«, DateTime.Now); | |
// Save Word as PDF | |
doc.Save(«output.pdf«, options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Document doc = new Document(«word.docx«); | |
// Set Jpeg quality | |
PdfSaveOptions options = new PdfSaveOptions(); | |
options.JpegQuality = 100; | |
// Convert Word to PDF | |
doc.Save(«output.pdf«, options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Document doc = new Document(«word.docx«); | |
PdfSaveOptions options = new PdfSaveOptions(); | |
// Convert 3 pages starting from index 1 where 0 is the first page’s index | |
options.PageIndex = 1; | |
options.PageCount = 3; | |
// Save Word as PDF | |
doc.Save(«output.pdf«, options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Document doc = new Document(«word.docx«); | |
PdfSaveOptions options = new PdfSaveOptions(); | |
// Text and image compression | |
options.TextCompression = PdfTextCompression.Flate; | |
options.ImageCompression = PdfImageCompression.Auto; | |
// Save Word as PDF | |
doc.Save(«output.pdf«, options); |
How i can convert word file (.docx & doc
) to .pdf
in c#
without using SaveAs()
or Save()
method ? or without uploading on server?
asked Jul 18, 2016 at 7:29
1
Try this, it works for me:
using Microsoft.Office.Interop.Word;
var appWord = new Application();
if (appWord.Documents != null)
{
//yourDoc is your word document
var wordDocument = appWord.Documents.Open(yourDoc);
string pdfDocName = "pdfDocument.pdf";
if (wordDocument != null)
{
wordDocument.ExportAsFixedFormat(pdfDocName,
WdExportFormat.wdExportFormatPDF);
wordDocument.Close();
}
appWord.Quit();
}
Keith
148k77 gold badges298 silver badges427 bronze badges
answered Jul 18, 2016 at 8:40
HumanHuman
671 silver badge3 bronze badges
5
Aspose.Words is really good solution for this purpose if you can buy the license. The free version adds warning messages to the output PDF.
If you’re looking for something free, I have used FreeSpire.Doc, the free version has the following limits:
Free version is limited to 500 paragraphs and 25 tables. This limitation is enforced during reading or writing files. When converting word documents to PDF and XPS files, you can only get the first 3 page of PDF file. Upgrade to Commercial Edition of Spire.Doc
MS Office, Office.Interop or Office automations are not required.
Install via NuGet:
Install-Package FreeSpire.Doc -Version 7.11.0
Code example:
using System;
using Spire.Doc;
using Spire.Doc.Documents;
namespace DoctoPDF
{
class toPDF
{
static void Main(string[] args)
{
//Load Document
Document document = new Document();
document.LoadFromFile(@"E:workdocumentsTestSample.docx");
//Convert Word to PDF
document.SaveToFile("toPDF.PDF", FileFormat.PDF);
//Launch Document
System.Diagnostics.Process.Start("toPDF.PDF");
}
}
}
NuGet Package page here
answered May 25, 2020 at 21:18
cristian.dcristian.d
1351 silver badge8 bronze badges
Try this, no extra compiler configuration needed if MS Office Word is installed on your computer:
using System;
using System.IO;
using System.Reflection;
namespace KUtil
{
public class Word2PDF
{
static void Main(string[] args)
{
var word = Type.GetTypeFromProgID("word.application");
dynamic app = Activator.CreateInstance(word);
if (args.Length < 1)
{
return;
}
var path = args[0];
var outPath = Path.ChangeExtension(path, "pdf");
dynamic doc = app.Documents.Open(path);
doc.ExportAsFixedFormat(outPath,
ExportFormat:17/*pdf*/);
doc.Close(0/*DoNotSaveChanges*/);
app.Quit();
}
}
}
answered May 10, 2020 at 5:53
wangkaibulewangkaibule
8081 gold badge9 silver badges20 bronze badges
Based on wangkaibule’s answer, PDF conversion with heading bookmarks. It also works under .NET 7 (similar to the linked post).
public static void Convert(string inputFileName, string outputFileName)
{
// Microsoft.Office.Interop.Word.WdSaveFormat enum
const int wdFormatPDF = 17;
// Microsoft.Office.Interop.Word.WdExportCreateBookmarks enum
const int wdExportCreateHeadingBookmarks = 1;
// Microsoft.Office.Interop.Word.WdSaveOptions enum
const int wdDoNotSaveChanges = 0;
var word = Type.GetTypeFromProgID("word.application");
if (word == null)
{
throw new ArgumentException("Microsoft Word is not installed on the system.");
}
dynamic app = Activator.CreateInstance(word);
try
{
dynamic doc = app.Documents.Open(inputFileName);
doc.ExportAsFixedFormat(outputFileName, ExportFormat: wdFormatPDF, CreateBookmarks: wdExportCreateHeadingBookmarks);
doc.Close(wdDoNotSaveChanges);
}
finally
{
app.Quit();
}
}
answered Apr 6 at 14:30
FoxFox
811 silver badge3 bronze badges