One of my recent tasks was to code to open a .doc or .xls as .pdf. The file should open as .pdf and then it’s upto the user to save the .pdf. I tell you I had to scourge the internet looking for good leads. There are many articles in www.codeproject.com, many threads in asp.net forums, and one project called iTextDotNet in www.sourceforge.net, a few blogs – all related to the same topic. But it was in www.msdn.com I found what I needed. I immediately tested it and cooooollllllll……gotcha. For developers, here you go….
I did this in VS 2008 and Office 2007. I know this works good on .docx, .xlsx(Office 2007), .doc (Microsoft Office Word 97 – 2003 Document), .xls (Microsoft Office Excel 97-2003 Worksheet) – I couldn’t try this code on Office 2003 directly but I saved the files in Office 2003 Compatible format and it works good.
TODO:
1. Add namespaces
using MSExcel = Microsoft.Office.Interop.Excel;//Add References-> COM
2. Install Addin : SaveAsPDFAndXPS
//code
object Unknown = Type.Missing;
MSWord.Application newApp = new Microsoft.Office.Interop.Word.Application();
//source document
object src_doc = “C:\Users\admin\Desktop\DocumentViewer\test.docx”;
newApp.Documents.Open(ref src_doc, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown); //# of params 16, # of Unknowns 14
//save as PDF
object format = MSWord.WdSaveFormat.wdFormatPDF;
newApp.ActiveDocument.SaveAs(ref des_doc, ref format, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
object des_doc= “C:\Users\admin\Desktop\DocumentViewer\test.pdf”;
newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
//now to open .xls as .pdf
MSExcel.Application newExcelAppn = new Microsoft.Office.Interop.Excel.Application ();
MSExcel.Workbook newbook = null;
string src_xls = “C:\Users\admin\Desktop\DocumentViewer\To_Bret_Conard.xlsx”;
object des_xls = “C:\Users\admin\Desktop\DocumentViewer\To_Bret_Conard.pdf”;
newbook = newExcelAppn.Workbooks.Open(src_xls, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown, Unknown);
//specify the format. PDF
MSExcel.XlFixedFormatType paramExportFormat = MSExcel.XlFixedFormatType.xlTypePDF;
// Save it in the target format.
newbook.ExportAsFixedFormat(paramExportFormat, des_xls, MSExcel.XlFixedFormatQuality.xlQualityStandard, true, true, Unknown, Unknown, false, Unknown);
newExcelAppn.Quit();
//view the pdf in browser
byte[] buffer = new byte[1024];
long byteCount;
inStr =File.OpenRead(“C:\Users\admin\Desktop\DocumentViewer\To_Bret_Conard.pdf”);
while((byteCount = inStr.Read(buffer, 0, buffer.Length)) > 0)
{
if(Context.Response.IsClientConnected)
{
Context.Response.ContentType = “application/pdf”;
Context.Response.OutputStream.Write(buffer, 0, buffer.Length);
Context.Response.Flush();
}
}
References:
http://msdn.microsoft.com/en-us/library/bb407651.aspx
http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/f8989c05-d04a-4b4a-be0f-fc0055691de7