We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Urgent ! Find width of content on a pdf page

Hi Team,

Is there a way i can find the width of the content on a particular page in a pdf. Suppose i have my content only on half of the page and the other half is empty. Can i know the width of either the empty part or the part that has ocntent ?

@dewan.ishi

Could you please share the sample input document with us. We will test the scenario in our environment and address it accordingly.

I have attached the sample pdf and an image signifying the area to be calculated.
So, i want to know the width occupied by the text and the width of the full page of pdf.sample.pdf (3.0 KB)
indent preformatted text by 4 spacessample.jpg (243.9 KB)

@dewan.ishi

Thanks for sharing sample PDF.

Please note that there is no direct method to calculate width of textual content inside PDF document. However, Aspose.PDF for .NET supports a feature to trim white space around the PDF page where you calculate the height and width of non-white area in PDF page. The complete example is given in Trim White-space Around a Page article of API documentation.

In above example, you can find complete width of page by following line of code:

/////////
// above code is present in example
/////////
Aspose.Pdf.Rectangle prevCropBox = doc.Pages[1].CropBox;

int toHeight = bmp.Height;
// Total width of page
int toWidth = bmp.Width;
/////////
// below code is present in example
/////////

The width of content can be calculated by determining width of Rectangle achieved at the end of code snippet before saving document:

/////////
// above code is present in example
/////////
leftNonWhite = leftNonWhite ?? 0;
rightNonWhite = rightNonWhite ?? toWidth;
topNonWhite = topNonWhite ?? 0;
bottomNonWhite = bottomNonWhite ?? toHeight;

Rectangle rect = new Aspose.Pdf.Rectangle(
                            leftNonWhite.Value + prevCropBox.LLX,
                            (toHeight + prevCropBox.LLY) - bottomNonWhite.Value,
                            rightNonWhite.Value + doc.Pages[1].CropBox.LLX,
                            (toHeight + prevCropBox.LLY) - topNonWhite.Value
                            );
// get width of non white space
var contentWidth = rect.Width;
/////////
// below code is present in example
/////////

We hope this information will be helpful. Please share your feedback with us if you face any issue.