[]
        
(Showing Draft Content)

Track Export Progress

GcExcel provides PagePrinting and PagePrinted events in PdfSaveOptions class to track the export progress of a workbook to PDF. The PagePrinting event occurs before printing a page and provides SkipThisPage property to skip pages while exporting. Similarly, the PagePrinted event occurs after printing a page and provides HasMorePages property to exit PDF exporting.

Display Export Progress

Refer to the following example code to display the export progress of a workbook to PDF.

//create a pdf file stream
FileStream outputStream = new FileStream("pageprinteventstrackprogress.pdf", FileMode.Create);

//create a new workbook
var workbook = new Workbook();

var activeSheet = workbook.ActiveSheet;
activeSheet.Range["A1"].Value = 1;
activeSheet.Range["A2:A100"].FormulaR1C1 = "=R[-1]C+1";
var options = new PdfSaveOptions();
options.PagePrinting += (sender, e) =>
Console.WriteLine($"Printing page {e.PageNumber} of {e.PageCount}");
activeSheet.PageSetup.CenterHeader = "Page &P of &N";
workbook.Save(outputStream, options);

//close the pdf stream
outputStream.Close();

Skip a Page while Exporting

Refer to the following example code to skip second page while exporting a workbook to PDF.

//create a pdf file stream
FileStream outputStream = new FileStream("pageprinteventsskippage.pdf", FileMode.Create);

//create a new workbook
var workbook = new GrapeCity.Documents.Excel.Workbook();

var activeSheet = workbook.ActiveSheet;
activeSheet.Range["A1"].Value = 1;
activeSheet.Range["A2:A100"].FormulaR1C1 = "=R[-1]C+1";
var options = new PdfSaveOptions();

//skip second page
options.PagePrinting += (sender, e) =>
{
    if (e.PageNumber == 2)
    {
        e.SkipThisPage = true;
    }
};
activeSheet.PageSetup.CenterHeader = "Page &P of &N";
workbook.Save(outputStream, options);

//close the pdf stream
outputStream.Close();

Exit Exporting

Refer to the following example code to exit PDF exporting after second page.

//create a pdf file stream
FileStream outputStream = new FileStream("pageprinteventsexitprinting.pdf", FileMode.Create);

//create a new workbook
var workbook = new GrapeCity.Documents.Excel.Workbook();

var activeSheet = workbook.ActiveSheet;
activeSheet.Range["A1"].Value = 1;
activeSheet.Range["A2:A100"].FormulaR1C1 = "=R[-1]C+1";
var options = new PdfSaveOptions();

//exit printing after second page
options.PagePrinted += (sender, e) =>
{
    if (e.PageNumber == 2)
    {
        e.HasMorePages = false;
    }
};
activeSheet.PageSetup.CenterHeader = "Page &P of &N";
workbook.Save(outputStream, options);

//close the pdf stream
outputStream.Close();