栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何以编程方式将Word文件转换为PDF?[关闭]

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何以编程方式将Word文件转换为PDF?[关闭]

使用foreach循环而不是for循环-它解决了我的问题。

int j = 0;foreach (Microsoft.Office.Interop.Word.Page p in pane.Pages){    var bits = p.EnhmetaFileBits;    var target = path1 +j.ToString()+  "_image.doc";    try    {        using (var ms = new MemoryStream((byte[])(bits)))        { var image = System.Drawing.Image.FromStream(ms); var pngTarget = Path.ChangeExtension(target, "png"); image.Save(pngTarget, System.Drawing.Imaging.ImageFormat.Png);        }    }    catch (System.Exception ex)    {        MessageBox.Show(ex.Message);      }    j++;}

这是对我有用的程序的修改。它使用安装了“
另存为PDF”加载项的
Word 2007
。它在目录中搜索.doc文件,在Word中打开它们,然后将它们另存为PDF。请注意,您需要在解决方案中添加对Microsoft.Office.Interop.Word的引用。

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 objectMicrosoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();// C# doesn't have optional arguments so we'll need a dummy valueobject oMissing = System.Reflection.Missing.Value;// Get list of Word files in specified directoryDirectoryInfo 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, 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;


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/400748.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号