I have a pdf of 10 pages , wherein i need to delete 2 pages from pdffile. The above requirement should be using iTEXT and JAVA. The pages should delete from PDF without creating any new file or pdf.
-
3What have you tried so far? – Thorbjørn Ravn Andersen Aug 10 '18 at 06:12
-
The requirement *"The pages should delete from PDF without creating any new file or pdf"* is a very dangerous one. I would consider that requirement a design flaw. See [How to update a PDF without creating a new PDF?](https://stackoverflow.com/questions/16081831) to understand why. (For instance: if you would be a student, you'd get an F from me if you suggested this in an exam.) As for deleting pages from an existing PDF, you aren't showing [what you have tried](http://idownvotedbecau.se/nocode/), and you aren't telling us which version of iText you are using. – Bruno Lowagie Aug 10 '18 at 06:36
2 Answers
Assuming that you are using the latest version, your code would look like this:
import java.io.IOException;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
public class RemovePage {
public static final String SRC = "original.pdf";
public static final String DEST = "changed.pdf";
public static void main(String[] args) throws IOException {
PdfReader reader = new PdfReader(SRC);
PdfWriter writer = new PdfWriter(DEST);
PdfDocument document = new PdfDocument(reader, writer);
document.removePage(2);
document.close();
}
}
This is a 5-step process:
- You create a
PdfReader
object with the path to the input file as parameter. - You create a
PdfWriter
object with the path to the output file as parameter. - You create a
PdfDocument
object with the reader and the writer. - You use the
removePage()
method to remove a page; in the example, we remove page 2. (Repeat this step wisely if you need to remove more pages). - You close the document instance.
IMPORTANT REMARK 1: If this code sample doesn't work, then you are using the wrong iText version. You should use iText 7.1.2: https://developers.itextpdf.com/itext7/download-and-install-information/Java
IMPORTANT REMARK 2: In your question, you also mention that "The pages should delete from PDF without creating any new file or pdf." I intentionally ignored that requirement, because I consider it to be a design flaw. I want to avoid that someone sees my example and starts using it without reading my caveats.
If you insist on implementing this design flaw, then please read the FAQ entry How to update a PDF without creating a new PDF? Basically, you have two options: a safe one (that works around the requirement) and a dangerous one (that implements the requirement literally).
This is the safe workaround:
- read the original file with
PdfReader
; - create a temporary file with
PdfWriter
either on disk or in memory, - after closing the document, replace the original file with the temporary file.
You still create a new file, but no one notices. This is how most document manipulating software (e.g. Word) works.
This is the dangerous approach:
- read the original file into a
byte[]
, - create
PdfReader
with thisbyte[]
, and - use the path to the original file as a parameter for
PdfWriter
.
Gentle warning: the dangerous approach might get you fired or make you fail your exam, as it will result in corrupting the original file beyond repair if an exception is thrown during the process.

- 75,994
- 9
- 109
- 165
You can use PdfStamper also,
PdfReader pdfReader = new PdfReader("source pdf file path");
reader.selectPages("2-4,10-15");
PdfStamper pdfStamper = new PdfStamper(pdfReader,
new FileOutputStream("destination pdf file path"));
pdfStamper.close();

- 60
- 4
-
1I don't know why this is downvoted because the example is correct for old versions of iText that are no longer supported. In iText 7 (of which the first version was released 2 years ago), the `PdfStamper` class no longer exists. This answer also ignores the requirement that no new file should be created. – Bruno Lowagie Aug 10 '18 at 07:39