首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >C#。文档转换为PDF

C#。文档转换为PDF
EN

Stack Overflow用户
提问于 2010-05-20 17:10:47
回答 4查看 4.6K关注 0票数 2

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

doc/docx转pdf xls/xlsx转pdf ppt/pps转pdf

ITextSharp是否提供了这样的功能?请只提及开源库或自由库。

谢谢

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2010-05-21 15:49:12

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

代码下面。

代码语言:javascript
运行
复制
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,意思是Office2007。

票数 1
EN

Stack Overflow用户

发布于 2010-05-20 17:31:35

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

如果它只是一个小的intranet站点或类似的站点,可能是一个可能的解决方案。

票数 1
EN

Stack Overflow用户

发布于 2010-05-20 18:31:20

我不知道有什么免费的库,因为这是一个非常复杂的领域。有些商业软件是可用的,但它们在转换文件时通常缺乏保真度。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2872351

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档