Apache PDFBox - Image to PDF

🔧 Operation Name

Apache PDFBox – Image to PDF imageToPdf


🧾 Description

Converts a single image (e.g., JPG, PNG, GIF, BMP) into a one-page PDF document. The resulting PDF has a single page sized to match the dimensions of the source image.

Perfect for embedding scanned receipts, screenshots, or images into a PDF workflow.


✅ Inputs

  • Image File [Binary] (InputStream) Binary content of the image (JPEG, PNG, etc.).


📤 Output

  • Payload: InputStream (binary stream) A one-page PDF containing the provided image.

  • Attributes: PdfBoxFileAttributes Metadata of the generated PDF: number of pages (always 1), file size, creation date, etc.



🔍 Notes

  • The generated page is automatically sized to the image’s pixel dimensions (no scaling).

  • You can adjust scaling or margins later by combining this with other operations (e.g., rotatePages, filterPages).

  • Currently supports formats loadable by PDImageXObject (PNG, JPEG, BMP, GIF).

  • Always outputs a single-page PDF regardless of the image dimensions.


Underlying Application Interface:

Pseudo Code
Methods used from the Apache PDFBox library

📂 Classes & Methods from PDFBox

  1. PDDocument

    • new PDDocument() → creates a new empty PDF document.

    • addPage(PDPage page) → adds a new page to the document.

    • save(OutputStream) → saves the document to an output stream.

    • close() → releases resources held by the document.

  2. PDImageXObject

    • PDImageXObject.createFromByteArray(PDDocument doc, byte[] imageData, String name) → creates a PDF image object from raw image bytes.

  3. PDRectangle

    • new PDRectangle(float width, float height) → defines a rectangle (page size) with the same dimensions as the image.

  4. PDPage

    • new PDPage(PDRectangle mediaBox) → creates a new page with the given dimensions.

  5. PDPageContentStream

    • new PDPageContentStream(PDDocument doc, PDPage page) → creates a content stream to draw onto a page.

    • drawImage(PDImageXObject image, float x, float y, float width, float height) → draws the image onto the page at (x, y) scaled to width/height.

    • close() → finalizes and closes the content stream.

Last updated