How to Check if PDF is of PDF/A Type or not using C#

Which API in ASPOSE can be used to if PDF file is of PDF/A type of not

@sreenath.techie

You can use Document.Validate method to validate document into the specified file.

@sreenath.techie

The PdfFileInfo class represents a class for accessing meta information of PDF document. You can use PdfFileInfo.GetPdfVersion method to get the version info of PDF document as shown below.

Facades.PdfFileInfo fileInfo = new Facades.PdfFileInfo(pdfDocument);
fileInfo.GetPdfVersion();

You can also use Document.PdfFormat property to achieve your requirement.

Document doc = new Document("source.pdf");
var format = doc.PdfFormat;

Do I need to check like this and include every PDF_A types on OR condition? is there any simpler way to just check if PDF file is PDFA or Not?

if(pdfDocument.PdfFormat == PdfFormat.PDF_A_1A
|| pdfDocument.PdfFormat == PdfFormat.PDF_A_1B…
)
{

@sreenath.techie

Please find the PDF formats returned by Document.PdfFormat property from PdfFormat Enumeration. You can write your code accordingly.

If you want to get the version in string format, you can use PdfFileInfo.GetPdfVersion method as suggested in my previous post.

@tahir.manzoor I understood this, but what I was asking is is there any enumetation of Aspose function which I can use to check all type of PDF/A files?

@sreenath.techie

Could you please share some more detail of your query?

As per our understanding, you want to check all types of PDF/A files from the disk. If this is the case, you still need to import the PDF into Aspose.PDF DOM and use Document.PdfFormat property.

@tahir.manzoor

  1. Does below code is the one you are trying to suggest?

  2. Will this cover all type of PDF/A files?

         public static bool IsPDFaFile(Document pdfDocument)
     {
         try
         {
    
             return (pdfDocument.PdfFormat == PdfFormat.PDF_A_1A
                     || pdfDocument.PdfFormat == PdfFormat.PDF_A_1B
                     || pdfDocument.PdfFormat == PdfFormat.PDF_A_2A
                     || pdfDocument.PdfFormat == PdfFormat.PDF_A_2B
                     || pdfDocument.PdfFormat == PdfFormat.PDF_A_3A
                     || pdfDocument.PdfFormat == PdfFormat.PDF_A_3B);
    
         }
         catch (IOException e)
         {
             Console.WriteLine(e.ToString());
             throw;
         }
         catch (Exception e)
         {
             Console.WriteLine(e.ToString());
             throw;
         }
    
         return false;
     }

@sreenath.techie

Yes, code is correct. You may add PDF_A_2U and PDF_A_3U formats in your code as well.

Ok. So, is this code correct and optmised for PDF/A to PDF conversion?

public static bool RunConvertPdfAToPdfAndSave(string filePath)
    {
        try
        {
            using (Document pdfDocument = new Document(filePath))
            {
                //TODO : Check if PDF/A type of not
                if (IsPDFaFile(pdfDocument))
                {
                    pdfDocument.RemovePdfaCompliance();
                    pdfDocument.Save(filePath);
                }
            }

            return true;
        }
        catch (IOException e)
        {
            Console.WriteLine(e.ToString());
            throw;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
            throw;
        }

        return false;
    }

    public static bool IsPDFaFile(Document pdfDocument)
    {
        try
        {
            return (pdfDocument.PdfFormat == PdfFormat.PDF_A_1A
                    || pdfDocument.PdfFormat == PdfFormat.PDF_A_1B
                    || pdfDocument.PdfFormat == PdfFormat.PDF_A_2A
                    || pdfDocument.PdfFormat == PdfFormat.PDF_A_2B
                    || pdfDocument.PdfFormat == PdfFormat.PDF_A_3A
                    || pdfDocument.PdfFormat == PdfFormat.PDF_A_3B
                    || pdfDocument.PdfFormat == PdfFormat.PDF_A_2U
                    || pdfDocument.PdfFormat == PdfFormat.PDF_A_3U);
        }
        catch (IOException e)
        {
            Console.WriteLine(e.ToString());
            throw;
        }
        catch (Exception e)
        {
            Console.WriteLine(e.ToString());
            throw;
        }

        return false;
    }

@sreenath.techie

Yes, your code and understanding is correct. Please feel free to ask if you have any question about Aspose.PDF, we will be happy to help you.