Trim White-space Around a Page for the whole PDF

Hi


The code from Aspose.PDF for .NET Documentation|Aspose.PDF for .NET only trims the first page.

Can you guys, please, update the code to trim the whole pdf?

Thanks

Does the aspose team respond to the forum?

Hi Hermes,


Thanks for your interest in Aspose. Please not the stated code sample is targeted for one page processing. If you want to process whole PDF document then you need to iterate through the pages and update these accordingly. Please check sample code snippet as following, hopefully it will help you to accomplish the task.

// Load source PDF file<o:p></o:p>

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");

Please feel free to contact us for any further assistance.


Best Regards,

Hi


In my document, the margins change by the page.

I see in the code, one of the margins is fixed:

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
);

Shouldn’t this code be .pages[pageno]?

Thanks

Hi Hermes,


Thanks for your feedback. You are right in For loop code, Page index should be replaced with iteration variable(pageno) instead hard coding it.

I am sorry for the inconvenience.

Best Regards,

what about doc.pages[pageno].Rotate


your code only works correct for doc.pages[pageno].Rotate == Rotation.None

can you update your examples so that they will also crop Pages with a rotation of
Rotation.On90, Rotation.On180 or Rotation.On270

I tried to figure out myself, but failed.

Hi Markus,


Thanks for your inquiry. I have tested the scenario with rotated PDF page and noticed the reported issue. So we have logged a ticket PDFNEWNET-39678 in our issue tracking system for further investigation and resolution.

We are sorry for the inconvenience caused.

Best Regards,
Hi!

The code is cutting some letters on the right of the page. You can try it out on the following document http://portal.mp.sc.gov.br/portal/conteudo/publicacoes/do_mpsc_2016-03-10.pdf

From page 4 onwards it cropping some letters on the right.

PLEASE, HELP

Hi Hermes,


Thanks for your inquriy. I have tested the scenario with Aspose.Pdf for .NET 11.4.0 and unable to notice the reported issue, I have also attached my sample output here for reference. We will appreciate it if you please share your sample code and output document here, so we will look into it and guide you accordingly.

We are sorry for the inconvenience caused.

Best Regards,