-1

I have an existing PDF and I need to remove the footer from the PDF on every page.

I fetch all pages from PDF but I am not able to remove the footer from the PDF.

I am using iText version 5.5.0.

Document document = null;
PdfCopy writer = null;
PdfReader reader = new PdfReader("input.pdf");
int n = reader.getNumberOfPages();
outFile = "output.pdf";
document = new Document();
writer = new PdfCopy(document, new FileOutputStream(outFile));
document.open();

for (int j = 0; j < n; j++) {                               
    
    PdfImportedPage page = writer.getImportedPage(reader, j);
    writer.addPage(page);
}

document.close();
writer.close();

How can I remove the footer from this input.pdf file and create a new output.pdf file?

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
Akash Chavda
  • 1,185
  • 1
  • 11
  • 29

2 Answers2

2

You can use pdfSweep to delete text. See the example https://kb.itextpdf.com/home/it7kb/examples/removing-content-with-pdfsweep

OR Check already given solutions

How to remove headers and footers from PDF file using iText in Java

Issue in Removing Header and Footer in PDF using iText PDF

Remove Footer from PDF using IText5.0

My suggestion is to avoid creating a footer in the first place so that you can avoid recreating the pdf just for removing the footer

shihabudheenk
  • 593
  • 5
  • 18
0

Using itextpdf lib crop the area and add header image hope this will help

package com.ashok;

import com.itextpdf.text.DocumentException;

import com.itextpdf.text.pdf.*;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;

public class ClipPdf {
    public static final String SRC = "/home/ashok/src-file.pdf";
    public static final String DEST = "/home/ashok/dest_new.pdf";
    public static final String imageFile = "/home/ashok/logo-1.jpeg";

    public static void main(String[] args) throws IOException, DocumentException {
        File file = new File(DEST);
        file.getParentFile().mkdirs();
        new ClipPdf().manipulatePdf(SRC, DEST);
    }

    /*
            Parameters:
        llx - lower left x // box size right
        lly - lower left y // box height up side
        urx - upper right x // box width
        ury - upper right y // box height down side
     */
    public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
        PdfReader.unethicalreading = true;
        PdfReader reader = new PdfReader(src);
        PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));

        int n = reader.getNumberOfPages();
        PdfDictionary page;
        PdfArray media;
        //ImageData data = ImageDataFactory.create(imageFile);
       // Image img = new Image(data);
        com.itextpdf.text.Image image = com.itextpdf.text.Image.getInstance(imageFile);

/* Page wise */
        for (int p = 1; p <= n; p++) {
            page = reader.getPageN(p);
            media = page.getAsArray(PdfName.CROPBOX);
            if (media == null) {
                media = page.getAsArray(PdfName.MEDIABOX);
            }
/* CROP the area all 4 side */

            float llx = media.getAsNumber(0).floatValue() + 1; // footer
            float lly = media.getAsNumber(1).floatValue() + 100; // left side
            float w = media.getAsNumber(2).floatValue() - media.getAsNumber(0).floatValue() - 5; // right side
            float h = media.getAsNumber(3).floatValue() - media.getAsNumber(1).floatValue() - 210; // header
            String command = String.format(Locale.ROOT,
                    "\nq %.2f %.2f %.2f %.2f re W n\nq\n",
                    llx, lly, w, h);
            stamper.getUnderContent(p).setLiteral(command);
            stamper.getOverContent(p).setLiteral("\nQ\nQ\n");
            PdfContentByte content = stamper.getOverContent(p);

/* Add header image for example */   


            int indentation = 0;
            float scaler = (( reader.getPageSize(p).getWidth() -  reader.getPageSize(p).getBorderWidthLeft()
                    - reader.getPageSize(p).getBorderWidthRight() - indentation) / image.getWidth()) * 100;

            image.scalePercent(scaler);
            image.setAbsolutePosition(-5f, 760f);
            content.addImage(image);

        }
        stamper.close();
        reader.close();

    }
}
Ashok Parmar
  • 336
  • 4
  • 4