How to custom the DPI when render .docx/.xlsx etc to PNG/JPG

Hi,

I found the DPI(resolution) of text is a little bit too low, How can I increase it?

Thanks & Regards
Aray

@ArayCN

To customize the DPI (dots per inch) when rendering documents such as .docx or .xlsx to PNG or JPG formats using GroupDocs.Viewer, you can specify rendering options that include the desired DPI settings. Here’s a general approach to achieve this:

  1. Define Output Directory: Set the directory where the rendered images will be saved.

  2. Specify Page File Path Format: Define how the file names for each rendered page will be formatted.

  3. Instantiate the Viewer Object: Create an instance of the Viewer class, providing the path to the document you want to render.

  4. Define Rendering Options: Use JpgViewOptions or PngViewOptions to set the rendering options. Within these options, you can specify the DPI. For example, you might set the Dpi property to a higher value to increase the resolution.

  5. Render the Document: Call the View method of the Viewer object, passing in the rendering options you defined.

  6. Output Results: After rendering, you can notify the user about the successful operation and provide the location of the saved images.

Here is a sample code snippet demonstrating how to set the DPI when rendering to PNG:

// Step 1: Define the output directory
String outputDirectory = "path/to/output/directory";

// Step 2: Create a Viewer instance
Viewer viewer = new Viewer("path/to/document.docx");

// Step 3: Define rendering options with custom DPI
PngViewOptions options = new PngViewOptions();
options.setDpi(300); // Set the desired DPI

// Step 4: Render the document
viewer.view(options, outputDirectory);

// Step 5: Clean up
viewer.close();

By adjusting the DPI value in the rendering options, you can control the resolution of the output images, ensuring they meet your quality requirements.

For more detailed information, you can refer to the official documentation on rendering documents to images using GroupDocs.Viewer.

If you have any further questions or need additional assistance, feel free to ask!

Hi @ArayCN

For the most accurate answer we need a little bit more info from you, because the answer on your question depends on what exact document format you want to process with the GroupDocs.Viewer. In general, there is no some single option, common for all formats, which is responsible for the DPI — every format family is unique and thus has its own options. Some format families allow to specify the DPI, while another — do not. But, basically, all four view options, including the JpgViewOptions and PngViewOptions (responsible for saving input document to the JPEG and PNG formats respectively) are inherited from the ViewOptions abstract class, which, in turn, is inherited from another BaseViewOptions abstract class. And the BaseViewOptions contains format-specific options, where for some formats the DPI can be found and specified.

In particular, for the Spreadsheet it is a PresentationOptions.Resolution property (more on this here), for PDF it is a PdfOptions.ImageQuality property (more on this here and here), and so on. In some cases, like for WordProcessing format family (DOC, DOCX, DOCM, RTF etc) in order to increase the output image quality you need to enlarge the dimensions of the output image files (see here). Also when output image format is JPEG, there is a JpgViewOptions.Quality property.

P.S. Regarding the answer from the Professionalize.Discourse bot, located above - please ignore it. The API, provided by the bot in its source code sample, does not exist, so those code will not compile at all.

With best regards,
Denis

Hi @Professionalize.Discourse

Thank you. But I did not find any DPI property in PngViewOptions & JpgViewOptions, including the setter setDpi.

I use Java version of groupdocs-viewer 24.8.

Thanks & Regards.
Aray