Articles in this section
Category / Section

How to avoid DPI changes while converting the Excel/Presentation/Word documents with chart into PDF

3 mins read

Converting Excel with charts to PDF will change the DPI. In this article, you can see different ways to avoid such DPI changes.

To convert the chart from Excel ,Word and Presentation files to image/PDF, the dependent assemblies ExcelChartToImageConverter.WPF and OfficeChartToImageConverter.WPF are required. These assemblies use WPF graphical system and by default WPF applications are system DPI-aware. So, DPI will be scaled automatically when running the application with these assemblies. Due to these DPI scaling the chart images are not rendered proper. For example: the chart images might get cropped in the PDF document on PDF conversion.

In this MSDN link , DPI awareness setting details for the application has been shared. Below, we have shared the set of ways available to disable DPI awareness in the application.

Note:

It is recommended to set in the applications, where the device DPI is not set to 96.

 

#1: Adding an attribute in the AssemblyInfo.cs

Attribute ’DisableDpiAwareness’ must be added in AssemblyInfo class file. This is declared in the namespace ‘System.Windows.Media’ and should be used only when the application has the ‘WindowsBase’ assembly reference. Otherwise, options #2 or #3 can be used.

C#

//In AssemblyInfo.cs
[assembly: System.Windows.Media.DisableDpiAwareness]

 

#2: Adding an application manifest file and set dpiAware value to false.

If application project does not contain the ‘app.manifest’ file, it can be added in the project by adding the new item ‘Application Manifest file’. By default, the given element (i.e. dpiAware) present in the manifest file is commented. Uncomment the element and set dpiAware value to false.

 

XML

//In app.manifest
<?xml version="1.0" encoding="utf-8"?>
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1">
  <!-- Indicates that the application is DPI-aware and will not be automatically scaled by Windows at higher DPIs. Windows Presentation Foundation (WPF) applications are automatically DPI-aware and do not need to opt in. 
 
 Windows Forms applications targeting .NET Framework 4.6 that opt into this setting, should also set the 'EnableWindowsFormsHighDpiAutoResizing' setting to 'true' in their app.config. -->
  
  <application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
      <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">false</dpiAware>
    </windowsSettings>
  </application>
</assembly>

 

Note:

If the DPI issue still persist in the application, then option #3 can be used.

 

#3: Calling SetProcessDPIAware() method in entry point of the application.

The following steps are required to call the method SetProcessDPIAware().

Step #1: Declare the function ‘SetProcessDPIAware()’ from the ‘user32.dll’ using DllImport attribute. The code example to set this is given below.

 C#

 
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool SetProcessDPIAware();
 

 

Step #2: invoke the method ‘SetProcessDPIAware()’ in the entry point of the application. The entry point of the applications is different, based on the project types. Mostly,

  • the Console and Desktop application has the entry point as ‘static void Main(string[] args)’
  • web application has the entry point as ‘protected void Application_Start()’.

Invoke the method as in following code example.

C#

     //In Main Method (Console or Desktop application)
     static void Main(string[] args)
     {
       //Call the SetProcessDPIAware() method at initial line
       if (Environment.OSVersion.Version.Major >= 6)
            SetProcessDPIAware();
       //...Other Codes
      } 

 

C#

     //In Application_Start Method (Web application)
     protected void Application_Start()
     {
       //Call the SetProcessDPIAware() method at initial line
       if (Environment.OSVersion.Version.Major >= 6)
            SetProcessDPIAware();
       //...Other Codes
      } 

 

Using any of these options will help you avoid DPI changes while converting Excel, Word, and PowerPoint files with chart into image or PDF.

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied