void OnConvertClicked(object sender, EventArgs e) { //Instantiate excel engine ExcelEngine excelEngine = new ExcelEngine(); //Excel application IApplication application = excelEngine.Excel; string resourcePath; //Load the input template from assembly. if (this.checkfontName.IsChecked.Value || this.checkfontStream.IsChecked.Value) { application.SubstituteFont += new Syncfusion.XlsIO.Implementation.SubstituteFontEventHandler(SubstituteFont); resourcePath = "SampleBrowser.Samples.XlsIO.Template.InvoiceTemplate.xlsx"; } else { resourcePath = "SampleBrowser.Samples.XlsIO.Template.ExcelToPDF.xlsx"; } Assembly assembly = Assembly.GetExecutingAssembly(); Stream fileStream = assembly.GetManifestResourceStream(resourcePath); //Open Workbook IWorkbook workbook = application.Workbooks.Open(fileStream); XlsIORenderer renderer = new XlsIORenderer(); XlsIORendererSettings settings = new XlsIORendererSettings(); settings.IsConvertBlankPage = false; int index = layoutList.IndexOf(selectedLayout); if (index == 0) { settings.LayoutOptions = LayoutOptions.NoScaling; } else if (index == 1) { settings.LayoutOptions = LayoutOptions.FitAllRowsOnOnePage; } else if (index == 2) { settings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage; } else { settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage; } PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, settings); MemoryStream memoryStream = new MemoryStream(); pdfDocument.Save(memoryStream); pdfDocument.Close(true); workbook.Close(); excelEngine.Dispose(); //Save and view the generated file. SaveAndView(memoryStream, "application/pdf"); }
public byte[] ExcelToPDF(Stream file) { _logger.LogDebug($"Start convert excel file"); byte[] buff; try { using (ExcelEngine excelEngine = new ExcelEngine()) using (MemoryStream memoryStream = new MemoryStream()) { IApplication application = excelEngine.Excel; application.DefaultVersion = ExcelVersion.Excel2013; IWorkbook workbook = application.Workbooks.Open(file); XlsIORenderer renderer = new XlsIORenderer(); using (PdfDocument pdfDocument = renderer.ConvertToPDF(workbook)) { pdfDocument.Save(memoryStream); memoryStream.Position = 0; buff = memoryStream.ToArray(); _logger.LogDebug("FileInfoPDFService.ExcelToPDF....OK"); return(buff); } } } catch (Exception ex) { _logger.LogError(ex.Message); } return(null); }
/// <summary> /// Save to pdf method, based on input from Excel file - Workbook vs Worksheet /// </summary> /// <param name="worksheet">worksheet object</param> private void saveToPdf(IWorksheet worksheet) { XlsIORenderer renderer = new XlsIORenderer(); if (!Directory.Exists(PdfOutputFilePath)) { Directory.CreateDirectory(PdfOutputFilePath); } string _worksheetName = FileNameUtil.GetFormattedFileName(worksheet.Name);; string outputFileName = Path.Combine(PdfOutputFilePath, $"{Path.GetFileNameWithoutExtension(ExcelFileName)}_{_worksheetName}"); using var pdfDocument = renderer.ConvertToPDF(worksheet, new XlsIORendererSettings() { LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage }); using var stream = new FileStream($"{outputFileName}.pdf", FileMode.Create, FileAccess.Write); pdfDocument.PageSettings.Margins = new Syncfusion.Pdf.Graphics.PdfMargins() { All = 10 }; pdfDocument.Compression = PdfCompressionLevel.Normal; pdfDocument.Save(stream); stream.Dispose(); }
internal void OnButtonClicked(object sender, EventArgs e) { ExcelEngine excelEngine = new ExcelEngine(); IApplication application = excelEngine.Excel; Stream stream = null; if (this.checkfontName.IsChecked.Value || this.checkfontStream.IsChecked.Value) { application.SubstituteFont += new Syncfusion.XlsIO.Implementation.SubstituteFontEventHandler(SubstituteFont); stream = GetFileStream("InvoiceTemplate.xlsx"); } else { stream = GetFileStream("ExcelToPDF.xlsx"); } IWorkbook workbook = application.Workbooks.Open(stream); XlsIORenderer renderer = new XlsIORenderer(); XlsIORendererSettings settings = new XlsIORendererSettings(); settings.IsConvertBlankPage = false; //Set the Layout Options for the output Pdf page. if (this.picker.SelectedIndex == 0) { settings.LayoutOptions = Syncfusion.XlsIORenderer.LayoutOptions.NoScaling; } else if (this.picker.SelectedIndex == 1) { settings.LayoutOptions = Syncfusion.XlsIORenderer.LayoutOptions.FitAllRowsOnOnePage; } else if (this.picker.SelectedIndex == 2) { settings.LayoutOptions = Syncfusion.XlsIORenderer.LayoutOptions.FitAllColumnsOnOnePage; } else if (this.picker.SelectedIndex == 3) { settings.LayoutOptions = Syncfusion.XlsIORenderer.LayoutOptions.FitSheetOnOnePage; } settings.EnableFormFields = true; PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, settings); MemoryStream memoryStream = new MemoryStream(); pdfDocument.Save(memoryStream); pdfDocument.Close(true); if (Device.RuntimePlatform == Device.UWP) { Xamarin.Forms.DependencyService.Get <ISaveWindowsPhone>().Save("ExcelToPDF.pdf", "application/pdf", memoryStream); } else { Xamarin.Forms.DependencyService.Get <ISave>().Save("ExcelToPDF.pdf", "application/pdf", memoryStream); } }
/// <summary> /// Convert Excel To PDF /// </summary> /// <param name="sender">contains a reference to the control/object that raised the event</param> /// <param name="e">contains the event data</param> private void btnConvertToPDF_Click(object sender, RoutedEventArgs e) { using (ExcelEngine engine = new ExcelEngine()) { IApplication application = engine.Excel; string inputPath = "syncfusion.xlsiodemos.winui.Assets.XlsIO.ExcelTopdfwithChart.xlsx"; Assembly assembly = typeof(ExcelToPDF).GetTypeInfo().Assembly; Stream input = assembly.GetManifestResourceStream(inputPath); IWorkbook book = application.Workbooks.Open(input); //Initialize XlsIORenderer XlsIORenderer renderer = new XlsIORenderer(); //Intialize the PDFDocument PdfDocument pdfDoc = new PdfDocument(); //Intialize the XlsIORendererSettings XlsIORendererSettings settings = new XlsIORendererSettings(); if (this.rdbtnNoScaling.IsChecked.Value) { settings.LayoutOptions = LayoutOptions.NoScaling; } else if (this.rdbtnFitAllRows.IsChecked.Value) { settings.LayoutOptions = LayoutOptions.FitAllRowsOnOnePage; } else if (this.rdbtnFitAllCols.IsChecked.Value) { settings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage; } else { settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage; } //Assign the PdfDocument to the templateDocument property of XlsIORendererSettings settings.TemplateDocument = pdfDoc; settings.DisplayGridLines = GridLinesDisplayStyle.Invisible; //Convert Excel Document into PDF document pdfDoc = renderer.ConvertToPDF(book, settings); //Save the PDF file MemoryStream stream = new MemoryStream(); pdfDoc.Save(stream); Save(stream, "ExcelToPDF.pdf"); #region Close and Dispose input.Dispose(); stream.Dispose(); #endregion } }
void OnConvertClicked(object sender, EventArgs e) { //Instantiate excel engine ExcelEngine excelEngine = new ExcelEngine(); //Excel application IApplication application = excelEngine.Excel; //Load the input template from assembly. string resourcePath = "SampleBrowser.Samples.XlsIO.Template.ExcelToPDF.xlsx"; Assembly assembly = Assembly.GetExecutingAssembly(); Stream fileStream = assembly.GetManifestResourceStream(resourcePath); //Open Workbook IWorkbook workbook = application.Workbooks.Open(fileStream); XlsIORenderer renderer = new XlsIORenderer(); XlsIORendererSettings settings = new XlsIORendererSettings(); //Initialize XlsIORenderer. application.XlsIORenderer = new XlsIORenderer(); ExportImageOptions imageOptions = new ExportImageOptions(); int index = layoutList.IndexOf(selectedLayout); if (index == 0) { settings.LayoutOptions = LayoutOptions.NoScaling; } else if (index == 1) { settings.LayoutOptions = LayoutOptions.FitAllRowsOnOnePage; } else if (index == 2) { settings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage; } else { settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage; } PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, settings); MemoryStream memoryStream = new MemoryStream(); pdfDocument.Save(memoryStream); pdfDocument.Close(true); workbook.Close(); excelEngine.Dispose(); //Save and view the generated file. SaveAndView(memoryStream, "application/pdf"); }
private void OnConvertButtonClicked(object sender, EventArgs e) { //Instantiate excel engine ExcelEngine excelEngine = new ExcelEngine(); //Excel application IApplication application = excelEngine.Excel; //Get assembly manifest resource Assembly assembly = Assembly.GetExecutingAssembly(); Stream fileStream = assembly.GetManifestResourceStream("SampleBrowser.Samples.XlsIO.Template.ExcelToPDF.xlsx"); //Open Workbook IWorkbook workbook = application.Workbooks.Open(fileStream); XlsIORenderer renderer = new XlsIORenderer(); XlsIORendererSettings settings = new XlsIORendererSettings(); settings.IsConvertBlankPage = false; int index = spinner.SelectedItemPosition; if (index == 0) { settings.LayoutOptions = LayoutOptions.NoScaling; } else if (index == 1) { settings.LayoutOptions = LayoutOptions.FitAllRowsOnOnePage; } else if (index == 2) { settings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage; } else { settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage; } PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, settings); MemoryStream memoryStream = new MemoryStream(); pdfDocument.Save(memoryStream); pdfDocument.Close(true); workbook.Close(); excelEngine.Dispose(); //Save and view the generated file. SaveAndView(memoryStream, "application/pdf"); }
/// <summary> /// Create an Excel document /// </summary> /// <returns>Return the created excel document as stream</returns> public MemoryStream ExceltoPDFXlsIO(string option) { XlsIORenderer renderer = new XlsIORenderer(); FileStream inputStream = new FileStream(ResolveApplicationPath("excel-to-pdf.xlsx"), FileMode.Open, FileAccess.Read); inputStream.Position = 0; //Intialize the PdfDocument Class PdfDocument document = new PdfDocument(); //Intialize the ExcelToPdfConverterSettings class XlsIORendererSettings settings = new XlsIORendererSettings(); settings.IsConvertBlankPage = false; //Set the Layout Options for the output Pdf page if (option == "NoScaling") { settings.LayoutOptions = LayoutOptions.NoScaling; } else if (option == "FitAllRowsOnOnePage") { settings.LayoutOptions = LayoutOptions.FitAllRowsOnOnePage; } else if (option == "FitAllColumnsOnOnePage") { settings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage; } else { settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage; } //Assign the output PdfDocument to the TemplateDocument property of ExcelToPdfConverterSettings settings.TemplateDocument = document; settings.DisplayGridLines = GridLinesDisplayStyle.Invisible; //Convert the Excel document to PDf document = renderer.ConvertToPDF(inputStream, settings); inputStream.Dispose(); //Save the document as a stream and retrun the stream using (MemoryStream stream = new MemoryStream()) { //Save the created PDF document to MemoryStream document.Save(stream); return(stream); } }
internal void OnButtonClicked(object sender, EventArgs e) { ExcelEngine excelEngine = new ExcelEngine(); IApplication application = excelEngine.Excel; #if COMMONSB Stream stream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.Samples.XlsIO.Samples.Template.ExcelToPDF.xlsx"); #else Stream stream = typeof(App).GetTypeInfo().Assembly.GetManifestResourceStream("SampleBrowser.XlsIO.Samples.Template.ExcelToPDF.xlsx"); #endif IWorkbook workbook = application.Workbooks.Open(stream); XlsIORenderer renderer = new XlsIORenderer(); XlsIORendererSettings settings = new XlsIORendererSettings(); settings.IsConvertBlankPage = false; //Set the Layout Options for the output Pdf page. if (this.picker.SelectedIndex == 0) { settings.LayoutOptions = Syncfusion.XlsIORenderer.LayoutOptions.NoScaling; } else if (this.picker.SelectedIndex == 1) { settings.LayoutOptions = Syncfusion.XlsIORenderer.LayoutOptions.FitAllRowsOnOnePage; } else if (this.picker.SelectedIndex == 2) { settings.LayoutOptions = Syncfusion.XlsIORenderer.LayoutOptions.FitAllColumnsOnOnePage; } else if (this.picker.SelectedIndex == 3) { settings.LayoutOptions = Syncfusion.XlsIORenderer.LayoutOptions.FitSheetOnOnePage; } PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, settings); MemoryStream memoryStream = new MemoryStream(); pdfDocument.Save(memoryStream); pdfDocument.Close(true); if (Device.RuntimePlatform == Device.UWP) { Xamarin.Forms.DependencyService.Get <ISaveWindowsPhone>().Save("ExcelToPDF.pdf", "application/pdf", memoryStream); } else { Xamarin.Forms.DependencyService.Get <ISave>().Save("ExcelToPDF.pdf", "application/pdf", memoryStream); } }
/// <summary> /// Save to pdf method, based on input from Excel file - Workbook vs Worksheet /// </summary> /// <param name="workbook">workbook object</param> private void saveToPdf(IWorkbook workbook) { XlsIORenderer renderer = new XlsIORenderer(); if (!Directory.Exists(PdfOutputFilePath)) { Directory.CreateDirectory(PdfOutputFilePath); } string outputFileName = Path.Combine(PdfOutputFilePath, $"{Path.GetFileNameWithoutExtension(ExcelFileName)}.pdf"); PdfDocument pdfDocument = renderer.ConvertToPDF(workbook, new XlsIORendererSettings() { LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage }); Stream stream = new FileStream(outputFileName, FileMode.Create, FileAccess.ReadWrite); pdfDocument.Save(stream); stream.Dispose(); }
public ActionResult ExcelToPDF(string button, string checkboxStream, string Group1, IFormFile file) { if (button == null) { return(View()); } basePath = _hostingEnvironment.WebRootPath; ExcelEngine excelEngine = new ExcelEngine(); IApplication application = excelEngine.Excel; if (checkboxStream != null) { application.SubstituteFont += new Syncfusion.XlsIO.Implementation.SubstituteFontEventHandler(SubstituteFont); } XlsIORenderer renderer = new XlsIORenderer(); Stream fileStream = null; if (file == null) { fileStream = new FileStream(basePath + @"/XlsIO/ExcelToPDF.xlsx", FileMode.Open, FileAccess.Read); } else { fileStream = file.OpenReadStream(); } fileStream.Position = 0; //Loads file stream into Word document IWorkbook workbook = application.Workbooks.Open(fileStream); //Intialize the PdfDocument Class PdfDocument pdfDoc = new PdfDocument(); //Intialize the ExcelToPdfConverterSettings class XlsIORendererSettings settings = new XlsIORendererSettings(); settings.IsConvertBlankPage = false; //Set the Layout Options for the output Pdf page. if (Group1 == "NoScaling") { settings.LayoutOptions = LayoutOptions.NoScaling; } else if (Group1 == "FitAllRowsOnOnePage") { settings.LayoutOptions = LayoutOptions.FitAllRowsOnOnePage; } else if (Group1 == "FitAllColumnsOnOnePage") { settings.LayoutOptions = LayoutOptions.FitAllColumnsOnOnePage; } else { settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage; } //Assign the output PdfDocument to the TemplateDocument property of ExcelToPdfConverterSettings settings.TemplateDocument = pdfDoc; settings.DisplayGridLines = GridLinesDisplayStyle.Invisible; //Convert the Excel document to PDf pdfDoc = renderer.ConvertToPDF(workbook, settings); fileStream.Dispose(); MemoryStream stream = new MemoryStream(); pdfDoc.Save(stream); try { stream.Position = 0; return(File(stream, "application/pdf", "ExcelToPDF.pdf")); } catch (Exception) { } finally { } return(View()); }
// // GET: /PivotTable/ public ActionResult PivotLayout(string button, string LayoutOption) { if (button == null) { return(View()); } string basePath = _hostingEnvironment.WebRootPath; //New instance of XlsIO is created.[Equivalent to launching Microsoft Excel with no workbooks open]. //The instantiation process consists of two steps. //Step 1 : Instantiate the spreadsheet creation engine. ExcelEngine excelEngine = new ExcelEngine(); //Step 2 : Instantiate the excel application object. IApplication application = excelEngine.Excel; IWorkbook workbook = null; application.DefaultVersion = ExcelVersion.Excel2016; if (button == "Convert to PDF") { FileStream inputStream = new FileStream(basePath + @"/XlsIO/PivotLayout.xlsx", FileMode.Open, FileAccess.Read); workbook = application.Workbooks.Open(inputStream); CreatePivotTable(workbook, LayoutOption); //Intialize the XlsIORenderer Class XlsIORenderer renderer = new XlsIORenderer(); //Intialize the PdfDocument Class PdfDocument pdfDoc = new PdfDocument(); //Intialize the ExcelToPdfConverterSettings class XlsIORendererSettings settings = new XlsIORendererSettings(); settings.LayoutOptions = LayoutOptions.FitSheetOnOnePage; pdfDoc = renderer.ConvertToPDF(workbook, settings); inputStream.Dispose(); MemoryStream stream = new MemoryStream(); pdfDoc.Save(stream); try { stream.Position = 0; return(File(stream, "application/pdf", "PivotLayout.pdf")); } catch (Exception) { } } else { FileStream inputStream = new FileStream(basePath + @"/XlsIO/PivotLayout.xlsx", FileMode.Open, FileAccess.Read); workbook = application.Workbooks.Open(inputStream); CreatePivotTable(workbook, LayoutOption); IPivotTable pivotTable = workbook.Worksheets[1].PivotTables[0]; pivotTable.Layout(); //To view the pivot table inline formatting in MS Excel, we have to set the IsRefreshOnLoad property as true. (workbook.PivotCaches[pivotTable.CacheIndex] as PivotCacheImpl).IsRefreshOnLoad = true; try { workbook.Version = ExcelVersion.Excel2016; MemoryStream ms = new MemoryStream(); workbook.SaveAs(ms); ms.Position = 0; return(File(ms, "Application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "PivotLayout.xlsx")); } catch (Exception) { } } //Close the workbook. workbook.Close(); excelEngine.Dispose(); return(View()); }