本文介绍了将RTF文件转换为PDF在c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提供一个功能,一个RTF / WORD文件转换为PDF并将其作为附件在电子邮件中,为了这个,我尝试了代码,如下所示:

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:

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

    }



但它给出了$ B错误$ b doc.SaveAs2000(REF outputFileName,FILEFORMAT文献,文献oMissing,裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing,裁判oMissing);
语句。

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

这可能有原因,我们有Microsoft Office 2007,并在这,没有任何选项,保存为PDF文件。虽然Microsoft Office 2010中有该选项,因此,这段代码可能会工作时的Microsoft Office 2010安装在服务器上。

This may have reason that we have Microsoft Office 2007 and in this, there is not any option to save as PDF file. While in Microsoft Office 2010 it has that option so this code may work when Microsoft Office 2010 is installed on server.

推荐答案

是的,它在2010年我最近用它工作,但我相信这是2007年的增加保存为PDF格式的功能太

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

也许尝试这个的

这篇关于将RTF文件转换为PDF在c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 05:51