加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

在VB.Net中将Doc文件转换为PDF

发布时间:2020-12-17 00:02:44 所属栏目:大数据 来源:网络整理
导读:我有一种情况需要将Doc文件转换为PDF文件.我在vb.net中开发windows应用程序.如果可能的话,我也不想使用第三方DLL. 所以有人能给我一些更多的想法吗? 您可以使用Office Interop.但最好使用像Aspose这样的托管库 using Microsoft.Office.Interop.Word;using S
我有一种情况需要将Doc文件转换为PDF文件.我在vb.net中开发windows应用程序.如果可能的话,我也不想使用第三方DLL.
所以有人能给我一些更多的想法吗?
您可以使用Office Interop.但最好使用像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(@"serverfolder");
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);
    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);

    // 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);
    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);
word = null;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读