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

com.ironsoftware.ironpdf.internal.staticapi.Page_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.ironsoftware.ironpdf.internal.proto.*;
import com.ironsoftware.ironpdf.page.PageInfo;
import com.ironsoftware.ironpdf.page.PageRotation;

import java.util.List;
import java.util.stream.Collectors;

/**
 * The type Page api.
 */
public final class Page_Api {

    /**
     * Removes a range of pages from the PDF
     *
     * @param internalPdfDocument the internal pdf document
     * @param pageIndexes         A list of pages indexes to remove.
     */
    public static void removePage(InternalPdfDocument internalPdfDocument, List pageIndexes) {
        RpcClient client = Access.ensureConnection();
        PdfiumRemovePagesRequestP.Builder req = PdfiumRemovePagesRequestP.newBuilder();
        req.setDocument(internalPdfDocument.remoteDocument);
        req.addAllPageIndexes(pageIndexes);

        PdfiumRemovePagesResultP res = client.blockingStub.pdfiumPageRemovePages(req.build());

        if (res.getResultOrExceptionCase() == PdfiumRemovePagesResultP.ResultOrExceptionCase.EXCEPTION) {
            throw Exception_Converter.fromProto(res.getException());
        }
    }

    /**
     * Static method that joins (concatenates) multiple PDF documents together into one compiled PDF
     * document. If the PDF contains form fields the form field in the resulting PDF's name will be
     * appended with '_{index}' e.g. 'Name' will be 'Name_0'
     *
     * @param pdfDocuments A List of PdfDocument.  To merge existing PDF files you may use the                     PdfDocument.FromFile static method in conjunction with Merge.
     * @return A new, merged {@link InternalPdfDocument}
     */
    public static InternalPdfDocument mergePage(List pdfDocuments) {
        RpcClient client = Access.ensureConnection();
        PdfiumPdfDocumentMergeRequestP.Builder req = PdfiumPdfDocumentMergeRequestP.newBuilder();

        req.addAllDocuments(
                pdfDocuments.stream().map(x -> x.remoteDocument).collect(Collectors.toList()));

        PdfDocumentResultP res = client.blockingStub.pdfiumPageMerge(req.build());

        return Utils_Util.handlePdfDocumentResult(res);
    }

    /**
     * Inserts another PDF into the current PdfDocument, starting at a given Page Index. If
     * AnotherPdfFile contains form fields, those fields will be appended with '_' in the resulting
     * PDF. e.g. 'Name' will be 'Name_'
     *
     * @param internalPdfDocument the internal pdf document
     * @param anotherPdf          internal pdf document to append.
     */
    public static void insertPage(InternalPdfDocument internalPdfDocument, InternalPdfDocument anotherPdf) {
        insertPage(internalPdfDocument, anotherPdf, 0);
    }

    /**
     * Inserts another PDF into the current PdfDocument, starting at a given Page Index. If
     * AnotherPdfFile contains form fields, those fields will be appended with '_' in the resulting
     * PDF. e.g. 'Name' will be 'Name_'
     *
     * @param internalPdfDocument the internal pdf document
     * @param anotherPdf          internal pdf document to append.
     * @param atIndex             Index at which to insert the new content.  Note: Page 1 has index 0...
     */
    public static void insertPage(InternalPdfDocument internalPdfDocument, InternalPdfDocument anotherPdf,
                                  int atIndex) {
        RpcClient client = Access.ensureConnection();
        PdfiumPdfDocumentInsertRequestP.Builder req = PdfiumPdfDocumentInsertRequestP.newBuilder();
        req.setMainDocument(internalPdfDocument.remoteDocument);
        req.setInsertionIndex(atIndex);
        req.setInsertedDocument(anotherPdf.remoteDocument);

        EmptyResultP res = client.blockingStub.pdfiumPageInsertPdf(req.build());
        Utils_Util.handleEmptyResult(res);
    }

    /**
     * Appends another PDF to the end of the current {@link InternalPdfDocument} If AnotherPdfFile
     * contains form fields, those fields will be appended with '_' in the resulting PDF. e.g. 'Name'
     * will be 'Name_'
     *
     * @param mainPdfDocument the main internal pdf document
     * @param anotherPdf      internal pdf document to append.
     */
    public static void appendPdf(InternalPdfDocument mainPdfDocument, InternalPdfDocument anotherPdf) {
        RpcClient client = Access.ensureConnection();
        PdfiumPdfDocumentInsertRequestP.Builder req = PdfiumPdfDocumentInsertRequestP.newBuilder();
        req.setMainDocument(mainPdfDocument.remoteDocument);
        req.setInsertionIndex(getPagesInfo(mainPdfDocument).size());
        req.setInsertedDocument(anotherPdf.remoteDocument);

        EmptyResultP res = client.blockingStub.pdfiumPageInsertPdf(req.build());
        Utils_Util.handleEmptyResult(res);
    }

