PDF to PDF Java Examples

Complete Java examples for common PDF to PDF tasks. See the Java SDK guide for installation and result handling.

You can run the examples below with the displayed API credentials. Update the input URLs and filenames as needed. Each Java example can be copied and run independently.

Merging

Add PDFs in the order they should appear in the merged document. Choose file, in-memory, or output-stream handling for the result. The stream example uses a binary file stream as its destination.

Merge PDF files

Merge first.pdf and second.pdf in that order.

Save to a file

import com.pdfcrowd.Pdfcrowd;

class Example {
    public static void main(String[] args) throws Exception {
        Pdfcrowd.PdfToPdfClient client =
            new Pdfcrowd.PdfToPdfClient("demo", "demo");

        client.addPdfFile("first.pdf");
        client.addPdfFile("second.pdf");
        client.convertToFile("merged.pdf");
    }
}

Keep in memory

import com.pdfcrowd.Pdfcrowd;

class Example {
    public static void main(String[] args) throws Exception {
        Pdfcrowd.PdfToPdfClient client =
            new Pdfcrowd.PdfToPdfClient("demo", "demo");

        client.addPdfFile("first.pdf");
        client.addPdfFile("second.pdf");
        byte[] pdf = client.convert();
        System.out.println(pdf.length);
    }
}

Write to a stream

import com.pdfcrowd.Pdfcrowd;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;

class Example {
    public static void main(String[] args) throws Exception {
        Pdfcrowd.PdfToPdfClient client =
            new Pdfcrowd.PdfToPdfClient("demo", "demo");

        try (OutputStream output_stream =
                 Files.newOutputStream(Path.of("merged.pdf"))) {
            client.addPdfFile("first.pdf");
            client.addPdfFile("second.pdf");
            client.convertToStream(output_stream);
        }
    }
}

Merge raw PDF bytes

Read first.pdf and second.pdf as binary data. In an application, pass the bytes you already hold in memory instead.

Save to a file

import com.pdfcrowd.Pdfcrowd;
import java.nio.file.Files;
import java.nio.file.Path;

class Example {
    public static void main(String[] args) throws Exception {
        Pdfcrowd.PdfToPdfClient client =
            new Pdfcrowd.PdfToPdfClient("demo", "demo");

        byte[] pdf_data_1 = Files.readAllBytes(Path.of("first.pdf"));
        byte[] pdf_data_2 = Files.readAllBytes(Path.of("second.pdf"));
        client.addPdfRawData(pdf_data_1);
        client.addPdfRawData(pdf_data_2);
        client.convertToFile("merged.pdf");
    }
}

Keep in memory

import com.pdfcrowd.Pdfcrowd;
import java.nio.file.Files;
import java.nio.file.Path;

class Example {
    public static void main(String[] args) throws Exception {
        Pdfcrowd.PdfToPdfClient client =
            new Pdfcrowd.PdfToPdfClient("demo", "demo");

        byte[] pdf_data_1 = Files.readAllBytes(Path.of("first.pdf"));
        byte[] pdf_data_2 = Files.readAllBytes(Path.of("second.pdf"));
        client.addPdfRawData(pdf_data_1);
        client.addPdfRawData(pdf_data_2);
        byte[] pdf = client.convert();
        System.out.println(pdf.length);
    }
}

Merge bytes with a file

Add the bytes from first.pdf, followed by second.pdf, and save the result to merged.pdf.

import com.pdfcrowd.Pdfcrowd;
import java.nio.file.Files;
import java.nio.file.Path;

class Example {
    public static void main(String[] args) throws Exception {
        Pdfcrowd.PdfToPdfClient client =
            new Pdfcrowd.PdfToPdfClient("demo", "demo");

        byte[] pdf_data_1 = Files.readAllBytes(Path.of("first.pdf"));
        client.addPdfRawData(pdf_data_1);
        client.addPdfFile("second.pdf");
        client.convertToFile("merged.pdf");
    }
}

PDF operations

Add a watermark

Apply watermark.pdf to proposal.pdf and save company_offer.pdf.

import com.pdfcrowd.Pdfcrowd;

class Example {
    public static void main(String[] args) throws Exception {
        Pdfcrowd.PdfToPdfClient client =
            new Pdfcrowd.PdfToPdfClient("demo", "demo");

        client.setPageWatermark("watermark.pdf");
        client.addPdfFile("proposal.pdf");
        client.convertToFile("company_offer.pdf");
    }
}

Linearize a PDF

Optimize not_linearized.pdf for progressive loading in a browser and save it as linearized.pdf.

import com.pdfcrowd.Pdfcrowd;

class Example {
    public static void main(String[] args) throws Exception {
        Pdfcrowd.PdfToPdfClient client =
            new Pdfcrowd.PdfToPdfClient("demo", "demo");

        client.setLinearize(true);
        client.addPdfFile("not_linearized.pdf");
        client.convertToFile("linearized.pdf");
    }
}

Extract pages

Extract page 3 and pages 7 through the end of 13_pages.pdf into output.pdf.

import com.pdfcrowd.Pdfcrowd;

class Example {
    public static void main(String[] args) throws Exception {
        Pdfcrowd.PdfToPdfClient client =
            new Pdfcrowd.PdfToPdfClient("demo", "demo");

        client.setAction("extract");
        client.setPageRange("3,7-");
        client.addPdfFile("13_pages.pdf");
        client.convertToFile("output.pdf");
    }
}

Delete pages

Remove pages 1–3 and page 10 from 13_pages.pdf and save the remaining pages to output.pdf.

import com.pdfcrowd.Pdfcrowd;

class Example {
    public static void main(String[] args) throws Exception {
        Pdfcrowd.PdfToPdfClient client =
            new Pdfcrowd.PdfToPdfClient("demo", "demo");

        client.setAction("delete");
        client.setPageRange("1-3,10");
        client.addPdfFile("13_pages.pdf");
        client.convertToFile("output.pdf");
    }
}

Split a PDF

Split 13_pages.pdf into two documents: pages 1–10 in pages1-10.pdf, and the remaining pages in pages11-end.pdf.

import com.pdfcrowd.Pdfcrowd;

class Example {
    public static void main(String[] args) throws Exception {
        Pdfcrowd.PdfToPdfClient client =
            new Pdfcrowd.PdfToPdfClient("demo", "demo");

        client.addPdfFile("13_pages.pdf");
        client.setAction("extract");
        client.setPageRange("1-10");
        client.convertToFile("pages1-10.pdf");
        client.setPageRange("11-");
        client.convertToFile("pages11-end.pdf");
    }
}