Print specified area of excel as PDF

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




Responses

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

Up
0
Down
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,http://www.e-iceblue.com/Introduce/excel-for-net-introduce.html ,http://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html 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];
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();
}




}
}
}


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

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

Up
0
Down
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.
http://stackoverflow.com/questions/28040477/converting-excel-to-pdf-in-c-sharp-using-office-2003

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

Login to post response