All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ironsoftware.ironpdf.internal.staticapi.Image_Api Maven / Gradle / Ivy

Go to download

IronPDF Java library offers an extensive compatibility range, making it a go-to solution for a wide array of developers. It fully supports JVM languages like Java, Scala, and Kotlin, making it incredibly versatile. This Java PDF library is also compatible with Java 8 and above, providing optimum performance across multiple platforms. It's been designed with a wide range of users in mind Here's a look at what it supports: JVM Languages: Java, Scala, Kotlin.Platforms: Java 8 and above.Operating Systems: Microsoft Windows, Linux, Docker, Azure, AWS.IDEs: Jetbrains IntelliJ IDEA, Eclipse. You can deploy IronPDF Java across various platforms, including Microsoft Windows, Linux, Docker, Azure, and AWS. It is also fully compatible with popular IDEs like Jetbrains IntelliJ IDEA and Eclipse, facilitating smooth project development and management. Your pom.xml file is essentially the backbone of your project when you're using Maven. It's here where you introduce new dependencies that you wish to include. To make IronPDF Java package a part of your Maven project, you simply need to add the following snippets to your pom.xml: Remember to replace '20xx.xx.xxxx' with the latest version of IronPDF. IronPDF Java simplifies the process of creating PDF files. Convert HTML files, HTML strings, or URLs directly to new PDF documents in a few lines of code. The variety of file formats it handles is vast, as it can even transform images into PDF documents and vice versa. Need to use base 64 encoding, base URLs, or custom file paths? No problem! IronPDF Java has got you coveredFor more detail about installing and using IronPDF Java. When you run your project for the first time post-integration, IronPDF's engine binaries will automatically be downloaded. The engine starts its journey when you call any IronPDF function for the first time and takes a breather when your application is either closed or enters an idle state. It is not an open source java PDF library but here's the best part - IronPDF Java is offering a 30-day free trial. So, why wait? Give it a go and boost your PDF operations today.

There is a newer version: 2024.10.1
Show newest version
package com.ironsoftware.ironpdf.internal.staticapi;

import com.google.protobuf.ByteString;
import com.ironsoftware.ironpdf.image.ImageBehavior;
import com.ironsoftware.ironpdf.internal.proto.*;
import com.ironsoftware.ironpdf.page.PageInfo;
import com.ironsoftware.ironpdf.render.ChromePdfRenderOptions;
import com.ironsoftware.ironpdf.render.PaperSize;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * The type Image api.
 */
public final class Image_Api {

    public static class ImageData {
        byte[] data;
        String fileExtension;

        public ImageData(byte[] data, String fileExtension) {
            this.data = data;
            this.fileExtension = fileExtension;
        }
    }

    /**
     * Converts multiple image files to a PDF document.  Each image creates 1 page which matches the
     * image dimensions. The default PaperSize is A4. You can set it via {@link ChromePdfRenderOptions#setPaperSize(PaperSize)}
     * Note: Imaging.ImageBehavior.CropPage will set PaperSize equal to ImageSize.
     *
     * @param imagesData   The {@link ImageData}
     * @param imageBehavior Describes how image should be placed on the PDF page
     * @param renderOptions Rendering options
     * @return the internal pdf document
     */
    public static InternalPdfDocument imageToPdf(List imagesData,
                                                 ImageBehavior imageBehavior, ChromePdfRenderOptions renderOptions) {
        RpcClient client = Access.ensureConnection();

        ChromeImageFilesToPdfRequestStreamP.InfoP.Builder info = ChromeImageFilesToPdfRequestStreamP.InfoP.newBuilder();
        info.setRenderOptions(
                Render_Converter.toProto((renderOptions != null ? renderOptions : new ChromePdfRenderOptions())));
        info.setImageBehavior(Image_Converter.toProto(imageBehavior));

        final CountDownLatch finishLatch = new CountDownLatch(1);
        ArrayList resultChunks = new ArrayList<>();

        io.grpc.stub.StreamObserver requestStream =
                client.stub.chromeImageImageFilesToPdf(
                        new Utils_ReceivingCustomStreamObserver<>(finishLatch, resultChunks));

        ChromeImageFilesToPdfRequestStreamP.Builder infoMsg =
                ChromeImageFilesToPdfRequestStreamP.newBuilder();
        infoMsg.setInfo(info);
        requestStream.onNext(infoMsg.build());

        for (int imageIndex : IntStream.range(0, imagesData.size()).toArray()) {
            ImageData imageData = imagesData.get(imageIndex);
            for (Iterator it = Utils_Util.chunk(imageData.data); it.hasNext(); ) {
                byte[] chunk = it.next();
                ChromeImageFilesToPdfRequestStreamP.Builder msg = ChromeImageFilesToPdfRequestStreamP.newBuilder();
                RawImageChunkWithIndexAndFileTypeP.Builder rawImageFilesChunkWithIndex = RawImageChunkWithIndexAndFileTypeP.newBuilder();
                rawImageFilesChunkWithIndex.setRawImageChunk(ByteString.copyFrom(chunk));
                rawImageFilesChunkWithIndex.setImageIndex(imageIndex);
                rawImageFilesChunkWithIndex.setFileType(imageData.fileExtension);
                msg.setRawImagesFileChunk(rawImageFilesChunkWithIndex);
                requestStream.onNext(msg.build());
            }
        }

        requestStream.onCompleted();

        Utils_Util.waitAndCheck(finishLatch, resultChunks);

        return Utils_Util.handlePdfDocumentChunks(resultChunks);
    }

