using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;using Spire.Pdf;using Spire.Xls;using System.Drawing;using System.Windows.Forms;using Spire.Pdf.Annotations;using Spire.Pdf.Widget;using System.Drawing.Printing;namespace Excel_to_PDF_print{ class Program { static void Main(string[] args) { Workbook workbook = new Workbook(); workbook.LoadFromFile("test.xlsx", ExcelVersion.Version2010); //Get the first sheet and add a new sheet to store your area. Worksheet sheet = workbook.Worksheets[0]; workbook.Worksheets.Add("Newsheet"); workbook.SaveToFile("result.xls"); //Copy your area to new sheet. workbook.Worksheets[0].Range["A1:G10"].Copy(workbook.Worksheets[1].Range["A7:D11"]); //convert new sheet to pdf workbook.Worksheets[1].SaveToPdf("result.pdf", Spire.Xls.FileFormat.PDF); // Save and preview PDF //Print PDF PdfDocument doc = new PdfDocument(); doc.LoadFromFile("result.pdf"); //Use the default printer to print all the pages //Set the printer and select the pages you want to print PrintDialog dialogPrint = new PrintDialog(); dialogPrint.AllowPrintToFile = true; dialogPrint.AllowSomePages = true; dialogPrint.PrinterSettings.MinimumPage = 1; dialogPrint.PrinterSettings.MaximumPage = doc.Pages.Count; dialogPrint.PrinterSettings.FromPage = 1; dialogPrint.PrinterSettings.ToPage = doc.Pages.Count; if (dialogPrint.ShowDialog() == DialogResult.OK) { doc.PrintFromPage = dialogPrint.PrinterSettings.FromPage; doc.PrintToPage = dialogPrint.PrinterSettings.ToPage; doc.PrinterName = dialogPrint.PrinterSettings.PrinterName; PrintDocument printDoc = doc.PrintDocument; dialogPrint.Document = printDoc; printDoc.Print(); } } }}
Login to post response