PDFDraw CropBox

I am using a very big pdf in my Android app and I would like to crop a part of it which is going to be displayed, instead of rendering the whole pdf. However, the cropbox is not working properly in any case.

Here is my code:

      Page page = getPage(); // MY PDF

            PDFDraw pdfDraw = new PDFDraw(); // I use PDFDraw because I need a transparent background.
            pdfDraw.setPageTransparent(true);
            pdfDraw.setAntiAliasing(true);
            //If I do not set this line, the PDF is not scaled properly
            pdfDraw.setImageSize((int) Math.ceil(page.getPageWidth()), (int) Math.ceil(page.getPageHeight()), false); // Is this setting the full pdf witdth??

            page.setCropBox(getMyCropBox()); // Set the cropbox // The bitmap will be the same size that the cropbox ??

            Bitmap bitmap = pdfDraw.getBitmap(page); // Get bitmap

I am not getting the correct crop box in any case. I have tried also some variations of this code.

Sometimes it tries to scale it to my cropbox width. Sometimes it gets cropped wrongly, and sometimes it is returning different sizes.

Please help!

What does getMyCropBox do?

You should modify the existing crop box, and not just set a new one. In particular, the crop box might have a displacement, which effectively moves the origin.

As for the bitmap size, that depends on if you use SetDPI or SetImageSize.

Since you set ImageSize, then that is the final size you get, meaning the DPI is variable. The DPI is calculated to fit the CropBox into the image.

If you use SetDPI, then the final image size is variable. Where PDF coordinates are in 1/72 inch. So standard 8.5"x11" page, is 612, 792 units, so with a DPI of 96, you would get an image 586x1056.

You are also turning off preserving aspect ratio in the call to SetImageSize.

What output are you trying to achieve exactly? The more I know of your requirements, the easier I can provide you with the best answer.

If your objective is to draw part of the page, and you are targeting a certain image size, say 1024x1024, then the following is what you would do.

// calculate your crop box, but based on the original crop box. Also, note that pages can be rotated, so you what you see on screen might not be how the page is defined. That is for a 90 degree page rotation, if you see a portaite page, then actually the page is landscaped. page.setCroptBox(new_crop_box); draw.setImageSize(1024, 1024, true); draw.getBitmap();

Note that since aspect ratio was to to true, the long side of the image will be 1024px, but the short side will be less.

Thank you very much Ryan. What I am trying is to allow zooming and render only the visible part. However, when I scale it, I am not getting the correct part.

In other part of my code, I change the scale of the pdf when the screen is zoomed:

`
page.scale(mCurrentZoom);

`


And later, I have the "render" method. I have one question. If draw.setImageSize sets the size of the resulting image, is the cropbox resized to fit the image size?

I used setImageSize because it had a better behaviour at the beginning, but obviusly I have a problem in the cropbox or in the image size.

And "getMyCropBox()" is the method that calculates my cropbox, which maybe I have to redefine.

Actually, I already have a code that works for non-transparent pdfs, so what I am trying to get, is the same code for a transparent pdf:

`
// mTile has my image properties
Matrix2D matrix = page.getDefaultMatrix(true, 0, 0);
matrix.translate(-mTile.mRect.left, -mTile.mRect.top);
mRasterizer.rasterize(page, // Pagina
mPixels,
mTile.TILE_WIDTH, mTile.TILE_HEIGHT,
true,
matrix,
mTile.getRectTRON());

`


And I expect my result stored in mPixels, so in the transparent case, I also do:

`
bitmap.getPixels(mPixels,0,width, 0, 0 ,width,height);

`

Which seems to work correctly.

Thank you very much!
El viernes, 7 de abril de 2017, 2:32:27 (UTC+2), Ryan escribió:

What does getMyCropBox do?

You should modify the existing crop box, and not just set a new one. In particular, the crop box might have a displacement, which effectively moves the origin.

As for the bitmap size, that depends on if you use SetDPI or SetImageSize.

Since you set ImageSize, then that is the final size you get, meaning the DPI is variable. The DPI is calculated to fit the CropBox into the image.

If you use SetDPI, then the final image size is variable. Where PDF coordinates are in 1/72 inch. So standard 8.5"x11" page, is 612, 792 units, so with a DPI of 96, you would get an image 586x1056.

You are also turning off preserving aspect ratio in the call to SetImageSize.

What output are you trying to achieve exactly? The more I know of your requirements, the easier I can provide you with the best answer.

If your objective is to draw part of the page, and you are targeting a certain image size, say 1024x1024, then the following is what you would do.

// calculate your crop box, but based on the original crop box. Also, note that pages can be rotated, so you what you see on screen might not be how the page is defined. That is for a 90 degree page rotation, if you see a portaite page, then actually the page is landscaped. page.setCroptBox(new_crop_box); draw.setImageSize(1024, 1024, true); draw.getBitmap();

