Set custom dpi in C# and .NET
In modern programming, working with PDF documents is an integral part of many solutions—whether it's document automation, report generation, or integration with other systems. One frequently asked question when working with PDF is how to control the dot density (DPI, or dots per inch), which affects document quality and scalability. In this article, we'll look at how to set a custom DPI in PDF using the PDF Focus .NET from SautinSoft library. We'll also discuss the advantages of this approach, how to use it effectively, and why you might want to do so.
DPI (dots per inch) is a measure of dot density that determines the detail of an image or document when printed or rendered. The higher the DPI, the sharper and more detailed the image will be. In the context of PDF, changing the DPI affects the quality of the document during conversion and display, as well as the file size.
For example, when converting images or scans to PDF, it's important to set the correct DPI to maintain quality or reduce it for a smaller file size.
Managing DPI through code enables automation of processes and ensures uniform standardization, especially when processing documents in bulk.
Setting a custom DPI allows you to:
- Ensure the desired image quality in PDFs.
- Optimize file size.
- Maintain selected printing or display standards.
- Create documents that meet specific client or project requirements.
Important information:
- In reality, changing the DPI when working with vector PDFs is not always necessary, as the vector format scales without loss of quality.
- For images (for example, when scanning), it is important to set the DPI at the scanning stage or process the image before conversion.
- If you are working with finished PDFs, changing the DPI is more of a preview or printing issue than a PDF generation issue.
Use Rate and Prospects.
Setting a custom DPI when working with PDFs is not the most common scenario, as PDFs are vector formats, and for most purposes, quality scales
without changing the DPI. However, when working with raster images, scans, or preparing documents for printing, this issue becomes critical.
This setting is widely used in design, printing, document archiving, and automated print production.
Complete code
using System;
using System.IO;
using System.Collections;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
// Before starting, we recommend to get a free key:
// https://sautinsoft.com/start-for-free/
// Apply the key here:
// SautinSoft.PdfFocus.SetLicense("...");
string pdfPath = Path.GetFullPath(@"..\..\..\Excel.pdf");
string imagePath = "Result.png";
SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
f.OpenPdf(pdfPath);
if (f.PageCount > 0)
{
//In most cases we recommend to set 200 dpi to decrease the image size and converting speed
//Now set 300 dpi - very high quality
f.ImageOptions.Dpi = 300;
//Convert 1st page from PDF to image file
f.ImageOptions.PageIndex = 0;
if (f.ToImage(imagePath) == 0)
{
// 0 - converting successfully
// 2 - can't create output file, check the output path
// 3 - converting failed
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(imagePath) { UseShellExecute = true });
}
}
}
}
}
Imports System
Imports System.IO
Imports System.Collections
Namespace Sample
Friend Class Sample
Shared Sub Main(ByVal args() As String)
' Before starting, we recommend to get a free key:
' https://sautinsoft.com/start-for-free/
' Apply the key here:
' SautinSoft.PdfFocus.SetLicense("...");
Dim pdfPath As String = Path.GetFullPath("..\..\..\Excel.pdf")
Dim imagePath As String = "Result.png"
Dim f As New SautinSoft.PdfFocus()
f.OpenPdf(pdfPath)
If f.PageCount > 0 Then
'In most cases we recommend to set 200 dpi to decrease the image size and converting speed
'Now set 300 dpi - very high quality
f.ImageOptions.Dpi = 300
'Convert 1st page from PDF to image file
f.ImageOptions.PageIndex = 0
If f.ToImage(imagePath) = 0 Then
' 0 - converting successfully
' 2 - can't create output file, check the output path
' 3 - converting failed
System.Diagnostics.Process.Start(New System.Diagnostics.ProcessStartInfo(imagePath) With {.UseShellExecute = True})
End If
End If
End Sub
End Class
End Namespace
If you need a new code example or have a question: email us at support@sautinsoft.com or ask at Online Chat (right-bottom corner of this page) or use the Form below: