/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.pdfbox.util;

import java.io.IOException;
import junit.framework.*;
import org.apache.pdfbox.pdmodel.PDDocument;

/**
 * Test suite for PageExtractor.
 *
 * This is just some simple tests based on a test document.  It merely ensures
 * that the correct number of pages are extracted as this is virtually the only
 * thing which could go wrong when coping pages from one PDF to a new one.
 *
 * @author Adam Nichols (adam@apache.org)
 */
public class PageExtractorTest extends TestCase {
    
    public PageExtractorTest(String testName) {
        super(testName);
    }

    protected void setUp() throws Exception {
    }

    protected void tearDown() throws Exception {
    }

    /**
     * Test of extract method, of class org.apache.pdfbox.util.PageExtractor.
     */
    public void testExtract() throws Exception {
        System.out.println("extract");
        
        PDDocument sourcePdf = null;
        try {
            try {
                // this should work for most users
                sourcePdf = PDDocument.load("src/test/resources/input/cweb.pdf");
            } catch(IOException e) {
                // in case your directory structure is different, just change this to be the correct path
                sourcePdf = PDDocument.load("pdfbox/pdfbox/src/test/resources/input/cweb.pdf");
            }
            PageExtractor instance = new PageExtractor(sourcePdf);
            PDDocument result = instance.extract();
            assertEquals(sourcePdf.getNumberOfPages(), result.getNumberOfPages());
            
            instance = new PageExtractor(sourcePdf, 1, 1);
            result = instance.extract();
            assertEquals(1, result.getNumberOfPages());
            
            instance = new PageExtractor(sourcePdf, 1, 5);
            result = instance.extract();
            assertEquals(5, result.getNumberOfPages());
            
            instance = new PageExtractor(sourcePdf, 5, 10);
            result = instance.extract();
            assertEquals(6, result.getNumberOfPages());
            
            instance = new PageExtractor(sourcePdf, -100, 50000);
            result = instance.extract();
            assertEquals(sourcePdf.getNumberOfPages(), result.getNumberOfPages());
            
            instance = new PageExtractor(sourcePdf, 2, 1);
            result = instance.extract();
            assertEquals(0, result.getNumberOfPages());
        } catch(Exception e) {
            e.printStackTrace();
            fail("Exception was thrown: " + e.getMessage());
        } finally {
            if(sourcePdf != null)
                try { sourcePdf.close(); } catch(Exception e) { /* Can't do much about this... */ };
        }
    }    
}

@@ -1535,24 +1535,274 @@
 %7B%0D%0A    %7D%0D%0A%0D%0A
+    private void closeDoc(PDDocument doc) %7B%0D%0A        if(doc != null) %7B%0D%0A            try %7B%0D%0A                doc.close(); %0D%0A            %7D catch(Exception e) %7B%0D%0A                /* Can't do much about this... */ %0D%0A            %7D;%0D%0A        %7D%0D%0A    %7D%0D%0A    %0D%0A
     /**%0D%0A   
@@ -1798,16 +1798,16 @@
    /**%0D%0A
-
      * T
@@ -2024,16 +2024,51 @@
  null;%0D%0A
+        PDDocument result = null;%0D%0A
         
@@ -2561,35 +2561,24 @@
             
-PDDocument 
 result = ins
@@ -2671,32 +2671,63 @@
 berOfPages());%0D%0A
+            closeDoc(result);%0D%0A
             %0D%0A  
@@ -2875,32 +2875,63 @@
 berOfPages());%0D%0A
+            closeDoc(result);%0D%0A
             %0D%0A  
@@ -3079,32 +3079,63 @@
 berOfPages());%0D%0A
+            closeDoc(result);%0D%0A
             %0D%0A  
@@ -3284,32 +3284,63 @@
 berOfPages());%0D%0A
+            closeDoc(result);%0D%0A
             %0D%0A  
@@ -3522,32 +3522,63 @@
 berOfPages());%0D%0A
+            closeDoc(result);%0D%0A
             %0D%0A  
@@ -3726,32 +3726,63 @@
 berOfPages());%0D%0A
+            closeDoc(result);%0D%0A
         %7D catch(
@@ -3926,18 +3926,24 @@
         
-if
+closeDoc
 (sourceP
@@ -3944,25 +3944,18 @@
 ourcePdf
- != null
 )
+;
 %0D%0A      
@@ -3964,95 +3964,24 @@
     
-    try %7B sourcePdf.close(); %7D catch(Exception e) %7B /* Can't do much about this... */ %7D
+closeDoc(result)
 ;%0D%0A 

Add utility class to easily extract a range of pages from a PDF. Now closes the newly created document, which provides for a good example.