Articles in this section
Category / Section

Convert Searchable PDFs to Non-Searchable PDFs in .NET Core

3 mins read

The Syncfusion Essential® PDF is a .NET PDF library used to create, read, and edit PDF documents. Using this library, you can convert searchable or selectable PDF documents into non-searchable formats by rendering them as images, thus disabling text selection and search functionality. This step-by-step guide illustrates how to perform this conversion using C#.

Steps to convert PDF to non-searchable format

  1. Create a New Project: Start a new console application project to facilitate the conversion process.
    Screenshot (1337).png
  2. Install Necessary Packages: Add Syncfusion.Pdf.Imaging.Net.Core and Syncfusion.PdfToImageConverter.Net NuGet packages to your project from Nuget.org.
    ImgNuget.png

    Img1Nuget.png
  3. Include Required Namespaces: Open Program.cs and ensure the following namespaces are included.
    C#
using Syncfusion.Pdf.Graphics;
using Syncfusion.Pdf;
using Syncfusion.PdfToImageConverter;
  1. Implement Conversion Logic: Use this code snippet in Program.cs to convert searchable PDFs to non-searchable by creating image-based PDFs.
    C#
// Instantiate the PdfToImageConverter.
PdfToImageConverter imageConverter = new PdfToImageConverter();

// Load a PDF document as a stream.
FileStream inputStream = new FileStream(@"../../../Input.pdf", FileMode.Open, FileAccess.ReadWrite);
imageConverter.Load(inputStream);

// Convert PDF pages to images.
Stream[] outputStream = imageConverter.Convert(0, imageConverter.PageCount - 1, false, false);
// Create a new PDF document.
PdfDocument document = new PdfDocument();

// Loop through each item in the outputStream array.
for (int i = 0; i < outputStream.Length; i++)
{
   // Create a PdfTiffImage object from the current outputStream item.
   PdfTiffImage image = new PdfTiffImage(outputStream[i]);
   // Add a new section to the document.
   PdfSection section = document.Sections.Add();
   // Set the size of the page in the section to match the physical dimensions of the image.
   section.PageSettings.Size = image.PhysicalDimension;
   // Set the margins of the page in the section to 0.
   section.PageSettings.Margins.All = 0;
   // Add a new page to the section.
   PdfPage page = section.Pages.Add();
   // Get the graphics context of the page
   PdfGraphics graphics = page.Graphics;
   // Draw the image on the page.
   graphics.DrawImage(image, 0, 0, graphics.ClientSize.Width, graphics.ClientSize.Height);
}

// Save the document to a memory stream.
MemoryStream memoryStream = new MemoryStream();
document.Save(memoryStream);

// Write the content of the memory stream to an output PDF file.
File.WriteAllBytes("Output.pdf", memoryStream.ToArray());

// Close the document.
document.Close(true);

A complete working sample can be downloaded from Convert_PDF_content.zip.

By executing the program, the output PDF document will be generated as shown below.

Screenshot (1401).png
Screenshot (1399).png

Take a moment to peruse the documentation for working with pages. You can find options like inserting, rotating, importing, and rearranging the pages from an existing document, removing the pages, and splitting a PDF file into individual pages.

Conclusion

I hope you enjoyed learning about how to convert searchable PDFs to non-searchable PDFs

You can refer to our ASP.NET Core PDF feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our ASP.NET Core PDF example to understand how to create and manipulate data.

For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion;, you can try our 30-day free trial to check out our other controls.

If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please  to leave a comment
Access denied
Access denied