Resize/Shrink PDF Content in Java | Aspose.PDF for Java

Aspose Support:

We have purchased Aspose.Pdf for Java. Using your trial version (for evaluation purpose), I have come up with some code to see if it will meet our requirement. The code seems to be working fine but I am writing this to get your input/suggestions/improvements if any to my code. My point here is as you own Aspose API, you can suggest me if there is any other better way to achieve same result which I am achieving through my code now.

My requirement is to read PDF source files from a directory one file at a time. Then for that particular PDF file convert each page to an individual .jpg image by adding blank space in top of page so that there is enough space in top where some other process can put a bar code (Please note here my process need to leave blank space in top only. Bar code needs to be put in blank space by some other process.). Also, please note here source PDF files which we can receive will be very diverse in nature e.g. some may have portrait orientation, some may be landscape, some may be 8.5 x 11 inch letter page size, some may have page size weird as 5.9 x 15.9 inch, some may be properly created, some may be a PDF created by scanning some documents, some may have images too apart from text and etc. In short our source PDFs are varied in nature.

Currently the code I have written has below steps using APIs provided in Aspose.Pdf for Java JAR:

1. Convert an individual PDF page to a .jpg image.

2. Create a new PDF with defined page size 8.5 x 11 in (Letter Size). Set the margin of page in such a way that we are leaving blank space on top. Now add the image created in step 1 to this page and save new PDF. We understand based on original page size there are chances that content of original PDF page will be scaled as the image created from it in step 1 has to fit in the new page size which has some blank space on top too. We are fine with scaling. Using the defined page size will help ensure that all converted documents to be of same size and so absolute blank space left in top (using page margin) for bar code will be uniform for all converted documents.

3. Convert the newly created PDF in step 2 to a .jpg image.

The .jpg created in step 3 is our final converted document.

I am also sharing my code here as file attachment to this post. Please have a look at it. Please let me know if you have any suggestion to improve this approach further using your APIs. As there may be some better APIs available in your package/JAR which I am not using and that may be better way to achieve same result.

Please let me know if you need any additional information. Looking forward for your response. Thanks for your help!

Sumit Jha

CVS Health

Hi Paul,


Thanks for your inquriy. As per my understanding you want to re-size PDF page contents to add barcode in header. You may use resizeContesnt() method of PdfFileEditor class for the purpose. Please check following documentation link for the purpose, hopefully it will help you to accomplish the task.


Please feel free to contact us for any further assistance.

Best Regards,

Hi Tilal,


Appreciate your response very much.

Based on suggestion given by you, I created an example program and tried to shrink some PDFs. One of the PDF when I tried shrink (5% all side), the output PDF has many texts, some bar code and image which were not visible in original PDF. So, I am not sure from where they are appearing. For your convenience, I have attached the PDF I used along with processed PDF and code I used to do so.

Also, even if I use this shrinking approach, I can leave some top margin as blank space where other process can put bar code. But as I have mentioned in my original post that input PDF for us are of different varieties like this one is of 5.86 x 15.93 in. So even if we have left top margin as absolute 50 points when this pdf will be printed in letter size page the only way the printer will print full content of page is using option “Shrink Oversize Page” at that point this absolute blank space in top will be shrinked from 50 points to some other lower value. The goal is to keep same absolute blank space in top for all input PDFs (some input PDF may be 8.5 x 11 in whereas some may be like this one 5.86 x 15.93 in) so that bar code of same height can be printed to them on same place on the PDF page for processed PDF. That is why we thought that whatever may be the original PDF page size we will convert it to a letter size PDF which will be later converted to .jpg (as we need .jpg file as final output).

I request you to kindly go through my original post once more to understand the constraints I have and also look my original code which I attached in my original post and provide suggestion in case there is a better way to do achieve same result with constraints I have.

Please let me know in case you have any questions for me.

Thanks,
Sumit Jha
CVS Health

Hi Sumit,