Note that since aspect ratio was to to true, the long side of the image will be 1024px, but the short side will be less.

I have one question. If draw.setImageSize sets the size of the resulting image, is the cropbox resized to fit the image size?

No, the crop box is not touched. The crop box is part of the PDF, and rendering does not modify the PDF.

PDF is a vector format, so everything is scaled to map the cropbox coordinates to the target output dimension, and then everything is sampled to go from the much higher precision of the PDF to the discreet pixel coordinates of the output format. Any images that happen to be in the PDF page would be sampled (hopefully down sampled).

So what I have to do if I want to render only part of the PDF ?
El martes, 11 de abril de 2017, 1:49:58 (UTC+2), Ryan escribió:

I have one question. If draw.setImageSize sets the size of the resulting image, is the cropbox resized to fit the image size?

No, the crop box is not touched. The crop box is part of the PDF, and rendering does not modify the PDF.

PDF is a vector format, so everything is scaled to map the cropbox coordinates to the target output dimension, and then everything is sampled to go from the much higher precision of the PDF to the discreet pixel coordinates of the output format. Any images that happen to be in the PDF page would be sampled (hopefully down sampled).

You probably want to switch to PDFRasterizer, if you want to support zooming.

Example #9 in the PDFDraw sample shows how to render only a section of the PDF page (essentially zooming).

There is more math involved, as you need to modify the transformation matrix, but if you start with the sample as is, and then make small changes, you should get a quick grasp of what is happening.

Hi Ryan, is this also you ?? Google Groups

I already have a method using PDFRasterizer that works, but the whole thing is that PDFRasterizer does not respect transparency, right ? So I started to Use PDFDraw with setTransparency(true), but it does not support zooming?
El martes, 11 de abril de 2017, 20:56:56 (UTC+2), Ryan escribió:

You probably want to switch to PDFRasterizer, if you want to support zooming.

Example #9 in the PDFDraw sample shows how to render only a section of the PDF page (essentially zooming).

There is more math involved, as you need to modify the transformation matrix, but if you start with the sample as is, and then make small changes, you should get a quick grasp of what is happening.

Yes, I was posting there.

So I started to Use PDFDraw with setTransparency(true), but it does not support zooming?

It does, by shrinking the crop box, and choosing a fixed output size, it would “zoom”. The issue is mainly with the math involved in finding the correct crop box value. Also, this involves modifying the PDFDoc object, which means you can’t do things concurrently (though that might not be important for you).

The PDFRasterizer is a better fit for zooming/panning, so if the issue with that was transparency, then we would need to expose that through the API.

Are you under a certain schedule/deadline?

What version of PDFNet for Android are you using? e.g. call PDFNet.getVersion()

Thank you Ryan. My version is 6.7.1

I posted my rasterizer parameters two messages before. Could you help me with the math ?

I don’t have a deadline but I am also looking for other libraries.
El jueves, 13 de abril de 2017, 19:44:07 (UTC+2), Ryan escribió:

Yes, I was posting there.

So I started to Use PDFDraw with setTransparency(true), but it does not support zooming?

It does, by shrinking the crop box, and choosing a fixed output size, it would “zoom”. The issue is mainly with the math involved in finding the correct crop box value. Also, this involves modifying the PDFDoc object, which means you can’t do things concurrently (though that might not be important for you).

The PDFRasterizer is a better fit for zooming/panning, so if the issue with that was transparency, then we would need to expose that through the API.

Are you under a certain schedule/deadline?

What version of PDFNet for Android are you using? e.g. call PDFNet.getVersion()

Can Anyone help me in this operation?
El martes, 18 de abril de 2017, 22:26:37 (UTC+2), (desconocido) escribió:

Thank you Ryan. My version is 6.7.1

I posted my rasterizer parameters two messages before. Could you help me with the math ?

I don’t have a deadline but I am also looking for other libraries.

El jueves, 13 de abril de 2017, 19:44:07 (UTC+2), Ryan escribió:

Yes, I was posting there.

So I started to Use PDFDraw with setTransparency(true), but it does not support zooming?

It does, by shrinking the crop box, and choosing a fixed output size, it would “zoom”. The issue is mainly with the math involved in finding the correct crop box value. Also, this involves modifying the PDFDoc object, which means you can’t do things concurrently (though that might not be important for you).

The PDFRasterizer is a better fit for zooming/panning, so if the issue with that was transparency, then we would need to expose that through the API.

Are you under a certain schedule/deadline?

What version of PDFNet for Android are you using? e.g. call PDFNet.getVersion()

Example 8 here is the best place to start.
https://www.pdftron.com/pdfnet/samplecode/PDFDrawTest.java.html

I suppose the example is not the best, since it adjusts the buffer size, while in normal usage the buffer would be fixed, and you would scale and move to draw what you want. But if you start with that sample, and then make small changes to the matrix, you can see what the effects are.