Page size calculation


#1

Hi.

I have a book with an image. I want to save this file to PDF.

is it possible to calculate, that page is over and i need to place PageBreak to save the image solid?

Best regards. Alexey
break.zip (14.6 KB)


#2

@makarovalv,
I have checked the documents but could not find any built-in feature to achieve this functionality. If we think about calculating the image size, it is also difficult to devise some logic. For example, if an image is 3 or more pages long in height then where should be the page break. Also if the image is more than the single page width in the worksheet and height is also more than one page, then it will also add complexity. So it seems to be a little difficult to provide some sample code which can exactly handle all the cases.

However, we have PageSetup.PaperSize, PageSetup.PaperWidth, PageSetup.PaperHeight properties for a worksheet. On the other hand, we can consider using Worksheet.Pictures[index] object properties to device some logic.

In the meanwhile, we need to look into it more. We have logged the issue in our database for investigation and for a fix. Once, we will have some news for you, we will update you in this topic.

This issue has been logged as

CELLSNET-46816 – Option to check if the rendered image in PDF is crossing the printable area limits

#3

Hi.

Thanks for you fast responce. Can you please clarify one point - PaperHeight measures in inches, row’s height in Points. How i can convert one to other?

Best regards. Alexey


#4

@makarovalv,
Please note that 72 points equal 1 inch so the conversions can be performed accordingly.


#5

Thank you. How i can measure footer and header padding. I tried to getTopMarginInch / getBottomMarginInch, but i unable to receive correct value.

    @Test
public void main() throws Exception {
    Workbook workbook = new Workbook();

    Worksheet worksheet = workbook.getWorksheets().get(0);

    double pageHeight = worksheet.getPageSetup().getPaperHeight();
    double topInch = worksheet.getPageSetup().getTopMarginInch();
    double bottomInch = worksheet.getPageSetup().getBottomMarginInch();

    double rowSize = worksheet.getCells().getRowHeightInch(0);

    double rows = (pageHeight-topInch-bottomInch)/rowSize;
    System.out.println(rows);

    workbook.save("E://out.xlsx");
}

But, when i open the book, count of rows is 52


#6

Even, if i count rows one be one result is wrong for modified sheets:

    @Test
public void main() throws Exception {
    Workbook workbook = new Workbook("E://test.xlsx");

    Worksheet worksheet = workbook.getWorksheets().get(0);

    double pageHeight = worksheet.getPageSetup().getPaperHeight();
    double topInch = worksheet.getPageSetup().getTopMarginInch();
    double bottomInch = worksheet.getPageSetup().getBottomMarginInch();

    double area = pageHeight - topInch - bottomInch;
    int row = 0;
    double offset = 0;
    while (offset < area) {
        offset += worksheet.getCells().getRowHeightInch(row++);
    }
    System.out.println(row);
}

test.zip (6.4 KB)

Can you please clarify - how i can calculate a rows count on page

Best regards. Alexey


#7

@makarovalv,
We were able to observe the issue but we need to look into it more. We have logged the issue in our database for investigation and for a fix. Once, we will have some news for you, we will update you in this topic.

This issue has been logged as

CELLSJAVA-42960 – Issue while counting rows in a page based on getRowHeightInch( )

#8

@makarovalv,

We evaluated your issue further. The page break is not just simply calculated by the code like double rows = (pageHeight-topInch-bottomInch)/rowSize; . Please try the API: WorkSheet.getPrintingPageBreaks(ImageOrPrintOptions options).
e.g
Sample code:

Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
System.out.println(sheet.getPageSetup().getPaperSize());

//Because the sheet is empty, nothing to print. So insert value at Cell A100 for test.
sheet.getCells().get("A100").setValue("test");
CellArea[] cellAreas = sheet.getPrintingPageBreaks(new ImageOrPrintOptions());
for(CellArea cellArea : cellAreas)
{
    System.out.println(cellArea);
}

workbook.save("E://out.xlsx");

You can see the first page is A1:A52. Count of rows is 52.


#9

@makarovalv,

Regarding the issue “CELLSNET-46816”, here are a few tips for your to try:

First, if the width or height of the image is larger than page content width or height, the image will be always crossing over pages.

Second, if both width and height of the image is smaller than page content width and height, you can

  1. use Worksheet.GetPrintingPageBreaks(ImageOrPrintOptions options) to get page breaks.
  2. check whether the image crosses pages according to Picture.UpLeftRow, UpLeftColumn, LowerRightRow, LowerRightColumn
  3. add proper page breaks in the sheet.
  4. check the page breaks again(same as 1.) to see if everything is OK.

Hope, this helps a bit.


#10

Hi.

Thank you very much. This API is closed my needs fully.

Best regards. Alexey


#11

@makarovalv,

It looks like suggested APIs and code segments work for your needs. If you still find any issue, let us know with details and we will be happy to assist you soon.