Converting word file to pdf

Asked

Viewed 1,448 times

5

There is a free library that I can convert word file to pdf ?

I have word file saved database with type varbinary(MAX) and I recover as byte[] in C# and I want to convert word to pdf to show on HTML page.

I show in the HTML page using Viewerjs, but she accepts only PDF and ODT.

Here’s the return code:

return new FileContentResult(_WordByte, "application/pdf");

The above code does not work, first I need to convert to pdf, some solution ?

3 answers

7


There is another option that is using the interoperability library with Office, Microsoft.Office.Interop.Word.

I tested it here and I got to do it like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Word;

static class Program
{
    public static Microsoft.Office.Interop.Word.Document wordDocument { get; set; }

    static void Main(string[] args)
    {
        // Definição dos endereços dos arquivos de entrada e saida - O endereço deve estar completo
        string docOrigem = "D:\\teste.docx"; 
        string pdfSaida = "D:\\saida.pdf";

        // Utiliza as próprias dll do wor para realizar a conversão
        Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
        wordDocument = appWord.Documents.Open(docOrigem);
        wordDocument.ExportAsFixedFormat(pdfSaida, WdExportFormat.wdExportFormatPDF);

        // Fecha a aplicação e o documento
        appWord.Quit()
        wordDocument.Close() 
    }
}

Then just treat the generated PDF file as you wish.
If this solution works, I advise you to put some checks before, for example, test if the file exists and use try/catch.

  • 1

    William on the last line, you forgot to close: wordDocument.Close() and appWord.Quit(). Take the example of my answer :)

  • Actually, I did it in a hurry and when it worked out I ended up not revising the code. This closure is very important so that files are not "locked open" .

  • How the Office interoperability library creates COM objects that are managed by the Trash Collector. NET, the Word process may be "hanging" indefinitely while the application is running, so it is important to force a collection of trash. Look at that answer: How to save an XLSX (Excel) file instead of CSV?.

3

I’ll leave my answer here to help the people who need to convert you. In my logic I am converting (word, excel and Powerpoint) to PDF and returns bytes[]:

Before going to logic, you need to install 3 components, they are:

PM> Install-Package Microsoft.Office.Interop.Word -Version 15.0.4797.1003

PM> Install-Package Microsoft.Office.Interop.Excel -Version 15.0.4795.1000

PM> Install-Package Microsoft.Office.Interop.Powerpoint -Version 15.0.4420.1017

HttpPostedFileBase file = Request.Files[0];

string ext = Path.GetExtension(file.FileName);

byte[] PDF = ConvertToPDF(file, ext);

private byte[] ConvertToPDF(HttpPostedFileBase file, string ext)
{
    //salva o arquivo na pasta App_Data
    string path = Server.MapPath($"~/App_Data/nome_arquivo");
    file.SaveAs($"{path}{ext}");

    //Micrososft Word
    if (ext == ".doc" || ext == ".docx")
    {
        Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
        Microsoft.Office.Interop.Word.Document doc = app.Documents.Open($"{path}{ext}");
        //Converter para PDF
        doc.ExportAsFixedFormat($"{path}.pdf", Microsoft.Office.Interop.Word.WdExportFormat.wdExportFormatPDF);
        doc.Close();
        app.Quit();
        //Leia o arquivo e retorna bytes[]
        return System.IO.File.ReadAllBytes($"{path}.pdf");
    }
    //Microsoft Excel
    if (ext == ".xls" || ext == ".xlsx")
    {
        Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
        Microsoft.Office.Interop.Excel.Workbook wkb = app.Workbooks.Open($"{path}{ext}");
        //Converter para PDF
        wkb.ExportAsFixedFormat(Microsoft.Office.Interop.Excel.XlFixedFormatType.xlTypePDF, $"{path}.pdf");
        wkb.Close();
        app.Quit();
        //Leia o arquivo e retorna bytes[]
        return System.IO.File.ReadAllBytes($"{path}.pdf");
    }
    //Microsoft PowerPoint
    else
    {
        //ppt || pptx 
        Microsoft.Office.Interop.PowerPoint.Application app = new Microsoft.Office.Interop.PowerPoint.Application();
        Microsoft.Office.Interop.PowerPoint.Presentation presentation = app.Presentations.Open($"{path}{ext}",
            Microsoft.Office.Core.MsoTriState.msoTrue,
            Microsoft.Office.Core.MsoTriState.msoFalse,
            Microsoft.Office.Core.MsoTriState.msoFalse);

        //Converter para PDF
        presentation.ExportAsFixedFormat($"{path}.pdf", Microsoft.Office.Interop.PowerPoint.PpFixedFormatType.ppFixedFormatTypePDF);
        presentation.Close();
        app.Quit();
        //Leia o arquivo e retorna bytes[]
        return System.IO.File.ReadAllBytes($"{path}.pdf");
    }
}

Remembering that on the server you need to Microsoft Office installed.

  • How the Office interoperability library creates COM objects that are managed by the Trash Collector. NET, the Word process may be "hanging" indefinitely while the application is running, so it is important to force a collection of trash. Look at that answer: How to save an XLSX (Excel) file instead of CSV?.

1

How there are two answers using the Interop, I decided to post a solution that does not use it, not needing Office installed on the server.

You can do this using the Freespire.DOC.

To install it use Nuget:

Install-Package Freespire.Doc

Below is an example method that takes the path of a Word file and converts it to PDF:

If you prefer see the example on Github.

public static bool ConverterWordParaPDF(string caminhoArquivoWord, string caminhoParaSalvarResultadoPDF, out string nomeArquivoPDF)
{
    nomeArquivoPDF = String.Empty;

    //Se o arquivo não existir, retornar falso
    if (!File.Exists(caminhoArquivoWord))
    {
        return false;
    }

    //Gera um guid para usar no nome do arquivo e concatena com o caminho onde o arquivo será armazenado:
    string caminhoCompletoPDF = caminhoParaSalvarResultadoPDF + "\\" + Guid.NewGuid() + ".pdf";

    using (Document document = new Document())
    {
        document.LoadFromFile(caminhoArquivoWord);
        document.SaveToFile(caminhoCompletoPDF, Spire.Doc.FileFormat.PDF);
    }

    if (!File.Exists(caminhoCompletoPDF))
    {
        return false;
    }
    else
    {
        nomeArquivoPDF = caminhoCompletoPDF;
    }
    return true;
}

Example of call to method:

string resultadoPDF;
if(ConverterWordParaPDF(CaminhoArquivoWord, caminhoConversaoPDF, out resultadoPDF))
{
    Console.WriteLine(String.Format("Arquivo Word convertido com sucesso. Caminho do arquivo: {0}", resultadoPDF));
}

On Github there is also an example for converting Excel files.

Read the Free Spire.DOC documentation on the developer’s website.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.