Thanks for your feedback. I have resized the page contents of your source document using resizeContents() method and noticed the reported issue, there is some document specific issue with the PDF document. I have logged a ticket PDFNEWJAVA-35618 in our issue tracking system for further investigation and resolution. We will keep you updated about the issue resolution progress.

Furthermore I have checked your code shared in first post, you can use it as a workaround. However I will suggest you to use my above suggested solution to resize PDF page contents using resizeContents() method and convert resultant PDF to JPEG image for further processing. Please note if you want to create some absolute blank space(50) on top of the PDF page then you can set top margin in units instead percentage.


PdfFileEditor.ContentsResizeValue.units(50),


We are sorry for the inconvenience caused.


Best Regards,

Hi Tilal,


Thanks for your response! Please keep me updated about issue resolution progress.

Are you going to keep me updated here in this message thread or by which medium you will let me know?

As we have to move our code in next 20 days in production, do you think your resolution will be available before this?

Also, while I am thinking about approach which you are suggesting, I am seeing an issue with fulfilling my requirement constraints. Let us say I have 2 PDF documents first of size 8.5 x 11 in and second of size 5.86 x 15.93 in. Now as you have suggested I will leave 50 points absolute blank space in top to both these PDFs. But when I will convert them to a letter size (8.5 x 11 in) image (using method like objConverter.getNextImage(“filePath”,com.aspose.pdf.PageSize.getPageLetter(), com.aspose.pdf.ImageType.getJpeg(), 100);) then in resultant image for first PDF still the 50 points blank space in top will remain but for second PDF along with PDF contents the blank space in top will be scaled to some value lower than 50 points (please note here the original size of second PDF whose height was larger than letter size page height). Please note here that the resultant .jpg which I want should be of same letter size for all PDF converted with 50 points blank space in top.

Please let me know your thoughts on how I will satisfy this constraint with your solution approach.

I am eagerly waiting for your response for my this post.

Thanks,
Sumit Jha


Hi Sumit,

Thanks for your feedback.

cvscorporate:

Thanks for your response! Please keep me updated about issue resolution progress.

Are you going to keep me updated here in this message thread or by which medium you will let me know?

As we have to move our code in next 20 days in production, do you think your resolution will be available before this?


Yes we will keep you updated in this thread.

Furthermore in reference to ETA, I am afraid we can not share the time lines until our product team completes the issue investigation.

cvscorporate:

Also, while I am thinking about approach which you are suggesting, I am seeing an issue with fulfilling my requirement constraints. Let us say I have 2 PDF documents first of size 8.5 x 11 in and second of size 5.86 x 15.93 in. Now as you have suggested I will leave 50 points absolute blank space in top to both these PDFs. But when I will convert them to a letter size (8.5 x 11 in) image (using method like objConverter.getNextImage("filePath",com.aspose.pdf.PageSize.getPageLetter(), com.aspose.pdf.ImageType.getJpeg(), 100);) then in resultant image for first PDF still the 50 points blank space in top will remain but for second PDF along with PDF contents the blank space in top will be scaled to some value lower than 50 points (please note here the original size of second PDF whose height was larger than letter size page height). Please note here that the resultant .jpg which I want should be of same letter size for all PDF converted with 50 points blank space in top.

Please let me know your thoughts on how I will satisfy this constraint with your solution approach.


In this reference to this query, if you are using same PDF document(5.86 x 15.93) shared above then you need to wait for above logged issue. However if you are using some other sample PDF document then please share the sample documents here along with code, we will look into it and guide you accordingly.

We are sorry for the inconvenience caused.

Best Regards,

Hi Tilal,


Please find attached some of PDF files with which I am trying and I have included my code too. There are total 3 attachment zip files.

Please see below points:

1. First of all it seems that resizeContents() method is having trouble with any PDF with page size greater 8.5 x 11 in (especially where page height is greater than 11 in). Like we had problem earlier with 5.86 x 15.93 in document for which you had opened an issue. Check in attachment Test2.pdf, Test3.pdf and Test4.pdf after resizing pdf shows contents which was not seen in original PDF. Due to this I am facing trouble to give you an example about the problem I am trying to raise.

