Package com.selectpdf

Class PdfMergeClient

java.lang.Object
com.selectpdf.ApiClient
com.selectpdf.PdfMergeClient

public class PdfMergeClient
extends ApiClient
Pdf Merge with SelectPdf Online API.
 
package com.selectpdf;

public class PdfMerge {
    public static void main(String[] args) throws Exception {
        String testUrl = "https://selectpdf.com/demo/files/selectpdf.pdf";
        String testPdf = "Input.pdf";
        String localFile = "Result.pdf";
        String apiKey = "Your API key here";

        System.out.println(String.format("This is SelectPdf-%s.", ApiClient.CLIENT_VERSION));

        try {
            PdfMergeClient client = new PdfMergeClient(apiKey);

            // set parameters - see full list at https://selectpdf.com/pdf-merge-api/
            client
                // specify the pdf files that will be merged (order will be preserved in the final pdf)

                .addFile(testPdf) // add PDF from local file
                .addUrlFile(testUrl) // add PDF From public url
                // .addFile(testPdf, "pdf_password") // add PDF (that requires a password) from local file
                // .addUrlFile(testUrl, "pdf_password") // add PDF (that requires a password) from public url
            ;

            System.out.println("Starting pdf merge...");

            // merge pdfs to local file
            client.saveToFile(localFile);

            // merge pdfs to memory
            // byte[] pdf = client.save();

            System.out.println(String.format("Finished! Number of pages: %d.", client.getNumberOfPages()));

            // get API usage
            UsageClient usageClient = new UsageClient(apiKey);
            String usage = usageClient.getUsage(false);
            System.out.printf("Usage details: %s.\r\n", usage);

            // org.json.JSONObject usageObject = new org.json.JSONObject(usage);
            // int available = usageObject.getInt("available");
            // System.out.printf("Conversions remained this month: %d.\r\n", available);

        }
        catch (Exception ex) {
            System.out.println("An error occured: " + ex.getMessage());
        }
    }
        
}
 
 
  • Constructor Details

    • PdfMergeClient

      public PdfMergeClient​(java.lang.String apiKey)
      Construct the Pdf Merge Client.
      Parameters:
      apiKey - API Key.
  • Method Details

    • addFile

      public PdfMergeClient addFile​(java.lang.String inputPdf)
      Add local PDF document to the list of input files.
      Parameters:
      inputPdf - Path to a local PDF file.
      Returns:
      Reference to the current object.
    • addFile

      public PdfMergeClient addFile​(java.lang.String inputPdf, java.lang.String userPassword)
      Add local PDF document to the list of input files.
      Parameters:
      inputPdf - Path to a local PDF file.
      userPassword - User password for the PDF document.
      Returns:
      Reference to the current object.
    • addUrlFile

      public PdfMergeClient addUrlFile​(java.lang.String inputUrl)
      Add remote PDF document to the list of input files.
      Parameters:
      inputUrl - Url of a remote PDF file.
      Returns:
      Reference to the current object.
    • addUrlFile

      public PdfMergeClient addUrlFile​(java.lang.String inputUrl, java.lang.String userPassword)
      Add remote PDF document to the list of input files.
      Parameters:
      inputUrl - Url of a remote PDF file.
      userPassword - User password for the PDF document.
      Returns:
      Reference to the current object.
    • save

      public byte[] save()
      Merge all specified input pdfs and return the resulted PDF.
      Returns:
      Byte array containing the resulted PDF.
    • saveToFile

      public void saveToFile​(java.lang.String filePath) throws java.io.IOException
      Merge all specified input pdfs and writes the resulted PDF to a local file.
      Parameters:
      filePath - Local output file including path if necessary.
      Throws:
      java.io.IOException
    • saveToStream

      public void saveToStream​(java.io.OutputStream stream) throws java.io.IOException
      Merge all specified input pdfs and writes the resulted PDF to a specified stream.
      Parameters:
      stream - The output stream where the resulted PDF will be written.
      Throws:
      java.io.IOException
    • saveAsync

      public byte[] saveAsync()
      Merge all specified input pdfs and return the resulted PDF. An asynchronous call is used.
      Returns:
      Byte array containing the resulted PDF.
    • saveToFileAsync

      public void saveToFileAsync​(java.lang.String filePath) throws java.io.IOException
      Merge all specified input pdfs and writes the resulted PDF to a local file. An asynchronous call is used.
      Parameters:
      filePath - Local output file including path if necessary.
      Throws:
      java.io.IOException
    • saveToStreamAsync

      public void saveToStreamAsync​(java.io.OutputStream stream) throws java.io.IOException
      Merge all specified input pdfs and writes the resulted PDF to a specified stream. An asynchronous call is used.
      Parameters:
      stream - The output stream where the resulted PDF will be written.
      Throws:
      java.io.IOException
    • setDocTitle

      public PdfMergeClient setDocTitle​(java.lang.String docTitle)
      Set the PDF document title.
      Parameters:
      docTitle - Document title.
      Returns:
      Reference to the current object.
    • setDocSubject

      public PdfMergeClient setDocSubject​(java.lang.String docSubject)
      Set the subject of the PDF document.
      Parameters:
      docSubject - Document subject.
      Returns:
      Reference to the current object.
    • setDocKeywords

      public PdfMergeClient setDocKeywords​(java.lang.String docKeywords)
      Set the PDF document keywords.
      Parameters:
      docKeywords - Document keywords.
      Returns:
      Reference to the current object.
    • setDocAuthor

      public PdfMergeClient setDocAuthor​(java.lang.String docAuthor)
      Set the name of the PDF document author.
      Parameters:
      docAuthor - Document author.
      Returns:
      Reference to the current object.
    • setDocAddCreationDate

      public PdfMergeClient setDocAddCreationDate​(java.lang.Boolean docAddCreationDate)
      Add the date and time when the PDF document was created to the PDF document information. The default value is False.
      Parameters:
      docAddCreationDate - Add creation date to the document metadata or not.
      Returns:
      Reference to the current object.
    • setViewerPageLayout

      public PdfMergeClient setViewerPageLayout​(ApiEnums.PageLayout pageLayout)
      Set the page layout to be used when the document is opened in a PDF viewer. The default value is PageLayout.OneColumn.
      Parameters:
      pageLayout - Page layout.
      Returns:
      Reference to the current object.
    • setViewerPageMode

      public PdfMergeClient setViewerPageMode​(ApiEnums.PageMode pageMode)
      Set the document page mode when the pdf document is opened in a PDF viewer. The default value is PageMode.UseNone.
      Parameters:
      pageMode - Page mode.
      Returns:
      Reference to the current object.
    • setViewerCenterWindow

      public PdfMergeClient setViewerCenterWindow​(java.lang.Boolean viewerCenterWindow)
      Set a flag specifying whether to position the document's window in the center of the screen. The default value is False.
      Parameters:
      viewerCenterWindow - Center window or not.
      Returns:
      Reference to the current object.
    • setViewerDisplayDocTitle

      public PdfMergeClient setViewerDisplayDocTitle​(java.lang.Boolean viewerDisplayDocTitle)
      Set a flag specifying whether the window's title bar should display the document title taken from document information. The default value is False.
      Parameters:
      viewerDisplayDocTitle - Display title or not.
      Returns:
      Reference to the current object.
    • setViewerFitWindow

      public PdfMergeClient setViewerFitWindow​(java.lang.Boolean viewerFitWindow)
      Set a flag specifying whether to resize the document's window to fit the size of the first displayed page. The default value is False.
      Parameters:
      viewerFitWindow - Fit window or not.
      Returns:
      Reference to the current object.
    • setViewerHideMenuBar

      public PdfMergeClient setViewerHideMenuBar​(java.lang.Boolean viewerHideMenuBar)
      Set a flag specifying whether to hide the pdf viewer application's menu bar when the document is active. The default value is False.
      Parameters:
      viewerHideMenuBar - Hide menu bar or not.
      Returns:
      Reference to the current object.
    • setViewerHideToolbar

      public PdfMergeClient setViewerHideToolbar​(java.lang.Boolean viewerHideToolbar)
      Set a flag specifying whether to hide the pdf viewer application's tool bars when the document is active. The default value is False.
      Parameters:
      viewerHideToolbar - Hide tool bars or not.
      Returns:
      Reference to the current object.
    • setViewerHideWindowUI

      public PdfMergeClient setViewerHideWindowUI​(java.lang.Boolean viewerHideWindowUI)
      Set a flag specifying whether to hide user interface elements in the document's window (such as scroll bars and navigation controls), leaving only the document's contents displayed. The default value is False.
      Parameters:
      viewerHideWindowUI - Hide window UI or not.
      Returns:
      Reference to the current object.
    • setUserPassword

      public PdfMergeClient setUserPassword​(java.lang.String userPassword)
      Set PDF user password.
      Parameters:
      userPassword - PDF user password.
      Returns:
      Reference to the current object.
    • setOwnerPassword

      public PdfMergeClient setOwnerPassword​(java.lang.String ownerPassword)
      Set PDF owner password.
      Parameters:
      ownerPassword - PDF owner password.
      Returns:
      Reference to the current object.
    • setTimeout

      public PdfMergeClient setTimeout​(int timeout)
      Set the maximum amount of time (in seconds) for this job. The default value is 30 seconds. Use a larger value (up to 120 seconds allowed) for large documents.
      Parameters:
      timeout - Timeout in seconds.
      Returns:
      Reference to the current object.
    • setCustomParameter

      public PdfMergeClient setCustomParameter​(java.lang.String parameterName, java.lang.String parameterValue)
      Set a custom parameter. Do not use this method unless advised by SelectPdf.
      Parameters:
      parameterName - Parameter name.
      parameterValue - Parameter value.
      Returns:
      Reference to the current object.