如何以编程方式将Word文件转换为PDF?
我发现了几个开源/免费软件程序,允许您将.doc文件转换为.pdf文件,但它们都是应用程序/打印机驱动程序,没有附加SDK。
我发现有几个程序确实有一个SDK,允许您将.doc文件转换为.pdf文件,但它们都是专有类型,每个许可证约2,000美元。
有谁知道任何清洁,廉价(最好是免费的)程序化解决方案来解决我的问题,使用C#或VB.NET?
谢谢!
使用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 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, 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;
总结它的vb.net用户,免费选项(必须安装办公室):
微软办公集体下载:
pia for office 2007
将引用添加到Microsoft.Office.Interop.Word.Application
将使用或导入(vb.net)语句添加到Microsoft.Office.Interop.Word.Application
VB.NET例子:
Dim word As Application = New Application()
Dim doc As Document = word.Documents.Open("c:document.docx")
doc.Activate()
doc.SaveAs2("c:document.pdf", WdSaveFormat.wdFormatPDF)
doc.Close()
PDFCreator有一个COM组件,可从.NET或VBScript调用(下载中包含示例)。
但是,在我看来,打印机正是您所需要的 - 只需将其与Word的自动化结合起来,您就应该顺利。
链接地址: http://www.djcxy.com/p/46761.html