我正在使用Microsoft.Office.Interop.Word.Document()接口将DOC / DOCX文件转换为PDF,但生成的PDF文件的打印选项已被阻止。我正在使用“SaveAs2”方法,只传递两个参数,即文件名和文件格式。
对于通过Interop生成的pdf,是否有任何方法可以始终解锁打印选项?或者这是MS Office配置吗?
public static string convertFileDocDocx(string fileDocDocx)
{
try
{
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();
Object 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;
}
答案 0 :(得分:0)
以下内容创建了一个可打印的PDF
文件:
using Word=Microsoft.Office.Interop.Word;
....
Word.Application winword = new Word.Application();
Word.Document document = winword.Documents.Open(@"C:\temp\Hallo.docx");
object SavePDFFormat = Word.WdSaveFormat.wdFormatPDF;
document.SaveAs2(@"C:\temp\Hallo.pdf", ref SavePDFFormat);
document.Close();
winword.Quit();