コード例 #1
0
        //////////////////////////////////////////////////////////
        //// public properties
        #region public properties

        /// <summary>
        /// Convert the Portable Document to text.
        /// </summary>
        /// <param name="file">The portable document file stream.</param>
        /// <param name="errors">Collection of errors that occur during reading the file stream.  Null if sucessful.</param>
        /// <returns>The text contents of the file.</returns>
        public static string Convert(Stream file, out List <Exception> errors)
        {
            StringBuilder result = new StringBuilder();

            errors = null;

            using (iText7.PdfReader reader = new iText7.PdfReader(file)) {
                using (iText7.PdfDocument doc = new iText7.PdfDocument(reader))
                {
                    int numberOfPages = doc.GetNumberOfPages();
                    for (int i = 1; i <= numberOfPages; i++)
                    {
                        try
                        {
                            var    page     = doc.GetPage(i);
                            string pagetext = iText7.Canvas.Parser.PdfTextExtractor.GetTextFromPage(page);
                            result.Append(Common.CleanPdfText(pagetext));
                        }
                        catch (Exception e)
                        {
                            if (errors == null)
                            {
                                errors = new List <Exception>();
                            }
                            errors.Add(e);
                        }
                    }
                }
            }

            return(result.ToString());
        }
コード例 #2
0
        public static byte[] Combine(IEnumerable <byte[]> pdfs)
        {
            using (var writerMemoryStream = new MemoryStream())
            {
                using (var writer = new PdfWriter(writerMemoryStream))
                {
                    using (var mergedDocument = new iText.Kernel.Pdf.PdfDocument(writer))
                    {
                        var merger = new PdfMerger(mergedDocument);

                        foreach (var pdfBytes in pdfs)
                        {
                            using (var copyFromMemoryStream = new MemoryStream(pdfBytes))
                            {
                                using (var reader = new iText.Kernel.Pdf.PdfReader(copyFromMemoryStream))
                                {
                                    //have to set unethical reading to true else will get password error
                                    reader.SetUnethicalReading(true);
                                    using (var copyFromDocument = new iText.Kernel.Pdf.PdfDocument(reader))
                                    {
                                        //second parameter 1 is page number from where to start merge
                                        merger.Merge(copyFromDocument, 1, copyFromDocument.GetNumberOfPages());
                                    }
                                }
                            }
                        }
                    }
                }
                return(writerMemoryStream.ToArray());
            }
        }
コード例 #3
0
        public List <string> Read()
        {
            List <string> dataLines = new List <string>();

            try
            {
                using (iText.Kernel.Pdf.PdfReader reader = new iText.Kernel.Pdf.PdfReader(new FileInfo(FilePath)))
                    using (PdfDocument pdfDocument = new PdfDocument(reader))
                    {
                        for (int pageNumber = 1; pageNumber <= pdfDocument.GetNumberOfPages(); pageNumber++)
                        {
                            var page = pdfDocument.GetPage(pageNumber);

                            ITextExtractionStrategy strategy = new LocationTextExtractionStrategy();

                            string unencodedText = PdfTextExtractor.GetTextFromPage(page, strategy);

                            if (TextContainsScheduleEntryHeader(unencodedText))
                            {
                                string encodedText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(unencodedText)));

                                List <string> lines = encodedText.Split('\n').ToList();

                                int entryStartIndex = GetScheduleEntriesStartLineIndex(lines);

                                for (int i = entryStartIndex; i < lines.Count; i++)
                                {
                                    string line = lines[i];

                                    if (IsPageNumberIndicator(line))
                                    {
                                        continue;
                                    }

                                    if (IsEndOfRegister(line))
                                    {
                                        break;
                                    }

                                    dataLines.Add(line.Trim());
                                }
                            }
                        }
                    }
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }

            return(dataLines);
        }
