2012-03-19 49 views
0

DocファイルをPDFファイルに変換する必要がある状況があります。私はvb.netでWindowsアプリケーションをdevepoingしています。また、可能であれば、私はサードパーティ製のDLLを使用したくありません。 誰も私にもう少し考えを与えることができますか?VB.NetでDocファイルをPDFに変換

+0

だから、ソリューションあなたはこれを実装するために採用している、のいずれか以下か、多少他の一つのようないくつかの管理ライブラリを使用する方が良いです? –

+0

私は次の回答から2番目を使用しています。私はMicrosoft.Office.Interop.Wordを使用しています。 –

答えて

2

あなたはこのためにOfficeの相互運用機能を使用することができます。しかし、Aspose社

using Microsoft.Office.Interop.Word; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 

... 

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

// Get list of Word files in specified directory 
DirectoryInfo dirInfo = new DirectoryInfo(@"\\server\folder"); 
FileInfo[] wordFiles = dirInfo.GetFiles("*.doc"); 

word.Visible = false; 
word.ScreenUpdating = false; 

foreach (FileInfo wordFile in wordFiles) 
{ 
    // Cast as Object for word Open method 
    Object filename = (Object)wordFile.FullName; 

    // Use the dummy value as a placeholder for optional arguments 
    Document doc = word.Documents.Open(ref filename, 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(); 

    object outputFileName = wordFile.FullName.Replace(".doc", ".pdf"); 
    object fileFormat = WdSaveFormat.wdFormatPDF; 

    // Save document into PDF Format 
    doc.SaveAs(ref outputFileName, 
     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); 

    // 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.     
    object saveChanges = WdSaveOptions.wdDoNotSaveChanges; 
    ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing); 
    doc = null; 
} 

// word has to be cast to type _Application so that it will find 
// the correct Quit method. 
((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing); 
word = null; 
+0

その偉大な。しかし、私は第三者DLLを使用したくありません。それ以外の方法はありますか? Microsoft.Office.Interop.Wordを使用している場合、私はexeをインストールする必要がある各PCにオフィスをインストールする必要があります。 PC上では不可能です。 –

0

2007 Microsoft Office Add-in:Microsoft PDFと2007 Microsoft Office Add-inとして保存:Microsoft XPSとして保存すると、Microsoft Office Word 2007でPDFおよびXPS形式の文書をエクスポートおよび保存できます。

はこれらをチェックしてください:
Saving Word 2007 Documents to PDF and XPS Formats
How to convert Word to PDF in asp.net

あなたはThirtパーティのDLLを使用したい場合は、このSOスレッドをチェックしてください。Converting MS Word Documents to PDF in ASP.NET

1
Imports Microsoft.Office.Interop 

'This code happens to be loading a template, but it isn't necessary... 

'Opens Word Application 

Dim MyApp As New Word.Application 

'Opens new WordDoc 

Dim MyWordDoc As Word.Document = MyApp.Documents.Add(template) 

MyApp.Visible = True 

MyWordDoc = MyApp.ActiveDocument 

'code to fill doc 

'code to fill doc 

'code to fill doc 

MyWordDoc.SaveAs(FileLocation, Word.WdSaveFormat.wdFormatPDF) 
関連する問題