iText pdf library
Website search

Introducing the new iText 7 Demo Lab!

Demonstrating the power of iText 7 to both developers and non-developers

We’re pleased to announce an exciting new addition to our website, the iText 7 Demo Lab. This completely replaces our old demos page where we had limited demonstrations of certain iText Core and add-on functionality. The new Demo Lab is much, much cooler though…

Drag and drop PDF operations

For the first time it’s possible for anyone to use iText to edit and manipulate PDFs, without needing to download and install anything. It’s all completely online!

Using our free online apps you can perform a whole range of PDF operations with iText, such as splitting and merging, rotating, deleting pages, adding and removing password protection, and converting images to PDF. You can also use add-on functionality, such as converting HTML to PDF and flattening XFA forms. If you have a PDF you need to edit, just drag and drop it onto the page, and choose the options you’re presented with. It really is that simple!

iText 7 Demo Lab

We’re really excited to be able to provide these demonstrations to our community. But that’s not all…

Online sandbox for developers

On each and every demo page is also an embedded compiler for each function with iText 7 set up and ready to go. This lets you see and manipulate example code for each of the demonstrated features, giving you a sandbox environment to understand exactly how these functions are implemented in iText 7. Java and C# examples are available on every page, and you’re able to directly edit the code to affect the output.

This is even more exciting for us to be honest. By showing how to achieve common operations with iText 7 we can easily demonstrate to both Java and .NET developers how easy it is to use. We might even convince some non-developers to give it a go too (we promise it’s not such a dark art!)

Don’t believe us?

OK, here’s an ultra-quick tutorial explaining how deleting pages from a PDF works.

Below we've embedded the online compiler with code from the PDF Page Deleter demo showing how to delete the first page from a PDF:

import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.PdfWriter; import java.io.IOException; public class PDFPageDeleter { private static final String ORIG = "/uploads/delete.pdf"; private static final String OUTPUT_FOLDER = "/myfiles/"; public static void main(String[] args) throws IOException { PdfDocument pdfDocument = new PdfDocument(new PdfReader(ORIG), new PdfWriter(OUTPUT_FOLDER + "deleted.pdf")); pdfDocument.removePage(1); //Note that as you remove a page the number of pages in your PDF will change pdfDocument.close(); } }
using iText.Kernel.Pdf; namespace PDFPageDeleter { public class PDFPageDeleter { private static string ORIG = "/uploads/delete.pdf"; private static string OUTPUT_FOLDER = "/myfiles/"; public static void Main(string[] args) { PdfDocument pdfDocument = new PdfDocument(new PdfReader(ORIG), new PdfWriter(OUTPUT_FOLDER + "deleted.pdf")); pdfDocument.RemovePage(1); //Note that as you remove a page the number of pages in your PDF will change pdfDocument.Close(); } } }

There are essentially four parts of this example you should pay attention to:

  1. private static final String ORIG = "/uploads/delete.pdf"; - the name of the PDF you’re deleting pages from. If your PDF is named something else, you’ll get an error stating the PDF was “not found as file or resource”. So, remember to either rename your PDF, or edit this line before you execute the example.
  2. PdfDocument pdfDocument = new PdfDocument(new PdfReader(ORIG), new PdfWriter(OUTPUT_FOLDER + "deleted.pdf")); - the name of the output file that iText will produce.
  3. pdfDocument.removePage(1); - the key part of the code as it tells iText to delete page 1 of the PDF.
  4. pdfDocument.close() – closes the PDF after you’ve finished working on it.

So, how would you delete the first three pages of a ten-page document? You could simply repeat the line shown in step 3, as shown in the following example: 

import com.itextpdf.kernel.pdf.PdfDocument; import com.itextpdf.kernel.pdf.PdfReader; import com.itextpdf.kernel.pdf.PdfWriter; import java.io.IOException; public class PDFPageDeleter { private static final String ORIG = "/uploads/delete.pdf"; private static final String OUTPUT_FOLDER = "/myfiles/"; public static void main(String[] args) throws IOException { PdfDocument pdfDocument = new PdfDocument(new PdfReader(ORIG), new PdfWriter(OUTPUT_FOLDER + "deleted.pdf")); pdfDocument.removePage(1); //Note that as you remove a page the number of pages in your PDF will change pdfDocument.removePage(1); //Note that as you remove a page the number of pages in your PDF will change pdfDocument.removePage(1); //Note that as you remove a page the number of pages in your PDF will change pdfDocument.close(); } }
using iText.Kernel.Pdf; namespace PDFPageDeleter { public class PDFPageDeleter { private static string ORIG = "/uploads/delete.pdf"; private static string OUTPUT_FOLDER = "/myfiles/"; public static void Main(string[] args) { PdfDocument pdfDocument = new PdfDocument(new PdfReader(ORIG), new PdfWriter(OUTPUT_FOLDER + "deleted.pdf")); pdfDocument.RemovePage(1); //Note that as you remove a page the number of pages in your PDF will change pdfDocument.RemovePage(1); //Note that as you remove a page the number of pages in your PDF will change pdfDocument.RemovePage(1); //Note that as you remove a page the number of pages in your PDF will change pdfDocument.Close(); } } }

Easy as 1,2,3?

You might think that you’d specify the pages to delete like this:

pdfDocument.removePage(1);

pdfDocument.removePage(2);

pdfDocument.removePage(3);

However, if you actually try this, you’ll end up deleting pages 1, 3 and 5 from your PDF. Why?

If you read the comment in our first example, you’ll understand why this happens. Since each instance of pdfDocument.removePage decreases the page count in your PDF until the PDF is closed, page 2 becomes page 3, and page 3 becomes page 5.

So, it’s important to keep track of which page is which when you’re deleting pages. In our example above, we’ve avoided this problem by simply using the removePage function to delete the first page three separate times.

Easy as 3,2,1?

Some of you might be saying “Hang on, couldn’t you just tell iText to delete the pages in reverse order, wouldn’t that work?”

Well, indeed it would. Simply change the code shown in the above example to delete pages 3, 2 and 1 from your PDF instead, and give yourself a cookie 😊.

Sometimes there may be multiple ways to achieve your goal with iText 7, and these demos enable you to experiment to your hearts content.

Coding is fun!

And there you go; you’ve just learned how to delete pages from a document, and a small part of how easy iText 7 makes it to harness the power of PDF.

Make sure you check out the rest of the demos, and have fun!



お問い合わせ

それでも問題が解決しませんか? 

お寄せいただいたご質問には、喜んでお答えいたします。当社へご連絡ください。内容を確認次第、追ってご連絡いたします。

問い合わせる
常に最新情報を得る

11,000人以上の購読者に加わって、iTextの新しい製品、アップデート、ヒント、記述的な解決策や出来事に関する最新情報を得ることでiText PDF専門家になりましょう。

今すぐ購読