Thomas Hawkins
Thomas Hawkins

Reputation: 101

Remove outer print marks on PDF iTextSharp

I have a pdf file with a cover that looks like the following: uncropped pdf file

Now, I need to remove the so-called 'galley marks' around the edges of the cover. I am using iTextSharp with C# and I need code using iTextSharp to create a new document with only the intended cover or use PdfStamper to remove that. Or any other solution using iTextSharp that would deliver the results.

I have been unable to find any good code samples in my search to this point.

Upvotes: 0

Views: 1507

Answers (1)

Chris Haas
Chris Haas

Reputation: 55457

Do you have to actually remove them or can you just crop them out? If you can just crop them out then the code below will work. If you have to actually remove them from the file then to the best of my knowledge there isn't a simple way to do that. Those objects aren't explicitly marked as meta-objects to the best of my knowledge. The only way I can think of to remove them would be to inspect everything and see if it fits into the document's active area.

Below is sample code that reads each page in the input file and finds the various boxes that might exist, trim, art and bleed. (See this page.)

As long as it finds at least one it sets the page's crop box to the first item in the list. In your case you might actually have to perform some logic to find the "smallest" of all of those items or you might be able to just know that "art" will always work for you. See the code for additional comments. This targets iTextSharp 5.4.0.0.

//Sample input file
var inputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Binder1.pdf");

//Sample output file
var outputFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Cropped.pdf");

//Bind a reader to our input file
using (var r = new PdfReader(inputFile)) {

    //Get the number of pages
    var pageCount = r.NumberOfPages;

    //See this for a list: http://api.itextpdf.com/itext/com/itextpdf/text/pdf/PdfReader.html#getBoxSize(int, java.lang.String)
    var boxNames = new string[] { "trim", "art", "bleed" };

    //We'll create a list of all possible boxes to pick from later
    List<iTextSharp.text.Rectangle> boxes;

    //Loop through each page
    for (var i = 1; i <= pageCount; i++) {

        //Initialize our list for this page
        boxes = new List<iTextSharp.text.Rectangle>();

        //Loop through the list of known boxes
        for (var j = 0; j < boxNames.Length; j++) {

            //If the box exists
            if(r.GetBoxSize(i, boxNames[j]) != null){

                //Add it to our collection
                boxes.Add(r.GetBoxSize(i, boxNames[j]));
            }
        }

        //If we found at least one box
        if (boxes.Count > 0) {

            //Get the page's entire dictionary
            var dict = r.GetPageN(i);

            //At this point we might want to apply some logic to find the "inner most" box if our trim/bleed/art aren't all the same
            //I'm just hard-coding the first item in the list for demonstration purposes
            //Set the page's crop box to the specified box
            dict.Put(PdfName.CROPBOX, new PdfRectangle(boxes[0]));
        }
    }

    //Create our output file
    using (var fs = new FileStream(outputFile, FileMode.Create, FileAccess.Write, FileShare.None)) {
        //Bind a stamper to our reader and output file
        using(var stamper = new PdfStamper(r,fs)){
            //We did all of our PDF manipulation above so we don't actually have to do anything here
        }
    }
}

Upvotes: 2

Related Questions