How to find corrupted PDF document using C# and VB.NET in WinForms?
The Syncfusion Essential PDF is a feature rich and high-performance .NET PDF library used to create, read, and edit PDF documents programmatically without Adobe dependencies. This library also offers functionality to merge, split, stamp, forms, compress, and secure PDF files.
Using this library, you can check whether the existing PDF document is corrupted or not with corruption details.
Steps to find corrupted PDF document programmatically in C#:
- Create a new C# console application project.
- Install the Syncfusion.Pdf.WinForms NuGet package as a reference to your .NET Framework application from NuGet.org.
- Include the following namespaces in the Program.cs file.
C#
using Syncfusion.Pdf; using Syncfusion.Pdf.Parsing;
VB.NET
Imports Syncfusion.Pdf Imports Syncfusion.Pdf.Parsing
- You can get the syntax error using the AnalyzeSyntax method available in PdfDocumentAnalyzer
class. The following code snippet explains how to find the corrupted PDF document.
C#
//Create a new instance for the PDF analyzer PdfDocumentAnalyzer analyzer = new PdfDocumentAnalyzer("Input.pdf"); //Get the syntax errors SyntaxAnalyzerResult result = analyzer.AnalyzeSyntax(); //Check whether the document is corrupted or not if (result.IsCorrupted) { //Get syntax error details from results.error StringBuilder builder = new StringBuilder(); builder.AppendLine("The PDF document is corrupted."); int count = 1; foreach (PdfException exception in result.Errors) { builder.AppendLine(count++.ToString() + ": " + exception.Message); } } else { //No syntax error found in the provided PDF document } analyzer.Close();
VB.NET
'Create a new instance for the PDF analyzer Dim analyzer As PdfDocumentAnalyzer = New PdfDocumentAnalyzer("Input.pdf") 'Get the syntax errors Dim result As SyntaxAnalyzerResult = analyzer.AnalyzeSyntax 'Check whether the document is corrupted or not If result.IsCorrupted Then 'Get syntax error details from results.error Dim builder As StringBuilder = New StringBuilder() builder.AppendLine("The PDF document is corrupted.") Dim count = 1 For Each exception As PdfException In result.Errors builder.AppendLine(Math.Min(Threading.Interlocked.Increment(count), count - 1).ToString & ": " & exception.Message) Next Else 'No syntax error found in the provided PDF document. End If analyzer.Close()
A complete working sample can be downloaded from CorruptionSample.zip.
By executing the program, you will get the corruption detail as follows.
Take a moment to peruse the documentation, where you will find other options like identify the corrupted pdf document ,opening a corrupted pdf document.
Click here to explore the rich set of Syncfusion Essential PDF features.
See Also:
Load the corrupted pdf document
Starting with v16.2.0.x, if you reference Syncfusion assemblies from trial setup or from the NuGet feed, include a license key in your projects. Refer to link to learn about generating and registering Syncfusion license key in your application to use the components without trail message.
Conclusion
I hope you enjoyed learning about how to find corrupted PDF document using C# and VB.NET in WinForms.
You can refer to our WinForms PDF feature tour page to know about its other groundbreaking feature representations and documentation, 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!