Converting word to pdf using c#
Can anybody help me with a code to convert a word file pdf
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mahesh ChandRank #1 · 393.3K reputationPosted Jun 20, 2007, 4:06 AM EDT
David Isaac19 reputationPosted Jun 20, 2007, 5:34 AM EDT
(using Microsoft.Office.Interop.Word;)
ApplicationClass wordApp = new ApplicationClass(); Document wordDoc = null; string paramExportFilePath = @"C:\\Test.xps";
WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatXPS;
bool paramOpenAfterExport = false;WdExportOptimizeFor paramExportOptimizeFor =
WdExportOptimizeFor.wdExportOptimizeForPrint;
WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
int paramStartPage = 0; int paramEndPage = 0;WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
bool paramIncludeDocProps = true; bool paramKeepIRM = true;WdExportCreateBookmarks paramCreateBookmarks
bool paramDocStructureTags = true; bool paramBitmapMissingFonts = true; bool paramUseISO19005_1 = false; try= WdExportCreateBookmarks.wdExportCreateWordBookmarks;
{
// Open the source document.wordDoc = wordApp.Documents.Open(
ref paramSourceDocPath, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing, ref paramMissing); // Export it in the specified format. if (wordDoc != null)wordDoc.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
paramBitmapMissingFonts, paramUseISO19005_1,
ref paramMissing);}
catch (Exception ex){
// Respond to the error}
finally{
// Close and release the Document object. if (wordDoc != null){
wordDoc.Close(
ref paramMissing, ref paramMissing, ref paramMissing);wordDoc =
null;}
// Quit Word and release the ApplicationClass object. if (wordApp != null){
wordApp.Quit(
ref paramMissing, ref paramMissing, ref paramMissing);wordApp =
null;}
GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers();rebecca yang18 reputationPosted Sep 5, 2013, 7:45 PM EDT
2. Save Word document as PDF by using document.SaveToFile() method. Parameters passed to this method is file name string and file format. The file format must be PDF.
You can download spire.DOC here :http://netword.codeplex.com/
C#
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:\work\documents\TestSample.docx");
//Convert Word to PDF
document.SaveToFile("toPDF.PDF", FileFormat.PDF);
//Launch Document
System.Diagnostics.Process.Start("toPDF.PDF");
}
}
}
{Visual Basic]
Imports System
Imports Spire.Doc
Imports Spire.Doc.Documents
Namespace DoctoPDF
Friend Class toPDF
Shared Sub Main(ByVal args() As String)
'Load Document
Dim document As New Document()
document.LoadFromFile("E:\work\documents\TestSample.docx")
'Convert Word to PDF
document.SaveToFile("toPDF.PDF", FileFormat.PDF)
'Launch Document
System.Diagnostics.Process.Start("toPDF.PDF")
End Sub
End Class
End Namespace
Dorothy Tincher10 reputationPosted May 15, 2014, 12:06 AM EDT
var application = new Application(); var document = application.Documents.Open(FileName: @"C:\Sample.docx"); document.ExportAsFixedFormat(OutputFileName: @"C:\Sample.pdf", ExportFormat: WdExportFormat.wdExportFormatPDF); document.Close(); application.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(application);You can also try this Word component in C#, I used it in quite a few scenarios and tasks that required a DOCX manipulations in .NET and I must say it is easy to work with.
For example here is a direct way you can convert your Word document into a PDF file in .NET: