PDF cropping using Aspose PDF

Hello,

I have a single page PDF document containing one vector image in it. However, there is white space around that image and I want to edit the PDF in a way that extra white space should be cropped out from the document.
As an example I have attached two files - “Source.pdf” is the one which needs to be cropped and “Target.pdf” is the one showing the desired output we require.
Source.pdf (16.3 KB)
Target.pdf (24.3 KB)

Can you please guide me with how it can be achieved using Aspose PDF java library APIs?

Thanks,
Neeraj

@neergupta

Thanks for contacting support.

Please check following code snippet to obtain desired output.

// load the source PDF document
Document document = new Document(dataDir + "Source.pdf");

// get page to trim white space
com.aspose.pdf.Page pdfPage = document.getPages().get_Item(1);

// get the content boundaries
com.aspose.pdf.Rectangle contentBBox = pdfPage.calculateContentBBox();

// set Page CropBox and MedioBos as per content boundries to tirm white space
pdfPage.setCropBox(contentBBox);
pdfPage.setMediaBox(contentBBox);

// save the resultant PDF
document.save(dataDir + "output_trim.pdf"); 

For your reference, I have attached an output document as well, generated by above code. In case of any further assistance, please feel free to contact us.

output_trim.pdf (14.4 KB)

Thanks very much for sharing the code snippet. However the output I am getting using this code is that of evaluation version as attached here and it varies form the one attached by you.

Following is the code I am using for setting the license where the license file is a valid file till June 2018:
InputStream licenseStream = new FileInputStream
(new File(“C:\temp\Aspose.Total.Java.lic”));
asposeCellsLicense.setLicense(licenseStream);

Can you please let me know what is going wrong here?

Thanks,
Neeraj
output_trim.pdf (41.2 KB)

@neergupta

Thanks for your feedback.

It seems that you are setting license only for Aspose.Cells API, whereas, you need to set license for every API, you are using in your code. For example please check following lines of code, where I have set license for each API to use them in my program.

com.aspose.pdf.License plic = new com.aspose.pdf.License();
plic.setLicense(dataDir + "Aspose.Total.Java.lic");
com.aspose.slides.License slic = new com.aspose.slides.License();
slic.setLicense(dataDir + "Aspose.Total.Java.lic");
com.aspose.words.License wlic = new com.aspose.words.License();
wlic.setLicense(dataDir + "Aspose.Total.Java.lic"); 

In case if you still face any issue, please share your license file with us, in a private message. We will test it in our environment and respond you accordingly.

Hello,

I have further questions with regards to cropping of PDF.

I am able to crop the PDF by using the code snippet you suggested. However, there is one more thing I wanted to achieve. While cropping I wanted to control the size of the cropped rectangle/box by means of following ways:

  1. Using page margins.
  2. Using page width.

I did try MarginInfo and PageSize specific methods of PdfPage but could not achieve the results.

Can you please provide me methods to be used along with appropriate code snippets to achieve the same?

For your better understanding, I have also attached the source, actual output and desired output PDF.

Thanks,
Neeraj

documents.zip (138.3 KB)

@neergupta

Thanks for your inquiry.

You need to manipulate Rectangle of content boundaries obtained by Page.calculateContentBBox method, in order to get desired results. However, please check following code snippet, where I have manipulated the values of Rectangle dimensions, so that the crop box would be larger than it was before. For your reference, I have attached an output document as well, which was generated by below code snippet.

// load the source PDF document
Document document = new Document(dataDir + "SourceGraph.pdf");

// get page to trim white space
com.aspose.pdf.Page pdfPage = document.getPages().get_Item(1);

// get the content boundaries
com.aspose.pdf.Rectangle contentBBox = pdfPage.calculateContentBBox();
contentBBox.setLLX(contentBBox.getLLX() - 10);
contentBBox.setLLY(contentBBox.getLLY() - 10);
contentBBox.setURX(contentBBox.getURX() + 10);
contentBBox.setURY(contentBBox.getURY() + 10);
// set Page CropBox and MedioBos as per content boundries to tirm white space
pdfPage.setCropBox(contentBBox);
pdfPage.setMediaBox(contentBBox);

// save the resultant PDF
document.save(dataDir + "output_trim_desired.pdf");

output_trim_desired.pdf (30.1 KB)

In case of any further assistance, please feel free to let us know.

Hi,
Thanks for your prompt response. It worked fine.

