[]
DsExcel allows you to determine where a worksheet will be divided into pages before exporting it to PDF. Use the GetPaginationInfo method of the PrintManager class to retrieve the page boundaries based on the worksheet's page setup.
Call the method separately for horizontal and vertical pagination. The returned collection contains the zero-based column or row indexes after which page breaks occur.
If PrintArea is not specified, DsExcel uses the range from cell A1 to the last cell that contains data.
The following example retrieves the horizontal and vertical page boundaries and then exports the worksheet to PDF.
// Create a new workbook and get the default worksheet.
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];
// Print the row and column headings.
worksheet.PageSetup.PrintHeadings = true;
// Set the print area to B6:N80.
worksheet.PageSetup.PrintArea = "B6:N80";
// Set data.
worksheet.Range["B6:S8"].Value = "1";
// Add a table.
worksheet.Tables.Add(worksheet.Range["B6:N20"], true);
// Create an instance of the PrintManager class.
PrintManager printManager = new PrintManager();
// Get the horizontal page boundaries.
// The returned column indexes are zero-based.
// For example, [9, 13] means that page breaks occur
// after columns 10 and 14.
IList<int> columnIndexes = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Horizontal);
foreach (int columnIndex in columnIndexes)
{
Console.WriteLine(
"A horizontal page break occurs after column "
+ (columnIndex + 1).ToString() + ".");
}
// Get the vertical page boundaries.
// The returned row indexes are zero-based.
// For example, [50, 79] means that page breaks occur
// after rows 51 and 80.
IList<int> rowIndexes = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Vertical);
foreach (int rowIndex in rowIndexes)
{
Console.WriteLine(
"A vertical page break occurs after row "
+ (rowIndex + 1).ToString() + ".");
}
// Export the worksheet to a PDF file using its page setup.
worksheet.Save(@"GetPagination.pdf", SaveFileFormat.Pdf);The following example applies keep-together ranges and repeat settings when calculating page boundaries and exporting the worksheet to PDF.
// Create a new workbook and get the default worksheet.
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.Worksheets[0];
// Print the row and column headings.
worksheet.PageSetup.PrintHeadings = true;
// Set the print area to B6:N80.
worksheet.PageSetup.PrintArea = "B6:N80";
// Set data.
worksheet.Range["B60:N80"].Value = 1;
// Add a table.
worksheet.Tables.Add(worksheet.Range["B6:N20"], true);
// Configure row 6 to repeat at the top of each page.
// TitleRowStart and TitleRowEnd use zero-based row indexes.
IList<RepeatSetting> repeatSettings = new List<RepeatSetting>();
RepeatSetting repeatSetting = new RepeatSetting();
repeatSetting.TitleRowStart = 5;
repeatSetting.TitleRowEnd = 5;
repeatSetting.Range = worksheet.Range["B6:N80"];
repeatSettings.Add(repeatSetting);
// Keep rows 25 through 60 together on the same page.
IList<IRange> keepTogetherRanges = new List<IRange>();
keepTogetherRanges.Add(worksheet.Range["$25:$60"]);
// Create an instance of the PrintManager class.
PrintManager printManager = new PrintManager();
// Get the horizontal page boundaries by applying
// the keep-together ranges and repeat settings.
// For example, [9, 13] means that page breaks occur
// after columns 10 and 14.
IList<int> columnIndexes = printManager.GetPaginationInfo(worksheet, PaginationOrientation.Horizontal, keepTogetherRanges, repeatSettings);
// Get the vertical page boundaries by applying
// the keep-together ranges and repeat settings.
// For example, [23, 66, 79] means that page breaks occur
// after rows 24, 67, and 80.
IList<int> rowIndexes = printManager.GetPaginationInfo(worksheet,PaginationOrientation.Vertical, keepTogetherRanges, repeatSettings);
// Generate the pages using the same keep-together
// ranges and repeat settings.
IList<PageInfo> pages = printManager.Paginate(worksheet,keepTogetherRanges,repeatSettings);
// Export the generated pages to a PDF file.
printManager.SavePDF(@"GetPaginationRangesRepeatSettings.pdf",pages);