Skip to content

This repo contains the examples about how to find the corrupted PDF files using Syncfusion's C# PDF library.

Notifications You must be signed in to change notification settings

SyncfusionExamples/find-corrupted-pdf-files-csharp-vb-net

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Easy way to find the corrupted PDF files in C#

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.

To save your effort and time, Syncfusion® 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.

Let’s dive into the details about how to find the corrupted PDF files.

  • PdfDocumentAnalyzer class is used to find the corrupted PDF files by analyzing the PDF document structure and syntax.
  • AnalyzeSyntax() method of PdfDocumentAnalyzer class will invoke analysis of the PDF document structure and syntax and returns the result (an instance of SyntaxAnalyzerResult).
  • IsCorrupted property of SyntaxAnalyzerResult is used to identify whether the processed PDF file is corrupted or not.

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

For example:

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

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

using Syncfusion.Pdf;
using Syncfusion.Pdf.Parsing;
using System;
using System.IO;
using System.Text;

namespace find_corrupted_pdf_file_demo
{
    class Program
    {
        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();
            }   
        }
    }
}

About

This repo contains the examples about how to find the corrupted PDF files using Syncfusion's C# PDF library.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published