5

How i can convert word file (.docx & doc ) to .pdf in c# without using SaveAs() or Save() method ? or without uploading on server?

rahul sharma
  • 505
  • 4
  • 17
Shahida
  • 97
  • 1
  • 1
  • 6
  • 9
    Possible duplicate of [How to convert .docx to .pdf in C#](http://stackoverflow.com/questions/19560170/how-to-convert-docx-to-pdf-in-c-sharp) – Chawin Jul 18 '16 at 07:31

5 Answers5

6

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
  • 150,284
  • 78
  • 298
  • 434
Human
  • 67
  • 1
  • 3
  • how i can download this converted file ? – Shahida Jul 21 '16 at 14:15
  • 1
    After this step it will be saved as pdfDocument.pdf, you will have to find it in your files. – Human Jul 21 '16 at 15:19
  • 1
    in my website i want to put an option for to convert .doc of .doxc to .pdf now how user will download this file as i dn't want to save use files on server? – Shahida Jul 21 '16 at 15:38
  • 5
    It [sounds like](https://stackoverflow.com/questions/9438577/using-microsoft-office-interop-word-in-asp-net) this is unsupported in a server environment. The recommended approach is to use [OpenXML](https://learn.microsoft.com/en-us/office/open-xml/open-xml-sdk) but there is no apparent way to use OpenXML to save as a PDF which is how I stumbled upon this question. – user875234 Mar 07 '19 at 15:26
  • 1
    @user875234 not just unsupported, it's simply a very bad idea - you need a license for every *browser user* and every request is going to start a *new* instance of Word. Even if you take care to kill instances by using a `using` block, a busy site may end up starting 10 or 20 Word instances at the same time – Panagiotis Kanavos Sep 01 '20 at 10:14
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:\work\documents\TestSample.docx");

            //Convert Word to PDF
            document.SaveToFile("toPDF.PDF", FileFormat.PDF);

            //Launch Document
            System.Diagnostics.Process.Start("toPDF.PDF");
        }
    }
}

NuGet Package page here

cristian.d
  • 135
  • 1
  • 8
1

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();
        }
    }
}
wangkaibule
  • 808
  • 1
  • 9
  • 20
1

Try this. It‘s the most useful and simple method in my opinion. You can easily accomplish this task by following just three simple steps with the help of Spire.Doc for .NET.

To view the full technical blog post follow this link.

using Spire.Doc;

namespace ToPDF
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a Document object
            Document document = new Document();

            //Load a sample Word document
            document.LoadFromFile(@"C:\Users\Administrator\Desktop\Test.docx");

            //Save the document to PDF
            document.SaveToFile("ToPDF.pdf", FileFormat.PDF);
        }
    }
}
Aymendps
  • 1,346
  • 4
  • 20
0

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();
  }
}
Fox
  • 81
  • 1
  • 3