【问题标题】:Converting RTF file to PDF in c#在 C# 中将 RTF 文件转换为 PDF
【发布时间】:2012-04-21 09:38:18
【问题描述】:

我需要提供将 RTF/WORD 文件转换为 PDF 并将其作为电子邮件附件发送的功能,为此我尝试了如下所示的代码:

    // 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;

    Document doc;
    protected void Page_Load(object sender, EventArgs e)
    {
        ConvertToPDF("test.doc");
    }

    void ConvertToPDF(string sFileName)
    {
        // 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;

        Document doc;
        try
        {
            word.Visible = false;
            word.ScreenUpdating = false;

            DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(".") + "\\TempDoc");
            FileInfo[] wordFile = dirInfo.GetFiles(sFileName);

            if (wordFile.Length > 0)
            {
                Object filename = (Object)wordFile[0].FullName;

                // Use the dummy value as a placeholder for optional arguments
                doc = word.Documents.Open2000(ref filename, 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[0].FullName.Replace(".doc", "");
                object fileFormat = WdSaveFormat.wdFormatPDF;

                // Save document into PDF Formats
                doc.SaveAs2000(ref outputFileName, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex);
        }
        finally
        {
            // 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.
            doc = null;

            // word has to be cast to type _Application so that it will find
            // the correct Quit method.
            word = null;
        }

    }

但它给出了错误 doc.SaveAs2000(ref outputFileName, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 声明。

这可能是我们有 Microsoft Office 2007 的原因,在此,没有任何选项可以保存为 PDF 文件。在 Microsoft Office 2010 中,它具有该选项,因此当 Microsoft Office 2010 安装在服务器上时,此代码可能会起作用。

【问题讨论】:

标签: c#


【解决方案1】:

是的,它在 2010 年确实有效。我最近使用过它,但我相信 2007 年有一个补丁也添加了另存为 PDF 功能

也许试试这个http://msdn.microsoft.com/en-us/library/bb412305(v=office.12).aspx

【讨论】:

  • 我也在尝试这个。非常感谢。
  • 检索具有 CLSID {000209FF-0000-0000-C000-000000000046} 的组件的 COM 类工厂失败,原因是以下错误:80070005.......我在我的应用程序中遇到了这个错误在此之前,我制作了一个将 docx 转换为 pdf 的演示项目。你有这方面的知识吗??
【解决方案2】:
 Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();

        appWord.Visible = false;

        //object oMissing = Type.Missing;
        object oMissing = System.Reflection.Missing.Value;
        // Declare variables for the Document.ExportAsFixedFormat method parameters.
        bool paramOpenAfterExport = false;
        Office.WdExportOptimizeFor paramExportOptimizeFor =
        Office.WdExportOptimizeFor.wdExportOptimizeForOnScreen;
        Office.WdExportRange paramExportRange = Office.WdExportRange.wdExportAllDocument;
        int paramStartPage = 0;
        int paramEndPage = 0;
        Office.WdExportItem paramExportItem = Office.WdExportItem.wdExportDocumentWithMarkup; //This is 
        //the key to keep track changes markup;
        bool paramIncludeDocProps = true;
        bool paramKeepIRM = true;
        Office.WdExportCreateBookmarks paramCreateBookmarks =
                    Office.WdExportCreateBookmarks.wdExportCreateWordBookmarks;
        bool paramDocStructureTags = true;
        bool paramBitmapMissingFonts = true;
        bool paramUseISO19005_1 = false;

        Microsoft.Office.Interop.Word.Document wordDocument = appWord.Documents.Open(fileToProcess);

        wordDocument.ExportAsFixedFormat(tempPDFFilePath, Office.WdExportFormat.wdExportFormatPDF, paramOpenAfterExport, paramExportOptimizeFor, paramExportRange, paramStartPage, paramEndPage, paramExportItem,
        paramIncludeDocProps, paramKeepIRM, paramCreateBookmarks, paramDocStructureTags, paramBitmapMissingFonts, paramUseISO19005_1, ref oMissing);

        ((Office._Document)wordDocument).Close(false, Type.Missing, Type.Missing);
        ((Office._Application)appWord).Quit(false);

首先需要从 nugget 包中添加 Microsoft.Office.Interop.dll。将 .rft 原始文件添加为 fileToProcess 并在 tempFilePath 添加您需要保存转换后的 pdf 文件的位置路径。

【讨论】:

  • 此解决方案也用于在c#中将文本文件转换为pdf
猜你喜欢
  • 2010-12-23
  • 1970-01-01
  • 2012-06-18
  • 2013-10-22
  • 2014-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多