    /**
     * Gets the list of pages with information in the PDF document.
     *
     * @param internalPdfDocument the internal pdf document
     * @return the pages info
     */
    public static List getPagesInfo(InternalPdfDocument internalPdfDocument) {
        RpcClient client = Access.ensureConnection();
        PdfiumGetPagesRequestP.Builder req = PdfiumGetPagesRequestP.newBuilder();
        req.setDocument(internalPdfDocument.remoteDocument);

        PdfiumGetPagesResultP res = client.blockingStub.pdfiumPageGetPages(req.build());

        if (res.getResultOrExceptionCase() == PdfiumGetPagesResultP.ResultOrExceptionCase.EXCEPTION) {
            throw Exception_Converter.fromProto(res.getException());
        }
        return res.getResult().getPagesList().stream()
                .map(Page_Converter::fromProto).collect(Collectors.toList());
    }

    /**
     * Rotates all page of the PdfDocument by a specified number of degrees.
     *
     * @param internalPdfDocument the internal pdf document
     * @param pageRotation        Degrees of rotation
     */
    public static void setPageRotation(InternalPdfDocument internalPdfDocument, PageRotation pageRotation) {
        setPageRotation(internalPdfDocument, pageRotation, null);
    }

    /**
     * Rotates pages of the PdfDocument by a specified number of degrees.
     *
     * @param internalPdfDocument the internal pdf document
     * @param pageRotation        Degrees of rotation
     * @param pageIndexes         Indexes of the pages to rotate in an IEnumerable, list or array. PageIndex                     is a 'Zero based' page number, the first page being 0
     */
    public static void setPageRotation(InternalPdfDocument internalPdfDocument, PageRotation pageRotation,
                                       List pageIndexes) {
        RpcClient client = Access.ensureConnection();
        PdfiumSetPagesRotationRequestP.Builder req = PdfiumSetPagesRotationRequestP.newBuilder();
        req.setDocument(internalPdfDocument.remoteDocument);
        req.setPageRotation(Page_Converter.toProto(pageRotation));

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

        EmptyResultP res = client.blockingStub.pdfiumPageSetPagesRotation(req.build());
        Utils_Util.handleEmptyResult(res);
    }

    /**
     * Creates a new PDF by copying a range of pages from this PdfDocument.
     *
     * @param internalPdfDocument the internal pdf document
     * @param pageIndexes         An Iterable of page indexes to copy into the new PDF.
     * @return A new {@link InternalPdfDocument}
     */
    public static InternalPdfDocument copyPage(InternalPdfDocument internalPdfDocument,
                                               List pageIndexes) {
        RpcClient client = Access.ensureConnection();
        PdfiumCopyPagesRequestP.Builder req = PdfiumCopyPagesRequestP.newBuilder();
        req.setDocument(internalPdfDocument.remoteDocument);

        req.addAllPageIndexes(pageIndexes);

        PdfDocumentResultP res = client.blockingStub.pdfiumPageCopyPages(req.build());

        return Utils_Util.handlePdfDocumentResult(res);
    }

    /**
     Resize a page to the specified dimensions (in millimeters)
     @param internalPdfDocument the internal pdf document
     @param pageWidth Desired page width, in millimeters
     @param pageHeight Desired page height, in millimeters
     @param pageIndex Selected page indexes.
     */
    public static void resizePage(InternalPdfDocument internalPdfDocument, double pageWidth, double pageHeight,
                                       Integer pageIndex) {
        RpcClient client = Access.ensureConnection();
        PdfiumResizePageRequestP.Builder req = PdfiumResizePageRequestP.newBuilder();
        req.setDocument(internalPdfDocument.remoteDocument);
        req.setPageHeight(pageHeight);
        req.setPageWidth(pageWidth);
        req.setPageIndex(pageIndex);

        EmptyResultP res = client.blockingStub.pdfiumPageResizePage(req.build());
        Utils_Util.handleEmptyResult(res);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy