AVAILABLE NOW: Spring 2025 Release

Screenshot To PDF

By Apryse | 2025 Apr 18

Sanity Image
Read time

6 min

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#:

C# Code

 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);  

     }  

  }  

See For Yourself - Free Evaluation

Copied to clipboard

Download the Apryse SDK for free. It’s fully functional and comes with free chat and email support.

Stay Tuned for More Conversion Samples

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.

Sanity Image

Apryse

Share this post

email
linkedIn
twitter