コード例 #4
0
ファイル: ReportPrinter.cs プロジェクト: amitdumka/eStore
        /// <summary>
        /// Add Page number at top of pdf file.
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns> filename saved as</returns>
        public static string AddPageNumberToPdf(string fileName)
        {
            using PdfReader reader = new PdfReader(fileName);
            string fName = "cashBook_" + (DateTime.Now.ToFileTimeUtc() + 1001) + ".pdf";

            using PdfWriter writer = new PdfWriter(Path.Combine(ReportHeaderDetails.WWWroot, fName));

            using PdfDocument pdfDoc2 = new PdfDocument(reader, writer);
            Document doc2 = new Document(pdfDoc2);

            int numberOfPages = pdfDoc2.GetNumberOfPages();

            for (int i = 1; i <= numberOfPages; i++)
            {
                doc2.ShowTextAligned(new Paragraph("Page " + i + " of " + numberOfPages),
                                     559, 806, i, TextAlignment.RIGHT, VerticalAlignment.BOTTOM, 0);
            }
            doc2.Close();
            return(fName);
        }
コード例 #5
0
ファイル: InformesServicio.cs プロジェクト: ander74/Orion
 /// <summary>
 /// Devuelve un PDF que se guardará en la ruta indicada, a partir de la plantilla necesaria para el tipo de informe indicado.
 /// </summary>
 /// <param name="ruta">Ruta completa (nombre de archivo incluido) en la que se guardará el PDF.</param>
 /// <param name="tipo">Tipo de informe que se va a generar.</param>
 /// <returns>El PDF listo para editar sus elementos.</returns>
 public iText.Kernel.Pdf.PdfDocument GetPdfDesdePlantilla(string ruta, TiposInforme tipo = TiposInforme.Ninguno)
 {
     // Definimos el pdf que se va a devolver.
     iText.Kernel.Pdf.PdfDocument docPdf = null;
     // Si hay un tipo de informe creamos el pdf.
     if (tipo != TiposInforme.Ninguno)
     {
         // Definimos la plantilla
         string plantilla = Utils.CombinarCarpetas(App.RutaInicial, $"/Plantillas/{tipo}.pdf");
         // Se crea el Reader con la plantilla necesaria.
         iText.Kernel.Pdf.PdfReader reader = new iText.Kernel.Pdf.PdfReader(plantilla);
         // Se crea el Writer con la ruta pasada.
         iText.Kernel.Pdf.PdfWriter writer = new iText.Kernel.Pdf.PdfWriter(ruta);
         // Creamos el documento, usando el reader y el writer anteriores.
         docPdf = new iText.Kernel.Pdf.PdfDocument(reader, writer);
         // Añadimos los datos de Metadata
         docPdf.GetDocumentInfo().SetAuthor("Orion - AnderSoft - A.Herrero");
         docPdf.GetDocumentInfo().SetCreator("Orion 1.0");
     }
     // Devolvemos el documento.
     return(docPdf);
 }
コード例 #6
0
        /// <summary>
        /// An enumeration of paragraphs of the portable document.
        /// </summary>
        /// <param name="file">The portable document file stream.</param>
        /// <returns>A <see cref="IEnumerable{T}"/> of paragraphs.</returns>
        public static IEnumerable <string> Paragraphs(Stream file)
        {
            using (iText7.PdfReader reader = new iText7.PdfReader(file))
            {
                using (iText7.PdfDocument doc = new iText7.PdfDocument(reader))
                {
                    int numberOfPages = doc.GetNumberOfPages();
                    for (int i = 1; i <= numberOfPages; i++)
                    {
                        iText7.PdfPage page     = doc.GetPage(i);
                        string         pagetext = iText7.Canvas.Parser.PdfTextExtractor.GetTextFromPage(page);
                        pagetext = Common.CleanPdfText(pagetext);

                        // Parse paragraphs.
                        IEnumerable <string> paragraphs = pagetext.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
                        foreach (string item in paragraphs)
                        {
                            yield return(item);
                        }
                    }
                }
            }
        }
