[]
        
(Showing Draft Content)

Export Worksheet to PDF

GcExcel .NET provides the option to paginate a worksheet automatically, according to page boundaries, while exporting to PDF file.

The GetPaginationInfo method of PrintManager class gets an array of the page boundaries for horizontal and vertical paging. The method needs to be called separately for horizontal and vertical pagination. The retrieved pagination information is based on the page setup settings. The print area of the worksheet can also be defined. In case it is not defined, the default area is considered from cell A1 till the last cell where any cell data is present.

In addition to the page setup settings, you can also define ranges which need to be kept together and repeat settings of a range by using the overload of GetPaginationInfo method.

Using Code

Refer to the following example code to paginate a worksheet while exporting to PDF file based on the page setup settings.

IWorkbook workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];

// The row and column headings are printed
worksheet.PageSetup.PrintHeadings = true;
// The range "B6:N80" will be printed
worksheet.PageSetup.PrintArea = "B6:N80";

// Set data
worksheet.Range["B6:S8"].Value = "1";
// Add a table
worksheet.Tables.Add(worksheet.Range["B6:N20"], true);

PrintManager printManager = new PrintManager();
       
// The columnIndexs is [9, 13], this means that the horizontal direction is split after the column 10th and 14th
IList columnIndexs = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Horizontal);
Console.WriteLine("In horizontal direction, page is split after column : " + columnIndexs[0].ToString() + " & " + columnIndexs[1].ToString());

// The rowIndexs is [50, 79], this means that the vertical direction is split after the row 51th and 80th
IList rowIndexs = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Vertical);
Console.WriteLine("In vertical direction, page is split after row : " + rowIndexs[0].ToString() + " & " + rowIndexs[1].ToString());

worksheet.Save(@"GetPagination.pdf", SaveFileFormat.Pdf);

Refer to the following example code to paginate a worksheet while exporting to PDF file based on the page setup settings, range to be kept together and repeat settings.

IWorkbook workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];

// The row and column headings are printed
worksheet.PageSetup.PrintHeadings = true;
// The range "B6:N80" will be printed
worksheet.PageSetup.PrintArea = "B6:N80"; ;

// Set data
worksheet.Range["B60:N80"].Value = 1;
// Add a table
worksheet.Tables.Add(worksheet.Range["B6:N20"], true);

// The row 6th will be printed at the top of each page
IList repeatSettings = new List();
RepeatSetting repeatSetting = new RepeatSetting();
repeatSetting.TitleRowStart = 5;
repeatSetting.TitleRowEnd = 5;
repeatSetting.Range = worksheet.Range["B6:N80"];
repeatSettings.Add(repeatSetting);

// The rows from 25th to 60th should be paged to one page
IList keepTogetherRanges = new List();
keepTogetherRanges.Add(worksheet.Range["$25:$60"]);

PrintManager printManager = new PrintManager();

// The columnIndexs is [9, 13], this means that the horizontal direction is split after the column 10th and 14th.
IList columnIndexs = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Horizontal, keepTogetherRanges, repeatSettings);
// The rowIndexs is [23, 66, 79], this means that the vertical direction is split after the row 24th, 67th and 80th.
IList rowIndexs = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Vertical, keepTogetherRanges, repeatSettings);

IList pages = printManager.Paginate(worksheet, keepTogetherRanges, repeatSettings);
printManager.SavePDF(@"GetPaginationRangesRepeatSettings.pdf", pages);