private PdfStream ParseStream(PdfDictionary dict) { if (!dict.ContainsKey("/Length")) { throw new ParserException($"Stream is missing length information"); } var length = dict.Get <PdfNumeric>("/Length"); if (length <= 0) { throw new ParserException($"Stream is missing length information"); } byte[] data = _lexer.Take(length); var stream = new PdfStream(dict); stream.SetStream(data); _lexer.Next(); Expect(TokenType.EndStream); return(stream); }
private static void CopyTo(PdfDocument destDocument, IDictionary<PdfPage, PdfPage> page2page, PdfDocument callingDocument, bool copyFromDestDocument, int insertIndex) { StructureTreeCopier.CopyStructureResult copiedStructure = CopyStructure(destDocument, page2page, callingDocument , copyFromDestDocument); PdfStructTreeRoot destStructTreeRoot = destDocument.GetStructTreeRoot(); destStructTreeRoot.MakeIndirect(destDocument); foreach (PdfDictionary copied in copiedStructure.GetTopsList()) { destStructTreeRoot.AddKidObject(insertIndex, copied); if (insertIndex > -1) { ++insertIndex; } } if (!copyFromDestDocument) { if (!copiedStructure.GetCopiedNamespaces().IsEmpty()) { destStructTreeRoot.GetNamespacesObject().AddAll(copiedStructure.GetCopiedNamespaces()); } PdfDictionary srcRoleMap = callingDocument.GetStructTreeRoot().GetRoleMap(); PdfDictionary destRoleMap = destStructTreeRoot.GetRoleMap(); foreach (KeyValuePair<PdfName, PdfObject> mappingEntry in srcRoleMap.EntrySet()) { if (!destRoleMap.ContainsKey(mappingEntry.Key)) { destRoleMap.Put(mappingEntry.Key, mappingEntry.Value); } else { if (!mappingEntry.Value.Equals(destRoleMap.Get(mappingEntry.Key))) { String srcMapping = mappingEntry.Key + " -> " + mappingEntry.Value; String destMapping = mappingEntry.Key + " -> " + destRoleMap.Get(mappingEntry.Key); ILog logger = LogManager.GetLogger(typeof(StructureTreeCopier)); logger.Warn(String.Format(iText.IO.LogMessageConstant.ROLE_MAPPING_FROM_SOURCE_IS_NOT_COPIED_ALREADY_EXIST , srcMapping, destMapping)); } } } } }
internal static TrueTypeFont CreateFontProgram(PdfDictionary fontDictionary, CMapToUnicode toUnicode) { iText.Kernel.Font.DocTrueTypeFont fontProgram = new iText.Kernel.Font.DocTrueTypeFont(fontDictionary); PdfDictionary fontDescriptor = fontDictionary.GetAsDictionary(PdfName.FontDescriptor); FillFontDescriptor(fontProgram, fontDescriptor); int dw = (fontDescriptor != null && fontDescriptor.ContainsKey(PdfName.DW)) ? (int)fontDescriptor.GetAsInt (PdfName.DW) : 1000; if (toUnicode != null) { IntHashtable widths = FontUtil.ConvertCompositeWidthsArray(fontDictionary.GetAsArray(PdfName.W)); fontProgram.avgWidth = 0; foreach (int cid in toUnicode.GetCodes()) { int width = widths.ContainsKey(cid) ? widths.Get(cid) : dw; Glyph glyph = new Glyph(cid, width, toUnicode.Lookup(cid)); if (glyph.HasValidUnicode()) { fontProgram.unicodeToGlyph[glyph.GetUnicode()] = glyph; } fontProgram.codeToGlyph[cid] = glyph; fontProgram.avgWidth += width; } if (fontProgram.codeToGlyph.Count != 0) { fontProgram.avgWidth /= fontProgram.codeToGlyph.Count; } } if (fontProgram.codeToGlyph.Get(0) == null) { fontProgram.codeToGlyph[0] = new Glyph(0, dw, -1); } return(fontProgram); }
private static int SeparateStructure(PdfDocument document, int startPage, int beforePage, int startPageStructTopIndex ) { if (!document.IsTagged() || 1 > startPage || startPage > beforePage || beforePage > document.GetNumberOfPages () + 1) { return -1; } else { if (beforePage == startPage) { return startPageStructTopIndex; } else { if (beforePage == document.GetNumberOfPages() + 1) { return document.GetStructTreeRoot().GetKidsObject().Size(); } } } // Here we separate the structure tree in two parts: struct elems that belong to the pages which indexes are // less then separateBeforePage and those struct elems that belong to other pages. Some elems might belong // to both parts and actually these are the ones that we are looking for. ICollection<PdfObject> firstPartElems = new HashSet<PdfObject>(); for (int i = startPage; i < beforePage; ++i) { PdfPage pageOfFirstHalf = document.GetPage(i); ICollection<PdfMcr> pageMcrs = document.GetStructTreeRoot().GetPageMarkedContentReferences(pageOfFirstHalf ); if (pageMcrs != null) { foreach (PdfMcr mcr in pageMcrs) { firstPartElems.Add(mcr.GetPdfObject()); PdfDictionary top = AddAllParentsToSet(mcr, firstPartElems); if (top != null && top.IsFlushed()) { throw new PdfException(PdfException.TagFromTheExistingTagStructureIsFlushedCannotAddCopiedPageTags); } } } } IList<PdfDictionary> clonedTops = new List<PdfDictionary>(); PdfArray tops = document.GetStructTreeRoot().GetKidsObject(); // Now we "walk" through all the elems which belong to the first part, and look for the ones that contain both // kids from first and second part. We clone found elements and move kids from the second part to cloned elems. int lastTopBefore = startPageStructTopIndex - 1; for (int i = 0; i < tops.Size(); ++i) { PdfDictionary top = tops.GetAsDictionary(i); if (firstPartElems.Contains(top)) { lastTopBefore = i; StructureTreeCopier.LastClonedAncestor lastCloned = new StructureTreeCopier.LastClonedAncestor(); lastCloned.ancestor = top; PdfDictionary topClone = top.Clone(ignoreKeysForClone); topClone.Put(PdfName.P, document.GetStructTreeRoot().GetPdfObject()); lastCloned.clone = topClone; SeparateKids(top, firstPartElems, lastCloned, document); if (topClone.ContainsKey(PdfName.K)) { topClone.MakeIndirect(document); clonedTops.Add(topClone); } } } for (int i = 0; i < clonedTops.Count; ++i) { document.GetStructTreeRoot().AddKidObject(lastTopBefore + 1 + i, clonedTops[i]); } return lastTopBefore + 1; }
public File(IInputStream stream) { Initialize(); reader = new Reader(stream, this); try // [FIX:45] File constructor didn't dispose reader on error. { Reader.FileInfo info = reader.ReadInfo(); version = info.Version; trailer = PrepareTrailer(info.Trailer); indirectObjects = new IndirectObjects(this, info.XrefEntries); document = new Document(trailer[PdfName.Root]); Configuration.XRefMode = (PdfName.XRef.Equals(trailer[PdfName.Type]) ? XRefModeEnum.Compressed : XRefModeEnum.Plain); if (trailer.ContainsKey(PdfName.Encrypt)) // Encrypted file. { throw new NotImplementedException("Encrypted files are currently not supported."); var encript = trailer.Resolve(PdfName.Encrypt); } } catch (Exception) { reader.Dispose(); throw; } }
protected internal override void CheckMetaData(PdfDictionary catalog) { if (!catalog.ContainsKey(PdfName.Metadata)) { throw new PdfAConformanceException(PdfAConformanceException.A_CATALOG_DICTIONARY_SHALL_CONTAIN_METADATA_ENTRY ); } }
protected internal override void CheckFileSpec(PdfDictionary fileSpec) { if (fileSpec.ContainsKey(PdfName.EF)) { throw new PdfAConformanceException(PdfAConformanceException.FILE_SPECIFICATION_DICTIONARY_SHALL_NOT_CONTAIN_THE_EF_KEY ); } }
protected virtual IDictionary <ByteArray, int> GetNativeEncoding( ) { PdfDictionary descriptor = Descriptor; if (descriptor.ContainsKey(PdfName.FontFile)) // Embedded noncompact Type 1 font. { PdfStream fontFileStream = (PdfStream)descriptor.Resolve(PdfName.FontFile); PfbParser parser = new PfbParser(fontFileStream.Body); return(parser.Parse()); } else if (descriptor.ContainsKey(PdfName.FontFile3)) // Embedded compact Type 1 font. { PdfStream fontFileStream = (PdfStream)descriptor.Resolve(PdfName.FontFile3); PdfName fontFileSubtype = (PdfName)fontFileStream.Header[PdfName.Subtype]; if (fontFileSubtype.Equals(PdfName.Type1C)) // CFF. { CffParser parser = new CffParser(fontFileStream.Body); IDictionary <ByteArray, int> codes = new Dictionary <ByteArray, int>(); foreach (KeyValuePair <int, int> glyphIndexEntry in parser.glyphIndexes) { /* * FIXME: Custom (non-unicode) encodings require name handling to match encoding * differences; this method (getNativeEncoding) should therefore return a glyphindex-to- * character-name map instead. * Constraining native codes into target byte-arrayed encodings is wrong -- that should * be only the final stage. */ codes[new ByteArray(new byte[] { ConvertUtils.IntToByteArray(glyphIndexEntry.Value)[3] })] = glyphIndexEntry.Key; } return(codes); } else if (fontFileSubtype.Equals(PdfName.OpenType)) // OpenFont/CFF. { throw new NotImplementedException("Embedded OpenFont/CFF font file."); } else { throw new NotSupportedException("Unsupported embedded font file format: " + fontFileSubtype); } } else // Non-embedded font. { return(Encoding.Get(PdfName.StandardEncoding).GetCodes()); } }
protected internal override void CheckTrailer(PdfDictionary trailer) { if (trailer.ContainsKey(PdfName.Encrypt)) { throw new PdfAConformanceException(PdfAConformanceException.KEYWORD_ENCRYPT_SHALL_NOT_BE_USED_IN_THE_TRAILER_DICTIONARY ); } }
protected internal override void CheckFileSpec(PdfDictionary fileSpec) { if (fileSpec.ContainsKey(PdfName.EF)) { throw new PdfAConformanceException(PdfAConformanceException.FileSpecificationDictionaryShallNotContainTheEFKey ); } }
private static String GetOrdering(PdfDictionary cidFont) { PdfDictionary cidinfo = cidFont.GetAsDictionary(PdfName.CIDSystemInfo); if (cidinfo == null) { return(null); } return(cidinfo.ContainsKey(PdfName.Ordering) ? cidinfo.Get(PdfName.Ordering).ToString() : null); }
protected internal override void CheckFileSpec(PdfDictionary fileSpec) { PdfName relationship = fileSpec.GetAsName(PdfName.AFRelationship); if (relationship == null || !allowedAFRelationships.Contains(relationship)) { throw new PdfAConformanceException(PdfAConformanceException.FileSpecificationDictionaryShallContainOneOfThePredefinedAFRelationshipKeys ); } if (fileSpec.ContainsKey(PdfName.EF)) { if (!fileSpec.ContainsKey(PdfName.F) || !fileSpec.ContainsKey(PdfName.UF) || !fileSpec.ContainsKey(PdfName .Desc)) { throw new PdfAConformanceException(PdfAConformanceException.FileSpecificationDictionaryShallContainFKeyUFKeyAndDescKey ); } PdfDictionary ef = fileSpec.GetAsDictionary(PdfName.EF); PdfStream embeddedFile = ef.GetAsStream(PdfName.F); if (embeddedFile == null) { throw new PdfAConformanceException(PdfAConformanceException.EFKeyOfFileSpecificationDictionaryShallContainDictionaryWithValidFKey ); } if (!embeddedFile.ContainsKey(PdfName.Subtype)) { throw new PdfAConformanceException(PdfAConformanceException.MimeTypeShallBeSpecifiedUsingTheSubtypeKeyOfTheFileSpecificationStreamDictionary ); } PdfDictionary @params = embeddedFile.GetAsDictionary(PdfName.Params); if (@params == null) { throw new PdfAConformanceException(PdfAConformanceException.EmbeddedFileShallContainParamsKeyWithDictionaryAsValue ); } if (@params.GetAsString(PdfName.ModDate) == null) { throw new PdfAConformanceException(PdfAConformanceException.EmbeddedFileShallContainParamsKeyWithValidModdateKey ); } } }
public static PdfObject zGet(this PdfDictionary pdfDictionary, PdfName name) { if (pdfDictionary.ContainsKey(name)) { return(pdfDictionary.Get(name)); } else { return(null); } }
private static bool ImageColorSpaceIsKnown(PdfDictionary imageDictionary, PdfDictionary colorSpaceDic) { PdfName cs = imageDictionary.GetAsName(PdfName.ColorSpace); if (cs == null || cs.Equals(PdfName.DeviceGray) || cs.Equals(PdfName.DeviceRGB) || cs.Equals(PdfName.DeviceCMYK )) { return(true); } return(colorSpaceDic != null && colorSpaceDic.ContainsKey(cs)); }
protected internal override void CheckCatalogValidEntries(PdfDictionary catalogDict) { if (catalogDict.ContainsKey(PdfName.AA)) { throw new PdfAConformanceException(PdfAConformanceException.CatalogDictionaryShallNotContainAAEntry); } if (catalogDict.ContainsKey(PdfName.OCProperties)) { throw new PdfAConformanceException(PdfAConformanceException.CatalogDictionaryShallNotContainOCPropertiesKey ); } if (catalogDict.ContainsKey(PdfName.Names)) { if (catalogDict.GetAsDictionary(PdfName.Names).ContainsKey(PdfName.EmbeddedFiles)) { throw new PdfAConformanceException(PdfAConformanceException.NameDictionaryShallNotContainTheEmbeddedFilesKey ); } } }
private static PdfObject CopyObjectKid(PdfObject kid, PdfDictionary copiedParent, PdfDictionary destPage, bool parentChangePg, StructureTreeCopier.StructElemCopyingParams copyingParams) { if (kid.IsNumber()) { if (!parentChangePg) { copyingParams.GetToDocument().GetStructTreeRoot().GetParentTreeHandler().RegisterMcr(new PdfMcrNumber((PdfNumber )kid, new PdfStructElem(copiedParent))); return(kid); } } else { if (kid.IsDictionary()) { PdfDictionary kidAsDict = (PdfDictionary)kid; if (copyingParams.GetObjectsToCopy().Contains(kidAsDict)) { bool hasParent = kidAsDict.ContainsKey(PdfName.P); PdfDictionary copiedKid = CopyObject(kidAsDict, destPage, parentChangePg, copyingParams); if (hasParent) { copiedKid.Put(PdfName.P, copiedParent); } else { PdfMcr mcr; if (copiedKid.ContainsKey(PdfName.Obj)) { mcr = new PdfObjRef(copiedKid, new PdfStructElem(copiedParent)); PdfDictionary contentItemObject = copiedKid.GetAsDictionary(PdfName.Obj); if (PdfName.Link.Equals(contentItemObject.GetAsName(PdfName.Subtype)) && !contentItemObject.ContainsKey(PdfName .P)) { // Some link annotations may be not copied, because their destination page is not copied. return(null); } contentItemObject.Put(PdfName.StructParent, new PdfNumber((int)copyingParams.GetToDocument().GetNextStructParentIndex ())); } else { mcr = new PdfMcrDictionary(copiedKid, new PdfStructElem(copiedParent)); } copyingParams.GetToDocument().GetStructTreeRoot().GetParentTreeHandler().RegisterMcr(mcr); } return(copiedKid); } } } return(null); }
protected internal override void CheckCatalogValidEntries(PdfDictionary catalogDict) { if (catalogDict.ContainsKey(PdfName.AA)) { throw new PdfAConformanceException(PdfAConformanceException.A_CATALOG_DICTIONARY_SHALL_NOT_CONTAIN_AA_ENTRY ); } if (catalogDict.ContainsKey(PdfName.OCProperties)) { throw new PdfAConformanceException(PdfAConformanceException.A_CATALOG_DICTIONARY_SHALL_NOT_CONTAIN_OCPROPERTIES_KEY ); } if (catalogDict.ContainsKey(PdfName.Names)) { if (catalogDict.GetAsDictionary(PdfName.Names).ContainsKey(PdfName.EmbeddedFiles)) { throw new PdfAConformanceException(PdfAConformanceException.A_NAME_DICTIONARY_SHALL_NOT_CONTAIN_THE_EMBEDDED_FILES_KEY ); } } }
private static PdfObject CopyObjectKid(PdfObject kid, PdfDictionary copiedParent, ICollection <PdfObject> objectsToCopy , PdfDocument toDocument, IDictionary <PdfDictionary, PdfDictionary> page2page, bool copyFromDestDocument ) { if (kid.IsNumber()) { toDocument.GetStructTreeRoot().GetParentTreeHandler().RegisterMcr(new PdfMcrNumber((PdfNumber)kid, new PdfStructElem (copiedParent))); return(kid); } else { // TODO do we always copy numbers? don't we need to check if it is supposed to be copied like objs in objectsToCopy? if (kid.IsDictionary()) { PdfDictionary kidAsDict = (PdfDictionary)kid; if (objectsToCopy.Contains(kidAsDict)) { bool hasParent = kidAsDict.ContainsKey(PdfName.P); PdfDictionary copiedKid = CopyObject(kidAsDict, objectsToCopy, toDocument, page2page, copyFromDestDocument ); if (hasParent) { copiedKid.Put(PdfName.P, copiedParent); } else { PdfMcr mcr; if (copiedKid.ContainsKey(PdfName.Obj)) { mcr = new PdfObjRef(copiedKid, new PdfStructElem(copiedParent)); PdfDictionary contentItemObject = copiedKid.GetAsDictionary(PdfName.Obj); if (PdfName.Link.Equals(contentItemObject.GetAsName(PdfName.Subtype)) && !contentItemObject.ContainsKey(PdfName .P)) { // Some link annotations may be not copied, because their destination page is not copied. return(null); } contentItemObject.Put(PdfName.StructParent, new PdfNumber((int)toDocument.GetNextStructParentIndex())); } else { mcr = new PdfMcrDictionary(copiedKid, new PdfStructElem(copiedParent)); } toDocument.GetStructTreeRoot().GetParentTreeHandler().RegisterMcr(mcr); } return(copiedKid); } } } return(null); }
public Trailer(PdfDictionary dict) { foreach (var kvp in dict) { this[kvp.Key] = kvp.Value; } if (dict.ContainsKey("/Info")) { Info = dict["/Info"] as PdfReference; } Root = dict["/Root"] as PdfReference; }
/// <summary> /// Parses the samples of the image from the underlying content parser, accounting for filters /// The parser must be positioned immediately after the ID operator that ends the inline image's dictionary. /// </summary> /// <remarks> /// Parses the samples of the image from the underlying content parser, accounting for filters /// The parser must be positioned immediately after the ID operator that ends the inline image's dictionary. /// The parser will be left positioned immediately following the EI operator. /// <b>Note:</b>This implementation does not actually apply the filters at this time /// </remarks> /// <param name="imageDictionary">the dictionary of the inline image</param> /// <param name="ps">the content parser</param> /// <returns>the samples of the image</returns> private static byte[] ParseSamples(PdfDictionary imageDictionary, PdfDictionary colorSpaceDic, PdfCanvasParser ps) { // by the time we get to here, we have already parsed the ID operator if (!imageDictionary.ContainsKey(PdfName.Filter) && ImageColorSpaceIsKnown(imageDictionary, colorSpaceDic) ) { return(ParseUnfilteredSamples(imageDictionary, colorSpaceDic, ps)); } // read all content until we reach an EI operator followed by whitespace. // then decode the content stream to check that bytes that were parsed are really all image bytes MemoryStream baos = new MemoryStream(); int ch; int found = 0; PdfTokenizer tokeniser = ps.GetTokeniser(); while ((ch = tokeniser.Read()) != -1) { if (ch == 'E') { // probably some bytes were preserved so write them baos.Write(EI, 0, found); // just preserve 'E' and do not write it immediately found = 1; } else { if (found == 1 && ch == 'I') { // just preserve 'EI' and do not write it immediately found = 2; } else { if (found == 2 && PdfTokenizer.IsWhitespace(ch)) { byte[] tmp = baos.ToArray(); if (InlineImageStreamBytesAreComplete(tmp, imageDictionary)) { return(tmp); } } // probably some bytes were preserved so write them baos.Write(EI, 0, found); baos.Write(ch); found = 0; } } } throw new InlineImageParsingUtils.InlineImageParseException(PdfException.CannotFindImageDataOrEI); }
/// <summary>Parses the samples of the image from the underlying content parser, ignoring all filters.</summary> /// <remarks> /// Parses the samples of the image from the underlying content parser, ignoring all filters. /// The parser must be positioned immediately after the ID operator that ends the inline image's dictionary. /// The parser will be left positioned immediately following the EI operator. /// This is primarily useful if no filters have been applied. /// </remarks> /// <param name="imageDictionary">the dictionary of the inline image</param> /// <param name="ps">the content parser</param> /// <returns>the samples of the image</returns> /// <exception cref="System.IO.IOException">if anything bad happens during parsing</exception> private static byte[] ParseUnfilteredSamples(PdfDictionary imageDictionary, PdfDictionary colorSpaceDic, PdfCanvasParser ps) { // special case: when no filter is specified, we just read the number of bits // per component, multiplied by the width and height. if (imageDictionary.ContainsKey(PdfName.Filter)) { throw new ArgumentException("Dictionary contains filters"); } PdfNumber h = imageDictionary.GetAsNumber(PdfName.Height); int bytesToRead = ComputeBytesPerRow(imageDictionary, colorSpaceDic) * h.IntValue(); byte[] bytes = new byte[bytesToRead]; PdfTokenizer tokeniser = ps.GetTokeniser(); int shouldBeWhiteSpace = tokeniser.Read(); // skip next character (which better be a whitespace character - I suppose we could check for this) // from the PDF spec: Unless the image uses ASCIIHexDecode or ASCII85Decode as one of its filters, the ID operator shall be followed by a single white-space character, and the next character shall be interpreted as the first byte of image data. // unfortunately, we've seen some PDFs where there is no space following the ID, so we have to capture this case and handle it int startIndex = 0; if (!PdfTokenizer.IsWhitespace(shouldBeWhiteSpace) || shouldBeWhiteSpace == 0) { // tokeniser treats 0 as whitespace, but for our purposes, we shouldn't bytes[0] = (byte)shouldBeWhiteSpace; startIndex++; } for (int i = startIndex; i < bytesToRead; i++) { int ch = tokeniser.Read(); if (ch == -1) { throw new InlineImageParsingUtils.InlineImageParseException(PdfException.EndOfContentStreamReachedBeforeEndOfImageData ); } bytes[i] = (byte)ch; } PdfObject ei = ps.ReadObject(); if (!ei.ToString().Equals("EI")) { // Some PDF producers seem to add another non-whitespace character after the image data. // Let's try to handle that case here. PdfObject ei2 = ps.ReadObject(); if (!ei2.ToString().Equals("EI")) { throw new InlineImageParsingUtils.InlineImageParseException(PdfException.OperatorEINotFoundAfterEndOfImageData ); } } return(bytes); }
public IFont Generate(PdfDictionary dictionary, IRandomAccessRead reader, bool isLenientParsing) { var baseFont = dictionary.GetName(CosName.BASE_FONT); var cMap = ReadEncoding(dictionary, out var isCMapPredefined); ICidFont cidFont; if (TryGetFirstDescendant(dictionary, out var descendantObject)) { var parsed = DirectObjectFinder.Find <PdfDictionary>(descendantObject, pdfObjectParser, reader, isLenientParsing); if (parsed is PdfDictionary descendantFontDictionary) { cidFont = ParseDescendant(descendantFontDictionary, reader, isLenientParsing); } else { throw new InvalidFontFormatException("Expected to find a Descendant Font dictionary, instead it was: " + parsed); } } else { throw new InvalidFontFormatException("No descendant font dictionary was declared for this Type 0 font. This dictionary should contain the CIDFont for the Type 0 font. " + dictionary); } var ucs2CMap = GetUcs2CMap(dictionary, isCMapPredefined, false); CMap toUnicodeCMap = null; if (dictionary.ContainsKey(CosName.TO_UNICODE)) { var toUnicodeValue = dictionary[CosName.TO_UNICODE]; var toUnicode = pdfObjectParser.Parse(((CosObject)toUnicodeValue).ToIndirectReference(), reader, isLenientParsing) as PdfRawStream; var decodedUnicodeCMap = toUnicode?.Decode(filterProvider); if (decodedUnicodeCMap != null) { toUnicodeCMap = cMapCache.Parse(new ByteArrayInputBytes(decodedUnicodeCMap), isLenientParsing); } } var font = new Type0Font(baseFont, cidFont, cMap, toUnicodeCMap); return(font); }
/// <summary>Embed a file to a PdfDocument.</summary> /// <param name="doc"/> /// <param name="is"/> /// <param name="description"/> /// <param name="fileDisplay"/> /// <param name="mimeType"/> /// <param name="fileParameter"/> /// <param name="afRelationshipValue"/> public static iText.Kernel.Pdf.Filespec.PdfFileSpec CreateEmbeddedFileSpec(PdfDocument doc, Stream @is, String description, String fileDisplay, PdfName mimeType, PdfDictionary fileParameter, PdfName afRelationshipValue ) { PdfStream stream = new PdfStream(doc, @is); PdfDictionary @params = new PdfDictionary(); if (fileParameter != null) { @params.MergeDifferent(fileParameter); } if ([email protected](PdfName.ModDate)) { @params.Put(PdfName.ModDate, new PdfDate().GetPdfObject()); } stream.Put(PdfName.Params, @params); return(CreateEmbeddedFileSpec(doc, stream, description, fileDisplay, mimeType, afRelationshipValue)); }
protected internal override void CheckLogicalStructure(PdfDictionary catalog) { if (CheckStructure(conformanceLevel)) { PdfDictionary markInfo = catalog.GetAsDictionary(PdfName.MarkInfo); if (markInfo == null || markInfo.GetAsBoolean(PdfName.Marked) == null || !markInfo.GetAsBoolean(PdfName.Marked ).GetValue()) { throw new PdfAConformanceException(PdfAConformanceException.CatalogShallIncludeMarkInfoDictionaryWithMarkedTrueValue ); } if (!catalog.ContainsKey(PdfName.Lang)) { ILogger logger = LoggerFactory.GetLogger(typeof(PdfAChecker)); logger.Warn(PdfAConformanceException.CatalogShallContainLangEntry); } } }
internal PdfType1Font(PdfDictionary fontDictionary) : base(fontDictionary) { newFont = false; CheckFontDictionary(fontDictionary, PdfName.Type1); CMapToUnicode toUni = FontUtil.ProcessToUnicode(fontDictionary.Get(PdfName.ToUnicode)); //if there is no FontDescriptor, it is most likely one of the Standard Font with StandardEncoding as base encoding. bool fillStandardEncoding = !fontDictionary.ContainsKey(PdfName.FontDescriptor); fontEncoding = DocFontEncoding.CreateDocFontEncoding(fontDictionary.Get(PdfName.Encoding), toUni, fillStandardEncoding ); fontProgram = DocType1Font.CreateFontProgram(fontDictionary, fontEncoding, toUni); if (fontProgram is IDocFontProgram) { embedded = ((IDocFontProgram)fontProgram).GetFontFile() != null; } subset = false; }
public File( IInputStream stream ) { reader = new Reader(stream, this); Reader.FileInfo info = reader.ReadInfo(); version = info.Version; trailer = info.Trailer; if (trailer.ContainsKey(PdfName.Encrypt)) // Encrypted file. { throw new NotImplementedException("Encrypted files are currently not supported."); } indirectObjects = new IndirectObjects(this, info.XrefEntries); document = new Document(trailer[PdfName.Root]); document.Configuration.XrefMode = (PdfName.XRef.Equals(trailer[PdfName.Type]) ? Document.Config.XRefModeEnum.Compressed : Document.Config.XRefModeEnum.Plain); }
protected internal override void CheckLogicalStructure(PdfDictionary catalog) { if (CheckStructure(conformanceLevel)) { PdfDictionary markInfo = catalog.GetAsDictionary(PdfName.MarkInfo); if (markInfo == null || markInfo.GetAsBoolean(PdfName.Marked) == null || !markInfo.GetAsBoolean(PdfName.Marked ).GetValue()) { throw new PdfAConformanceException(PdfAConformanceException.A_CATALOG_SHALL_INCLUDE_MARK_INFO_DICTIONARY_WITH_MARKED_TRUE_VALUE ); } if (!catalog.ContainsKey(PdfName.Lang)) { ILog logger = LogManager.GetLogger(typeof(PdfAChecker)); logger.Warn(PdfAConformanceLogMessageConstant.CATALOG_SHOULD_CONTAIN_LANG_ENTRY); } } }
private PdfMcr PrepareMcrForMovingToNewParent(PdfMcr mcrKid, PdfStructElem newParent) { PdfObject mcrObject = mcrKid.GetPdfObject(); PdfDictionary mcrPage = mcrKid.GetPageObject(); PdfDictionary mcrDict = null; if (!mcrObject.IsNumber()) { mcrDict = (PdfDictionary)mcrObject; } if (mcrDict == null || !mcrDict.ContainsKey(PdfName.Pg)) { if (!EnsureElementPageEqualsKidPage(newParent, mcrPage)) { if (mcrDict == null) { mcrDict = new PdfDictionary(); mcrDict.Put(PdfName.Type, PdfName.MCR); mcrDict.Put(PdfName.MCID, mcrKid.GetPdfObject()); } // Explicitly using object indirect reference here in order to correctly process released objects. mcrDict.Put(PdfName.Pg, mcrPage.GetIndirectReference()); } } if (mcrDict != null) { if (PdfName.MCR.Equals(mcrDict.Get(PdfName.Type))) { mcrKid = new PdfMcrDictionary(mcrDict, newParent); } else { if (PdfName.OBJR.Equals(mcrDict.Get(PdfName.Type))) { mcrKid = new PdfObjRef(mcrDict, newParent); } } } else { mcrKid = new PdfMcrNumber((PdfNumber)mcrObject, newParent); } return(mcrKid); }
public override void Run( ) { // 1. Opening the PDF file... string filePath = PromptFileChoice("Please select a PDF file"); using (files::File file = new files::File(filePath)) { // 2. Iterating through the indirect object collection... int index = 0; foreach (PdfIndirectObject indirectObject in file.IndirectObjects) { // Get the data object associated to the indirect object! PdfDataObject dataObject = indirectObject.DataObject; // Is this data object a stream? if (dataObject is PdfStream) { PdfDictionary header = ((PdfStream)dataObject).Header; // Is this stream an image? if (header.ContainsKey(PdfName.Type) && header[PdfName.Type].Equals(PdfName.XObject) && header[PdfName.Subtype].Equals(PdfName.Image)) { // Which kind of image? if (header[PdfName.Filter].Equals(PdfName.DCTDecode)) // JPEG image. { // Get the image data (keeping it encoded)! IBuffer body = ((PdfStream)dataObject).GetBody(false); // Export the image! ExportImage( body, "ImageExtractionSample_" + (index++) + ".jpg" ); } else // Unsupported image. { Console.WriteLine("Image XObject " + indirectObject.Reference + " couldn't be extracted (filter: " + header[PdfName.Filter] + ")"); } } } } } }
protected internal override void CheckPageObject(PdfDictionary pageDict, PdfDictionary pageResources) { PdfDictionary actions = pageDict.GetAsDictionary(PdfName.AA); if (actions != null) { foreach (PdfName key in actions.KeySet()) { PdfDictionary action = actions.GetAsDictionary(key); CheckAction(action); } } if (pageDict.ContainsKey(PdfName.Group) && PdfName.Transparency.Equals(pageDict.GetAsDictionary(PdfName.Group ).GetAsName(PdfName.S))) { throw new PdfAConformanceException(PdfAConformanceException.AGroupObjectWithAnSKeyWithAValueOfTransparencyShallNotBeIncludedInAPageObject ); } }
public File( IInputStream stream ) { Initialize(); reader = new Reader(stream, this); Reader.FileInfo info = reader.ReadInfo(); version = info.Version; trailer = PrepareTrailer(info.Trailer); if(trailer.ContainsKey(PdfName.Encrypt)) // Encrypted file. throw new NotImplementedException("Encrypted files are currently not supported."); indirectObjects = new IndirectObjects(this, info.XrefEntries); document = new Document(trailer[PdfName.Root]); document.Configuration.XrefMode = (PdfName.XRef.Equals(trailer[PdfName.Type]) ? Document.ConfigurationImpl.XRefModeEnum.Compressed : Document.ConfigurationImpl.XRefModeEnum.Plain); }
private byte[] DecodePredictor( byte[] data, PdfDictionary parameters ) { if(parameters == null) return data; int predictor = (parameters.ContainsKey(PdfName.Predictor) ? ((PdfInteger)parameters[PdfName.Predictor]).RawValue : 1); if(predictor == 1) // No predictor was applied during data encoding. return data; int sampleComponentBitsCount = (parameters.ContainsKey(PdfName.BitsPerComponent) ? ((PdfInteger)parameters[PdfName.BitsPerComponent]).RawValue : 8); int sampleComponentsCount = (parameters.ContainsKey(PdfName.Colors) ? ((PdfInteger)parameters[PdfName.Colors]).RawValue : 1); int rowSamplesCount = (parameters.ContainsKey(PdfName.Columns) ? ((PdfInteger)parameters[PdfName.Columns]).RawValue : 1); MemoryStream input = new MemoryStream(data); MemoryStream output = new MemoryStream(); switch (predictor) { case 2: // TIFF Predictor 2 (component-based). { int[] sampleComponentPredictions = new int[sampleComponentsCount]; int sampleComponentDelta = 0; int sampleComponentIndex = 0; while((sampleComponentDelta = input.ReadByte()) != -1) { int sampleComponent = sampleComponentDelta + sampleComponentPredictions[sampleComponentIndex]; output.WriteByte((byte)sampleComponent); sampleComponentPredictions[sampleComponentIndex] = sampleComponent; sampleComponentIndex = ++sampleComponentIndex % sampleComponentsCount; } break; } default: // PNG Predictors [RFC 2083] (byte-based). { int sampleBytesCount = (int)Math.Ceiling(sampleComponentBitsCount * sampleComponentsCount / 8d); // Number of bytes per pixel (bpp). int rowSampleBytesCount = (int)Math.Ceiling(sampleComponentBitsCount * sampleComponentsCount * rowSamplesCount / 8d) + sampleBytesCount; // Number of bytes per row (comprising a leading upper-left sample (see Paeth method)). int[] previousRowBytePredictions = new int[rowSampleBytesCount]; int[] currentRowBytePredictions = new int[rowSampleBytesCount]; int[] leftBytePredictions = new int[sampleBytesCount]; int predictionMethod; while((predictionMethod = input.ReadByte()) != -1) { Array.Copy(currentRowBytePredictions, 0, previousRowBytePredictions, 0, currentRowBytePredictions.Length); Array.Clear(leftBytePredictions, 0, leftBytePredictions.Length); for( int rowSampleByteIndex = sampleBytesCount; // Starts after the leading upper-left sample (see Paeth method). rowSampleByteIndex < rowSampleBytesCount; rowSampleByteIndex++ ) { int byteDelta = input.ReadByte(); int sampleByteIndex = rowSampleByteIndex % sampleBytesCount; int sampleByte; switch(predictionMethod) { case 0: // None (no prediction). sampleByte = byteDelta; break; case 1: // Sub (predicts the same as the sample to the left). sampleByte = byteDelta + leftBytePredictions[sampleByteIndex]; break; case 2: // Up (predicts the same as the sample above). sampleByte = byteDelta + previousRowBytePredictions[rowSampleByteIndex]; break; case 3: // Average (predicts the average of the sample to the left and the sample above). sampleByte = byteDelta + (int)Math.Floor(((leftBytePredictions[sampleByteIndex] + previousRowBytePredictions[rowSampleByteIndex])) / 2d); break; case 4: // Paeth (a nonlinear function of the sample above, the sample to the left, and the sample to the upper left). { int paethPrediction; { int leftBytePrediction = leftBytePredictions[sampleByteIndex]; int topBytePrediction = previousRowBytePredictions[rowSampleByteIndex]; int topLeftBytePrediction = previousRowBytePredictions[rowSampleByteIndex - sampleBytesCount]; int initialPrediction = leftBytePrediction + topBytePrediction - topLeftBytePrediction; int leftPrediction = Math.Abs(initialPrediction - leftBytePrediction); int topPrediction = Math.Abs(initialPrediction - topBytePrediction); int topLeftPrediction = Math.Abs(initialPrediction - topLeftBytePrediction); if(leftPrediction <= topPrediction && leftPrediction <= topLeftPrediction) {paethPrediction = leftBytePrediction;} else if(topPrediction <= topLeftPrediction) {paethPrediction = topBytePrediction;} else {paethPrediction = topLeftBytePrediction;} } sampleByte = byteDelta + paethPrediction; break; } default: throw new NotSupportedException("Prediction method " + predictionMethod + " unknown."); } output.WriteByte((byte)sampleByte); leftBytePredictions[sampleByteIndex] = currentRowBytePredictions[rowSampleByteIndex] = (byte)sampleByte; } } break; } } return output.ToArray(); }