private static PdfContentInfo ConvertPortfolioToPdf(IPdfContent pdf) { IList <ContentInfo> extractedPortfolios = iTextSharpHelper.ExtractPortfolio(pdf); PdfContentInfo result = pdf.ToPdfContent(); if (result.XfaPresent || result.HasAnnots) { if (!result.TryToFlattenedPdf(out result)) { logger.Warn("Errore di appiattimento PDF. TryToFlattenedPdf ha restituito il file originale e non il file appiattito. Nel file verrà inserito un warning nella firma."); } } if (extractedPortfolios == null) { return(result); } IEnumerable <PdfContentInfo> toMergeContentInfos = Enumerable.Empty <PdfContentInfo>(); toMergeContentInfos = toMergeContentInfos.Concat(new List <PdfContentInfo>() { result }) .Concat(extractedPortfolios.Select(ConvertToPdf)); byte[] mergedContent = iTextSharpHelper.Merge(toMergeContentInfos).Content; return(new PdfContentInfo(pdf.FileName, mergedContent)); }
private static PdfContentInfo RenderSignatures(IPdfContent pdf) { logger.Debug("RenderSignatures pdf"); if (pdf.HasSignatures) { logger.Debug("RenderSignatures pdf enter"); var mergeable = new List <PdfContentInfo>(); PdfContentInfo result = pdf.ToPdfContent(); bool enableErrorSection = !pdf.TryToFlattenedPdf(out result); logger.Debug(string.Format("RenderSignatures pdf enableErrorSection: {0}", enableErrorSection)); mergeable.Add(result); mergeable.Add(pdf.PrintSignContentReport(enableErrorSection)); return(iTextSharpHelper.Merge(mergeable)); } return(pdf.ToPdfContent()); }
public static IEnumerable <int> GetScannedPageNumbers(IPdfContent pdf) { try { using (var reader = pdf.ToPdfContent().GetReader()) return(reader.GetScannedPageNumbers()); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); throw; } }
public static Dictionary <int, string> GetTextFromAllPages(IPdfContent pdf) { try { using (var reader = pdf.ToPdfContent().GetReader()) return(reader.GetTextFromAllPages()); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); throw; } }
public static List <ContentInfo> ExtractPortfolio(IPdfContent pdf) { try { using (var reader = pdf.ToPdfContent().GetReader()) { if (reader.Catalog.IsNullOrEmpty()) { return(null); } var catalogNames = reader.Catalog.GetAsDict(PdfName.NAMES); if (catalogNames.IsNullOrEmpty()) { return(null); } var embeddedFiles = catalogNames.GetAsDict(PdfName.EMBEDDEDFILES); if (embeddedFiles.IsNullOrEmpty()) { return(null); } var embeddedNames = embeddedFiles.GetAsArray(PdfName.NAMES); if (embeddedNames.IsNullOrEmpty()) { return(null); } var result = new List <ContentInfo>(); for (int i = 1; i < embeddedNames.Size; i += 2) { var name = embeddedNames.GetAsDict(i); if (name.IsNullOrEmpty()) { continue; } var files = name.GetAsDict(PdfName.EF); if (files.IsNullOrEmpty()) { continue; } foreach (var item in files.Keys) { var indirect = files.GetAsIndirectObject(item); if (indirect == null) { throw new ArgumentNullException("PdfIndirectReference allegato non valido, impossibile procedere."); } var stream = (PRStream)PdfReader.GetPdfObject(indirect); if (stream == null) { throw new ArgumentNullException("PRStream allegato non valido, impossibile procedere."); } var content = PdfReader.GetStreamBytes(stream); if (content.IsNullOrEmpty()) { throw new ArgumentNullException("Allegato con dimensione zero, impossibile procedere."); } var fileName = name.GetAsString(item).ToUnicodeString().Trim(); var contentInfo = new ContentInfo(fileName, content); result.Add(contentInfo); if (!contentInfo.Extension.EqualsIgnoreCase(PathUtil.EXTENSIONPDF)) { continue; } var recursive = ExtractPortfolio(contentInfo.ToPdfContent()); if (recursive.IsNullOrEmpty()) { continue; } result.AddRange(recursive); } } return(result); } } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); throw; } }
public static DocumentWrapper LoadFrom(this DocumentWrapper source, IPdfContent pdf, PdfTransformer tx) { PdfReader.unethicalreading = tx.UnethicalReading; PdfReader reader = null; PdfWriter writer = null; MemoryStream ms = null; try { reader = pdf.ToPdfContent().GetReader(); source.IsEncrypted = reader.IsEncrypted(); using (ms = new MemoryStream()) using (writer = PdfWriter.GetInstance(source, ms)) { if (tx.PdfA) { writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_7); writer.PDFXConformance = PdfWriter.PDFX1A2001; writer.CreateXmpMetadata(); } source.Open(); foreach (var p in reader.PageNumbers()) { PdfImportedPage sourcePage = null; try { sourcePage = writer.GetImportedPage(reader, p); } catch (ArgumentException) { PdfReader.unethicalreading = true; sourcePage = writer.GetImportedPage(reader, p); PdfReader.unethicalreading = tx.UnethicalReading; } var sourcePageSize = reader.GetPageSizeWithRotation(p); var template = tx; template.SetOrigin(sourcePage, sourcePageSize); if (reader.HasAnnotations(p)) { Debug.WriteLine("Trovate annotazioni a pagina {0}, lo scaling del contenuto verrà ignorato.", p); template.ClearScaling(); } source.NewPage(template.DestinationPageSize); writer.DirectContent.TransformTo(template); } source.Close(); source.Content = ms.ToDeepCopyArray(); return(source); } } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace); throw ex; } finally { source.CloseQuietly(); ms.CloseQuietly(); writer.CloseQuietly(); reader.CloseQuietly(); } }