Complete Java examples for common Image 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.
Choose file, in-memory, or output-stream handling for the PDF result. Stream examples use a binary file stream as their destination.
Upload an existing logo.png file.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); client.convertFileToFile("logo.png", "logo.pdf"); } }
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); byte[] pdf = client.convertFile("logo.png"); System.out.println(pdf.length); } }
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.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); try (OutputStream output_stream = Files.newOutputStream(Path.of("logo.pdf"))) { client.convertFileToStream("logo.png", output_stream); } } }
Fetch an image from its URL.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); client.convertUrlToFile("https://your-server.com/logo.png", "logo.pdf"); } }
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); byte[] pdf = client.convertUrl("https://your-server.com/logo.png"); System.out.println(pdf.length); } }
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.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); try (OutputStream output_stream = Files.newOutputStream(Path.of("logo.pdf"))) { client.convertUrlToStream( "https://your-server.com/logo.png", output_stream ); } } }
Read logo.png as binary data. In an application, pass the bytes you already hold in memory instead.
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.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); byte[] image_data_1 = Files.readAllBytes(Path.of("logo.png")); client.convertRawDataToFile(image_data_1, "logo.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.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); byte[] image_data_1 = Files.readAllBytes(Path.of("logo.png")); byte[] pdf = client.convertRawData(image_data_1); System.out.println(pdf.length); } }
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.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); try (OutputStream output_stream = Files.newOutputStream(Path.of("logo.pdf"))) { byte[] image_data_1 = Files.readAllBytes(Path.of("logo.png")); client.convertRawDataToStream(image_data_1, output_stream); } } }
Open logo.png as a binary input stream and save the result to logo.pdf.
import com.pdfcrowd.Pdfcrowd; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); try (InputStream input_stream = Files.newInputStream(Path.of("logo.png"))) { client.convertStreamToFile(input_stream, "logo.pdf"); } } }
These examples use scan.png, a 1224 × 1584 pixel image with text, a chart, and a uniform white border. The rotation example uses its sideways variant, scan-sideways.png.
Fit the whole image onto A4 portrait or Letter landscape paper with 12mm margins and 150 DPI. The fit mode preserves the image's aspect ratio. Setting a page size without it can clip a larger image.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setMarginBottom("12mm"); client.setMarginLeft("12mm"); client.setMarginRight("12mm"); client.setMarginTop("12mm"); client.setPageSize("A4"); client.setPrintPageMode("fit"); client.convertFileToFile("scan.png", "a4.pdf"); } }
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setMarginBottom("12mm"); client.setMarginLeft("12mm"); client.setMarginRight("12mm"); client.setMarginTop("12mm"); client.setOrientation("landscape"); client.setPageSize("Letter"); client.setPrintPageMode("fit"); client.convertFileToFile("scan.png", "landscape.pdf"); } }
Fit scan.png onto a 4 × 6 inch page and save custom-page.pdf. Preserve the aspect ratio and fill the unused page area with a pale blue background.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setPageBackgroundColor("EEF2F6"); client.setPageHeight("6in"); client.setPageWidth("4in"); client.setPrintPageMode("fit"); client.convertFileToFile("scan.png", "custom-page.pdf"); } }
Interpret scan.png at 300 pixels per inch without setting a fixed page size. Its 1224 × 1584 pixels produce a 4.08 × 5.28 inch page in print-size.pdf, with the original image pixels retained.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(300); client.convertFileToFile("scan.png", "print-size.pdf"); } }
Extract the chart from scan.png into chart.pdf, or remove its uniform outer border and save trimmed.pdf.
The chart starts 96 pixels from the left and 370 pixels from the top, and measures 1024 × 384 pixels. Set 96 DPI so the px crop coordinates match the source image pixels; adjust the region for your image. Automatic border removal works on solid-color borders; use an explicit crop for uneven scan edges.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); client.setCropAreaHeight("384px"); client.setCropAreaWidth("1024px"); client.setCropAreaX("96px"); client.setCropAreaY("370px"); client.setDpi(96); client.convertFileToFile("scan.png", "chart.pdf"); } }
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setRemoveBorders(true); client.convertFileToFile("scan.png", "trimmed.pdf"); } }
Rotate scan-sideways.png 90 degrees clockwise to restore the upright orientation, then save upright.pdf.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(150); client.setRotate("90"); client.convertFileToFile("scan-sideways.png", "upright.pdf"); } }
Render scan.png onto A4 paper at 72 DPI and save compact.pdf. Keep the same fit and 12mm margins as the 150 DPI A4 example above, while reducing resolution for screen viewing. Smaller files come at the cost of fine detail.
import com.pdfcrowd.Pdfcrowd; class Example { public static void main(String[] args) throws Exception { Pdfcrowd.ImageToPdfClient client = new Pdfcrowd.ImageToPdfClient("demo", "demo"); client.setDpi(72); client.setMarginBottom("12mm"); client.setMarginLeft("12mm"); client.setMarginRight("12mm"); client.setMarginTop("12mm"); client.setPageSize("A4"); client.setPrintPageMode("fit"); client.convertFileToFile("scan.png", "compact.pdf"); } }