AVAILABLE NOW: Spring 2025 Release
By Apryse | 2025 Apr 18
6 min
Tags
Sometimes, developers can get images that have been copied to the clipboard using .NET’s Clipboard Class and they need an efficient way to extract the text. This post demonstrates how to use Apryse OCR to save screenshots as searchable PDFs.
For this task, you’ll need the Apryse SDK and the Apryse OCR Module. Here is the code to convert a screenshot to PDF using C#:
class Program
{
public static string imageOutputPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"output/output.jpg");
[STAThread]
public static void Main(string[] args)
{
Directory.CreateDirectory(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "output"));
PDFNet.Initialize("place your license key here");
PDFNet.AddResourceSearchPath("../../../../../Lib/");
if (!OCRModule.IsModuleAvailable())
{
Console.WriteLine("");
Console.WriteLine("Unable to run OCRTest: Apryse SDK OCR module not available.");
Console.WriteLine("---------------------------------------------------------------");
Console.WriteLine("The OCR module is an optional add-on, available for download");
Console.WriteLine("at http://www.pdftron.com/. If you have already downloaded this");
Console.WriteLine("module, ensure that the SDK is able to find the required files");
Console.WriteLine("using the PDFNet.AddResourceSearchPath() function.");
Console.WriteLine("");
return;
}
ImageToPdf();
}
public static void ImageToPdf()
{
try
{
GetClipboardImage();
using (PDFDoc doc = new PDFDoc())
{
OCRModule.ImageToPDF(doc, imageOutputPath, null);
doc.Save(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "output/output.pdf"), pdftron.SDF.SDFDoc.SaveOptions.e_linearized);
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
[System.Runtime.InteropServices.DllImport("user32.dll")]
static extern IntPtr GetOpenClipboardWindow();
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern bool OpenClipboard(IntPtr hWndNewOwner);
[System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
static extern bool CloseClipboard();
public static void GetClipboardImage()
{
//The Snipping tool/screen capture tool holds onto the Windows clipboard service, which forces us to do the below in order to access the image in the clipboard
IntPtr hWnd = GetOpenClipboardWindow();
OpenClipboard(IntPtr.Zero);
CloseClipboard();
System.Drawing.Image image = null;
if (Clipboard.ContainsImage())
{
image = Clipboard.GetImage();
}
image.Save(imageOutputPath);
}
}
Download the Apryse SDK for free. It’s fully functional and comes with free chat and email support.
Stay tuned for more conversion examples to see how Apryse easily fits into any workflow converting PDF files into other document files or images and back again. Need help in the meantime? Contact our support team for free technical support! For pricing or licensing questions, you can contact our sales team.
Tags
Apryse
Share this post
PRODUCTS
Platform Integrations
End User Applications
Popular Content