2. Please check my source code where I had put comments. Please read those comments which will explain the issue I am trying to raise to you for my requirement related constraint apart from issue with resizeContents() method. I have also tried my best to explain issue in point 3 below.

3. Here the point am I trying to state is that my source PDFs can come in variety of sizes. Let us take an example - I have 2 PDFs. The first PDF is of page size 8.5 x 11 in and second PDF has page size let us assume 8.5 x 22 in. Now using the resizeContents() method if I define top margin to 50 points in both the documents then in resized PDFs both the pdf page size will still be same as their original size respectively but both will have a top margin of 50 points and rest of page contents will be scaled to fit in remaining area of page after leaving 50 points top margin. Now in the next step I am converting this resized pdf to a letter size (8.5 x 11 in) .jpg image (this is needed as I need all .jpg which is converted from source PDFs to Letter size so they will print correctly on a letter size page). At this stage for first PDF .jpg created will have still 50 points top margin. But for second PDF .jpg created will have (50 / (22/11) ) top margin = 25 points. Note for second PDF the source/resized pdf had height of 22 in but .jpg has height of 11 in. So, the whole resized pdf will be scaled to half size to fit in a .jpg of 11 in height. Do you agree with me here?


So, if I am saying that I want 50 points = 50/72 in = 0.69 in top margin in final .jpg file then I can not blindly add 50 points top margin in resizeContents() method instead I will have to come up with a formula like below:

top margin = 50 / (Letter Size Page Height / Source PDF Page Height) = 50 / (792 / Source PDF Page Height)

1. This way for first PDF which has Page Size = 8.5 x 11 in = 612 x 792 points in aspose:

top margin = 50 / (792/792) = 50 / 1 = 50 points to be used in resizeContents() method.

Now when this resized PDF with 50 points top margin will be converted to a Letter Size .jpg the 50 points will reamin 50 points as the source/resized PDF was of letter size (8.5 x 11 in) itself and no further scaling needs to happen.

2. But for second PDF which has Page Size = 8.5 x 22 in = 612 x 1584 points in aspose:

top margin = 50 / (792/1584) = 50 / 0.5 = 100 points to be used in resizeContents() method.

Now when this resized PDF with 100 points top margin will be converted to a Letter Size .jpg the 100 points will scale to 50 points as the source/resized PDF height was 22 in and Letter Size .jpg height is 11 in so whole content of resized PDF page will be scaled by a factor of 2.

Similarly we can come up formula for bottom, left and right margin. Left and right margin will depend on page width instead of page height.

Currently this formula I have not included in my source code example in current approach but I just wanted to get your thoughts on this if my understanding is correct. Please let me know your thought.

But we need to remember that this approach will work only when problem with resizeContents() methods is fixed for which you had opened a ticket.

I also wanted to bring to your notice my original approach was not having any such issue as I had not used resizeContents() methods there. But as you were suggesting to use resizeContents() methods I was looking for the ways how my all requirement constraints can be satisfied even when I am using resizeContents() methods approach.

Please let me know if you have any further suggestion or more alternative approaches.

Thanks,
Sumit Jha
CVS Health

Hi Sumit,


Thanks for your sharing the additional information. We are looking into it and will guide you accordingly.

Best Regards,

Hi Sumit,


I am sorry for the delayed response. I have gone through your code and description, your formula for calculating top margin according to document height make sense, otherwise same space will be created regardless of the document height for all PDF documents.

Furthermore, I have noticed that Test2.pdf and Test3.pdf showing same behavior as I noticed in above logged issue(PDFNEWJAVA-35618), our product team will analyse these document during the issue investigation. However Test4.pdf results blank document after resize, so I have logged another ticket PDFNEWJAVA-35792 to investigate and rectify the issue. We will keep you updated about the issue resolution progress within this forum thread.

We are sorry for the inconvenience.

Best Regards,