2009-03-03 136 views
192

我發現了幾個開放源代碼/免費軟件程序,允許您將.doc文件轉換爲.pdf文件,但它們都是應用程序/打印機驅動程序種類,沒有附加任何SDK。如何以編程方式將Word文件轉換爲PDF?

我發現有幾個程序確實有一個SDK,允許您將.doc文件轉換爲.pdf文件,但它們都是專有類型,每個許可證約2,000美元。

有誰知道任何清潔,廉價(最好是免費的)程序化解決方案,我的問題,使用C#或VB.NET?

謝謝!

+0

檢查[Pandoc(http://pandoc.org/)具有[綁定您最喜愛的語言(https://github.com/jgm/pandoc/wiki/Pandoc-Extras#pandoc-包裝器和接口)。命令行界面也很簡單:`pandoc manual.docx -o manual.pdf` – 2016-09-30 15:29:20

回答

181

使用foreach循環而不是for循環 - 它解決了我的問題。

int j = 0; 
foreach (Microsoft.Office.Interop.Word.Page p in pane.Pages) 
{ 
    var bits = p.EnhMetaFileBits; 
    var target = path1 +j.ToString()+ "_image.doc"; 
    try 
    { 
     using (var ms = new MemoryStream((byte[])(bits))) 
     { 
      var image = System.Drawing.Image.FromStream(ms); 
      var pngTarget = Path.ChangeExtension(target, "png"); 
      image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png); 
     } 
    } 
    catch (System.Exception ex) 
    { 
     MessageBox.Show(ex.Message); 
    } 
    j++; 
} 

這是對我有用的程序的修改。它使用安裝了Save As PDF add-in的Word 2007。它搜索.doc文件的目錄,在Word中打開它們,然後將它們保存爲PDF。請注意,您需要將對Microsoft.Office.Interop.Word的引用添加到解決方案中。

using Microsoft.Office.Interop.Word; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 

... 

// Create a new Microsoft Word application object 
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); 

// C# doesn't have optional arguments so we'll need a dummy value 
object oMissing = System.Reflection.Missing.Value; 

// Get list of Word files in specified directory 
DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder"); 
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc"); 

word.Visible = false; 
word.ScreenUpdating = false; 

foreach (FileInfo wordFile in wordFiles) 
{ 
    // Cast as Object for word Open method 
    Object filename = (Object)wordFile.FullName; 

    // Use the dummy value as a placeholder for optional arguments 
    Document doc = word.Documents.Open(ref filename, ref oMissing, 
     ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
     ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
     ref oMissing, ref oMissing, ref oMissing, ref oMissing); 
    doc.Activate(); 

    object outputFileName = wordFile.FullName.Replace(".doc", ".pdf"); 
    object fileFormat = WdSaveFormat.wdFormatPDF; 

    // Save document into PDF Format 
    doc.SaveAs(ref outputFileName, 
     ref fileFormat, ref oMissing, ref oMissing, 
     ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
     ref oMissing, ref oMissing, ref oMissing, ref oMissing, 
     ref oMissing, ref oMissing, ref oMissing, ref oMissing); 

    // Close the Word document, but leave the Word application open. 
    // doc has to be cast to type _Document so that it will find the 
    // correct Close method.     
    object saveChanges = WdSaveOptions.wdDoNotSaveChanges; 
    ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing); 
    doc = null; 
} 

// word has to be cast to type _Application so that it will find 
// the correct Quit method. 
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing); 
word = null; 
1

似乎有些培訓相關資訊:

Converting MS Word Documents to PDF in ASP.NET

而且,與Office 2007具有公共區域噓PDF功能,我想你可以使用辦公自動化在Word 2007中打開* .DOC文件並另存爲PDF。我不太喜歡辦公自動化,因爲它很慢,容易掛,但只是把它扔在那裏...

+0

Aspose可能工作,但它非常昂貴。 – 2009-03-03 19:43:30

11

PDFCreatorPDFCreator有一個COM組件,可以從.NET或VBScript(包含在下載中的樣本)調用。