    /**
     * Draw an image multiple times according to the specified parameters; all occurrences of the
     * image will share a single data stream
     *
     * @param internalPdfDocument the internal pdf document
     * @param imageBytes          images to draw
     * @param pageIndexes         Target page indexes
     * @param x                   X coordinate
     * @param y                   Y coordinate
     * @param desiredWidth        Desired widths
     * @param desiredHeight       Desired heights
     */
    public static void drawImage(InternalPdfDocument internalPdfDocument, byte[] imageBytes,
                                 List pageIndexes, double x,
                                 double y, double desiredWidth, double desiredHeight) {
        RpcClient client = Access.ensureConnection();

        PdfiumDrawBitmapRequestStreamP.InfoP.Builder info = PdfiumDrawBitmapRequestStreamP.InfoP.newBuilder();
        info.setDocument(internalPdfDocument.remoteDocument);
        info.setX(x);
        info.setY(y);
        info.setDesiredWidth(desiredWidth);
        info.setDesiredHeight(desiredHeight);
        info.addAllPageIndexes(pageIndexes);

        final CountDownLatch finishLatch = new CountDownLatch(1);
        ArrayList resultChunks = new ArrayList<>();

        io.grpc.stub.StreamObserver requestStream = client.stub.pdfiumImageDrawBitmap(
                new Utils_ReceivingCustomStreamObserver<>(finishLatch, resultChunks));

        requestStream.onNext(PdfiumDrawBitmapRequestStreamP.newBuilder().setInfo(info).build());

        for (Iterator it = Utils_Util.chunk(imageBytes); it.hasNext(); ) {
            byte[] bytes = it.next();
            PdfiumDrawBitmapRequestStreamP.Builder msg = PdfiumDrawBitmapRequestStreamP.newBuilder();
            msg.setRawImageChunk(ByteString.copyFrom(bytes));
            requestStream.onNext(msg.build());
        }
        requestStream.onCompleted();

        Utils_Util.waitAndCheck(finishLatch, resultChunks);

        Utils_Util.handleEmptyResultChunks(resultChunks);
    }

    /**
     * Finds all embedded Images from within the PDF and returns as list of image bytes
     *
     * @param internalPdfDocument the internal pdf document
     * @return the list
     * @throws IOException the io exception
     */
    public static List extractAllImages(InternalPdfDocument internalPdfDocument) throws IOException {
        return extractAllImages(internalPdfDocument, null);
    }

    /**
     * Finds all embedded Images from within the PDF and returns then as image byte[] objects
     *
     * @param internalPdfDocument the internal pdf document
     * @param pageIndexes         Index of the page.  Note: Page 1 has index 0. Defaults to all pages
     * @return the list
     * @throws IOException the io exception
     */
    public static List extractAllImages(InternalPdfDocument internalPdfDocument,
                                                List pageIndexes) throws IOException {
        RpcClient client = Access.ensureConnection();

        PdfiumExtractAllRawImagesRequestP.Builder req = PdfiumExtractAllRawImagesRequestP.newBuilder();
        req.setDocument(internalPdfDocument.remoteDocument);

        if(pageIndexes == null || pageIndexes.isEmpty()){
            pageIndexes = Page_Api.getPagesInfo(internalPdfDocument).stream().map(PageInfo::getPageIndex).collect(Collectors.toList());
        }
        req.addAllPageIndexes(pageIndexes);

        final CountDownLatch finishLatch = new CountDownLatch(1);
        ArrayList resultChunks = new ArrayList<>();

        client.stub.pdfiumImageExtractAllRawImages(req.build(),
                new Utils_ReceivingCustomStreamObserver<>(finishLatch, resultChunks));

        Utils_Util.waitAndCheck(finishLatch, resultChunks);

        return Utils_Util.handleImagesResult(resultChunks);
    }

