Beispiel #1
0
        static void Main(string[] args)
        {
            try
            {
                //File.WriteAllText("D:\\pipInput.txt", args[0]);
                MemoryStream ms       = null;
                byte[]       password = null;
                for (int i = 0; i < args.Length; i++)
                {
                    int splitIndex = args[i].IndexOf(':');

                    string prefix = null;
                    string value  = null;

                    if (splitIndex == -1)
                    {
                        prefix = args[i];
                    }
                    else
                    {
                        prefix = args[i].Substring(0, splitIndex);
                        value  = args[i].Substring(splitIndex + 1);
                    }

                    switch (prefix.ToUpper())
                    {
                    case "-N":
                    case "/N":
                        if (ms != null)
                        {
                            throw new ArgumentException("incorrect input arguments: filedata already initialized");
                        }

                        if (string.IsNullOrWhiteSpace(value) || !File.Exists(value))
                        {
                            throw new ArgumentException("incorrect input arguments: filename not exists");
                        }

                        if (File.Exists(value))
                        {
                            ms = new MemoryStream(File.ReadAllBytes(value));
                        }
                        break;

                    case "-D":
                    case "/D":
                        if (ms != null)
                        {
                            throw new ArgumentException("incorrect input arguments: filedata already initialized");
                        }

                        if (string.IsNullOrWhiteSpace(value))
                        {
                            throw new ArgumentException("incorrect input arguments: filedata is empty");
                        }

                        ms = new MemoryStream(Convert.FromBase64String(value));
                        break;

                    case "-P":
                    case "/P":
                        if (value == null)
                        {
                            throw new ArgumentException("incorrect input arguments: password is empty");
                        }

                        password = Encoding.ASCII.GetBytes(value);
                        break;

                    case "-HELP":
                    case "/HELP":
                    case "-?":
                    case "/?":
                        Console.Write("Retrieves PAdES signatures and returns informations structured into XML" + Environment.NewLine +
                                      "document." + Environment.NewLine +
                                      Environment.NewLine +
                                      "PadesInfoProcessor [/N:[path][filename]] [/D:[filedata]] [/P:[password]]" + Environment.NewLine +
                                      "  /N:[path][filename]" + Environment.NewLine +
                                      "               Specifies PDF document file path to retrieve informations." + Environment.NewLine +
                                      "  /D:[filedata]" + Environment.NewLine +
                                      "               Specifies PDF document data encoded with Base64." + Environment.NewLine +
                                      "  /P:[password]" + Environment.NewLine +
                                      "               Specifies password if PDF document is encrypted." + Environment.NewLine +
                                      Environment.NewLine);
                        break;

                    default:
                        break;
                    }
                }


                string output = string.Empty;
                if (ms != null)
                {
                    iText.Kernel.Pdf.PdfReader pdfReader;
                    if (password == null)
                    {
                        pdfReader = new iText.Kernel.Pdf.PdfReader(ms);
                    }
                    else
                    {
                        pdfReader = new iText.Kernel.Pdf.PdfReader(ms, (new iText.Kernel.Pdf.ReaderProperties()).SetPassword(password));
                    }

                    output += "<PdfSignatures>";
                    PdfDocument document = new PdfDocument(pdfReader);
                    if (iText.Forms.PdfAcroForm.GetAcroForm(document, false) != null)
                    {
                        SignatureUtil  su       = new SignatureUtil(document);
                        IList <string> sigNames = su.GetSignatureNames();

                        foreach (string sigName in sigNames)
                        {
                            PdfSignature sig = su.GetSignature(sigName);

                            //string cert = sig.GetCert().GetValue();
                            string coversWholeDoc = su.SignatureCoversWholeDocument(sigName).ToString();
                            string signingTime    = getDate(sig.GetDate());
                            string contentType    = sig.GetSubFilter().ToString().Replace("/", "");
                            string reason         = sig.GetReason();
                            string location       = sig.GetLocation();

                            output += "<PdfSignature>";
                            output += "<SignatureName>" + sigName + "</SignatureName>";
                            output += "<PdfSigningTimeUtc>" + signingTime + "</PdfSigningTimeUtc>";
                            output += "<Reason>" + reason + "</Reason>";
                            output += "<Location>" + location + "</Location>";
                            output += "<CoversWholeDocument>" + coversWholeDoc + "</CoversWholeDocument>";
                            output += "<ContentType>" + contentType + "</ContentType>";
                            output += processByPdfPKCS7(new PdfDocument(pdfReader), sig, contentType);

                            output += "</PdfSignature>";
                        }
                    }
                    output += "</PdfSignatures>";
                }
                //File.WriteAllText(@"D:\PadesInfoProcessorOutput.xml", output);
                Console.Write(output);
            }
            catch (ArgumentException argEx)
            {
                Console.Error.Write("PadesInfoProcessor 1: " + argEx.Message);
            }
            catch (Exception ex)
            {
                //File.WriteAllText("D:\\pipException.txt", ex.ToString());
                Console.Error.Write("PadesInfoProcessor 1: PDF document unexpected exception: " + ex.ToString());
            }
        }