簡體   English   中英

在C#中將doc轉換為pdf

[英]convert doc to pdf in c#

我如何使用 asp.net c# 將.doc 轉換為.pdf。 我不能使用任何第三方組件。

代碼應該在

  1. C# 或 vb.net
  2. 與VS 2005兼容。(如果不兼容,那么也請發布您的回復,然后我會手動轉換為VS 2005)

讓我知道是否有任何疑問。

謝謝!

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

先決條件:

  • MS word2007 with (Primary Interoperability assembly will be installed by default).
  • 插件SaveAsPDFandXPS (MS 站點免費)

確保您參考了 Word.12。 它會自動將 Microsoft.Office.interop.word 添加到您的引用中。 按照這些用於其他辦公應用程序。 (注意:您應該已經安裝了 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);

您可以使用Microsoft.Office.Interop.Word.dll將 Word 文件轉換為 PDF。

首先安裝包並添加對它的引用。

using Microsoft.Office.Interop.Word;

然后使用以下代碼將 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");

您可以使用我的代碼,它毫無例外地工作並且不會在后台進程中保留打開的 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