Do you have any free .Net managed code for converting DocX to PDF?

后端 未结 5 1650
时光说笑
时光说笑 2020-12-15 23:51

In my web project, I use DocX file type for containing report template. I need to convert DocX file type to PDF. Do you have any .Net managed code for doing that?

I

5条回答
  •  时光说笑
    2020-12-16 00:14

    Installing Office 2007 and using the Word 12 Object Library is definitely the option I'd go for (and have done so on some of my own projects).

    If you don't want to install Word on a production web server, why not have it on a secondary server. You can get this second server to communicate with the first (using a web service or something like that) -- it could request the next Word document that needs exporting, do the conversion and then return the PDF data.

    Let me know if you want a C# example of the Word automation that does this conversion (it's very trivial).

    Adrian


    Here's my code, posted for Jason. This works with Word 2007. You need to download and install the PDF exporter from the Office web site:

    using Microsoft.Office.Interop.Word;
    
    ...
    
    object _read_only = false;
    object _visible = true;
    object _false = false;
    object _true = true;
    object _dynamic = 2;
    object  _missing = System.Reflection.Missing.Value;
    
    object _htmlFormat = 8;        
    object _pdfFormat = 17;
    object _xpsFormat = 18;
    
    object fileName = "C:\\Test.docx";
    
    ApplicationClass ac = new ApplicationClass();
    //ac.Visible = true; // Uncomment to see Word as it opens and converts the document
    //ac.Activate();
    
    Document d = ac.Documents.Open(ref fileName, ref _missing, ref _true, ref _read_only, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _visible, ref _missing, ref _missing, ref _missing, ref _missing);
    
    object newFileName = ((string)fileName).Substring(0, ((string)fileName).LastIndexOf(".")) + ".pdf";
    
    d.SaveAs(ref newFileName, ref _pdfFormat, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing, ref _missing);
    
    d.Close(ref _false, ref _missing, ref _missing);
    ac.Quit(ref _false, ref _missing, ref _missing);
    
    ac = null;
    

    So, Soul_Master, what you are saying is that you don't want to use interop (though you don't say why, which I'd be interested to know), you don't want to pay for a commercial exporter, and you want perfect results?

    I cant help you, I'm afraid. Interop will give you perfect results, every time, and you already have the software. If you won't use that, you are going to have to make a sacrifice -- either cost or quality.

提交回复
热议问题