Convert PDF to 1-bit black and white PNG in C# and .NET
In today's digital world, working with PDF files has become an integral part of business, education, and personal use. Sometimes it's necessary to
convert a PDF document to an image with a minimal color palette, such as a black-and-white PNG with one bit per pixel. These images are ideal for previewing,
archiving, emailing, or publishing on systems where file size is important.
In this article, we'll look at how to convert a PDF to a 1-bit black-and-white PNG image on the .NET platform using the
SautinSoft's SDK from PDF Focus .NET.
We'll also discuss the advantages of this approach, situations in which it's especially useful, and implementation considerations.
The main advantages of converting a PDF to a 1-bit PNG are:
- Small file size: A black-and-white image takes up less space than a full-color image, saving disk space and data transfer costs.
- Legacy hardware support: For devices and systems that work better with monochrome images.
- Optimization for OCR: In some cases, especially when preparing documents for optical character recognition (OCR), a black-and-white image produces better results.
- Improved readability: Black-and-white images can appear sharper in print or when viewed on certain devices.
Despite the existence of built-in PDF-to-image conversion tools, using component PDF Focus from SautinSoft API provides stable and predictable results, especially when automating the processing of large volumes of documents. Minimizing color to 1 bit significantly reduces the size of the final files, which is important for systems with limited resources or when mass archiving is required. This approach is also suitable for the preparatory stages of OCR processing, where a black-and-white image provides better conditions for text recognition.
Interesting aspects and recommendations:
- To improve the quality of a black-and-white image with minimal loss, dithering algorithms can be used.
- If necessary, automate the process for batch processing of large numbers of PDF files.
- In some cases, it may be beneficial to set a higher brightness threshold to produce higher-contrast images.
- Before using the library, please review the license and capabilities of the free version if it suits you.
Complete code
using System;
using System.IO;
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("...");
//How to convert PDF to 1-bit black and white PNG
SautinSoft.PdfFocus f = new SautinSoft.PdfFocus();
string pdfPath = Path.GetFullPath(@"..\..\..\Potato Beetle.pdf");
string imagePath = "Result.png";
f.OpenPdf(pdfPath);
if (f.PageCount > 0)
{
//save 1st page to png file, 300 dpi
f.ImageOptions.ImageFormat = SautinSoft.PdfFocus.CImageOptions.ImageFormats.Png;
f.ImageOptions.Dpi = 300;
//Make "Black and White 1-bit indexed" image
f.ImageOptions.ColorDepth = SautinSoft.PdfFocus.CImageOptions.eColorDepth.BlackWhite1bpp;
f.ImageOptions.SelectedPages = new int[] { 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
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("...");
'How to convert PDF to 1-bit black and white PNG
Dim f As New SautinSoft.PdfFocus()
Dim pdfPath As String = Path.GetFullPath("..\..\..\Potato Beetle.pdf")
Dim imagePath As String = "Result.png"
f.OpenPdf(pdfPath)
If f.PageCount > 0 Then
'save 1st page to png file, 300 dpi
f.ImageOptions.ImageFormat = SautinSoft.PdfFocus.CImageOptions.ImageFormats.Png
f.ImageOptions.Dpi = 300
'Make "Black and White 1-bit indexed" image
f.ImageOptions.ColorDepth = SautinSoft.PdfFocus.CImageOptions.eColorDepth.BlackWhite1bpp
f.ImageOptions.SelectedPages = New Integer() { 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: