C# - Cannot convert an object of type 'iTextSharp.text.pdf.Pdfarray' in type 'iTextSharp.text.pdf.Pdfdictionary

Asked

Viewed 148 times

0

I’m developing a certified digital signature system A1 and A3 (token) for pdfs along with iTextsharp (v5.5.13.1, via NuGet Package Manager). I use the c# and I came across the following error in creating an object PdfReader:

$Exception {"Cannot convert an object of type 'iTextSharp.text.pdf.Pdfarray' to type 'iTextSharp.text.pdf.Pdfdictionary'." } System.Invalidcastexception

The file in question is a pdf digitized, with the other forms, nothing occurs, works normally.

I do the reading of all pdfs from a directory as below, and proceed with a foreach in the array:

pgbAssinaturas.Maximum = aArquivos.Length;

string[] aArquivos = Directory.GetFiles(txbPathPdfs2.Text, "*.pdf");
foreach (string sArquivo in aArquivos) {

   // atualiza progressbar
   iContadorProgressBar++;
   pgbAssinaturas.Value = iContadorProgressBar;
   pgbAssinaturas.Refresh();
   
   // abre arquivo original
   PdfReader pdfReader = new PdfReader(sArquivo); // <-- linha do erro

   // prepara um nome final
   sNomeArquivoFinal = "Assinados\\sign_" + Path.GetFileName(sArquivo);
   sNomeArquivoFinal = txbPathPdfs2.Text + "\\" + sNomeArquivoFinal;

   // [...]
}

Does anyone know what might be going on with this PDF?

IDE: visual studio community 2019, .Net Framework 4.8.03752

1 answer

0


The problem was the pdf same. For some reason when I first saved, it ended up corrupting.

However, I created a code to validate the problems, the good old try{ }catch(Exception ex){ }

try {
   // abre arquivo original
   // tenta criar
   PdfReader pdfReaderTest = new PdfReader(sArquivo);
   pdfReaderTest.Dispose();
}
catch (Exception ex) {
   // se for um pdf inválido, informa
   MessageBox.Show("erro: " + ex.Message)
   continue;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.