SelectPdf Online REST API is a platform independent PDF manipulation API. SelectPdf REST API is cloud based and it can be used with any language: .NET (C# or VB.NET), Java, PHP, Python, Go, Ruby, Node.js, Perl and many more. We are presenting today the dedicated Java client library for SelectPdf API.
SelectPdf Java client library can be used to take advance of the features offered by SelectPdf Online REST API:
HTML to PDF REST API – Use SelectPdf HTML To PDF Online REST API to generate PDF documents from web page urls or raw HTML code.
PDF to TEXT REST API – SelectPdf Pdf To Text REST API is a cloud based solution that can be used to extract text from PDF documents or search PDF documents for specific words.
PDF Merge REST API – SelectPdf Pdf Merge REST API is an online solution that can be used to merge local or remote PDFs into a final PDF document.
All these APIs can be easily integrated with Java code and applications using the dedicated client library.
Installation
Download selectpdf-api-java-client-1.4.0.zip, unzip it and copy selectpdf-api-java-client-1.4.0.jar to your CLASSPATH.
OR
Install SelectPdf Java Client for Online API via Maven: SelectPdf on Maven.
<groupId>com.selectpdf</groupId>
<artifactId>selectpdf-api-client</artifactId>
<version>1.4.0</version>
</dependency>
OR
Install the client library by Gradle (e.g. for Android Studio) from Maven by adding the following line to your Gradle dependencies:
OR
Clone selectpdf-api-java-client from Github and install the library.
cd selectpdf-api-java-client
mvn install
Get a trial key for SelectPdf online REST API
Once the library is installed, you need a key to be able to access the API.
GET A DEMO LICENSE KEY NOW
The free trial key for the online API is valid for 7 days and it includes 200 conversions.
Sample Code
The Java client library makes accessing SelectPdf online REST API very easy. Here are a few samples that present the main features of the API. For details and full list of parameters access the online documentation of the client library or the individual pages of the APIs: HTML to PDF API or PDF to TEXT API or PDF Merge API.
Convert HTML to PDF in Java
The following sample shows the main features of the HTML To PDF API. Comment/uncomment code to convert an url to file or memory or also convert raw HTML to file or memory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
package com.selectpdf; public class HtmlToPdfMain { public static void main(String[] args) throws Exception { String url = "https://selectpdf.com"; String localFile = "Test.pdf"; String apiKey = "Your API key here"; System.out.println(String.format("This is SelectPdf-%s.", ApiClient.CLIENT_VERSION)); try { HtmlToPdfClient client = new HtmlToPdfClient(apiKey); // set parameters - see full list at https://selectpdf.com/html-to-pdf-api/ client // main properties .setPageSize(ApiEnums.PageSize.A4) // PDF page size .setPageOrientation(ApiEnums.PageOrientation.Portrait) // PDF page orientation .setMargins(0) // PDF page margins .setRenderingEngine(ApiEnums.RenderingEngine.WebKit) // rendering engine .setConversionDelay(1) // conversion delay .setNavigationTimeout(30) // navigation timeout .setShowPageNumbers(false) // page numbers .setPageBreaksEnhancedAlgorithm(true) // enhanced page break algorithm // additional properties // .setUseCssPrint(true) // enable CSS media print // .setDisableJavascript(true) // disable javascript // .setDisableInternalLinks(true) // disable internal links // .setDisableExternalLinks(true) // disable external links // .setKeepImagesTogether(true) // keep images together // .setScaleImages(true) // scale images to create smaller pdfs // .setSinglePagePdf(true) // generate a single page PDF // .setUserPassword("password") // secure the PDF with a password // generate automatic bookmarks // .setPdfBookmarksSelectors("H1, H2") // create outlines (bookmarks) for the specified elements // .setViewerPageMode(ApiEnums.PageMode.UseOutlines) // display outlines (bookmarks) in viewer ; System.out.println("Starting conversion..."); // convert url to file client.convertUrlToFile(url, localFile); // convert url to memory // byte[] pdf = client.convertUrl(url); // convert html string to file // client.convertHtmlStringToFile("This is some <b>html</b>.", localFile); // convert html string to memory // byte[] pdf = client.convertHtmlString("This is some <b>html</b>."); 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()); } } } |
Convert HTML to PDF with custom header/footer in Java
The following sample shows how to convert a web page to PDF and also setting a custom header or footer.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
package com.selectpdf; public class HtmlToPdfHeadersAndFooters { public static void main(String[] args) throws Exception { String url = "https://selectpdf.com"; String localFile = "Test.pdf"; String apiKey = "Your API key here"; System.out.println(String.format("This is SelectPdf-%s.", ApiClient.CLIENT_VERSION)); try { HtmlToPdfClient client = new HtmlToPdfClient(apiKey); // set parameters - see full list at https://selectpdf.com/html-to-pdf-api/ client .setMargins(0) // PDF page margins .setPageBreaksEnhancedAlgorithm(true) // enhanced page break algorithm // header properties .setShowHeader(true) // display header // .setHeaderHeight(50) // header height // .setHeaderUrl(url) // header url .setHeaderHtml("This is the <b>HEADER</b>!!!!") // header html // footer properties .setShowFooter(true) // display footer // .setFooterHeight(60) // footer height // .setFooterUrl(url) // footer url .setFooterHtml("This is the <b>FOOTER</b>!!!!") // footer html // footer page numbers .setShowPageNumbers(true) // show page numbers in footer .setPageNumbersTemplate("{page_number} / {total_pages}") // page numbers template .setPageNumbersFontName("Verdana") // page numbers font name .setPageNumbersFontSize(12) // page numbers font size .setPageNumbersAlignment(ApiEnums.PageNumbersAlignment.Center) // page numbers alignment (2-Center) ; System.out.println("Starting conversion..."); // convert url to file client.convertUrlToFile(url, localFile); // convert url to memory // byte[] pdf = client.convertUrl(url); // convert html string to file // client.convertHtmlStringToFile("This is some <b>html</b>.", localFile); // convert html string to memory // byte[] pdf = client.convertHtmlString("This is some <b>html</b>."); 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()); } } } |
Extract text from PDF in Java
The following sample shows how to extract the text from a PDF document using SelectPdf API. Comment/uncomment code to convert a local PDF or a PDF from a remote url to file or memory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
package com.selectpdf; public class PdfToText { public static void main(String[] args) throws Exception { String testUrl = "https://selectpdf.com/demo/files/selectpdf.pdf"; String testPdf = "Input.pdf"; String localFile = "Result.txt"; String apiKey = "Your API key here"; System.out.println(String.format("This is SelectPdf-%s.", ApiClient.CLIENT_VERSION)); try { PdfToTextClient client = new PdfToTextClient(apiKey); // set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ client .setStartPage(1) // start page (processing starts from here) .setEndPage(0) // end page (set 0 to process file til the end) .setOutputFormat(ApiEnums.OutputFormat.Text) // set output format (0-Text or 1-HTML) ; System.out.println("Starting pdf to text..."); // convert local pdf to local text file client.getTextFromFileToFile(testPdf, localFile); // extract text from local pdf to memory // String text = client.getTextFromFile(testPdf); // print text // System.out.println(text); // convert pdf from public url to local text file // client.getTextFromUrlToFile(testUrl, localFile); // extract text from pdf from public url to memory // String text = client.getTextFromUrl(testUrl); // print text // System.out.println(text); 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()); } } } |
Search for text in PDF using Java
The following sample shows how to search a PDF document for a specific text.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
package com.selectpdf; public class SearchPdf { public static void main(String[] args) throws Exception { String testUrl = "https://selectpdf.com/demo/files/selectpdf.pdf"; String testPdf = "Input.pdf"; String apiKey = "Your API key here"; System.out.println(String.format("This is SelectPdf-%s.", ApiClient.CLIENT_VERSION)); try { PdfToTextClient client = new PdfToTextClient(apiKey); // set parameters - see full list at https://selectpdf.com/pdf-to-text-api/ client .setStartPage(1) // start page (processing starts from here) .setEndPage(0) // end page (set 0 to process file til the end) .setOutputFormat(ApiEnums.OutputFormat.Text) // set output format (0-Text or 1-HTML) ; System.out.println("Starting search pdf..."); // search local pdf String results = client.searchFile(testPdf, "pdf"); // search pdf from public url // String results = client.searchUrl(testUrl, "pdf"); System.out.printf("Search results:\r\n%s\r\n", results); // org.json.JSONArray textPositions = new org.json.JSONArray(results); // System.out.printf("Search results count: %d\r\n", textPositions.length()); System.out.println(String.format("Finished! Number of pages processed: %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()); } } } |
Merge PDFs using Java
The following sample shows how merge several PDF documents into a final file. The source PDFs can be local files or PDFs from remote urls. The final PDF can be retrieved in memory or saved to a local file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
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()); } } } |
The above Java samples can also be found in GitHub repository: Java Samples.