コード例 #7
0
ファイル: ReportPrinter.cs プロジェクト: amitdumka/eStore
        public static string PrintCashBook(List <CashBook> cbList)
        {
            string fName = "cashBook_" + DateTime.Now.ToFileTimeUtc() + ".pdf";

            string fileName = Path.Combine(ReportHeaderDetails.WWWroot, fName);

            using PdfWriter pdfWriter = new PdfWriter(fileName);
            using PdfDocument pdfDoc  = new PdfDocument(pdfWriter);
            using Document doc        = new Document(pdfDoc, PageSize.A4);
            Paragraph header = new Paragraph(ReportHeaderDetails.FirstLine + "\n")
                               .SetTextAlignment(iText.Layout.Properties.TextAlignment.CENTER)
                               .SetFontColor(ColorConstants.RED);

            header.Add(ReportHeaderDetails.SecondLine + "\n");
            doc.Add(header);
            float[] columnWidths = { 1, 5, 15, 5, 5, 5 };
            Table   table        = new Table(UnitValue.CreatePercentArray(columnWidths)).SetBorder(new OutsetBorder(2));

            PdfFont f    = PdfFontFactory.CreateFont(StandardFonts.HELVETICA);
            Cell    cell = new Cell(1, 6)
                           .Add(new Paragraph(ReportHeaderDetails.CashBook))
                           .SetFont(f)
                           .SetFontSize(13)
                           .SetFontColor(DeviceGray.WHITE)
                           .SetBackgroundColor(DeviceGray.BLACK)
                           .SetTextAlignment(TextAlignment.CENTER);

            table.AddHeaderCell(cell);

            Cell[] headerFooter = new Cell[] {
                new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("#")),
                new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Date").SetTextAlignment(TextAlignment.CENTER)),
                new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Particulars").SetTextAlignment(TextAlignment.CENTER)),
                new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("In").SetTextAlignment(TextAlignment.CENTER)),
                new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Out").SetTextAlignment(TextAlignment.CENTER)),
                new Cell().SetBackgroundColor(new DeviceGray(0.75f)).Add(new Paragraph("Balance").SetTextAlignment(TextAlignment.CENTER))
            };

            Cell[] footer = new[]
            {
                new Cell(1, 4).Add(new Paragraph(ReportHeaderDetails.FirstLine + " / " + ReportHeaderDetails.SecondLine).SetFontColor(DeviceGray.GRAY)),
                new Cell(1, 2).Add(new Paragraph("D:" + DateTime.Now).SetFontColor(DeviceGray.GRAY)),
            };

            foreach (Cell hfCell in headerFooter)
            {
                table.AddHeaderCell(hfCell);
            }
            foreach (Cell hfCell in footer)
            {
                table.AddFooterCell(hfCell);
            }

            int count = 0;

            foreach (var item in cbList)

            {
                table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph((++count) + "")));
                table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(item.EDate.ToShortDateString())));
                table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(item.Particulars + "")));
                table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(item.CashIn.ToString("0.##"))));
                table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(item.CashOut.ToString("0.##"))));
                table.AddCell(new Cell().SetTextAlignment(TextAlignment.CENTER).Add(new Paragraph(item.CashBalance.ToString("0.##"))));
            }
            doc.Add(table);

            doc.Close();

            using PdfReader reader = new PdfReader(fileName);
            fName = "cashBook_" + (DateTime.Now.ToFileTimeUtc() + 1001) + ".pdf";
            using PdfWriter writer = new PdfWriter(Path.Combine(ReportHeaderDetails.WWWroot, fName));

            using PdfDocument pdfDoc2 = new PdfDocument(reader, writer);
            Document doc2 = new Document(pdfDoc2);

            int numberOfPages = pdfDoc2.GetNumberOfPages();

            for (int i = 1; i <= numberOfPages; i++)
            {
                // Write aligned text to the specified by parameters point
                doc2.ShowTextAligned(new Paragraph("Page " + i + " of " + numberOfPages),
                                     559, 806, i, TextAlignment.RIGHT, VerticalAlignment.BOTTOM, 0);
            }

            doc2.Close();


            return(fName);
        }
コード例 #8
0
ファイル: Program.cs プロジェクト: ditecsk/PadesInfoProcessor
        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());
            }
        }