Convert N consecutive pages

GroupDocs.Conversion allows you to convert a specific range of consecutive pages from a document. This feature is useful when you only need a portion of the document instead of converting the entire file.

To convert a set of consecutive pages, follow these steps:

  1. Initialize the Converter
    • Create an instance of the Converter class and provide the source document path.
  2. Configure Conversion Options
    • Instantiate the appropriate ConvertOptions class (e.g., PdfConvertOptions, WordProcessingConvertOptions, etc.).
  3. Specify Page Range
  4. Perform the Conversion
    • Call the convert() method on the Converter instance, passing the output file name and the ConvertOptions instance.

The following Java example demonstrates how to convert four consecutive pages, beginning from the second page of the document:

import com.groupdocs.conversion.Converter;
import com.groupdocs.conversion.options.convert.PdfConvertOptions;

public class ConvertConsecutivePages {
    public static void convert() {
        // Load the source document
        Converter converter = new Converter("annual-review.docx");

        // Define conversion options
        PdfConvertOptions options = new PdfConvertOptions();
        options.setPageNumber(2); // Start from page 2
        options.setPagesCount(4); // Convert 4 pages

        // Perform the conversion
        converter.convert("converted.pdf", options);
    }

    public static void main(String[] args){
        convert();
    }
}

annual-review.docx is sample file used in this example. Click here to download it.

converted.pdf is converted attachment PDF document. Click here to download it.

Additional Notes

  • The page numbering starts at 1, not 0.
  • If the specified pagesCount exceeds the document’s total page count, conversion will continue until the last page.
  • This approach applies to different document formats by selecting the appropriate ConvertOptions subclass (e.g., WordProcessingConvertOptions, SpreadsheetConvertOptions).