Along with that I wanted to update the page size also.
As you can see in the PDF “output_trim_desired.pdf” you just shared to me, the page size is “6.63 x 2.43 inches”. Whereas, the page size I require is say “3.3 x 1.21 in”.
I did try method setPageSize(3.3, 1.21) of the Page however it didn’t work as desired.

Kindly let me know how can I achieve this?

Thanks,
Neeraj

@neergupta

Thanks for writing back.

Please note that the basic measurement unit in PDF is point, where 72 points = 1 inch, so we have tried following code snippet to change the Page Size as per your requirement (i.e “3.3 x 1.21 in”), but it seemed that the content inside the PDF Page was also cropped due to reduced page size.

com.aspose.pdf.facades.PdfPageEditor pe = new com.aspose.pdf.facades.PdfPageEditor();
pe.bindPdf(new Document(dataDir + "output_trim_desired.pdf"));
PageSize ps = new PageSize(237.6f, 88f);
pe.setPageSize(ps);
pe.save(dataDir + "output_trim_desired_resized.pdf");

output_trim_resized.pdf (30.4 KB)

However, we have logged an issue as PDFJAVA-37088 in our issue tracking system. We will further investigate it in details and keep you updated with the status of its resolution. Please be patient and spare us little time.

We are sorry for the inconvenience.

I was looking for various options possible with regards to PDF page manipulations.

Does Aspose PDF provide scaling API for the PDF?

For example if there is PDF document whose page size is 8x4. Is there any API which allows me to scale up/down so that I can increase its page size to 16x8 by scaling it up by 100% and reduce page size to 4x2 by scaling it down by 50%.

Thanks,
Neeraj

@neergupta

Thanks for your inquiry.

Aspose.Pdf does support the feature to scale page contents according to its height and width and using that feature, you may also obtain the desired output (i.e “3.3 x 1.21 in”) document. Please check following code snippet, where I have used PdfFileEditor to resize the contents of the file as per specified height/width (i.e “3.3 x 1.21 in”).

// load the source PDF document
Document document = new Document(dataDir + "SourceGraph.pdf");

// get page to trim white space
com.aspose.pdf.Page pdfPage = document.getPages().get_Item(1);

// get the content boundaries
com.aspose.pdf.Rectangle contentBBox = pdfPage.calculateContentBBox();
contentBBox.setLLX(contentBBox.getLLX() - 10);
contentBBox.setLLY(contentBBox.getLLY() - 10);
contentBBox.setURX(contentBBox.getURX() + 10);
contentBBox.setURY(contentBBox.getURY() + 10);
// set Page CropBox and MedioBox as per content boundaries to trim white space
pdfPage.setCropBox(contentBBox);
pdfPage.setMediaBox(contentBBox);
// save the resultant PDF
document.save(dataDir + "output_trim_desired.pdf");
		
document = new Document(dataDir + "output_trim_desired.pdf");
		
PdfFileEditor.ContentsResizeParameters parameters = new PdfFileEditor.ContentsResizeParameters(
		PdfFileEditor.ContentsResizeValue.units(33),
		PdfFileEditor.ContentsResizeValue.units(237.6),
		PdfFileEditor.ContentsResizeValue.units(-33),
		PdfFileEditor.ContentsResizeValue.units(-72 * 2.2),
		PdfFileEditor.ContentsResizeValue.units(88),
		PdfFileEditor.ContentsResizeValue.units(72 * 2.2));

int[] page_cnt1 = new int[document.getPages().size()];
for (int i = 0; i < document.getPages().size(); i++) {
	page_cnt1[i] = i + 1;
}

PdfFileEditor fileEditor = new PdfFileEditor();

fileEditor.resizeContents(document, page_cnt1, parameters);
document.save(dataDir + "output_trim_rescaled_out.pdf");

output_trim_rescaled_out.pdf (30.5 KB)

In case of any further assistance, please feel free to let us know.

Can you please quickly provide the documentation for the following method parameters, as I could not find this particular method in your API reference documentation in the link: Class PdfFileEditor.ContentsResizeParameters | Aspose.PDF for .NET API Reference

PdfFileEditor.ContentsResizeParameters parameters = new PdfFileEditor.ContentsResizeParameters(
PdfFileEditor.ContentsResizeValue.units(33),
PdfFileEditor.ContentsResizeValue.units(237.6),
PdfFileEditor.ContentsResizeValue.units(-33),
PdfFileEditor.ContentsResizeValue.units(-72 * 2.2),
PdfFileEditor.ContentsResizeValue.units(88),
PdfFileEditor.ContentsResizeValue.units(72 * 2.2));

