I can successfully take an excel file and export it as a PDF file in c#
private static void ExportWorkbookToPDF(string workbook, string output)
{
if (string.IsNullOrEmpty(workbook) || string.IsNullOrEmpty(output))
{
throw new NullReferenceException("Cannot create PDF copy " +
"from empty workbook.");
}
Application excelApplication = new Application();
excelApplication.ScreenUpdating = false;
excelApplication.DisplayAlerts = false;
excelApplication.Visible = false;
Workbook excelWorkbook = excelApplication.Workbooks.Open(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) +
"\\" + workbook);
if (excelWorkbook == null)
{
excelApplication.Quit();
excelApplication = null;
excelWorkbook = null;
throw new NullReferenceException("Cannot create new excel workbook.");
}
try
{
excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF,
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) +
"\\" + output);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
}
finally
{
excelWorkbook.Close();
excelApplication.Quit();
excelApplication = null;
excelWorkbook = null;
}
}
What parameter or object do I need to access in order to save the excel file as page width instead of page height?
The ExportAsFixedFormat method is used to publish a workbook to either the PDF or XPS format.
Before you run the macro, select the sheet(s) that you want to export to the PDF file. When the macro starts, it sets variables for the active sheet, and the active workbook. Those will be used to set the default file name and folder. A time stamp will be added to the default name, in the format yyyymmdd_hhmm.
I have found the property required to force the export of your workbook in a PDF with Landscape view.
try
{
((Microsoft.Office.Interop.Excel._Worksheet)
excelWorkbook.ActiveSheet).PageSetup.Orientation =
Microsoft.Office.Interop.Excel.XlPageOrientation.xlLandscape;
excelWorkbook.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF,
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) +
"\\" + output);
}
Install The 2007 Microsoft Office Suite Service Pack 3 (SP3) http://www.microsoft.com/en-in/download/details.aspx?id=27838
Install 2007 Microsoft Office Add-in: Microsoft Save as PDF or XPS http://www.microsoft.com/en-in/download/details.aspx?id=7
Mandatory : Set Microsoft XPS Document Writer as default printer in server. Because ExportAsFixedFormat function executes Microsoft XPS Document Writer to convert Excel to PDF.
This is working for me.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With