【发布时间】:2018-04-30 12:22:47
【问题描述】:
我正在使用接口 Microsoft.Office.Interop.Word.Document() 将 DOC/DOCX 文件转换为 PDF,但生成的 PDF 文件的打印选项被阻止。我使用的是“SaveAs2”方法,只传递文件名和文件格式两个参数。
对于通过 Interop 生成的 pdf,是否有任何方法可以始终解锁打印选项?还是这是一个 MS Office 配置?
公共静态字符串 convertFileDocDocx(字符串 fileDocDocx) { 尝试 { if (fileDocDocx.ToUpper().Contains(".DOTX") || fileDocDocx.ToUpper().Contains(".DOCX") || fileDocDocx.ToUpper().Contains(".DOT") || fileDocDocx.ToUpper().Contains(".DOC")) { var wordDoc = new Microsoft.Office.Interop.Word.Document(); var wordApp = new Microsoft.Office.Interop.Word.Application(); 对象 oMissing = System.Type.Missing; 对象文件格式 = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatPDF; wordDoc = wordApp.Documents.Add(fileDocDocx, oMissing, oMissing, oMissing);
wordDoc = wordApp.Documents.Open(fileDocDocx, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing, oMissing, oMissing, oMissing, oMissing,
oMissing, oMissing, oMissing);
wordDoc.Activate();
var secao = wordDoc.Sections[1];
var rng = secao.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
var rng2 = secao.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
if (fileDocDocx.Substring(fileDocDocx.Length - 4).ToUpper() == "DOTX" || fileDocDocx.Substring(fileDocDocx.Length - 4).ToUpper() == "DOCX")
wordDoc.SaveAs2(fileDocDocx.Substring(0, fileDocDocx.Length - 5) + ".pdf", fileFormat);
else if (fileDocDocx.Substring(fileDocDocx.Length - 3).ToUpper() == "DOT" || fileDocDocx.Substring(fileDocDocx.Length - 3).ToUpper() == "DOC")
wordDoc.SaveAs2(fileDocDocx.Substring(0, fileDocDocx.Length - 4) + ".pdf", fileFormat);
wordDoc.Close();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
wordDoc = null;
wordApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
wordApp = null;
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
}
}
catch (Exception ex)
{
Util.insertLog(ex);
Process[] processList = Process.GetProcesses();
for (int i = 0; i < processList.Length; i++)
{
if (processList[i].ProcessName.Contains("WINWORD"))
processList[i].Kill();
}
return "Error:" + ex.Message;
}
return fileDocDocx;
}
【问题讨论】:
-
are with the print option blocked.你是怎么得出这个结论的? -
@mjwills 准备好了,我放代码
-
@mjwills 我不能 100% 确定 Interop 是否是“罪魁祸首”,我只是想问这是否会发生,或者是否是一些 MS Office 设置。
标签: c# pdf ms-word office-interop