Thanks,
Neeraj

@neergupta

Please find “Resize Page Contents of Specific Pages in a PDF file” article in our API documentation, demonstrating the usage of PdfFileEditor Class.

Thanks for your quick response.

Currently I am using following method to get the width and height of the PDF page.
pdfPage.getPageInfo().getHeight();
pdfPage.getPageInfo().getWidth();

However it is not same as shown in the PDF document properties.

Which method of the PDF page should I use to get the width and height of the page whose equivalent is shown in the PDF document properties. Kindly see attached screenshot for what I am referring to.

Thanks,
Neeraj

Capture.PNG (16.3 KB)

Thanks for sharing the documentation.

I can understand that this method allows you to specify margins, width and height either based on percentage or units.
However, I cannot understand in the code snippet you shared that on what basis you are specifying margin values.

Following are my questions with regards to this:
PdfFileEditor.ContentsResizeValue.units(33), – From where you got this value?
PdfFileEditor.ContentsResizeValue.units(237.6), – Understood. It is desired width which is 3.3 x 72
PdfFileEditor.ContentsResizeValue.units(-33), – From where you got this value?
PdfFileEditor.ContentsResizeValue.units(-72 * 2.2), – From where you got this value?
PdfFileEditor.ContentsResizeValue.units(88), – Understood. It is desired height which is 1.21.x 72
PdfFileEditor.ContentsResizeValue.units(72 * 2.2)); From where you got this value?

Thanks,
Neeraj

@neergupta

Thanks for writing back.

Please use following code snippet, in order to get height/width of the page as shown in PDF document properties.

document = new Document(dataDir + "output_trim_desired.pdf");
com.aspose.pdf.Page pdfP = document.getPages().get_Item(1);
System.out.println("Height: " + String.format( "%.2f",pdfP.getCropBox().getWidth() / 72));
System.out.println("Width: " + String.format( "%.2f",pdfP.getCropBox().getHeight() / 72));

@neergupta

Thanks for your inquiry.

After changing/modifying the page size of the PDF document, I have observed that the graph inside the page was cropped from edges, so I have adjusted the margins (i.e top, left, bottom, right) of the content until the graph was completely appeared in the frame. As you know that the PDF document does not contain any image but lines and text, so we cannot perform any calculations to squeeze the image according to desired page size.

In case of any further assistance, please feel free to let us know.

Thanks for your response.
However, what we wanted is that the adjustment you performed over here should be on certain basis of the source documents properties (like dimensions, margins etc…). We can not hard code margin values as you did over here. Although we can calculate them on certain basis.

In our case this is going to be automated where the source PDF document along with desired width and height will be provided to us. We have to resize the source PDF to the specified width and height.
I hope this clarifies our usecase well now.

So let let me know how can I achieve this?

Thanks,
Neeraj

@neergupta

We are looking into the scenario as per your requirements and as soon as we have some definite results, we will get back to you. Please be patient.

Can you please provide me with update on this?

Thanks,
Neeraj

@neergupta

Thanks for your patience.

We have tried to resize the PDF content on Dimensions and Margin Ratio basis, by following code snippet, but I am afraid that we did not get much success.

// Source Width = 477.36, Left/Right Margins = 90 <= Margins are 18.85 percent of width
// Source Height = 174.24, Top/Bottom Margins = 72 <= Margins are 41.32 percent of height
PdfFileEditor.ContentsResizeParameters parameters = new PdfFileEditor.ContentsResizeParameters(
PdfFileEditor.ContentsResizeValue.units(44.78), // as per 18.85% of required width
PdfFileEditor.ContentsResizeValue.units(148.04), // sum of margins - required width
PdfFileEditor.ContentsResizeValue.units(44.78), // as per 18.85% of required width
PdfFileEditor.ContentsResizeValue.units(36.3616), // as per 41.32% of required height
PdfFileEditor.ContentsResizeValue.units(15.2768), // sum of margins - required height
PdfFileEditor.ContentsResizeValue.units(36.3616)); // as per 41.32% of required height

Therefore we have logged another investigation ticket as PDFJAVA-37113 in our issue tracking system. Our product team will further look into the details of the issue and we will keep you posted with the status of its correction. Please be patient and spare us little time.

We are sorry for the inconvenience.