I have dynamically generated data that I have exported to excel (after applying some formatting). Now before being able to print I need to apply the page break in such a way that any row of data is not going to be divided among different pages. Data that falls into one row has be to displayed continuously on one printed page. Rows have border properties. Sometimes data in a row can be very short and some time it my be very long and sometime not at all (in that case i am hiding the rows).
Hi Andy,
- You can get the printing page breaks of an existing worksheet that will help you understand where the pages will break. You can use the Worksheet.GetPrintingPageBreaks method that will return an array of CellsArea which in turn could tell you where the current page breaks are. You can use this information to move the breaks at your desired locations. You can also call the aforesaid method just before saving the spreadsheet so that the API could calculate the page breaks according to the amount of data and considering the formatting (font size) as well as the height of rows and widths of the columns, in case you are calling AutoFitRows & AutoFitColumns.
- You can use the Worksheet.AddPageBreaks method or Add methods from HorizontalPageBreakCollection & VerticalPageBreakCollection classes to put the page breaks on the desired locations. At the bottom of this post, you will find the code snippet to add manual page breaks.
Workbook workbook = new Workbook();
//Clearing all page breaks
workbook.Worksheets[0].HorizontalPageBreaks.Clear();
workbook.Worksheets[0].VerticalPageBreaks.Clear();
//Add a page break at cell Y30
workbook.Worksheets[0].HorizontalPageBreaks.Add(“Y30”);
workbook.Worksheets[0].VerticalPageBreaks.Add(“Y30”);
Thank you for pointing few methods. But it does not seem to help me in my scenario. How do I send you the spreadsheet for your review?
Hi Andy,
C#
var book = new Aspose.Cells.Workbook(dir + “aspose formatting.xls”);
book.Worksheets[0].PageSetup.FitToPagesTall = 1;
book.Worksheets[0].PageSetup.FitToPagesWide = 0;
book.Save(dir + “output.pdf”, new PdfSaveOptions());
Thank you for your response. This works if I am limited to few page documents. If I have to print more pages and constraining to fit all in one page does not come out good. In fact the report is unreadable.
Hi Andy,
Hi,
Thanks for using Aspose.Cells.
It seems that you need a whole row to be displayed in one page. So you have two options while rendering to pdf.
1. Set
FitToPagesWide = 1
FitToPagesTall = 0
With this setting, we will shrink a row to one page width when the content of the row is larger than the page width. But this has the limit that the minimum shrink value is 10%, when the content of a row is too long, it may be divided to several pages.
2. Set in PdfSaveOptions
AllColumnsInOnePagePerSheet = true
With this setting, we will expand the page width when the content of a row is larger than the page width. The width of paper size of PageSetup will be ignored as our API document stated.
Thank you, I am sending you the spreadsheet. Hope this one helps.
This feature does not work well when i have more than 2 pages document. In my case it is random. Sometime it can be less than a page and sometime it can be 20 pages long.
AndyCooper:
Thank you, I am sending you the spreadsheet. Hope this one helps.here, the 4th section is very long (I know it will not fit in single page, but it should start in its own page. So, in this scenario I need a page break in after row 13. But like I said, this is dynamically generated content. There may be situation where this section is really small and other section may be even longer.After opening the spreadsheet, when you try to print from file menu on (8.5*11)
Hi again,
var sheet = book.Worksheets[0];
var pageSetup = sheet.PageSetup;
pageSetup.PrintArea = “A4:F7,A9:F9,A11:F11,A15:F35,A37:F41,A43:F43”;
book.Save(dir + “output.pdf”, Aspose.Cells.SaveFormat.Pdf);
That is correct. The section only need to start from new page if the section is not going to fit in that page. However if the section is very long and contain multiple page of data then it can break automatically.
I cannot have each section printed out on each page. I only need to print the section which is large enough to fit in single page. However if section is large and does not fit in one page then they can span over multiple pages with automatic page break.
Hi Andy,
- If you do not wish to render each section to separate page, you can merge those sections in a single print area. For instance, if you wish to 1, 2, 3 & 4 on single page, all you have do is to set the PrintArea property as “A1:F13,A15:F35,A37:F41,A43:F43”. Please note, the last two sections at the bottom will still be split with above PrintArea settings, however, you can also merge them as suggested above.
- In case a specified print area has contents spanning over multiple pages then such print area will automatically be split according to the predefined paper size, that is; as per automatic page breaks.