2

次のコードを使用しています プログラムで Word ファイルを PDF に変換するにはどうすればよいですか? doc ファイルを pdf に変換します。しかし、コードでは、特定のディレクトリからすべての .doc ファイルを取得することが言及されていますが、アプリケーションから選択したファイルのみ、または場合によっては単一のファイルのみが必要です。

ガイドしてください

ありがとう!

4

3 に答える 3

5

1 つのファイル用に変更された同じコード:

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 a Word file
FileInfo wordFile = new FileInfo("myDoc.doc");

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

// 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;
于 2011-03-11T08:03:49.283 に答える
2

このコードは、フォルダー内のすべての .doc ファイルを検索し、それらをループ処理しています。フォルダに 1 つのファイルがある場合は、そのファイルを変換するだけです。

次のコード行を変更できます。

FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

ファイルを探すだけです。

FileInfo[] wordFiles = dirInfo.GetFiles("myselectedfile.doc");
于 2011-03-11T07:59:26.040 に答える
0

こんにちは、

これらのリンクが大いに役立つと思います:

https://stackoverflow.com/questions/891531/convert-xls-doc-files-to-pdf-with-c

http://www.codeproject.com/KB/cs/convertdocintootherformat.aspx

于 2011-03-11T07:59:08.413 に答える