How do I convert Word files to PDF programmaticall

2018-12-31 06:49发布

I have found several open-source/freeware programs that allow you to convert .doc files to .pdf files, but they're all of the application/printer driver variety, with no SDK attached.

I have found several programs that do have an SDK allowing you to convert .doc files to .pdf files, but they're all of the proprietary type, $2,000 a license or thereabouts.

Does anyone know of any clean, inexpensive (preferably free) programmatic solution to my problem, using C# or VB.NET?

Thanks!

15条回答
梦该遗忘
2楼-- · 2018-12-31 07:54

Just wanted to add that I used Microsoft.Interop libraries, specifically ExportAsFixedFormat function which I did not see used in this thread.

using Microsoft.Office.Interop.Word;
using System.Runtime.InteropServices;
using System.IO;
using Microsoft.Office.Core;Application app;

public string CreatePDF(string path, string exportDir)
{
    Application app = new Application();
    app.DisplayAlerts = WdAlertLevel.wdAlertsNone;
    app.Visible = true;

    var objPresSet = app.Documents;
    var objPres = objPresSet.Open(path, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

    var baseFileName = Path.GetFileNameWithoutExtension(path);
    var pdfFileName = baseFileName + ".pdf";
    var pdfPath = Path.Combine(exportDir, pdfFileName);

    try
    {
        objPres.ExportAsFixedFormat(
            pdfPath,
            WdExportFormat.wdExportFormatPDF,
            false,
            WdExportOptimizeFor.wdExportOptimizeForPrint,
            WdExportRange.wdExportAllDocument
        );
    }
    catch
    {
        pdfPath = null;
    }
    finally
    {
        objPres.Close();
    }
    return pdfPath;
}
查看更多
步步皆殇っ
3楼-- · 2018-12-31 07:54

I went through the Word to PDF pain when someone dumped me with 10000 word files to convert to PDF. Now I did it in C# and used Word interop but it was slow and crashed if I tried to use PC at all.. very frustrating.

This lead me to discovering I could dump interops and their slowness..... for Excel I use (EPPLUS) and then I discovered that you can get a free tool called Spire that allows converting to PDF... with limitations!

http://www.e-iceblue.com/Introduce/free-doc-component.html#.VtAg4PmLRhE

查看更多
残风、尘缘若梦
4楼-- · 2018-12-31 07:54

I have been impressed with Gembox (http://www.gemboxsoftware.com/) who provide a limited free edition of document management (includes pdf conversion). They also do libraries for spreadsheets. The 1 developer licence if you exceed their limits (which I imagine you will) though is around $580 (http://www.gemboxsoftware.com/document/pricelist). OK, it's not free (or in my opinion relatively inexpensive) but it is a lot cheaper than $2000. As I understand it from their price list there is no royalty either for server deployments. Might be worth approaching them and seeing if they'll do a deal if you don't want to roll your own.

查看更多
登录 后发表回答