The Portable Document Format (PDF) is a file format to present documents, including text formatting and images, in a manner independent of the application software, hardware, and operating systems. Based on the PostScript language, each PDF file encapsulates a complete description of a fixed-layout flat document, including the text, fonts, vector graphics, raster images and other information needed to display it.

This post demonstrates the usage of the GroupDocs.Viewer APIs (GroupDocs.Viewer for .NET and GroupDocs.Viewer for Java) to render the supported file formats as PDF documents.

How to Render Using GroupDocs.Viewer

Both the viewer handlers (ViewerHtmlHandler,ViewerImageHandler) provide an overloaded method GetPdfFile() which accepts the document name as parameter and returns FileContainer. The File stream can be fetched from the Stream property of the FileContainer in order to save the resultant document on the disk.

The following code snippet shows the way to render a word document as PDF:

// For complete examples and data files, please go to https://github.com/groupdocs-viewer/GroupDocs.Viewer-for-.NET
// Create/initialize image handler
ViewerImageHandler imageHandler = new ViewerImageHandler(Utilities.GetConfigurations());
string DocumentName = "sample.docx";
// To Apply transformations on PDF file
// options.Transformations = Transformation.Rotate | Transformation.Reorder | Transformation.AddPrintAction;
// Call GetPdfFile to get FileContainer type object which contains the stream of pdf file.
FileContainer container = imageHandler.GetPdfFile(DocumentName);
//Change the extension of the file and assign to a string type variable filename
String filename = Path.GetFileNameWithoutExtension(DocumentName) + ".pdf";
//Save each image at disk
Utilities.SaveFile(filename, container.Stream);
view raw RenderAsPdf.cs hosted with ❤ by GitHub

Java lovers can write the code like following:

// For complete examples and data files, please go to https://github.com/groupdocs-viewer/GroupDocs.Viewer-for-Java
try {
// Setup GroupDocs.Viewer config
ViewerConfig config = Utilities.getConfiguration();
// Create image handler
ViewerImageHandler imageHandler = new ViewerImageHandler(config);
PdfFileOptions options = new PdfFileOptions();
options.setTransformations(Transformation.AddPrintAction);
// Get file as pdf
FileContainer container = imageHandler.getPdfFile(fileName, options);
// Save file
Utilities.saveAsFile(fileName, container.getStream(), ".pdf");
} catch (Exception exp) {
System.out.println("Exception: " + exp.getMessage());
exp.printStackTrace();
}

Add Print Action while Rendering as PDF

A Print Action is an option to appear the print popup when open the PDF document in Adobe reader. The popup will appear like below screenshot:

The below given steps can be followed, in order to add print action in the resultant PDF document.

  • Create/initialize PdfFileOptions object
  • Set Transformations property of PdfFileOptions to Transformation.AddPrintAction
  • Call htmlHandler.GetPdfFile method

The .NET code snippet will look like:

// For complete examples and data files, please go to https://github.com/groupdocs-viewer/GroupDocs.Viewer-for-.NET
// get Configurations
ViewerConfig config = Utilities.GetConfigurations();
// create html handler
ViewerHtmlHandler htmlHandler = new ViewerHtmlHandler(config);
// guid implies that unique document name
string guid = DocumentName;
PdfFileOptions options = new PdfFileOptions();
// set add print action property
options.Transformations = Transformation.AddPrintAction;
// get file as pdf with print action
FileContainer container = htmlHandler.GetPdfFile(guid, options);
// save PDF file
Utilities.SaveFile(guid, container.Stream);

The Java code can be written as:

// For complete examples and data files, please go to https://github.com/groupdocs-viewer/GroupDocs.Viewer-for-Java
try {
// Setup GroupDocs.Viewer config
ViewerConfig config = Utilities.getConfiguration();
// Create image handler
ViewerImageHandler imageHandler = new ViewerImageHandler(config);
// Set add print action property
PdfFileOptions options = new PdfFileOptions();
options.setTransformations(Transformation.AddPrintAction);
// Get file as pdf with print action
FileContainer container = imageHandler.getPdfFile(fileName, options);
Utilities.saveAsFile(fileName, container.getStream(), ".pdf");
} catch (Exception exp) {
System.out.println("Exception: " + exp.getMessage());
exp.printStackTrace();
}

The complete ready to run code sample is available on GitHub.

  • The .NET guys can visit here
  • The code sample for Java lovers is available here