简体   繁体   English

在C#中将doc转换为pdf

[英]convert doc to pdf in c#

How can i convert.doc to.pdf using asp.net c#.我如何使用 asp.net c# 将.doc 转换为.pdf。 I cannot use any third party component.我不能使用任何第三方组件。

The code should be in代码应该在

  1. C# or vb.net C# 或 vb.net
  2. Compatible with VS 2005. (If not, then also please post your replies, i would then manually convert to VS 2005)与VS 2005兼容。(如果不兼容,那么也请发布您的回复,然后我会手动转换为VS 2005)

Let me know if any query.让我知道是否有任何疑问。

Thanks!谢谢!

private Microsoft.Office.Interop.Word.ApplicationClass MSdoc;       

        //Use for the parameter whose type are not known or say Missing
        object Unknown = Type.Missing;

  private void word2PDF(object Source, object Target)
        {   //Creating the instance of Word Application          
       if (MSdoc == null)MSdoc = new Microsoft.Office.Interop.Word.ApplicationClass();

            try
            {  
                MSdoc.Visible = false;               
                MSdoc.Documents.Open(ref Source, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
                MSdoc.Application.Visible = false;
                MSdoc.WindowState = Microsoft.Office.Interop.Word.WdWindowState.wdWindowStateMinimize;               

                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF;

                MSdoc.ActiveDocument.SaveAs(ref Target, ref format,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                       ref Unknown, ref Unknown);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            finally
            {
                if (MSdoc != null)
                {
                    MSdoc.Documents.Close(ref Unknown, ref Unknown, ref Unknown);
                    //WordDoc.Application.Quit(ref Unknown, ref Unknown, ref Unknown);
                }               
                // for closing the application
                WordDoc.Quit(ref Unknown, ref Unknown, ref Unknown);
            }
        }

Prerequisites:先决条件:

  • MS word2007 with (Primary Interoperability assembly will be installed by default). MS word2007 with (Primary Interoperability assembly will be installed by default).
  • plugin SaveAsPDFandXPS (free from MS Site)插件SaveAsPDFandXPS (MS 站点免费)

Make sure you have reference to Word.12.确保您参考了 Word.12。 It will automatically add Microsoft.Office.interop.word to your reference.它会自动将 Microsoft.Office.interop.word 添加到您的引用中。 Follow these for other office application.按照这些用于其他办公应用程序。 (Note: you should have installed VS 2005 Tools for Office 2nd Ed. Runtime (VSTO 2005 SE) (x86) (注意:您应该已经安装了 VS 2005 Tools for Office 2nd Ed. Runtime (VSTO 2005 SE) (x86)

//Add Office Library

using Word = Microsoft.Office.Interop.Word;

object str_letter_path = @"D:\DOCTEST.doc";
object outputFilePathPDF = @"D:\PDFTEST.PDF";

Word.Application wordApp = new Word.Application();
wordApp.Visible = false;
wordApp.ScreenUpdating = false;

object oMissing = System.Reflection.Missing.Value;
object fileFormat = Word.WdSaveFormat.wdFormatPDF;

Word.Document doc = wordApp.Documents.Open(ref str_letter_path, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            doc.Activate();

            doc.SaveAs(ref outputFilePathPDF,
                            ref fileFormat, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
            if (doc != null)
                ((Word._Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
            ((Microsoft.Office.Interop.Word._Application)wordApp).Quit(ref saveChanges, ref oMissing, ref oMissing);

You can use Microsoft.Office.Interop.Word.dll to convert Word file to PDF.您可以使用Microsoft.Office.Interop.Word.dll将 Word 文件转换为 PDF。

Install the package first and add a reference to it.首先安装包并添加对它的引用。

using Microsoft.Office.Interop.Word;

Then use the following code to Convert word document to PDF .然后使用以下代码将 word 文档转换为 PDF

Application app = new Application();
Document doc = app.Documents.Open(@"D:/test.docx");
doc.SaveAs2(@"D:/test.pdf", WdSaveFormat.wdFormatPDF);
doc.Close();
app.Quit();
Console.WriteLine("Completed");

You can use my code,it works without any exception and doesn't keep opened COM objects on the background processes.您可以使用我的代码,它毫无例外地工作并且不会在后台进程中保留打开的 COM 对象。

Application app = new Application();
                app.Visible = false;
                app.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                Documents documents = app.Documents;
                Document doc = documents.Open(fileLocation);
                newPath = Path.GetDirectoryName(fileLocation);
                newPath = newPath.Replace(newPath, outLocation);
                if (!File.Exists(newPath))
                {
                    doc.SaveAs2(newPath, WdSaveFormat.wdFormatPDF);
                }

                Marshal.ReleaseComObject(documents);
                doc.Close();
                Marshal.ReleaseComObject(doc);
                app.Quit();
                Marshal.ReleaseComObject(app);

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

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