DEV Community

Atir Tahir
Atir Tahir

Posted on

3 2

A Universal Document Conversion API for Java

You might be looking for a solution to add watermark or rotate pages while converting a document. GroupDocs.Conversion for Java API allows you to do so. It supports back and forth conversion of multitude of file formats. In this post we will see conversion to PDF and image formats. You must try open-source example project or if interested in a web application, visit this.
First of all lets see basic conversion code:

// For complete examples and data files, please go to https://github.com/groupdocs-conversion/GroupDocs.Conversion-for-Java
String storagePath = "C:/Storage";
String outputPath = "C:/ConvertedStorage";
// Setup Conversion configuration
ConversionConfig conversionConfig = new ConversionConfig();
conversionConfig.setStoragePath(storagePath);
conversionConfig.setOutputPath(outputPath);
// Instantiating the conversion handler
ConversionHandler conversionHandler = new ConversionHandler(conversionConfig);
PdfSaveOptions saveOption = new PdfSaveOptions();
// Set absolute path to file
String guid = "source file";
ConvertedDocument convertedDocumentPath = conversionHandler.<String> convert(guid, saveOption);
convertedDocumentPath.save(fileName + "." + convertedDocumentPath.getFileType());

And now lets explore PdfSaveOptions, following code elaborates how to add watermark.
// For complete examples and data files, please go to https://github.com/groupdocs-conversion/GroupDocs.Conversion-for-Java
PdfSaveOptions saveOptions = new PdfSaveOptions();
WatermarkOptions watermarkOptions = new WatermarkOptions();
watermarkOptions.setColor(Color.blue);
watermarkOptions.setText("Watermark text");
watermarkOptions.setFont(new Font("Arial", 40, 12));
watermarkOptions.setRotationAngle(45);
watermarkOptions.setTransparency(0.1);
watermarkOptions.setLeft(200);
watermarkOptions.setTop(400);
saveOptions.setWatermarkOptions(watermarkOptions);

And how to add page rotate option.
// For complete examples and data files, please go to https://github.com/groupdocs-conversion/GroupDocs.Conversion-for-Java
PdfSaveOptions saveOptions = new PdfSaveOptions();
saveOptions.setRotate(PdfSaveOptions.Rotation.On90);
view raw rotation.java hosted with ❤ by GitHub

This is how you do conversion to image format.
// For complete examples and data files, please go to https://github.com/groupdocs-conversion/GroupDocs.Conversion-for-Java
String storagePath = "C:/Storage";
String outputPath = "C:/ConvertedStorage";
// Setup Conversion configuration
ConversionConfig conversionConfig = new ConversionConfig();
conversionConfig.setStoragePath(storagePath);
conversionConfig.setOutputPath(outputPath);
// Instantiating the conversion handler
ConversionHandler conversionHandler = new ConversionHandler(conversionConfig);
SaveOptions saveOption = new ImageSaveOptions();
//ImageFileType enumeration contains the following members:
//BMP, GIF, ICO, JP2, JPEG, JPG, PNG, PSD, SVG, TIF, TIFF, WEBP
saveOption.setConvertFileType(ImageSaveOptions.ImageFileType.JPG);
ConvertedDocument convertedDocumentPath = conversionHandler.<List<String>>convert(fileName, saveOption);
convertedDocumentPath.save(fileName + "." + convertedDocumentPath.getFileType());

Support of rotation.
// For complete examples and data files, please go to https://github.com/groupdocs-conversion/GroupDocs.Conversion-for-Java
ImageSaveOptions options = new ImageSaveOptions();
options.setRotateAngle(45);

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)