How to avoid DPI changes while converting the Excel/Presentation/Word documents with chart into PDF
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 the WPF graphical system, and by default, WPF applications are system DPI-aware. So, the DPI will be scaled automatically when running the application with these assemblies. Due to this DPI scaling, the chart images are not rendered properly. For example, the chart images might get cropped in the PDF document during PDF conversion.
In this MSDN link , DPI awareness setting details for the application have been shared. Below, we have shared the set of ways available to disable DPI awareness in the application.
It is recommended to set this in applications, where the device DPI is not set to 96.
#1: Adding an attribute in the AssemblyInfo.cs
The attribute ’DisableDpiAwareness’ must be added in the 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 the application project does not contain the ‘app.manifest’ file, it can be added to 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 the 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>
If the DPI issue still persists in the application, then option #3 can be used.
#3: Calling the SetProcessDPIAware() method in the 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 the 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.
Take a moment to peruse the documentation where you can find basic Excel document processing options along with the features like import and export data, chart, formulas, conditional formatting, data validation, tables, pivot tables and protect the Excel documents, and most importantly, the PDF, CSV and Image conversions with code examples.
Conclusion
I hope you enjoyed learning about how to avoid DPI changes while converting the Excel/Presentation/Word documents with chart into PDF.
You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.
If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion® components.
If you have any queries or require clarification, please let us know in the comments below or contact us through our support forums, Support Tickets, or feedback portal. We are always happy to assist you!