Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to Convert Excel to PDF using Java API
package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.util.List;
// How to Convert XLSX File into PDF Online in Java using REST API
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
ConvertApi apiInstance = new ConvertApi(configuration);
try {
// Prepare convert settings
ConvertSettings settings = new ConvertSettings();
settings.setStorageName(MyStorage);
settings.setFilePath("java-testing/input-sample-file.xlsx");
settings.setFormat("pdf");
settings.setOutputPath("java-testing/output-sample-file.pdf");
// convert to specified format
List<StoredConvertedResult> response = apiInstance.convertDocument(new ConvertDocumentRequest(settings));
System.out.println("Document converted successfully: " + response);
} catch (ApiException e) {
System.err.println("Exception while calling Java API: ");
e.printStackTrace();
}
}
}
package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.util.List;
// How to Convert Excel Spreadsheet to PDF in Java using Advanced Settings
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
ConvertApi apiInstance = new ConvertApi(configuration);
try {
// Prepare convert settings
ConvertSettings settings = new ConvertSettings();
settings.setStorageName(MyStorage);
settings.setFilePath("java-testing/input-sample-file.xls");
settings.setFormat("pdf");
PdfConvertOptions convertOptions = new PdfConvertOptions();
convertOptions.setFromPage(1);
convertOptions.setPagesCount(1);
convertOptions.setZoom(50);
convertOptions.setDpi(124.0);
convertOptions.setCenterWindow(true);
convertOptions.setCompressImages(false);
convertOptions.setDisplayDocTitle(true);
convertOptions.setFitWindow(false);
convertOptions.setGrayscale(false);
convertOptions.setImageQuality(100);
convertOptions.setLinearize(false);
convertOptions.setMarginTop(5);
convertOptions.setMarginLeft(5);
convertOptions.setPassword("password");
convertOptions.setUnembedFonts(true);
convertOptions.setRemoveUnusedStreams(true);
convertOptions.setRemoveUnusedObjects(true);
convertOptions.setRemovePdfaCompliance(false);
convertOptions.setHeight(1024);
settings.setConvertOptions(convertOptions);
settings.setOutputPath("java-testing/output-sample-file.pdf");
// convert to specified format
List<StoredConvertedResult> response = apiInstance.convertDocument(new ConvertDocumentRequest(settings));
System.out.println("Document conveted successfully: " + response);
} catch (ApiException e) {
System.err.println("Exception while calling Java API: ");
e.printStackTrace();
}
}
}
package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.io.File;
// Download File from the Cloud Storage in Java
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
FileApi apiInstance = new FileApi(configuration);
try {
DownloadFileRequest request = new DownloadFileRequest("java-testing\\output-sample-file.pdf", MyStorage, null);
File response = apiInstance.downloadFile(request);
System.err.println("Expected response type is File: " + response.length());
} catch (ApiException e) {
System.err.println("Exception while calling FileApi:");
e.printStackTrace();
}
}
}
package com.groupdocsdev.classes;
import com.groupdocs.cloud.conversion.api.*;
import com.groupdocs.cloud.conversion.model.*;
import com.groupdocs.cloud.conversion.model.requests.*;
import com.groupdocs.cloud.conversion.client.Configuration;
import com.groupdocs.cloud.conversion.client.ApiException;
import java.io.File;
// Upload File to Cloud Storage in Java
public class App {
public static void main(String[] args) {
// Create an instance of the convert API
FileApi apiInstance = new FileApi(configuration);
try {
File fileStream = new File("H:\\groupdocs-cloud-data\\input-sample-file.xlsx");
UploadFileRequest request = new UploadFileRequest("java-testing\\input-sample-file.xlsx", fileStream, MyStorage);
FilesUploadResult response = apiInstance.uploadFile(request);
System.out.println("Expected response type is FilesUploadResult: " + response.getUploaded().size());
} catch (ApiException e) {
System.err.println("Exception while calling FileApi:");
e.printStackTrace();
}
}
}
# Get your client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
String ClientId = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
String ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
String MyStorage = "test-internal-storage";
Configuration configuration = new Configuration(ClientId, ClientSecret);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment