For my internship, I need to verify a signed PFD/A file to guarantee that the client will receive the official document.
Now I've created a signed PDF/A file with iTextSharp. When I open this file in Adobe Reader it shows me that there is a signature present.
Now I need to verify the file in the code before I can send it to the clients, but when I try to verify it using the GetSignatureNames function or the GetBlankSignatures function, it keeps returning that there are no signature names.
I'm using the following code to verify:
PdfReader reader = new PdfReader(cdr.PDFAkteNaam);
AcroFields af = reader.AcroFields;
var names = af.GetSignatureNames();
if (names.Count == 0)
{
MessageBox.Show("Error","No Signature Present in PDF file.");
}
else
{
MessageBox.Show("Succes!", "Success!");
}
EDIT:
Here is the signed test-file we got from notaryship. It contains fictional data just for testing. Link to file
Related
I'm trying to export multiple Google Docs files via Google Drive API into Pdf and merge them into one using iText7 but it throws exception iText.IO.Exceptions.IOException: 'PDF header not found.' because of the weird PDF format from Google export.
Google Disk generated PDF content (read with notepad) is not valid PDF.
File content starts like this 倥䙄ㄭ㐮┊ㄊ instead of something like %PDF-1.4
The uploaded PDF file is readable from Google Disk without any problem and it is readable even if I export the Stream directly to the disk. File content is exactly the same when I download file manually through Google Docs GUI.
Here is my code to export files via API:
var mimeType = "application/pdf";
var file = GetFile(sourceFile);
var pdfRequest = _driveService.Files.Export(sourceFile, mimeType);
var stream = pdfRequest.ExecuteAsStream();
Then I'm uploading PDF back into Google Drive via it's API
var newFile = new Google.Apis.Drive.v3.Data.File();
newFile.MimeType = mimeType;
newFile.Parents = new List<string>() { targetFolder };
var createRequest = _driveService.Files.Create(newFile, stream, mimeType);
createRequest.SupportsAllDrives = true;
var createResult = createRequest.Upload();
Weirdly enough the format of exported PDF is ok when I use
var text = pdfRequest.Execute(); instead of pdfRequest.ExecuteAsStream (it starts with %PDF-1.7).
But Execute() returns string instead of Stream.
Is there any way to get standard PDF format from Google Disk API or convert it in any possible way?
The problem was in the iText7 itself. It considered PDF as invalid but it probably just does not support PDFs in iso8859_2 encoding.
I tried to use PDFSharp instead and everything went smoothly.
I've used ExecuteAsStream() from Google Disk API to get PDF Stream with no problems at all so it wasnt at fault.
Thanks for all your tips.
I have a PDF document that needs to be both digitally signed and encrypted.
I am using ABCPDF and when I apply the digital signature to a document that is encrypted the signature gets invalidated.
The error that is provided by Adobe Acrobat Reader is: "There have been changes made to this document that invalidate the signature"
Source code:
using (Doc doc = new Doc())
{
doc.Read(pdfPath);
if (options.Encrypt)
{
doc.Encryption.Type = 4;
doc.Encryption.SetCryptMethods(CryptMethodType.AESV3);
doc.Encryption.Password = Encryption.Decrypt(options.UserPassword, PdfSecurityOptions.EncryptionPassword);
doc.Encryption.OwnerPassword = Encryption.Decrypt(options.OwnerPassword, PdfSecurityOptions.EncryptionPassword);
}
if (options.Sign)
{
byte[] bytes = Convert.FromBase64String(options.Certificate);
X509Certificate2 certificate = new X509Certificate2(bytes, Encryption.Decrypt(options.CertificatePassword, PdfSecurityOptions.EncryptionPassword), X509KeyStorageFlags.Exportable | X509KeyStorageFlags.PersistKeySet);
PdfUtils.DigitallySign(
doc,
options.SignatureText,
options.Rectangle,
certificate
);
}
doc.Save(savePath);
}
I have tried:
Apply the encryption before the signing
Apply the encryption after the signing
Apply the encryption, save the document and then load and sign it
You can't perform a signature after another it's done; doing so, you will corrupt first signature.
To avoid this, you have to use incremental updates, each time a signature is done.
Check out Abcpdf Online documentation
http://www.websupergoo.com/helppdfnet/source/6-abcpdf.objects/signature/1-methods/commit.htm
I have tested your code, and the signature appears ok. I think that you have an issue in the way you're adding the field. I see you're using the Abcpdf example code. Maybe you need some changes on it. Can you provide a pdf sample?
I'm generating a pdf file from a template with iTextSharp, filling each field in this code portion:
PdfReader pdfReader = new PdfReader(templatePath);
try
{
using (FileStream newFileStream = new FileStream(newFilePath, FileMode.Create))
{
using (PdfStamper stamper = new PdfStamper(pdfReader, newFileStream))
{
// fill each field
AcroFields pdfFormFields = stamper.AcroFields;
foreach (KeyValuePair<string, string> entry in content)
{
if (!String.IsNullOrEmpty(entry.Value))
pdfFormFields.SetField(entry.Key, entry.Value);
}
//The below will make sure the fields are not editable in
//the output PDF.
stamper.FormFlattening = true;
stamper.Close();
}
}
}
finally
{
pdfReader.Close();
}
Everything goes fine, file looks ok, but when i try to reopen the file to merge it with some other files I've generated in a unique document i get this error:
2015-11-23 09:46:54,651||ERROR|UrbeWeb|System.IO.IOException: The process cannot access the file 'D:\Sviluppo\communitygov\MaxiAnagrafeImmobiliare\MaxiAnagrafeImmobiliare\cache\IMU\E124\admin\Stampe\Provvedimento_00223850306_2015_11_23_094654.pdf' because it is being used by another process.
Error occurs at this point
foreach (Documento item in docs)
{
string fileName = item.FilePath;
pdfReader = new PdfReader(fileName); // IOException
// some other operations ...
}
Edit: Using Process monitor as suggested I can see there is no close CloseFile operation as I would expect. Can this be the source of the issue?
I've been stuck on this for hours any help is really really appreciated.
Had the same issue with me. This helped a lot.
"You're problem is that you are writing to a file while you are also reading from it. Unlike some file types (JPG, PNG, etc) that "load" all of the data into memory, iTextSharp reads the data as a stream. You either need to use two files and swap them at the end or you can force iTextSharp to "load" the first file by binding your PdfReader to a byte array of the file."
PdfReader reader = new PdfReader(System.IO.File.ReadAllBytes(filePath));
Ref: Cris Haas answer to Cannot access the file because it is being used by another process
I had a similar problem with opening pdf files (for read only) with iTextSharp PdfReader. The first file gave no problem, the second one gave that exception (can not access the file, etc.).
After hours and googling and searching for complicate solutions and twisting my brain, only the simple following code resolved it fully:
iTextSharp_pdf.PdfReader pdfReader = null;
pdfReader = new iTextSharp_pdf.PdfReader(fileName);
I want to make my PDF document protected by not allowing fill in and copy from it. I am using iTextSharp for this. I have following code:
PdfReader reader = new PdfReader(document, System.Text.Encoding.UTF8.GetBytes(PASSWORD));
using (MemoryStream ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(reader, ms))
{
stamper.SetEncryption(
null,
Encoding.ASCII.GetBytes(PASSWORD),
PdfWriter.ALLOW_PRINTING,
PdfWriter.ENCRYPTION_AES_128);
}
}
reader.Close();
When the document is generated I use that code to encrypt the document. But later when I open the document in Adobe Reader (tested on 9 and 11) and check the 'File > Properties > Security' their are no restrictions applied on fill in and copy of the document and their status is Allowed.
Is there any issue in that code?
According to the ITextSharp documentation for PdfStamper, the second parameter to this method is an output stream representing the destination for the encrypted PDF document data. The code you show in the question simply disposes the MemoryStream after you setup the encryption so any changes this code could apply to your PDF document will never be saved to disk or otherwise be available outside your application.
Is it possible to verify if we can copy the content of a PDF document with iTextSharp?
I have a method that copy the content of the PDF and add a new page at the end with project's information but it throw a "System.ArgumentException: PdfReader not opened with owner password". I get this error when I do writer.GetImportedPage(reader, i);
Thanks for the help!
You should be able to just check the property PdfReader.IsOpenedWithFullPermissions.
PdfReader r = new PdfReader("YourFile.pdf");
if (r.IsOpenedWithFullPermissions)
{
//Do something
}