private void PrintExcelBtnClick(object sender, RoutedEventArgs e) { if (this.textBox1.Text != String.Empty && (string)this.textBox1.Tag != string.Empty) { if (File.Exists((string)this.textBox1.Tag)) { ExcelEngine engine = new ExcelEngine(); IApplication application = engine.Excel; IWorkbook book = application.Workbooks.Open((string)textBox1.Tag); //Open the Excel Document to Convert ExcelToPdfConverter converter = new ExcelToPdfConverter(book); if (printWithPrinterBtn.IsChecked == true || printWithConverterAndPrinterBtn.IsChecked == true) { //Create new printdialog instance. System.Windows.Forms.PrintDialog printDialog = new System.Windows.Forms.PrintDialog(); printDialog.AllowSomePages = true; if (printDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if (printWithConverterAndPrinterBtn.IsChecked == true) { //Print excel document with specified printer settings and converter settings. converter.Print(printDialog.PrinterSettings, GetConverterSettings()); this.Close(); } else { //Print excel document with specified printer settings. converter.Print(printDialog.PrinterSettings); this.Close(); } } } else if (printWithConverterBtn.IsChecked == true) { //Print excel document with specified and converter settings. converter.Print(GetConverterSettings()); this.Close(); } else if (defaultPrintBtn.IsChecked == true) { //print excel document with default printer settings. converter.Print(); this.Close(); } } else { MessageBox.Show("File doesn’t exist"); } } else { MessageBox.Show("Browse an Excel document to convert to Pdf", "Error", MessageBoxButton.OK, MessageBoxImage.Exclamation); } }
private void printExcelBtnClick(object sender, EventArgs e) { if (this.textBox1.Text != String.Empty) { ExcelEngine engine = new ExcelEngine(); IApplication application = engine.Excel; IWorkbook book = application.Workbooks.Open((string)textBox1.Tag); // Initialize the chart to image converter. application.ChartToImageConverter = new ChartToImageConverter(); //Set the Quality of chart Image application.ChartToImageConverter.ScalingMode = ScalingMode.Best; //Open the Excel Document to Convert ExcelToPdfConverter converter = new ExcelToPdfConverter(book); if (printWithPrinterBtn.Checked || printWithConverterAndPrinterBtn.Checked) { //Create new printdialog instance. PrintDialog printDialog = new PrintDialog(); printDialog.AllowSomePages = true; if (printDialog.ShowDialog() == DialogResult.OK) { if (printWithConverterAndPrinterBtn.Checked) { //Print excel document with specified printer settings and converter settings. converter.Print(printDialog.PrinterSettings, GetConverterSettings()); this.Close(); } else { //Print excel document with specified printer settings. converter.Print(printDialog.PrinterSettings); this.Close(); } } } else if (printWithConverterBtn.Checked) { //Print excel document with specified and converter settings. converter.Print(GetConverterSettings()); this.Close(); } else if (defaultPrintBtn.Checked) { //print excel document with default printer settings. converter.Print(); this.Close(); } } else { MessageBox.Show("Browse a word document and click the button to convert as a PDF."); } }
// // GET: /ExcelToPDF/ public ActionResult Print(string button, string Group1, string Group2) { if (button == null) { return(View()); } else if (button == "Input Template") { //Step 1 : Instantiate the spreadsheet creation engine. ExcelEngine excelEngine = new ExcelEngine(); //Step 2 : Instantiate the excel application object. IApplication application = excelEngine.Excel; IWorkbook workbook = application.Workbooks.Open(ResolveApplicationDataPath(@"ExcelTopdfwithChart.xlsx"), ExcelOpenType.Automatic); return(excelEngine.SaveAsActionResult(workbook, "InputTempalte.xlsx", HttpContext.ApplicationInstance.Response, ExcelDownloadType.PromptDialog, ExcelHttpContentType.Excel2013)); } else { ExcelToPdfConverter converter = new ExcelToPdfConverter(ResolveApplicationDataPath("ExcelTopdfwithChart.xlsx")); converter.ChartToImageConverter = new ChartToImageConverter(); //Set the image quality converter.ChartToImageConverter.ScalingMode = ScalingMode.Best; //Intialize the PdfDocument Class PdfDocument pdfDoc = new PdfDocument(); //Intialize the ExcelToPdfConverterSettings class ExcelToPdfConverterSettings converterSettings = new ExcelToPdfConverterSettings(); //Set the Layout Options for the output Pdf page. if (Group2 == "NoScaling") { converterSettings.LayoutOptions = LayoutOptions.NoScaling; } else if (Group2 == "FitAllRowsOnOnePage") { converterSettings.LayoutOptions = LayoutOptions.FitAllRowsOnOnePage; } else if (Group2 == "FitAllColumnsOnOnePage") { converterSettings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage; } else { converterSettings.LayoutOptions = LayoutOptions.FitSheetOnOnePage; } //Assign the output PdfDocument to the TemplateDocument property of ExcelToPdfConverterSettings converterSettings.TemplateDocument = pdfDoc; converterSettings.DisplayGridLines = GridLinesDisplayStyle.Invisible; //Create new printerSettings instance. PrinterSettings printerSettings = new PrinterSettings(); printerSettings.FromPage = 1; printerSettings.ToPage = 3; printerSettings.Collate = true; printerSettings.Copies = 2; printerSettings.Duplex = Duplex.Simplex; if (Group1 == "DefaultPrint") { converter.Print(); } else if (Group1 == "PrintWithConverterSettings") { converter.Print(converterSettings); } else if (Group1 == "PrintWithPrinterSettings") { converter.Print(printerSettings); } else { converter.Print(printerSettings, converterSettings); } return(View()); } }
private void PrintExcelbtn_Click(object sender, EventArgs e) { if (this.textBox1.Text != String.Empty) { ExcelEngine engine = new ExcelEngine(); IApplication application = engine.Excel; IWorkbook book = application.Workbooks.Open((string)textBox1.Tag); //Open the Excel Document to Convert ExcelToPdfConverter converter = new ExcelToPdfConverter(book); //Intialize the ExcelToPdfconverterSettings ExcelToPdfConverterSettings converterSettings = new ExcelToPdfConverterSettings(); if (noScaleRadioBtn.Checked) { converterSettings.LayoutOptions = LayoutOptions.NoScaling; } else if (allRowsRadioBtn.Checked) { converterSettings.LayoutOptions = LayoutOptions.FitAllRowsOnOnePage; } else if (allColumnRadioBtn.Checked) { converterSettings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage; } else { converterSettings.LayoutOptions = LayoutOptions.FitSheetOnOnePage; } if (printWithPrinterBtn.Checked || printWithConverterAndPrinterBtn.Checked) { //Create new printdialog instance. PrintDialog printDialog = new PrintDialog(); printDialog.AllowSomePages = true; if (printDialog.ShowDialog() == DialogResult.OK) { if (printWithConverterAndPrinterBtn.Checked) { //Print excel document with specified printer settings and converter settings. converter.Print(printDialog.PrinterSettings, GetConverterSettings()); this.Close(); } else { //Print excel document with specified printer settings. converter.Print(printDialog.PrinterSettings); this.Close(); } } } else if (printWithConverterBtn.Checked) { //Print excel document with specified and converter settings. converter.Print(GetConverterSettings()); this.Close(); } else if (defaultPrintBtn.Checked) { //print excel document with default printer settings. converter.Print(); this.Close(); } } else { MessageBox.Show("Browse a word document and click the button to convert as a PDF."); } }