0

I'm working on a project, where I have various incoming PDF files with images, texts, linearts, etc. mixed together.

As a first step I need to find the smallest rectangle that includes all the contents and crop the rest. For that probably a C# implementation of MarginFinder could do the job.(ITextSharp crop PDF to remove white margins)

The second step would be to take the "margin free" contents and put it on a new PDF document, where the following could be user defined:

  • Dimension of the new page size,
  • Justification of the contents: should the container rectangle be on left, center or right justified,
  • Rotation of the container rectangle.

Could this be done solely with iTextSharp? If yes, anyone could share/point out some examples or documentations?

ZeitGeist
  • 19
  • 3
  • *"all the (visible) contents"* - this "(visible)" might be a problem. iText does not yet have an actual rendering feature, the margin finders the question you referenced talks about finds the rectangle containing all the content, visible or not. – mkl Jan 31 '18 at 11:32
  • In 99.9% of the cases, there are no problems with the invisible parts, so I'm not concerned about that part.The second step is the one that I'm a bit lost with. – ZeitGeist Jan 31 '18 at 12:08
  • The second step actually is simple, at least in principle. After you have determined the content rectangle, simply create a page of the desired size in your target document, import the source page, and add it to the target page using an affine transformation that depends on the desired location and rotation angle, so there merely is some math to do. Furthermore, if the rotation angle is limited to multiples of 90°, the cleaner way would be to manipulate and store the PDF using a stamper. – mkl Jan 31 '18 at 13:57
  • I just started to wonder about one thing: *"should the container rectangle be on left, center or right justified, Rotation of the container rectangle"* - do you want the *container* at the left / center / right *of the new page*? Or do you want the *contents of the container* at the left / center / right *of the container*? – mkl Jan 31 '18 at 14:10
  • Let's say we have a CAD drawing with random margins. As the first step I have to find the rectangle that contains the CAD drawing with 0 margins. Once it's done I need to import the cropped CAD drawing to the newly defined page. The CAD drawing should be aligned to the left, center or right side of the new page. The user may also set if the drawing should be rotated, and here the multiplies of 90 degrees is good. – ZeitGeist Jan 31 '18 at 14:21

1 Answers1

0
  1. step: Marginfinder translated to C# works good.
  2. step: The steps that mkl has suggested do the job.

The second step actually is simple, at least in principle. After you have determined the content rectangle, simply create a page of the desired size in your target document, import the source page, and add it to the target page using an affine transformation that depends on the desired location and rotation angle, so there merely is some math to do. Furthermore, if the rotation angle is limited to multiples of 90°, the cleaner way would be to manipulate and store the PDF using a stamper. – mkl

ZeitGeist
  • 19
  • 3