Remaining space on a page

We have a condition where we would like to know when creating a new PDF document for a given page, how much space is not used (blank condition) for the body of the page. This would exclude any paragraphs/tables in the Header Footer areas. We are using .NET

Is there a way to do this (kind a new to ASPOSE)

@johne42

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 code sample is as below:

// Load source PDF file
Document doc = new Document("input.pdf");
// Render the page to image with 72 DPI
PngDevice device = new PngDevice(new Resolution(72));
for (int pageno = 1; pageno <= doc.Pages.Count; pageno++)
{
using (MemoryStream imageStr = new MemoryStream())
{
device.Process(doc.Pages[pageno], imageStr);
Bitmap bmp = (Bitmap)Bitmap.FromStream(imageStr);
System.Drawing.Imaging.BitmapData imageBitmapData = null;
// Determine white areas
try
{
imageBitmapData = bmp.LockBits(new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height),
System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
Aspose.Pdf.Rectangle prevCropBox = doc.Pages[1].CropBox;
int toHeight = bmp.Height;
int toWidth = bmp.Width;
int? leftNonWhite = null;
int? rightNonWhite = null;
int? topNonWhite = null;
int? bottomNonWhite = null;
for (int y = 0; y < toHeight; y++)
{
byte[] imageRowBytes = new byte[imageBitmapData.Stride];
// Copy the row data to byte array
if (IntPtr.Size == 4)
System.Runtime.InteropServices.Marshal.Copy(new IntPtr(imageBitmapData.Scan0.ToInt32() + y * imageBitmapData.Stride), imageRowBytes, 0, imageBitmapData.Stride);
else
System.Runtime.InteropServices.Marshal.Copy(new IntPtr(imageBitmapData.Scan0.ToInt64() + y * imageBitmapData.Stride), imageRowBytes, 0, imageBitmapData.Stride);
int? leftNonWhite_row = null;
int? rightNonWhite_row = null;
for (int x = 0; x < toWidth; x++)
{
if (imageRowBytes[x * 4] != 255
|| imageRowBytes[x * 4 + 1] != 255
|| imageRowBytes[x * 4 + 2] != 255)
{
if (leftNonWhite_row == null)
leftNonWhite_row = x;
rightNonWhite_row = x;
}
}
if (leftNonWhite_row != null || rightNonWhite_row != null)
{
if (topNonWhite == null)
topNonWhite = y;
bottomNonWhite = y;
}
if (leftNonWhite_row != null
&& (leftNonWhite == null || leftNonWhite > leftNonWhite_row))
{
leftNonWhite = leftNonWhite_row;
}
if (rightNonWhite_row != null
&& (rightNonWhite == null || rightNonWhite < rightNonWhite_row))
{
rightNonWhite = rightNonWhite_row;
}
}
leftNonWhite = leftNonWhite ?? 0;
rightNonWhite = rightNonWhite ?? toWidth;
topNonWhite = topNonWhite ?? 0;
bottomNonWhite = bottomNonWhite ?? toHeight;
// Set crop box with correction to previous crop box
doc.Pages[pageno].CropBox =
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
);
}
finally
{
if (imageBitmapData != null)
bmp.UnlockBits(imageBitmapData);
}
}
}
// Save the document
doc.Save("finalupdatedDocument.pdf");

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.