但是,在我看來,打印機正是你所需要的 - 只需將它與Word's automation混合在一起,你應該很好。

+0

這個COM組件在哪裏? 「mik」是什麼意思?這是否意味着「混合」? – 2009-03-03 19:49:37

+0

COM組件包含在下載中,以及樣本。是的,這應該是「混合」。 – 2009-03-04 11:01:20

+1

僅供參考 - 如果您使用此路線,PDFCreator會在安裝程序中捆綁惡意軟件。這是自2009年以來PDFCreator一直存在的問題。 – 2015-05-28 20:22:24

-4

我以前使用iTextSharp來生成PDF。它是來自Java世界的iText的開源端口,功能非常強大。

我還沒有明確地完成Word到PDF的轉換,但我已經用它編程創建和操縱了PDF。

這是另一個link來到該項目。

3

當我偶然發現了一些問題,服務器端辦公自動化,我們看着技術描述here on codeproject。 它使用的OpenOffice在組合便攜式版本(其可以通過XCOPY部署)與宏。 雖然我們還沒有做過自己的開關,它看起來非常有前途。

1

我用ABCpdf這是一個綱領性的選項,是不是太昂貴,$ 300 /許可證。它可以與OpenOffice一起使用,或者在OpenOffice不可用的情況下回退到Word。使用OpenOffice COM權限的設置有點棘手,但絕對值得將該部分應用程序外包。

1

微軟PDF插件的詞似乎是最好的解決方案,但現在你應該考慮到它不正確地轉換所有Word文檔爲PDF格式,並在某些情況下,你會看到這個詞和之間的巨大差異輸出pdf。不幸的是,我找不到任何能夠正確轉換所有word文檔的api。 我發現確保轉換的唯一解決方案是100%正確的是通過打印機驅動程序轉換文檔。缺點是文檔排隊並逐個轉換,但您可以確定所得到的PDF與Word文檔佈局完全相同。 我使用UDC(通用文檔轉換器),個人傾向於和服務器上安裝了福昕閱讀器(免費版)太然後通過啓動「過程」並設置其屬性動詞「打印」打印文檔。轉換完成後,您還可以使用FileSystemWatcher設置信號。

26

概括起來爲vb.net用戶的自由選擇(必須安裝的Office):

微軟辦公assembies下載:

VB.NET例如:

 Dim word As Application = New Application() 
     Dim doc As Document = word.Documents.Open("c:\document.docx") 
     doc.Activate() 
     doc.SaveAs2("c:\document.pdf", WdSaveFormat.wdFormatPDF) 
     doc.Close() 
1

只要你有Word 2010或更高版本您可以使用DocTo,它提供了一個命令行應用程序來執行此操作。

4

我藉着話就去PDF疼痛,當有人把我甩了10000頁字的文件轉換爲PDF。現在我在C#中完成了它,並且使用了Word interop,但是如果我嘗試使用PC,它會很慢並且崩潰..非常令人沮喪。

這導致我發現我可以轉儲interops和他們的緩慢.....我使用的Excel(EPPLUS),然後我發現你可以得到一個免費的工具稱爲Spire,允許轉換爲PDF ...與限制!

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

1

我留下深刻印象Gembox(http://www.gemboxsoftware.com/)誰提供文檔管理的有限的免費版本(包括PDF轉換)。他們還爲電子表格製作圖書館。如果你超過了他們的限制(我想你會這麼做),那麼這個1開發者許可證的價格大約是580美元(http://www.gemboxsoftware.com/document/pricelist)。好吧,這不是免費的(或者我認爲相對便宜),但是它比2000美元便宜很多。據我瞭解,從他們的價目表中,服務器部署沒有版稅。可能值得接近他們,看看他們是否會做出一個交易,如果你不想推出自己的。

3

只是想補充一點,我使用Microsoft.Interop庫,特別是ExportAsFixedFormat函數,我沒有看到在這個線程中使用。

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; 
    } 
相關問題