Here is three solutions I found, if it can help someone (using iTextSharp, Amyuni or Tracker-Software, as @Hetote said in the comments he was looking for another library):
Using iTextSharp
As answered by @martinbuberl in another question:
public static void CropDocument(string file, string oldchar, string repChar)
{
int pageNumber = 1;
PdfReader reader = new PdfReader(file);
iTextSharp.text.Rectangle size = new iTextSharp.text.Rectangle(
Globals.fX,
Globals.fY,
Globals.fWidth,
Globals.fHeight);
Document document = new Document(size);
PdfWriter writer = PdfWriter.GetInstance(document,
new FileStream(file.Replace(oldchar, repChar),
FileMode.Create, FileAccess.Write));
document.Open();
PdfContentByte cb = writer.DirectContent;
document.NewPage();
PdfImportedPage page = writer.GetImportedPage(reader,
pageNumber);
cb.AddTemplate(page, 0, 0);
document.Close();
}
Another answer by @rafixwpt in his question, but it doesn't remove the invisible elements, it cleans an area of the page, which can affect other parts of the page:
static void textsharpie()
{
string file = "C:\\testpdf.pdf";
string oldchar = "testpdf.pdf";
string repChar = "test.pdf";
PdfReader reader = new PdfReader(file);
PdfStamper stamper = new PdfStamper(reader, new FileStream(file.Replace(oldchar, repChar), FileMode.Create, FileAccess.Write));
List<PdfCleanUpLocation> cleanUpLocations = new List<PdfCleanUpLocation>();
cleanUpLocations.Add(new PdfCleanUpLocation(1, new iTextSharp.text.Rectangle(0f, 0f, 600f, 115f), iTextSharp.text.BaseColor.WHITE));
PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper);
cleaner.CleanUp();
stamper.Close();
reader.Close();
}
Using Amyuni
As answered by @yms in another question:
IacDocument.GetObjectsInRectangle Method
The GetObjectsInRectangle method gets all the objects that are in the
specified rectangle.
Then you can iterate all the objects in the page and delete those that you are not interested in:
//open a pdf document
document.Open(testfile, "");
IacPage page1 = document.GetPage(1);
Amyuni.PDFCreator.IacAttribute attribute = page1.AttributeByName("Objects");
// listObj is an array list of graphic objects
System.Collections.ArrayList listobj = (System.Collections.ArrayList) attribute.Value.Cast<IacObject>();;
// listObjToKeep is an array list of graphic objects inside a rectangle
var listObjToKeep = document.GetObjectsInRectangle(0f, 0f, 600f, 115f, IacGetRectObjectsConstants.acGetRectObjectsIntersecting).Cast<IacObject>();
foreach (IacObject pdfObj in listObj.Except(listObjToKeep))
{
// if pdfObj is not in visible inside the rectangle then call pdfObj.Delete();
pdfObj.Delete(false);
}
As said by @yms in the comments, another solution using the new method IacDocument.Redact in version 5.0 can also be used to delete all the objects in the specified rectangle and draw a solid color rectangle at their place.
Using Tracker-Software Editor SDK
I didn't try it but it seems possible, see this post.
PdfStamper
class for cropping: itextpdf.com/examples/iia.php?id=231 – Markus Palme