2022-12-02 13:43:20
c# 实现word转换成pdf (带图片的word)
private string adobepdfprint = "adobe pdf";
private string adobedisprint = "acrobat distiller";
private string regroot = "software//adobe//acrobat distiller//";
private string printerfilename = "acrodist.exe";
private string regname = "installpath";
/// <summary>
/// 获取acrodist.exe的安装路径
/// </summary>
/// <returns></returns>
private string getadobedisfilepath()
{
registrykey regkey = null;
registrykey acrodistkey = null;
string printername = string.empty;
int i;
string regrootversion = string.empty;
regkey = registry.localmachine;
// acrodist的4-8版本适用
for (i = 4; i < 9; i++)
{
regrootversion = string.format("{0}{1}.0", regroot, i);
acrodistkey = regkey.opensubkey(regrootversion);
if (acrodistkey != null)
{
printername = acrodistkey.getvalue(regname, "") + "//" + printerfilename;
if (file.exists(printername))
{
return printername;
}
}
}
throw new exception("acrobat distiller printer not found!");
}
/// <summary>
/// 获取adobe printer
/// "adobe pdf" 或 "acrobat distiller"
/// </summary>
/// <returns></returns>
private string getadobeprinter()
{
foreach (string printername in printersettings.installedprinters)
{
if (printername.toupper().indexof(adobepdfprint.toupper()) != -1 || printername.toupper().indexof(adobedisprint.toupper()) != -1)
{
return printername;
}
}
return string.empty;
}
public void convertword2pdf(string sourcefile)
{
if (printersettings.installedprinters.count == 0)
throw new exception("did not find the printer, please install the printer.");
if (!file.exists(sourcefile))
throw new exception(string.format("file not found:{0}", sourcefile));
string strdir = path.getdirectoryname(sourcefile);
string strname = path.getfilenamewithoutextension(sourcefile);
string prnfile = string.format("{0}//{1}.prn",strdir,strname);
string pdffile = string.format("{0}//{1}.pdf",strdir,strname);
object objprnfile = (object)prnfile;
object background = true;
object printtofile = true;
object collate = true;
object append = false;
object manualduplexprint = false;
object copies = false;
object range = word.wdprintoutrange.wdprintalldocument;
object missing = system.reflection.missing.value;
string msg = string.empty;
string adobeprinter = getadobeprinter();
if (adobeprinter.length == 0)
throw new exception("did not find adobe pdf printer or acrobat distiller printer, please install the printer.");
iword word = new word2003factory().createword();
// 打开word文档
word.document doc = word.opendocument(sourcefile);
string oldprint = doc.application.activeprinter;
// 将adobe printer设置为默认打印机
doc.application.activeprinter = adobeprinter;
datetime start = datetime.now;
datetime end = start.addseconds(20);
// word文档打印到prn文件
doc.printout(ref background, ref append, ref range, ref objprnfile, ref missing, ref missing,
ref missing, ref missing,ref missing, ref printtofile, ref collate, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
while (!file.exists(prnfile))
{
if (datetime.now > end)
{
throw new exception("word document print to prn document overtime");
}
else
{
application.doevents();
}
}
doc.application.activeprinter = oldprint;
word.close(false);
doc = null;
word = null;
// prn装pdf
process objprocess = new process();
objprocess.startinfo.createnowindow = true;
objprocess.startinfo.useshellexecute = false;
objprocess.startinfo.filename = getadobedisfilepath();
objprocess.startinfo.arguments = prnfile;
start = datetime.now;
end = start.addseconds(20);
objprocess.start();
while (!file.exists(pdffile))
{
if (datetime.now > end)
{
throw new exception("word document print to prn document overtime");
}
else
{
application.doevents();
}
}
objprocess.closemainwindow();
//if (file.exists(prnfile))
//{
// file.delete(prnfile);
//}
/*
acrodistxlib.pdfdistiller pdfdis = new acrodistxlib.pdfdistiller();
pdfdis.filetopdf(prnfile, pdffile, string.empty);
start = datetime.now;
end = start.addseconds(20);
while(!file.exists(pdffile))
{
if (datetime.now > end)
{
throw new exception("prn document print to pdf document overtime");
}
else
{
application.doevents();
}
}
pdfdis = null;
*/
}
其实这个网上有不少案例,你可以都搜搜,主要的是去下载个别案例,对比写写,这样就知道了
vb代码如何实现将word文档转换成pdf文档?
1、首先你在网上下载word转换成pdf格式的软件,如我的是dopdf-v7,或者是wordtopdftool,
2、安装完成后,打开word文档点打印,选择安装的转换虚拟打印机打印,我的如下图所示
3、点击打印保存桌面即可
希望能帮到你,祝你工作愉快~如有需要软件请联系腾讯号378823078
word转成pdf格式
第一种方法:安装虚拟pdf打印机pdf995,然后,打开word文档,选择:打印;选择打印机类型:pdf995,确定即可;
第二种方法:安装adobe acrobt专业软件后,打开word文档后,选反:另存为pdf文件,即可生产pdf文件。
如何将word转化成pdf
有很多转换器可用,推荐用pdffactory pro或adobe acrobat,安装后会在系统中生成类似打印机,你用word编辑好文档选择这个打印机就可以输出pdf文档了。安装adobe acrobat还可以在word中直接另存为pdf文档。
利用word的宏批量把word转换为pdf
我不会用宏,提供以下方法:
步骤:
1. 把所有需转换的word文档放在一下文件夹里
2. ctrl+a 选择全部文档 (如果太多可能转换起来会很慢)
3. 右击->选择 [转换成adobe pdf]
4. 会提示你另存为
前提要装pdf程式,acrobat8 或 acrobat9都可以,网上有下载。 求采纳啊