    /**
     * Renders the PDF and exports image Files in convenient formats.  Page Numbers may be specified.
     * 1 image file is created for each page. 

FileNamePattern should normally contain an asterisk * (*) character which will be substituted for the page numbers

* * @param internalPdfDocument the internal pdf document * @param pageIndexes A list of the specific zero based page number to render as images. * @param dpi The desired resolution of the output Images. * @param imageMaxWidth The target maximum width(in pixel) of the output images. * @return An array of the file paths of the image files created.

The DPI will be ignored under Linux and macOS. * @throws IOException the io exception */ public static List pdfToImage(InternalPdfDocument internalPdfDocument, List pageIndexes, int dpi, Integer imageMaxWidth) throws IOException { return pdfToImage(internalPdfDocument, pageIndexes, dpi, imageMaxWidth, null); } /** * Renders the PDF and exports image Files in convenient formats. Page Numbers may be specified. * 1 image file is created for each page.

FileNamePattern should normally contain an asterisk * (*) character which will be substituted for the page numbers

* * @param internalPdfDocument the internal pdf document * @param pageIndexes A list of the specific zero based page number to render as images. * @param dpi The desired resolution of the output Images. * @param imageMaxWidth The target maximum width(in pixel) of the output images. * @param imageMaxHeight The target maximum height(in pixel) of the output images. * @return An array of the file paths of the image files created. * @throws IOException the io exception */ public static List pdfToImage(InternalPdfDocument internalPdfDocument, List pageIndexes, int dpi, Integer imageMaxWidth, Integer imageMaxHeight) throws IOException { RpcClient client = Access.ensureConnection(); PdfiumPdfToImagesRequestP.Builder request = PdfiumPdfToImagesRequestP.newBuilder(); request.setDocument(internalPdfDocument.remoteDocument); request.setDpi(dpi); if(pageIndexes == null || pageIndexes.isEmpty()){ pageIndexes = Page_Api.getPagesInfo(internalPdfDocument).stream().map(PageInfo::getPageIndex).collect(Collectors.toList()); } request.addAllPageIndexes(pageIndexes); if (imageMaxWidth != null) { request.setMaxWidth(imageMaxWidth); } if (imageMaxHeight != null) { request.setMaxHeight(imageMaxHeight); } final CountDownLatch finishLatch = new CountDownLatch(1); ArrayList resultChunks = new ArrayList<>(); client.stub.pdfiumImagePdfToImages(request.build(), new Utils_ReceivingCustomStreamObserver<>(finishLatch, resultChunks)); Utils_Util.waitAndCheck(finishLatch, resultChunks); return Utils_Util.handleImagesResult(resultChunks); } /** * Renders the PDF and exports image Files in convenient formats. Page Numbers may be specified. * 1 image file is created for each page.

FileNamePattern should normally contain an asterisk * (*) character which will be substituted for the page numbers

* * @param internalPdfDocument the internal pdf document * @param pageIndexes A list of the specific zero based page number to render as images. * @param dpi The desired resolution of the output Images. * @return An array of the file paths of the image files created. * @throws IOException the io exception */ public static List pdfToImage(InternalPdfDocument internalPdfDocument, List pageIndexes, int dpi) throws IOException { return pdfToImage(internalPdfDocument, pageIndexes, dpi, null, null); } /** * Renders the PDF and exports image Files in convenient formats. Page Numbers may be specified. * 1 image file is created for each page.

FileNamePattern should normally contain an asterisk * (*) character which will be substituted for the page numbers

* * @param internalPdfDocument the internal pdf document * @param pageIndexes A list of the specific zero based page number to render as images. * @return An array of the file paths of the image files created. * @throws IOException the io exception */ public static List pdfToImage(InternalPdfDocument internalPdfDocument, List pageIndexes) throws IOException { return pdfToImage(internalPdfDocument, pageIndexes, 92, null, null); } /** * Renders the PDF and exports image Files in convenient formats. Page Numbers may be specified. * 1 image file is created for each page.

FileNamePattern should normally contain an asterisk * (*) character which will be substituted for the page numbers

* * @param internalPdfDocument the internal pdf document * @return An array of the file paths of the image files created. * @throws IOException the io exception */ public static List pdfToImage(InternalPdfDocument internalPdfDocument) throws IOException { return pdfToImage(internalPdfDocument, null, 92, null, null); } public static byte[] toMultiPageTiff(InternalPdfDocument internalPdfDocument, List pageIndexes, int dpi, Integer imageMaxWidth, Integer imageMaxHeight) throws IOException { RpcClient client = Access.ensureConnection(); PdfiumPdfToMultiPageTiffImageRequestP.Builder request = PdfiumPdfToMultiPageTiffImageRequestP.newBuilder(); request.setDocument(internalPdfDocument.remoteDocument); request.setDpi(dpi); if(pageIndexes == null || pageIndexes.isEmpty()){ pageIndexes = Page_Api.getPagesInfo(internalPdfDocument).stream().map(PageInfo::getPageIndex).collect(Collectors.toList()); } request.addAllPageIndexes(pageIndexes); if (imageMaxWidth != null) { request.setMaxWidth(imageMaxWidth); } if (imageMaxHeight != null) { request.setMaxHeight(imageMaxHeight); } final CountDownLatch finishLatch = new CountDownLatch(1); ArrayList resultChunks = new ArrayList<>(); client.stub.pdfiumImagePdfToMultiPageTiffImage(request.build(), new Utils_ReceivingCustomStreamObserver<>(finishLatch, resultChunks)); Utils_Util.waitAndCheck(finishLatch, resultChunks); return Utils_Util.handleImageResult(resultChunks); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy