2010-05-20 149 views
2

我需要使用C#/ VB.Net将下面提到的文件格式转换为pdf。用户将使用FileUpload控件上传文件,系统将在转换文档后返回PDF文件。C#。文档转换为PDF

DOC/DOCX到PDF XLS/XLSX为PDF PPT/PPS为PDF

是否iTextSharp的提供这样的设施?请只提到开源或免费的图书馆。

谢谢

+0

它与原始文档有多重要?某些解决方案可能无法100%复制MS格式,但是免费。 – Chris 2010-05-20 17:18:26

回答

1

我只对Word格式做了同样的事情。我使用Microsoft.Office.Interop.Word

下面的代码。

public static void Word2PDF(string fileName) 
{ 
    ApplicationClass wordApplication = new ApplicationClass(); 
    Document wordDocument = null; 
    object paramSourceDocPath = fileName + @".doc"; 
    object paramMissing = Type.Missing; 
    string paramExportFilePath = fileName + @".pdf"; 
    WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF; 
    bool paramOpenAfterExport = false; 

    WdExportOptimizeFor paramExportOptimizeFor =WdExportOptimizeFor.wdExportOptimizeForPrint; 

    WdExportRange paramExportRange = WdExportRange.wdExportAllDocument; 
    int paramStartPage = 0; 

    int paramEndPage = 0; 
    WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent; 

    bool paramIncludeDocProps = true; 
    bool paramKeepIRM = true; 

    WdExportCreateBookmarks paramCreateBookmarks = 
    WdExportCreateBookmarks.wdExportCreateWordBookmarks; 

    bool paramDocStructureTags = true; 
    bool paramBitmapMissingFonts = true; 

    bool paramUseISO19005_1 = false; 

    try 
    { 
     // Open the source document. 
     wordDocument = wordApplication.Documents.Open(
     ref paramSourceDocPath, ref paramMissing, ref paramMissing, 
     ref paramMissing, ref paramMissing, ref paramMissing, 
     ref paramMissing, ref paramMissing, ref paramMissing, 
     ref paramMissing, ref paramMissing, ref paramMissing, 
     ref paramMissing, ref paramMissing, ref paramMissing, 
     ref paramMissing); 

     // Export it in the specified format. 
     if (wordDocument != null) 
      wordDocument.ExportAsFixedFormat(paramExportFilePath, 
      paramExportFormat, paramOpenAfterExport, 
      paramExportOptimizeFor, paramExportRange, paramStartPage, 
      paramEndPage, paramExportItem, paramIncludeDocProps, 
      paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, 
      paramBitmapMissingFonts, paramUseISO19005_1, 
      ref paramMissing); 
    } 
    catch (Exception ex) 
    { 

     // Respond to the error 
    } 
    finally 
    { 
     // Close and release the Document object. 
     if (wordDocument != null) 
     { 
      wordDocument.Close(ref paramMissing, ref paramMissing, 
      ref paramMissing); 
      wordDocument = null; 
     } 
     // Quit Word and release the ApplicationClass object. 
     if (wordApplication != null) 
     { 
      wordApplication.Quit(ref paramMissing, ref paramMissing, 
      ref paramMissing); 
      wordApplication = null; 
     } 
     GC.Collect(); 
     GC.WaitForPendingFinalizers(); 
     GC.Collect(); 
     GC.WaitForPendingFinalizers(); 
    } 
} 

希望这会有所帮助。

注意:我使用的是版本12,表示办公室2007.

1

我从来没有见过任何免费的图书馆将office文档转换为pdf。但是,有免费的PDF打印机驱动程序,如PDFCreatorhttp://sourceforge.net/projects/pdfcreator/files/,所以可能您可以安装其中一个,然后让您的应用程序自动将文档打印到其中一台PDF打印机。

如果它只是一个小型的内部网站或类似网站,可能是一个可行的解决方案。

0

我不知道任何免费的图书馆,因为这是一个非常复杂的领域。有些商业版本可用,但在转换文件时通常不会保真。

我已经在Web Services based PDF Converter上工作过,效果很好,但它不是免费的。

0

有许多处理PDF的开源库。不过,我不确定在格式之间会有什么转换。

您可能需要两个库。一个阅读DOC/DOCX,一个阅读PDF。阅读DOC/DOCX实际上可能是更困难的部分,至少不安装Microsoft Word。如果您有Word,那么您可以访问COM接口来操作Word文档,但显然您必须为Word付费。

维基百科列出了一些图书馆,开源和商业,包括你提到的iTextSharp。

http://en.wikipedia.org/wiki/List_of_PDF_software

由于OpenOffice是开源的,它可能是值得研究的,他们是怎么做到的,因为他们可以读取DOC(和DOCX?),并写出到PDF。

或者,您可以查看PDF打印机解决方案。同样,我不确定开源/免费解决方案,但如果有的话,您基本上只需从C#打印到一个特殊的PDF打印机,它将转换为PDF文件。有些产品也只是让你把一个文件转储到一个文件夹中并转换它。

我已经使用Adobe Distiller(Acrobat的一部分)和ActivePDF,但这些都是商业解决方案。尽管ActivePDF确实提供了一个库。

还有CutePDF,声称是免费的。没有尝试过,也不确定它们的专业版本有什么限制。