生成的PDF打印选项被阻止-Interop



我使用界面Microsoft.Office.Interop.Word.Document()将DOC/DOCX文件转换为PDF,但生成的PDF文件的打印选项被阻止。我使用"SaveAs2"方法,只传递两个参数,即文件名和文件格式。

对于通过Interop生成的pdf,是否有任何方法可以始终解锁打印选项?或者这是MS Office配置?

公共静态字符串convertFileDocDocx(字符串fileDocDocx){尝试{if(fileDocx.ToUpper().Contains(".DOX")||fileDocx.ToUpper().Contains(".DOT"){var wordDoc=新建Microsoft.Office.Interop.Word.Document();var wordApp=新建Microsoft.Office.Interop.Word.Application();对象oMissing=System.Type.Missing;object fileFormat=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;
}

下面创建一个可打印的PDF文件:

using Word=Microsoft.Office.Interop.Word;
....
Word.Application winword = new Word.Application();
Word.Document document = winword.Documents.Open(@"C:tempHallo.docx");
object SavePDFFormat = Word.WdSaveFormat.wdFormatPDF;
document.SaveAs2(@"C:tempHallo.pdf", ref SavePDFFormat);
document.Close();
winword.Quit();

最新更新