Frantumn
Frantumn

Reputation: 1774

Crop the footer from PDF using Ghostscript

I am trying to crop the bottom inch of a PDF with Ghostscript and C#.

When I run my code, it successfully creates the new PDF, but it is completely blank. My understanding of gsArgsList.Add(" -c \"[/CropBox [0 72 0 0] /PAGES pdfmark\""); is that is will crop 72pt (1 inch) off of the bottom of the page. I've included some code in hopes someone will see something obvious.

I have played with the 4 number parameters of CropBox but no matter what I pass, it always results in a blank PDF. I only want to remove the footer.

UPDATE - My code now shows the working solution.

/// <summary>
/// Crop the bottom inch from the temporary PDF file using GhostScript.
/// A new PDF file (test_cropped.pdf) will be created with the modifications.
/// </summary>
/// <param name="tempPDF">the path to the temporary PDF</param>
/// <param name="croppedPDF">the path to the modified PDF</param>
public static void cropPDFFooter(string inputPDF, string outputPDF, string pdfPostScript)
{
     try
        {
            string gsPath = @"C:\gs\gs9.14\bin\gswin64c.exe";

            List<string> gsArgsList = new List<string>();
            gsArgsList.Add(" -o " + outputFile);
            gsArgsList.Add(" -sDEVICE=pdfwrite");
            gsArgsList.Add(" -o " + outputFile + " -c \"mark /.HWMargins [ 0.0 288.0 0.0 0.0 ] .dicttomark setpagedevice\" -f " + inputFile + "");

            var gsArgs = String.Join(null, gsArgsList);

            System.Diagnostics.Process.Start(gsPath, gsArgs);

        }
    catch (Exception ex)
    {
        Console.WriteLine(ex.ToString());
        Console.ReadLine();
    }
}

Upvotes: 1

Views: 1603

Answers (2)

Ray Johnston
Ray Johnston

Reputation: 613

Ghostscript also supports .HWMargins [ lx by rx uy ] which is an array of four reals that are the margins in points (1/72 inch). If I do:

 gs -sDEVICE=pdfwrite -o x.pdf \
    -c "mark /.HWMargins [ 144.0 288.0 0.0 0.0 ] .dicttomark setpagedevice" \
    -f examples/annots.pdf

Then the output has the left two inches cropped and the bottom 4 inches cropped.

Note that this can be used with -dPDFFitPage -dFIXEDMEDIA before the -c and the page will be fit (with all information including footers) to the page with the larger margins.

There is no way to fit a page to a smaller size AND crop part of it in a single pass through Ghostscript (AFAIK).

Upvotes: 3

KenS
KenS

Reputation: 31207

The pdfwrite device simply maintains much of the content of an input PDF file. Your CropBox pdfmark will simply be overwritten by the CropBox from the input PDF file (it would work for PostScript input, provided there are no other CropBox pdfmarks in the PostScript file).

If ou want to alter the page size and contents you could try putting the PostScript containing the CropBox into a file and add that file as an input file after the input PDF file on the command line.

Alternatively you could set the page size you want using the -sPAPERSIZE or -g switches, and also set the -dFIXEDMEDIA switch but note that in PostScript the origin (0,0) is bottom left and so you would also have to translate the origin. This is probably more difficult than you want to get into, so I would suggest you try placing the pdfmark into a separate file and add it to the command line after the input PDF file that you want modified.

Upvotes: 1

Related Questions