Set output image size limits when rendering single image to PDF/HTML.

GroupDocs.Viewer also provides the feature to set limits for width/height for the output image. Follow the below steps to achieve this functionality. If you want to render single image in PDF/HTML you can set width/height for the output image. If you set ImageMaxWidth/ImageMaxHeight options, if the image exceeds one of these limits - it will be resized proportionally.

  • Instantiate the Viewer object;

  • Instantiate the  PdfViewOptions or HtmlViewOptions;

  • Set ImageMaxWidth and/or ImageMaxHeight values;

  • Call View method.

  • The following code sample shows how to set the output image size limits when rendering the document.

using (Viewer viewer = new Viewer("sample.jpg"))
{
    PdfViewOptions viewOptions = new PdfViewOptions("result.pdf");
    //HtmlViewOptions viewOptions = new HtmlViewOptions.ForEmbeddedResources("result_{0}.html");
    //HtmlViewOptions viewOptions = new HtmlViewOptions.ForExternalResources("page_{0}.html", "page_{0}_{1}", "page_{0}_{1}");
    
    viewOptions.ImageMaxWidth = 800;
    viewOptions.ImageMaxHeight = 600;

    viewer.View(viewOptions);
}

In PdfViewOptions and HtmlViewOptions have following properties to set single image width/height in HTML.

 /// <summary>
/// Max width of an output image in pixels. (When converting single image to HTML only)
/// </summary>
public int ImageMaxWidth { get; set; }

/// <summary>
/// Max height of an output image in pixels. (When converting single image to HTML only)
/// </summary>
public int ImageMaxHeight { get; set; }

/// <summary>
/// The width of the output image in pixels. (When converting single image to HTML only)
/// </summary>
public int ImageWidth { get; set; }

/// <summary>
/// The height of an output image in pixels. (When converting single image to HTML only)
/// </summary>
public int ImageHeight { get; set; }

Note: If you set Width/Height in options, MaxWidth/MaxHeight options will be ignored.

If you want to render single image to JPG/PNG, please refer to the following article.

More resources

GitHub Examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free Online App

Along with full-featured .NET library we provide simple but powerful free Apps. You are welcome to view Word, PDF, Excel, PowerPoint documents with free to use online GroupDocs Viewer App.