How to find corrupted PDF files in C# easily
Tags: csharp, dotnet, productivity
Published: 2019-10-03 08:43:15

---
title: How to find corrupted PDF files in C# easily
published: true
date: 2019-10-01 11:00:44 UTC
tags: csharp, dotnet, productivity, 
canonical_url: https://www.syncfusion.com/blogs/post/how-to-find-corrupted-pdf-files-in-c-sharp.aspx
cover_image: https://thepracticaldev.s3.amazonaws.com/i/2v2elxzfe29hsvxd3lih.png
---

You might have a lot of PDF files on your disc or database; to automate or process the PDF files, you need to find any corrupted files and take necessary actions. But it’s tedious for anyone to open every single file with a PDF reader to check whether it is corrupt or not.

To save effort and time, [Syncfusion PDF Library](https://www.syncfusion.com/pdf-framework/net/pdf-library) provides you support to identify corrupted PDF files using C# or VB.NET by checking the PDF format syntax from the [2019 Volume 3 beta release](https://www.syncfusion.com/forums/147704/essential-studio-2019-volume-3-beta-release-v17-3-0-9-is-available-for-download).

Let’s dive into the details of how to find corrupted PDF files:

- [**PdfDocumentAnalyzer**](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Base~Syncfusion.Pdf.Parsing.PdfDocumentAnalyzer.html) class can be used to find corrupted PDF files by analyzing the PDF document structure and syntax.
- [**AnalyzeSyntax()**](https://help.syncfusion.com/cr/cref_files/file-formats/Syncfusion.Pdf.Base~Syncfusion.Pdf.Parsing.PdfDocumentAnalyzer~AnalyzeSyntax.html) method of **PdfDocumentAnalyzer** class will initiate analysis of the PDF document structure and syntax and return the result (an instance of [**SyntaxAnalyzerResult**](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Base~Syncfusion.Pdf.Parsing.SyntaxAnalyzerResult.html)).
- [**IsCorrupted**](https://help.syncfusion.com/cr/cref_files/file-formats/Syncfusion.Pdf.Base~Syncfusion.Pdf.Parsing.SyntaxAnalyzerResult~IsCorrupted.html) property of **SyntaxAnalyzerResult** is used to identify whether the processed PDF file is corrupted or not.

Using these APIs, you can ensure that a PDF document is not corrupted and start processing it.

For example:

- To avoid uploading any corrupted PDF report or resume to your web applications.
- To avoid unexpected behavior or application hanging when invoking PDF print programmatically.

The following C# code example will check whether the given PDF file is corrupted or not.

```csharp
static void Main(string[] args)
{
   //Load the PDF file as stream.
   using (FileStream pdfStream = new FileStream(“inputFile.pdf", FileMode.Open, FileAccess.Read))
   {
        //Create a new instance of PDF document syntax analyzer.
        PdfDocumentAnalyzer analyzer = new PdfDocumentAnalyzer(pdfStream);
        //Analyze the syntax and return the results.
        SyntaxAnalyzerResult analyzerResult = analyzer.AnalyzeSyntax();
 
        //Check whether the document is corrupted or not.
        if (analyzerResult.IsCorrupted)
        {
            StringBuilder strBuilder = new StringBuilder();
            strBuilder.AppendLine("The PDF document is corrupted.");
            int count = 1;
            foreach (PdfException exception in analyzerResult.Errors)
            {
                strBuilder.AppendLine(count++.ToString() + ": " + exception.Message);
            }
            Console.WriteLine(strBuilder);
        }
        else
        {
            Console.WriteLine("No syntax error found in the provided PDF document");
        }
        analyzer.Close();
    }   
}
```

## Open and repair PDF file

Syncfusion PDF Library can repair basic cross-reference offset issues in PDF files and open them for further processing. This is done using the overloads of [**PdfLoadedDocument constructors**](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Base~Syncfusion.Pdf.Parsing.PdfLoadedDocument~_ctor.html) with **openAndRepair** parameters.

The following code example will repair the basic cross-reference offset issues and open the PDF document.

```csharp
static void Main(string[] args)
{
    using (FileStream pdfStream = new FileStream(@"input.pdf", FileMode.Open, FileAccess.Read))
    {
        //load the corrupted document by setting the openAndRepair flag to true to repair the document.
        PdfLoadedDocument loadedPdfDocument = new PdfLoadedDocument(pdfStream, true);
 
        //Do PDF processing.
 
        //Save the document.
        using (FileStream outputStream = new FileStream(@"result.pdf", FileMode.Create))
        {
            loadedPdfDocument.Save(outputStream);
        }
        //Close the document.
        loadedPdfDocument.Close(true);
    }
}
```

**Note:** It cannot repair complex document corruption.

You can use these PDF corruption validation and repair APIs in .NET Framework, .NET Core, UWP, and Xamarin applications.

## GitHub Sample

You can download the samples to check for the corrupted PDF files and repair the PDF file from this [location](https://github.com/SyncfusionExamples/find-corrupted-pdf-files-csharp-vb-net).

## Conclusion

As you can see, [Syncfusion PDF Library](https://www.syncfusion.com/pdf-framework/net/pdf-library) provides APIs to find out whether a PDF file is corrupt or not by analyzing its structure and syntax. It also provides APIs to repair basic cross-reference offset-level corruption in PDF files. You can use these to avoid unexpected behavior while processing the PDF files in your .NET applications.

If you are new to our PDF Library, we highly recommend you to follow our [Getting Started guide](https://help.syncfusion.com/file-formats/pdf/getting-started).

If you’re already a Syncfusion user, you can download the product setup [here](https://www.syncfusion.com/account/downloads/studio/licensed/17_3_0_9). Otherwise, you can download a free, 30-day trial [here](https://www.syncfusion.com/downloads/fileformats).

Have any questions or require clarification about these features? Please let us know in the comments below. You can also contact us through our [support forum](https://www.syncfusion.com/forums), [Direct-Trac](https://www.syncfusion.com/support/directtrac/) or [feedback portal](https://www.syncfusion.com/feedback/pdf). We are happy to assist you!

If you liked this blog post, we think you’ll also enjoy the following related blog posts:

- [HTML to PDF Conversion Using ASP.NET Core in Linux Docker](https://www.syncfusion.com/blogs/post/html-to-pdf-conversion-using-asp-net-core-in-linux-docker.aspx)
- [7 Ways to Compress PDF Files in C#, VB.NET](https://www.syncfusion.com/blogs/post/7-ways-to-compress-pdf-files.aspx)
- [Convert Images to PDF in .NET Core](https://www.syncfusion.com/blogs/post/how-to-convert-image-to-pdf-in-asp-net-core.aspx)
- [Convert Word to PDF in .NET Core and Xamarin](https://www.syncfusion.com/blogs/post/word-to-pdf-conversion-in-xamarin-and-net-core.aspx)
- [Optical Character Recognition in PDF Using Tesseract Open-Source Engine](https://www.syncfusion.com/blogs/post/optical-character-recognition-in-pdf-using-tesseract-open-source-engine.aspx)

The post [How to detect corrupted PDF files in C# easily](https://www.syncfusion.com/blogs/post/how-to-find-corrupted-pdf-files-in-c-sharp.aspx) appeared first on [Syncfusion Blogs](https://www.syncfusion.com/blogs).