Excel to PDF conversion with PageSetup and other options via Aspose.Cells for .NET

@shakeel.faiz : i tried the same but actually the columns are not overflowing it is the same header repeating again and this is happening when a particular sheet is breaking into multiple pages in a pdf.

@dewan.ishi

For the issue you said:

One possible way is to move the notes after the table to another sheet.

Besides, if the repeat rows/columns is set in page setup, it will repeat on every page of the sheet.

@shakeel.faiz but the notes appear on every sheet and the location could be any row.
secondly like you said if the repeat rows/columns is set in page setup, it will repeat on every page of the sheet. Is there a way where i can set it to false using aspose ?

@dewan.ishi

Please set the Worksheet.PageSetup.PrintTitleRows to null or empty string. Please see the following sample code, its Excel file, its output Pdfs. Open the both Pdfs and see the difference. In one Pdf, title row is repeating and in other Pdf, it is not repeating.

Download Link:
Sample Excel File and Output Pdf Files.zip (424.0 KB)

C#

Workbook wb = new Workbook("sample.xlsx");

//Remove title rows
//Worksheet ws = wb.Worksheets[0];
//ws.PageSetup.PrintTitleRows = "";

wb.Save("output.pdf");

Screenshot:

@shakeel.faiz : thank you for all the help.thanks for helping me with all the above issues. Thanks a lot.

@dewan.ishi

Thank you. If you need any other help relating to Aspose.Cells Family APIs, please feel free to let us know, we will be glad to help you further.

@shakeel.faiz : in my pdf for some sheets notes are flowing to the next page and i want to add notes heading just before they start. i have tried the following code… but this is adding at an abrupt position and also it is breaking the sheets which can fit in one pdf page also. Any insight ?

for (int i = 0; i < sheetCount; i++)
            {
                Worksheet worksheet = workBook.Worksheets[i];
                worksheet.PageSetup.Orientation = PageOrientationType.Landscape;
                worksheet.PageSetup.Order = PrintOrderType.OverThenDown;

                worksheet.PageSetup.PrintTitleRows = "";
                worksheet.Cells.UngroupRows(worksheet.Cells.MinRow, worksheet.Cells.MaxRow + 1, false);
                worksheet.HorizontalPageBreaks.Clear();
                worksheet.VerticalPageBreaks.Clear();

                string text = "notes";
                FindOptions findOptions = new FindOptions();
                findOptions.CaseSensitive = false;
                findOptions.LookInType = LookInType.Values;
                Aspose.Cells.Cell foundCell = worksheet.Cells.Find(text, null, findOptions);
                //If a cell found with the value
                if (foundCell != null)
                {
                    int row = foundCell.Row;
                    string name = foundCell.Name;

                    ImageOrPrintOptions printoption = new ImageOrPrintOptions();
                    printoption.PrintingPage = PrintingPageType.Default;
                    SheetRender sr = new SheetRender(worksheet, printoption);
                    int pageCount = sr.PageCount;

                    CellArea[] area = worksheet.GetPrintingPageBreaks(printoption);

                    for (int x = 0; x < area.Length; x++)
                    {
                        //Get the first page rows
                        int strow = area[x].StartRow;
                        int stcol = area[x].StartColumn;

                        if (strow > row && strow < worksheet.Cells.MaxDataRow)
                        {
                            int[] abc = new int[] { strow };

                            if (abc.Length >= 1)
                            {
                               
                                worksheet.HorizontalPageBreaks.Add("A"+(strow-2));

                                //strow = strow + 5;

                                Aspose.Cells.Cell cell_d = worksheet.Cells["A" + strow]; ;
                                Style style_f4 = cell_d.GetStyle();
                                style_f4.BackgroundColor = Color.Black;
                                style_f4.Font.Color = Color.White;
                                style_f4.Font.IsBold = true;
                                style_f4.ForegroundColor = Color.Black;
                                style_f4.Pattern = BackgroundType.Solid;
                                StyleFlag flag_f4 = new StyleFlag();
                                flag_f4.CellShading = true;
                                flag_f4.FontColor = true;
                                flag_f4.Font = true;

                                cell_d = worksheet.Cells[strow, stcol];
                                Style style_date = cell_d.GetStyle();

                                worksheet.Cells.Merge((strow), 0, 1, 16);
                                worksheet.Cells["A"+(strow+1)].PutValue("Notes");
                                cell_d.SetStyle(style_f4, flag_f4);
                            }
                        }

                    }
                }

@dewan.ishi

We will look into this issue and update you asap.

@shakeel.faiz : ok thank you

@dewan.ishi

Please share your runnable sample console application project along with the sample Excel files. The code you shared in the previous post is not runnable. We need to run your code so that we could understand it better and possibly fix it. Please do the needful. Thank you.

Hi @shakeel.faiz : i am attaching the sample ecel and pdf. In the pdf you can observe that the notes are flowing to the next page and there is no heading on the top of the page. i wanted the Notes heading to repeat again on th next page programatically. That is what above code is about.abc (2).zip (70.7 KB)

@dewan.ishi

Please see the following sample code. The code copies your notes into a new worksheet, sets the print area of the first worksheet, then it saves the workbook to pdf format.

Download Link:
output.pdf (55.9 KB)

C#

//Load your source Excel file
Workbook wb = new Workbook("abc.xlsx");

//Access first worksheet and set its print area
Worksheet ws1 = wb.Worksheets[0];
ws1.PageSetup.PrintArea = "A1:AA35";

//Create source range, which we will copy to next sheet
Range srcRange = ws1.Cells.CreateRange("A37:P187");

//Create a new worksheet and copy source range to destination range
int idx = wb.Worksheets.Add();
Worksheet ws2 = wb.Worksheets[idx];

//We want to repeate first rows to new pages
ws2.PageSetup.PrintTitleRows = "$1:$1";

//Copy source range to destination range
Range dstRange = ws2.Cells.CreateRange("A1:P151");
dstRange.Copy(srcRange);

//Save the workbook in pdf format
wb.Save("output.pdf");