Converting word file to pdf

2

Is there a free library that I can convert word file to pdf?

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

I show on the HTML page using ViewerJS , but it only accepts PDF and ODT >.

Here is the return code:

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

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

    
asked by anonymous 19.07.2018 / 20:08

2 answers

5

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

I tested here and managed to do 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 handle the generated PDF file as you see fit. If this solution works, I advise you to put some checks before, such as testing if the file exists and using try / catch .

    
19.07.2018 / 21:01
0

I'll leave my answer here to help the people who need it to convert. In my logic I'm 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 the server needs Microsoft Office installed.     

20.07.2018 / 20:44