Convert PDF to EPUB, Text, XPS, and More in Java

Aspose.PDF for Java can export PDF documents into text, ebook, print, and markup-oriented output formats.

Convert PDF to EPUB

Use this example when a PDF document should be exported to the EPUB ebook format.

  1. Open the source PDF in a Document instance.
  2. Create EpubSaveOptions and set the recognition mode to Flow.
  3. Call document.save(outputFile.toString(), saveOptions) so the PDF content is exported as reflowable EPUB markup.
  4. Save the converted EPUB file.
public static void convertPdfToEpub(Path inputFile, Path outputFile) {
        try (Document document = new Document(inputFile.toString())) {
            EpubSaveOptions saveOptions = new EpubSaveOptions();
            saveOptions.setContentRecognitionMode(EpubSaveOptions.RecognitionMode.Flow);
            document.save(outputFile.toString(), saveOptions);
        }
        System.out.println(inputFile + " converted into " + outputFile);
    }

Convert PDF to TeX

Use this example when PDF content should be exported into TeX markup.

  1. Open the source PDF in a Document instance.
  2. Create TeXSaveOptions for TeX serialization.
  3. Call document.save(outputFile.toString(), saveOptions) so the PDF content is emitted as TeX markup.
  4. Save the resulting TeX file.
public static void convertPdfToTex(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        document.save(outputFile.toString(), new TeXSaveOptions());
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

Convert PDF to plain text

Use this example when a PDF document should be exported as a text file.

  1. Open the source PDF in a Document instance.
  2. Create a TextDevice to extract textual content from PDF pages.
  3. Call device.process(document.getPages().get_Item(1), outputFile.toString()) to write the first page as plain text.
  4. Save the text output file.
public static void convertPdfToTxt(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        TextDevice device = new TextDevice();
        device.process(document.getPages().get_Item(1), outputFile.toString());
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

Convert PDF to XPS

Use this example when a PDF document should be converted into XPS format.

  1. Open the source PDF in a Document instance.
  2. Create XpsSaveOptions and enable embedded TrueType fonts.
  3. Call document.save(outputFile.toString(), saveOptions) so the PDF is serialized as XPS with embedded font resources.
  4. Save the converted XPS file.
public static void convertPdfToXps(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        XpsSaveOptions saveOptions = new XpsSaveOptions();
        saveOptions.setUseEmbeddedTrueTypeFonts(true);
        document.save(outputFile.toString(), saveOptions);
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

Convert PDF to Markdown

Use this example when PDF content should be exported as Markdown.

  1. Open the source PDF in a Document instance. «««< HEAD
  2. Create MarkdownSaveOptions and configure the image resource directory plus HTML image-tag output. =======
  3. Create MarkdownSaveOptionsand configure the image resource directory plus HTML image-tag output.

fix-java-docs

  1. Call document.save(outputFile.toString(), saveOptions) so the PDF content is emitted as Markdown with external image resources.
  2. Save the generated Markdown file.
public static void convertPdfToMd(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        MarkdownSaveOptions saveOptions = new MarkdownSaveOptions();
        saveOptions.setResourcesDirectoryName("images");
        saveOptions.setUseImageHtmlTag(true);
        document.save(outputFile.toString(), saveOptions);
    }
    System.out.println(inputFile + " converted into " + outputFile);
}

Convert PDF to Mobi XML

Use this example when PDF content should be exported into Mobi-compatible XML.

  1. Open the source PDF in a Document instance.
  2. Select SaveFormat MobiXml as the target serialization format.
  3. Call document.save(outputFile.toString(), SaveFormat.MobiXml) so the PDF is exported as Mobi-compatible XML.
  4. Save the converted file.
public static void convertPdfToMobiXml(Path inputFile, Path outputFile) {
    try (Document document = new Document(inputFile.toString())) {
        document.save(outputFile.toString(), SaveFormat.MobiXml);
    }
    System.out.println(inputFile + " converted into " + outputFile);
}