2017-11-16 14 views
0

DOC/DOCXファイルをPDFに変換するためにMicrosoft.Office.Interop.Word.Document()インターフェイスを使用していますが、生成されたPDFファイルは印刷オプションがブロックされています。私はファイル名とファイル形式である2つのパラメータだけを渡し、 "SaveAs2"メソッドを使用しています。PDFが生成されました印刷オプションがブロックされました - Interop

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; }

+1

'あなたがその結論に達しましたかblocked.'印刷オプションでありますか? – mjwills

+0

@mjwills準備ができました。 – dsd

+0

@mjwills Interopが「犯人」である場合、私は100%確信していません。これが起こるかどうか、またはMS Officeのセットアップが必要かどうかを尋ねています。 – dsd

答えて

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(); 
関連する問題