|
1 | | -# How to find the corrupted PDF files using Syncfusion PDF Library |
| 1 | +# Easy way to find the corrupted PDF files in C# |
2 | 2 |
|
3 | | -Syncfusion PDF Library provides support to find the existing PDF document corruptions and provides the corruption details |
| 3 | +You might have a lot of PDF files in your disc or database; you need to find out the corrupted files and take necessary actions. But it is not possible for you to open every single file with a PDF reader to check whether it is corrupted or not. |
4 | 4 |
|
5 | | -## Finding the corrupted PDF document |
| 5 | +To save your effort and time, [Syncfusion PDF library](https://www.syncfusion.com/pdf-framework/net/pdf-library) provides you the support to identify the corrupted PDF files using C#, VB.NET by checking whether the PDF format syntax are proper. |
6 | 6 |
|
7 | | -Install the [Syncfusion.Pdf.Net.Core](https://www.nuget.org/packages/Syncfusion.Pdf.Net.Core/) NuGet package as a reference to your .NET Core applications from [NuGet.org](https://www.nuget.org/). |
| 7 | +Let’s dive into the details about how to find the corrupted PDF files. |
8 | 8 |
|
9 | | -The following namespace should be included in the application: |
| 9 | +* [PdfDocumentAnalyzer](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Base~Syncfusion.Pdf.Parsing.PdfDocumentAnalyzer.html) class is used to find the corrupted PDF files by analyzing the PDF document structure and syntax. |
| 10 | +* [AnalyzeSyntax()](https://help.syncfusion.com/cr/cref_files/file-formats/Syncfusion.Pdf.Base~Syncfusion.Pdf.Parsing.PdfDocumentAnalyzer~AnalyzeSyntax.html) method of PdfDocumentAnalyzer class will invoke analysis of the PDF document structure and syntax and returns the result (an instance of [SyntaxAnalyzerResult](https://help.syncfusion.com/cr/file-formats/Syncfusion.Pdf.Base~Syncfusion.Pdf.Parsing.SyntaxAnalyzerResult.html)). |
| 11 | +* [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. |
10 | 12 |
|
11 | | -```C# |
12 | | -using Syncfusion.Pdf; |
13 | | -using Syncfusion.Pdf.Parsing; |
14 | | -``` |
| 13 | +Using these APIs, you can ensure that the PDF document is not corrupted and then start processing it. |
15 | 14 |
|
| 15 | +For example: |
16 | 16 |
|
17 | | -The following code snippet explains how to find the corrupted PDF document. |
| 17 | +1. To avoid uploading corrupted any PDF report or resume to your web applications. |
| 18 | +1. To avoid unexpected behavior or hanging when invoking PDF print programmatically. |
| 19 | + |
| 20 | +The following code example will check whether the given PDF file is corrupted or not. |
18 | 21 |
|
19 | 22 | ```C# |
20 | | -using (FileStream pdfStream = new FileStream(@"..\..\..\PDF-Files\barcode.pdf", FileMode.Open, FileAccess.Read)) |
21 | | -{ |
22 | | - //Create a new instance of PDF document syntax analyzer. |
23 | | - PdfDocumentAnalyzer analyzer = new PdfDocumentAnalyzer(pdfStream); |
24 | | - //Analyze the syntax and return the results |
25 | | - SyntaxAnalyzerResult analyzerResult = analyzer.AnalyzeSyntax(); |
| 23 | +using Syncfusion.Pdf; |
| 24 | +using Syncfusion.Pdf.Parsing; |
| 25 | +using System; |
| 26 | +using System.IO; |
| 27 | +using System.Text; |
26 | 28 |
|
27 | | - //Check whether the document is corrupted or not |
28 | | - if (analyzerResult.IsCorrupted) |
| 29 | +namespace find_corrupted_pdf_file_demo |
| 30 | +{ |
| 31 | + class Program |
29 | 32 | { |
30 | | - StringBuilder strBuilder = new StringBuilder(); |
31 | | - strBuilder.AppendLine("The PDF document is corrupted."); |
32 | | - int count = 1; |
33 | | - foreach (PdfException exception in analyzerResult.Errors) |
| 33 | + static void Main(string[] args) |
34 | 34 | { |
35 | | - strBuilder.AppendLine(count++.ToString() + ": " + exception.Message); |
| 35 | + //Load the PDF file as stream |
| 36 | + using (FileStream pdfStream = new FileStream(“inputFile.pdf", FileMode.Open, FileAccess.Read)) |
| 37 | + { |
| 38 | + //Create a new instance of PDF document syntax analyzer. |
| 39 | + PdfDocumentAnalyzer analyzer = new PdfDocumentAnalyzer(pdfStream); |
| 40 | + //Analyze the syntax and return the results |
| 41 | + SyntaxAnalyzerResult analyzerResult = analyzer.AnalyzeSyntax(); |
| 42 | + |
| 43 | + //Check whether the document is corrupted or not |
| 44 | + if (analyzerResult.IsCorrupted) |
| 45 | + { |
| 46 | + StringBuilder strBuilder = new StringBuilder(); |
| 47 | + strBuilder.AppendLine("The PDF document is corrupted."); |
| 48 | + int count = 1; |
| 49 | + foreach (PdfException exception in analyzerResult.Errors) |
| 50 | + { |
| 51 | + strBuilder.AppendLine(count++.ToString() + ": " + exception.Message); |
| 52 | + } |
| 53 | + Console.WriteLine(strBuilder); |
| 54 | + } |
| 55 | + else |
| 56 | + { |
| 57 | + Console.WriteLine("No syntax error found in the provided PDF document"); |
| 58 | + } |
| 59 | + analyzer.Close(); |
| 60 | + } |
36 | 61 | } |
37 | | - Console.WriteLine(strBuilder); |
38 | | - } |
39 | | - else |
40 | | - { |
41 | | - Console.WriteLine("No syntax error found in the provided PDF document"); |
42 | 62 | } |
43 | | - analyzer.Close(); |
44 | | -} |
45 | | -``` |
| 63 | +} |
| 64 | + |
| 65 | +``` |
| 66 | + |
| 67 | + |
0 commit comments