将单个 doc 文件转换为 pdf

发布于 2024-10-21 17:23:38 字数 248 浏览 6 评论 0原文

我正在使用以下代码 如何以编程方式将 Word 文件转换为 PDF ? 将 doc 文件转换为 pdf。但代码提到从特定目录获取所有 .doc 文件,而我希望只包含我从应用程序中选择的文件,或者有时只包含单个文件。

请指导我

谢谢!

I am using following code How do I convert Word files to PDF programmatically? to convert the doc file to pdf. but the code mentions getting all .doc files from specific directory whereas i wish to have only those i have selected from application or sometimes only a single file.

Please guide me

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心奴独伤 2024-10-28 17:23:38

相同的代码,针对单个文件进行修改:

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;

The same code, modified for one single file:

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;
亽野灬性zι浪 2024-10-28 17:23:38

该代码正在查找文件夹中的所有 .doc 文件并循环遍历它们。如果您只有文件夹中的一个文件,它只会转换该文件。

您可以更改这行代码:

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

仅查找您的文件,例如

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

The code is looking for all .doc files in a folder and looping through them. If you jut had the one file in the folder, it would just convert that one.

You could change this line of code:

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

to just look for your file, eg

FileInfo[] wordFiles = dirInfo.GetFiles("myselectedfile.doc");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文