Reputation: 33
I'm using the interface Microsoft.Office.Interop.Word.Document() to convert DOC/DOCX files to PDF, but the PDF files that are generated are with the print option blocked. I'm using the "SaveAs2" method, passing only two parameters which are the file name and file format.
Is there any way for the print option to always be unlocked for the pdf that is generated via Interop? Or is this an MS Office configuration?
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;
}
Upvotes: 1
Views: 478
Reputation: 11322
The following creates a printable PDF
file:
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();
Upvotes: 0