Click here to Skip to main content
15,881,831 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I am using iText and want to fetch the pdf version of my pdf. I am using the following code:
C#
using (PdfReader reader = new PdfReader(filePath))
            {
                // Check the PDF version against the specified standard
                // string pdfVersion = reader.GetPdfVersion().ToString();
                string pdfVersion = reader.GetPdfVersion().ToString();

                // Remove the "PDF-" prefix to get the version number only
                pdfVersion = pdfVersion.Replace("PDF-", "");

                // Compare the version
                return string.Compare(pdfVersion, pdfStandard) >= 0;
            }

But at line:
C#
string pdfVersion = reader.GetPdfVersion().ToString();

it is throwing error "PDf reader does not contain any definition for GetPdfVersion".

What I have tried:

I tried various code but was not successful.
Posted
Updated 6-Nov-23 3:52am
v2

The GetPdfVersion method is defined on the PdfDocument class, NOT the PdfReader class.

iText 7 7.1.8 API: iText.Kernel.Pdf.PdfDocument Class Reference[^]

You can't just call a method from one class on a completely different class which doesn't inherit from the first class and expect it to work!

If you want to access the method, then you need an instance of the PdfDocument class:
C#
using (PdfReader reader = new PdfReader(filePath))
using (PdfDocument document = new PdfDocument(reader))
{
    string pdfVersion = document.GetPdfVersion().ToString();
    ...
 
Share this answer
 
Comments
Akshay malvankar 26-Oct-23 0:46am    
in above code on line no "using (PdfReader reader = new PdfReader(filePath))" is giving me following error "'TN 55 AC 0010 VAL.pdf not found as file or resource.'". i am uploading above file from my local computer
Richard Deeming 26-Oct-23 4:09am    
Which is a totally different question to the one you asked.

There's nowhere near enough information in this question to answer your new question. But at a guess, you're trying to open the file using the path sent in the upload, rather than saving the file to your server. That won't work; the code running on the server has no access to the client's file system.

It might appear to work when you debug it in Visual Studio. But that's only because, in that specific case, the client and server are the same machine. Once you deploy your code to a real server, it will break.

Save the uploaded file to a path on the server. Or see if there's a way to use the uploaded file's Stream to read the PDF file without saving it.
Looking at the source code for iTextSharp, it's not GetPdfVersion you should be calling, but just PdfVersion, which is a property of the PdfReader object, not a method.
C#
using (PdfReader reader = new PdfReader(filePath))
{
    // Check the PDF version against the specified standard
    //string pdfVersion = reader.GetPdfVersion().ToString();
    string pdfVersion = reader.PdfVersion;

    // Remove the "PDF-" prefix to get the version number only
    pdfVersion = pdfVersion.Replace("PDF-", "");

    // Compare the version
    return string.Compare(pdfVersion, pdfStandard) >= 0;
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900