Print specified area of excel as PDF

Posted by Hitu under .NET Framework on 4/9/2015 | Points: 10 | Views : 1368 | Status : [Member] | Replies : 2
Any solution to print a specific area of excel as a PDF files?Thank you. Can anyone offer an example?


Posted by: Yesi on: 4/9/2015 [Member] Starter | Points: 25

Convert that area as a PDF page first, then print the PDF page.

Recommend an Excel tool for u, download this two .dll of an excel library, , and add reference to your project, then refer the following code:
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];
//Copy your area to new sheet.
//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();

//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;


Hitu, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Abhayabhedi on: 5/7/2015 [Member] Starter | Points: 25

One approach I can think of is to print the area you want (selected cells) to a virtual printer driver like lead tools then save the print job as PDF.

Another approach if you know the area you want before opening the excel sheet, is to parse the excel and take the area you want then use a library to generate the PDF.

Hitu, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response