Convert documents for RAG pipelines

Retrieval-augmented generation (RAG) systems need documents in a clean, structured text format for chunking and embedding. Markdown is ideal — it preserves document structure (headings, lists, tables) while being easy to parse.

flowchart LR
    A["PDF / DOCX / XLSX"]
    B["GroupDocs.Markdown"]
    C["Markdown"]
    D["Text Chunking"]
    E["Vector Embeddings"]
    F["LLM Query"]
    A --> B --> C --> D --> E --> F

Basic conversion for RAG

import com.groupdocs.markdown.*;

import java.util.regex.Pattern;

public class RagBasic {

    // Matches a newline followed by a level-1 or level-2 heading marker
    private static final Pattern HEADING = Pattern.compile("\\n#{1,2} ");

    public static void main(String[] args) {
        // Convert document to Markdown — skip images for text-only RAG
        DocumentConvertOptions options = new DocumentConvertOptions();
        options.setImageExportStrategy(new SkipImagesStrategy());
        options.setFlavor(MarkdownFlavor.COMMON_MARK);

        String markdown = MarkdownConverter.toMarkdown("professional-services.pdf", options);

        // Split into chunks by headings
        String[] chunks = HEADING.split(markdown);

        for (String chunk : chunks) {
            if (chunk.isEmpty()) {
                continue;
            }
            // Send each chunk to your embedding model
            String preview = chunk.substring(0, Math.min(80, chunk.length()));
            System.out.println("Chunk (" + chunk.length() + " chars): " + preview + "...");
        }
    }
}

professional-services.pdf is a sample file used in this example. Click here to download it.

Chunk (8065 chars): 
**Professional Services**

A catalog of engagements offered by Meridian Outd...

Download full output

Batch processing a document library

import com.groupdocs.markdown.*;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;

public class RagBatch {

    public static void main(String[] args) throws IOException {
        DocumentConvertOptions options = new DocumentConvertOptions();
        options.setImageExportStrategy(new SkipImagesStrategy());

        File[] files = new File("documents").listFiles(
                (dir, name) -> name.toLowerCase().endsWith(".pdf"));
        if (files == null) {
            files = new File[0];
        }

        for (File file : files) {
            try {
                String markdown = MarkdownConverter.toMarkdown(file.getPath(), options);
                String outputPath = changeExtension(file.getPath(), ".md");
                Files.write(Paths.get(outputPath), markdown.getBytes(StandardCharsets.UTF_8));
                System.out.println("Converted: " + file.getPath());
            } catch (GroupDocsMarkdownException e) {
                System.out.println("Skipped " + file.getPath() + ": " + e.getMessage());
            }
        }
    }

    /** Java has no Path.ChangeExtension equivalent. */
    private static String changeExtension(String path, String newExtension) {
        int dot = path.lastIndexOf('.');
        return (dot < 0 ? path : path.substring(0, dot)) + newExtension;
    }
}
Converted: documents\business-plan.pdf

Download full output

Close
Loading

Analyzing your prompt, please hold on...

An error occurred while retrieving the results. Please refresh the page and try again.