Arsiwaldi
Arsiwaldi

Reputation: 523

iTextSharp Resize each page to fit the pagesize

I have a .pdf document which has for example 7 pages. I split this document into 7 .pdf document, so it means that each document has only one page. But mainly I need to make fit the content of pages. So delete whitespaces, margins, resize. Have you got some simple advice? I add links for images and also code for split pdf document. Thank you for your response.

INPUT:

enter image description here

DESIRED OUTPUT:

enter image description here

CODE:

        public void PdfSplitDocument(string filename)
    {
        String path = "C:/Doc/" + filename;
        String result = "d:/output/result";
        PdfCopy copy;
        PdfReader reader = new PdfReader(path);

        for (int i = 1; i <= reader.NumberOfPages; i++)
        {

            Document document = new Document(PageSize.A4, 0, 0, 0, 0);
            copy = new PdfCopy(document, new FileStream(result + i + ".pdf", FileMode.Create));
            document.Open();
            copy.AddPage(copy.GetImportedPage(reader, i));
            document.Close();

        }

    }

Upvotes: 1

Views: 5205

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77606

Take a look at the ShowTextMargins example. It uses the TextMarginFinder class to find the margins within which text is found. In this example, taken from my book "iText in Action - Second Edition", I use this class to draw a rectangle:

public void addMarginRectangle(String src, String dest)
    throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfReaderContentParser parser = new PdfReaderContentParser(reader);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(RESULT));
    TextMarginFinder finder;
    for (int i = 1; i <= reader.getNumberOfPages(); i++) {
        finder = parser.processContent(i, new TextMarginFinder());
        PdfContentByte cb = stamper.getOverContent(i);
        cb.rectangle(finder.getLlx(), finder.getLly(),
            finder.getWidth(), finder.getHeight());
        cb.stroke();
    }
    stamper.close();
    reader.close();
}

In your case, you want to crop the pages based on the rectangle. You have a finder object that allows you to get the coordinate of the lower-left corner (llx and lly) and the coordinate of the upper-right corner (urx and ury). You can use these coordinates to crop pages as is done in the CropPages example:

public void manipulatePdf(String src, String dest)
    throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    int n = reader.getNumberOfPages();
    PdfDictionary pageDict;
    PdfRectangle rect = new PdfRectangle(llx, lly, urx, ury);
    for (int i = 1; i <= n; i++) {
        pageDict = reader.getPageN(i);
        pageDict.put(PdfName.CROPBOX, rect);
    }
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    stamper.close();
    reader.close();
}

Another option is to change the media box instead of the crop box:

pageDict.put(PdfName.MEDIABOX, rect);

The C# version of these examples can be found here:

Upvotes: 3

Related Questions