简体   繁体   English

在C#中将RTF文件转换为PDF

[英]Converting RTF file to PDF in c#

I need to provide a functionality to convert a RTF/WORD file to PDF and send it as attachment in an email, for this i tried the code as shown below: 我需要提供将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;
        }

    }

But it gives error on doc.SaveAs2000(ref outputFileName, ref fileFormat, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 但是它在doc.SaveAs2000上给出了错误(ref outputFileName,ref fileFormat,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing); statement. 声明。

This may have reason that we have Microsoft Office 2007 and in this, there is not any option to save as PDF file. 这可能是因为我们拥有Microsoft Office 2007的原因,并且在其中没有任何选择另存为PDF文件。 While in Microsoft Office 2010 it has that option so this code may work when Microsoft Office 2010 is installed on server. 在Microsoft Office 2010中,它具有该选项,因此,在服务器上安装Microsoft Office 2010时,此代码可能会起作用。

Yes, it does work in 2010. I've used it recently but I believe there is a patch for 2007 that add the save as PDF functionality too 是的,它在2010年确实可以使用。我最近使用过它,但我相信2007年有一个补丁,它也添加了“另存为PDF”功能。

Maybe try this http://msdn.microsoft.com/en-us/library/bb412305(v=office.12).aspx 也许尝试一下http://msdn.microsoft.com/en-us/library/bb412305(v=office.12).aspx

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

First need to add Microsoft.Office.Interop.dll from nugget package. 首先需要从块包中添加Microsoft.Office.Interop.dll。 Add .rft orginal file as fileToProcess and in tempFilePath add location path where you need to save converted pdf file. 将.rft原始文件添加为fileToProcess,并在tempFilePath中添加需要保存转换后的pdf文件的位置路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM