Contains() public méthode

public Contains ( PdfName key ) : bool
key PdfName
Résultat bool
 /**
 * Retrieves the page labels from a PDF as an array of String objects.
 * @param reader a PdfReader object that has the page labels you want to retrieve
 * @return  a String array or <code>null</code> if no page labels are present
 */
 public static String[] GetPageLabels(PdfReader reader) {
     
     int n = reader.NumberOfPages;
     
     PdfDictionary dict = reader.Catalog;
     PdfDictionary labels = (PdfDictionary)PdfReader.GetPdfObjectRelease(dict.Get(PdfName.PAGELABELS));
     if (labels == null)
         return null;
     
     String[] labelstrings = new String[n];
     Dictionary<int, PdfObject> numberTree = PdfNumberTree.ReadTree(labels);
     
     int pagecount = 1;
     String prefix = "";
     char type = 'D';
     for (int i = 0; i < n; i++) {
         if (numberTree.ContainsKey(i)) {
             PdfDictionary d = (PdfDictionary)PdfReader.GetPdfObjectRelease(numberTree[i]);
             if (d.Contains(PdfName.ST)) {
                 pagecount = ((PdfNumber)d.Get(PdfName.ST)).IntValue;
             }
             else {
                 pagecount = 1;
             }
             if (d.Contains(PdfName.P)) {
                 prefix = ((PdfString)d.Get(PdfName.P)).ToUnicodeString();
             }
             if (d.Contains(PdfName.S)) {
                 type = ((PdfName)d.Get(PdfName.S)).ToString()[1];
             }
             else {
                 type = 'e';
             }
         }
         switch (type) {
         default:
             labelstrings[i] = prefix + pagecount;
             break;
         case 'R':
             labelstrings[i] = prefix + RomanNumberFactory.GetUpperCaseString(pagecount);
             break;
         case 'r':
             labelstrings[i] = prefix + RomanNumberFactory.GetLowerCaseString(pagecount);
             break;
         case 'A':
             labelstrings[i] = prefix + RomanAlphabetFactory.GetUpperCaseString(pagecount);
             break;
         case 'a':
             labelstrings[i] = prefix + RomanAlphabetFactory.GetLowerCaseString(pagecount);
             break;
         case 'e':
             labelstrings[i] = prefix;
             break;
         }
         pagecount++;
     }
     return labelstrings;
 }
Exemple #2
0
        /**
         * Translate a PRIndirectReference to a PdfIndirectReference
         * In addition, translates the object numbers, and copies the
         * referenced object to the output file.
         * NB: PRIndirectReferences (and PRIndirectObjects) really need to know what
         * file they came from, because each file has its own namespace. The translation
         * we do from their namespace to ours is *at best* heuristic, and guaranteed to
         * fail under some circumstances.
         */
        protected virtual PdfIndirectReference CopyIndirect(PRIndirectReference inp, bool keepStructure, bool directRootKids)
        {
            PdfIndirectReference theRef;
            RefKey             key = new RefKey(inp);
            IndirectReferences iRef;

            indirects.TryGetValue(key, out iRef);
            PdfObject obj = PdfReader.GetPdfObjectRelease(inp);

            if ((keepStructure) && (directRootKids))
            {
                if (obj is PdfDictionary)
                {
                    PdfDictionary dict = (PdfDictionary)obj;
                    if (dict.Contains(PdfName.PG))
                    {
                        return(null);
                    }
                }
            }
            if (iRef != null)
            {
                theRef = iRef.Ref;
                if (iRef.Copied)
                {
                    return(theRef);
                }
            }
            else
            {
                theRef         = body.PdfIndirectReference;
                iRef           = new IndirectReferences(theRef);
                indirects[key] = iRef;
            }
            if (obj != null && obj.IsDictionary())
            {
                PdfObject type = PdfReader.GetPdfObjectRelease(((PdfDictionary)obj).Get(PdfName.TYPE));
                if (type != null && PdfName.PAGE.Equals(type))
                {
                    return(theRef);
                }
            }
            iRef.SetCopied();
            parentObjects[obj] = inp;
            PdfObject res = CopyObject(obj, keepStructure, directRootKids);

            if (disableIndirects.ContainsKey(obj))
            {
                iRef.Copied = false;
            }
            if ((res != null) && !(res is PdfNull))
            {
                AddToBody(res, theRef);
                return(theRef);
            }
            indirects.Remove(key);
            return(null);
        }
Exemple #3
0
        /**
         * Translate a PRDictionary to a PdfDictionary. Also translate all of the
         * objects contained in it.
         */
        protected PdfDictionary CopyDictionary(PdfDictionary inp, bool keepStruct, bool directRootKids)
        {
            PdfDictionary outp = new PdfDictionary();
            PdfObject     type = PdfReader.GetPdfObjectRelease(inp.Get(PdfName.TYPE));

            if (keepStruct)
            {
                if ((directRootKids) && (inp.Contains(PdfName.PG)))
                {
                    PdfObject curr = inp;
                    disableIndirects.Add(curr, null);
                    while (parentObjects.ContainsKey(curr) && !(disableIndirects.ContainsKey(curr)))
                    {
                        curr = parentObjects[curr];
                        disableIndirects.Add(curr, null);
                    }
                    return(null);
                }

                PdfName structType = inp.GetAsName(PdfName.S);
                structTreeController.AddRole(structType);
                structTreeController.AddClass(inp);
            }

            foreach (PdfName key in inp.Keys)
            {
                PdfObject value = inp.Get(key);
                if (structTreeController != null && structTreeController.reader != null && (key.Equals(PdfName.STRUCTPARENT) || key.Equals(PdfName.STRUCTPARENTS)))
                {
                    outp.Put(key, new PdfNumber(currentStructArrayNumber));
                    structTreeController.CopyStructTreeForPage((PdfNumber)value, currentStructArrayNumber++);
                    continue;
                }
                if (type != null && PdfName.PAGE.Equals(type))
                {
                    if (!key.Equals(PdfName.B) && !key.Equals(PdfName.PARENT))
                    {
                        parentObjects[value] = inp;
                        PdfObject res = CopyObject(value, keepStruct, directRootKids);
                        if ((res != null) && !(res is PdfNull))
                        {
                            outp.Put(key, res);
                        }
                    }
                }
                else
                {
                    PdfObject res = CopyObject(value, keepStruct, directRootKids);
                    if ((res != null) && !(res is PdfNull))
                    {
                        outp.Put(key, res);
                    }
                }
            }
            return(outp);
        }
Exemple #4
0
        private void Init(PdfDictionary parent, PdfName structureType)
        {
            if (!top.Writer.GetStandardStructElems().Contains(structureType))
            {
                PdfDictionary roleMap = top.GetAsDict(PdfName.ROLEMAP);
                if (roleMap == null || !roleMap.Contains(structureType))
                {
                    throw new DocumentException(MessageLocalization.GetComposedMessage("unknown.structure.element.role.1", structureType.ToString()));
                }
                else
                {
                    this.structureType = roleMap.GetAsName(structureType);
                }
            }
            else
            {
                this.structureType = structureType;
            }
            PdfObject kido = parent.Get(PdfName.K);
            PdfArray  kids = null;

            if (kido == null)
            {
                kids = new PdfArray();
                parent.Put(PdfName.K, kids);
            }
            else if (kido is PdfArray)
            {
                kids = (PdfArray)kido;
            }
            else
            {
                kids = new PdfArray();
                kids.Add(kido);
                parent.Put(PdfName.K, kids);
            }
            if (kids.Size > 0)
            {
                if (kids.GetAsNumber(0) != null)
                {
                    kids.Remove(0);
                }
                if (kids.Size > 0)
                {
                    PdfDictionary mcr = kids.GetAsDict(0);
                    if (mcr != null && PdfName.MCR.Equals(mcr.GetAsName(PdfName.TYPE)))
                    {
                        kids.Remove(0);
                    }
                }
            }
            kids.Add(this);
            Put(PdfName.S, structureType);
            reference = top.Writer.PdfIndirectReference;
        }
 /**
 * Retrieves the page labels from a PDF as an array of {@link PdfPageLabelFormat} objects.
 * @param reader a PdfReader object that has the page labels you want to retrieve
 * @return  a PdfPageLabelEntry array, containing an entry for each format change
 * or <code>null</code> if no page labels are present
 */
 public static PdfPageLabelFormat[] GetPageLabelFormats(PdfReader reader) {
     PdfDictionary dict = reader.Catalog;
     PdfDictionary labels = (PdfDictionary)PdfReader.GetPdfObjectRelease(dict.Get(PdfName.PAGELABELS));
     if (labels == null) 
         return null;
     Dictionary<int, PdfObject> numberTree = PdfNumberTree.ReadTree(labels);
     int[] numbers = new int[numberTree.Count];
     numberTree.Keys.CopyTo(numbers, 0);
     Array.Sort(numbers);
     PdfPageLabelFormat[] formats = new PdfPageLabelFormat[numberTree.Count];
     String prefix;
     int numberStyle;
     int pagecount;
     for (int k = 0; k < numbers.Length; ++k) {
         int key = numbers[k];
         PdfDictionary d = (PdfDictionary)PdfReader.GetPdfObjectRelease(numberTree[key]);
         if (d.Contains(PdfName.ST)) {
             pagecount = ((PdfNumber)d.Get(PdfName.ST)).IntValue;
         } else {
             pagecount = 1;
         }
         if (d.Contains(PdfName.P)) {
             prefix = ((PdfString)d.Get(PdfName.P)).ToUnicodeString();
         } else {
             prefix = "";
         }
         if (d.Contains(PdfName.S)) {
             char type = ((PdfName)d.Get(PdfName.S)).ToString()[1];
             switch (type) {
                 case 'R': numberStyle = UPPERCASE_ROMAN_NUMERALS; break;
                 case 'r': numberStyle = LOWERCASE_ROMAN_NUMERALS; break;
                 case 'A': numberStyle = UPPERCASE_LETTERS; break;
                 case 'a': numberStyle = LOWERCASE_LETTERS; break;
                 default: numberStyle = DECIMAL_ARABIC_NUMERALS; break;
             }
         } else {
             numberStyle = EMPTY;
         }
         formats[k] = new PdfPageLabelFormat(key + 1, numberStyle, prefix, pagecount);
     }
     return formats;
 }
Exemple #6
0
        /**
         * Gets number of indirect. If type of directed indirect is PAGES, it refers PAGE object through KIDS.
         * (Contributed by Kazuya Ujihara)
         * @param indirect
         * 2004-06-13
         */
        private static int GetNumber(PdfIndirectReference indirect)
        {
            PdfDictionary pdfObj = (PdfDictionary)PdfReader.GetPdfObjectRelease(indirect);

            if (pdfObj.Contains(PdfName.TYPE) && pdfObj.Get(PdfName.TYPE).Equals(PdfName.PAGES) && pdfObj.Contains(PdfName.KIDS))
            {
                PdfArray kids = (PdfArray)pdfObj.Get(PdfName.KIDS);
                indirect = (PdfIndirectReference)kids.ArrayList[0];
            }
            return(indirect.Number);
        }
Exemple #7
0
        /**
         * Gets the first entarance of attribute.
         * @returns PdfObject
         * @since 5.3.4
         */
        virtual public PdfObject GetAttribute(PdfName name)
        {
            PdfDictionary attr = GetAsDict(PdfName.A);

            if (attr != null)
            {
                if (attr.Contains(name))
                {
                    return(attr.Get(name));
                }
            }
            return(null);
        }
Exemple #8
0
        /**
         * Gets the first entarance of attribute.
         * @returns PdfObject
         * @since 5.3.4
         */

        public PdfObject GetAttribute(PdfName name)
        {
            PdfDictionary attr = GetAsDict(PdfName.A);

            if (attr != null && attr.Contains(name))
            {
                return(attr.Get(name));
            }
            PdfDictionary parent = Parent;

            if (parent is PdfStructureElement)
            {
                return(((PdfStructureElement)parent).GetAttribute(name));
            }
            if (parent is PdfStructureTreeRoot)
            {
                return(((PdfStructureTreeRoot)parent).GetAttribute(name));
            }

            return(new PdfNull());
        }
Exemple #9
0
        /**
        * Creates a file specification with the file embedded. The file may
        * come from the file system or from a byte array.
        * @param writer the <CODE>PdfWriter</CODE>
        * @param filePath the file path
        * @param fileDisplay the file information that is presented to the user
        * @param fileStore the byte array with the file. If it is not <CODE>null</CODE>
        * it takes precedence over <CODE>filePath</CODE>
        * @param mimeType the optional mimeType
        * @param fileParameter the optional extra file parameters such as the creation or modification date
        * @param compressionLevel the level of compression
        * @throws IOException on error
        * @return the file specification
        * @since   2.1.3
        */    
        public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, String mimeType, PdfDictionary fileParameter, int compressionLevel) {
            PdfFileSpecification fs = new PdfFileSpecification();
            fs.writer = writer;
            fs.Put(PdfName.F, new PdfString(fileDisplay));
            fs.SetUnicodeFileName(fileDisplay, false);
            PdfEFStream stream;
            Stream inp = null;
            PdfIndirectReference refi;
            PdfIndirectReference refFileLength = null;
            try {
                if (fileStore == null) {
                    refFileLength = writer.PdfIndirectReference;
                    if (File.Exists(filePath)) {
                        inp = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    }
                    else {
                        if (filePath.StartsWith("file:/") || filePath.StartsWith("http://") || filePath.StartsWith("https://")) {
                            WebRequest wr = WebRequest.Create(filePath);
                            wr.Credentials = CredentialCache.DefaultCredentials;
                            inp = wr.GetResponse().GetResponseStream();
                        }
                        else {
                            inp = StreamUtil.GetResourceStream(filePath);
                            if (inp == null)
                                throw new IOException(MessageLocalization.GetComposedMessage("1.not.found.as.file.or.resource", filePath));
                        }
                    }
                    stream = new PdfEFStream(inp, writer);
                }
                else
                    stream = new PdfEFStream(fileStore);
                stream.Put(PdfName.TYPE, PdfName.EMBEDDEDFILE);
                stream.FlateCompress(compressionLevel);

                PdfDictionary param = new PdfDictionary();
                if (fileParameter != null)
                    param.Merge(fileParameter);
                if (!param.Contains(PdfName.MODDATE)) {
                    param.Put(PdfName.MODDATE, new PdfDate());
                }
                if (fileStore != null) {
                    param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength));
                    stream.Put(PdfName.PARAMS, param);
                }
                else {
                    stream.Put(PdfName.PARAMS, refFileLength);
                }

                if (mimeType != null)
                    stream.Put(PdfName.SUBTYPE, new PdfName(mimeType));

                refi = writer.AddToBody(stream).IndirectReference;
                if (fileStore == null) {
                    stream.WriteLength();
                    param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength));
                    writer.AddToBody(param, refFileLength);
                }
            }
            finally {
                if (inp != null)
                    try{inp.Close();}catch{}
            }
            PdfDictionary f = new PdfDictionary();
            f.Put(PdfName.F, refi);
            f.Put(PdfName.UF, refi);
            fs.Put(PdfName.EF, f);
            return fs;
        }
Exemple #10
0
        /**
        * Translate a PRDictionary to a PdfDictionary. Also translate all of the
        * objects contained in it.
        */
        protected PdfDictionary CopyDictionary(PdfDictionary inp, bool keepStruct, bool directRootKids) {
            PdfDictionary outp = new PdfDictionary();
            PdfObject type = PdfReader.GetPdfObjectRelease(inp.Get(PdfName.TYPE));
            
             if (keepStruct)
            {
                if ((directRootKids) && (inp.Contains(PdfName.PG)))
                {
                    PdfObject curr = inp;
                    disableIndirects.Add(curr);
                    while (parentObjects.ContainsKey(curr) && !(disableIndirects.Contains(curr))) {
                        curr = parentObjects[curr];
                        disableIndirects.Add(curr);
                    }
                    return null;
                }
                    
                PdfName structType = inp.GetAsName(PdfName.S);
                structTreeController.AddRole(structType);
                structTreeController.AddClass(inp);
            }

            if (structTreeController != null && structTreeController.reader != null && (inp.Contains(PdfName.STRUCTPARENTS) || inp.Contains(PdfName.STRUCTPARENT))) {
                PdfName key = PdfName.STRUCTPARENT;
                if (inp.Contains(PdfName.STRUCTPARENTS)) {
                    key = PdfName.STRUCTPARENTS;
                }
                PdfObject value = inp.Get(key);
                outp.Put(key, new PdfNumber(currentStructArrayNumber));
                structTreeController.CopyStructTreeForPage((PdfNumber) value, currentStructArrayNumber++);
            }

            foreach (PdfName key in inp.Keys) {
                PdfObject value = inp.Get(key);
                if (structTreeController != null && structTreeController.reader != null &&
                    (key.Equals(PdfName.STRUCTPARENTS) || key.Equals(PdfName.STRUCTPARENT))) {
                    continue;
                }
                if (PdfName.PAGE.Equals(type))
                {
                    if (!key.Equals(PdfName.B) && !key.Equals(PdfName.PARENT))
                    {
                        parentObjects[value] = inp;
                        PdfObject res = CopyObject(value, keepStruct, directRootKids);
                        if ((res != null) && !(res is PdfNull))
                            outp.Put(key, res);
                    }
                }
                else
                {
                    PdfObject res;
                    if (tagged && value.IsIndirect() && IsStructTreeRootReference((PRIndirectReference) value))
                        res = structureTreeRoot.Reference;
                    else
                        res = CopyObject(value, keepStruct, directRootKids);
                    if ((res != null) && !(res is PdfNull))
                        outp.Put(key, res);
                }
            }
            return outp;
        }
        /**
         * Creates a file specification with the file embedded. The file may
         * come from the file system or from a byte array.
         * @param writer the <CODE>PdfWriter</CODE>
         * @param filePath the file path
         * @param fileDisplay the file information that is presented to the user
         * @param fileStore the byte array with the file. If it is not <CODE>null</CODE>
         * it takes precedence over <CODE>filePath</CODE>
         * @param mimeType the optional mimeType
         * @param fileParameter the optional extra file parameters such as the creation or modification date
         * @param compressionLevel the level of compression
         * @throws IOException on error
         * @return the file specification
         * @since   2.1.3
         */
        public static PdfFileSpecification FileEmbedded(PdfWriter writer, String filePath, String fileDisplay, byte[] fileStore, String mimeType, PdfDictionary fileParameter, int compressionLevel)
        {
            PdfFileSpecification fs = new PdfFileSpecification();

            fs.writer = writer;
            fs.Put(PdfName.F, new PdfString(fileDisplay));
            fs.SetUnicodeFileName(fileDisplay, false);
            PdfEFStream          stream;
            Stream               inp = null;
            PdfIndirectReference refi;
            PdfIndirectReference refFileLength = null;

            try {
                if (fileStore == null)
                {
                    refFileLength = writer.PdfIndirectReference;
                    if (File.Exists(filePath))
                    {
                        inp = new FileStream(filePath, FileMode.Open, FileAccess.Read);
                    }
                    else
                    {
                        if (filePath.StartsWith("file:/") || filePath.StartsWith("http://") || filePath.StartsWith("https://"))
                        {
                            WebRequest wr = WebRequest.Create(filePath);
                            wr.Credentials = CredentialCache.DefaultCredentials;
                            inp            = wr.GetResponse().GetResponseStream();
                        }
                        else
                        {
                            inp = StreamUtil.GetResourceStream(filePath);
                            if (inp == null)
                            {
                                throw new IOException(MessageLocalization.GetComposedMessage("1.not.found.as.file.or.resource", filePath));
                            }
                        }
                    }
                    stream = new PdfEFStream(inp, writer);
                }
                else
                {
                    stream = new PdfEFStream(fileStore);
                }
                stream.Put(PdfName.TYPE, PdfName.EMBEDDEDFILE);
                stream.FlateCompress(compressionLevel);

                PdfDictionary param = new PdfDictionary();
                if (fileParameter != null)
                {
                    param.Merge(fileParameter);
                }
                if (!param.Contains(PdfName.MODDATE))
                {
                    param.Put(PdfName.MODDATE, new PdfDate());
                }
                if (fileStore == null)
                {
                    stream.Put(PdfName.PARAMS, refFileLength);
                }
                else
                {
                    param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength));
                    stream.Put(PdfName.PARAMS, param);
                }

                if (mimeType != null)
                {
                    stream.Put(PdfName.SUBTYPE, new PdfName(mimeType));
                }

                refi = writer.AddToBody(stream).IndirectReference;
                if (fileStore == null)
                {
                    stream.WriteLength();
                    param.Put(PdfName.SIZE, new PdfNumber(stream.RawLength));
                    writer.AddToBody(param, refFileLength);
                }
            }
            finally {
                if (inp != null)
                {
                    try{ inp.Close(); }catch {}
                }
            }
            PdfDictionary f = new PdfDictionary();

            f.Put(PdfName.F, refi);
            f.Put(PdfName.UF, refi);
            fs.Put(PdfName.EF, f);
            return(fs);
        }
        /**
         * Extracts locations from the redact annotations contained in the document and applied to the given page.
         */
        private IList<PdfCleanUpLocation> ExtractLocationsFromRedactAnnots(int page, PdfDictionary pageDict) {
            List<PdfCleanUpLocation> locations = new List<PdfCleanUpLocation>();

            if (pageDict.Contains(PdfName.ANNOTS)) {
                PdfArray annotsArray = pageDict.GetAsArray(PdfName.ANNOTS);

                for (int i = 0; i < annotsArray.Size; ++i) {
                    PdfIndirectReference annotIndirRef = annotsArray.GetAsIndirectObject(i);
                    PdfDictionary annotDict = annotsArray.GetAsDict(i);
                    PdfName annotSubtype = annotDict.GetAsName(PdfName.SUBTYPE);

                    if (annotSubtype.Equals(PdfName.REDACT)) {
                        SaveRedactAnnotIndirRef(page, annotIndirRef.ToString());
                        locations.AddRange(ExtractLocationsFromRedactAnnot(page, i, annotDict));
                    }
                }
            }

            return locations;
        }
        /**
         * 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.
         * @param imageDictionary the dictionary of the inline image
         * @param ps the content parser
         * @return the samples of the image
         * @throws IOException if anything bad happens during parsing
         */
        private static byte[] ParseUnfilteredSamples(PdfDictionary imageDictionary, PdfDictionary colorSpaceDic, PdfContentParser 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.Contains(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];
            PRTokeniser 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 (!PRTokeniser.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 InlineImageParseException("End of content stream reached before end of image data");

                bytes[i] = (byte)ch;
            }
            PdfObject ei = ps.ReadPRObject();
            if (!ei.ToString().Equals("EI"))
                throw new InlineImageParseException("EI not found after end of image data");

            return bytes;
        }
        /**
         * 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
         * @param imageDictionary the dictionary of the inline image
         * @param ps the content parser
         * @return the samples of the image
         * @throws IOException if anything bad happens during parsing
         */
        private static byte[] ParseInlineImageSamples(PdfDictionary imageDictionary, PdfDictionary colorSpaceDic, PdfContentParser ps)
        {
            // by the time we get to here, we have already parsed the ID operator

            if (!imageDictionary.Contains(PdfName.FILTER)){
                return ParseUnfilteredSamples(imageDictionary, colorSpaceDic, ps);
            }

            // read all content until we reach an EI operator surrounded by whitespace.
            // The following algorithm has two potential issues: what if the image stream
            // contains <ws>EI<ws> ?
            // Plus, there are some streams that don't have the <ws> before the EI operator
            // it sounds like we would have to actually decode the content stream, which
            // I'd rather avoid right now.
            MemoryStream baos = new MemoryStream();
            MemoryStream accumulated = new MemoryStream();
            int ch;
            int found = 0;
            PRTokeniser tokeniser = ps.GetTokeniser();
            byte[] ff = null;

            while ((ch = tokeniser.Read()) != -1){
                if (found == 0 && PRTokeniser.IsWhitespace(ch)){
                    found++;
                    accumulated.WriteByte((byte)ch);
                } else if (found == 1 && ch == 'E'){
                    found++;
                    accumulated.WriteByte((byte)ch);
                } else if (found == 1 && PRTokeniser.IsWhitespace(ch)){
                    // this clause is needed if we have a white space character that is part of the image data
                    // followed by a whitespace character that precedes the EI operator.  In this case, we need
                    // to flush the first whitespace, then treat the current whitespace as the first potential
                    // character for the end of stream check.  Note that we don't increment 'found' here.
                    baos.Write(ff = accumulated.ToArray(), 0, ff.Length);
                    accumulated.SetLength(0);
                    accumulated.WriteByte((byte)ch);
                } else if (found == 2 && ch == 'I'){
                    found++;
                    accumulated.WriteByte((byte)ch);
                } else if (found == 3 && PRTokeniser.IsWhitespace(ch)){
                    return baos.ToArray();
                } else {
                    baos.Write(ff = accumulated.ToArray(), 0, ff.Length);
                    accumulated.SetLength(0);

                    baos.WriteByte((byte)ch);
                    found = 0;
                }
            }
            throw new InlineImageParseException("Could not find image data or EI");
        }
Exemple #15
0
 /**
  * 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. 
  * @param imageDictionary the dictionary of the inline image
  * @param ps the content parser
  * @return the samples of the image
  * @throws IOException if anything bad happens during parsing
  */
 private static byte[] ParseUnfilteredSamples(PdfDictionary imageDictionary, PdfContentParser 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.Contains(PdfName.FILTER))
         throw new ArgumentException("Dictionary contains filters");
     
     PdfNumber h = imageDictionary.GetAsNumber(PdfName.HEIGHT);
     
     int bytesToRead = ComputeBytesPerRow(imageDictionary) * h.IntValue;
     byte[] bytes = new byte[bytesToRead];
     PRTokeniser tokeniser = ps.GetTokeniser();
     
     tokeniser.Read(); // skip next character (which better be a whitespace character - I suppose we could check for this)
     for (int i = 0; i < bytesToRead; i++){
         int ch = tokeniser.Read();
         if (ch == -1)
             throw new InlineImageParseException("End of content stream reached before end of image data");
         
         bytes[i] = (byte)ch;
     }
     PdfObject ei = ps.ReadPRObject();
     if (!ei.ToString().Equals("EI"))
         throw new InlineImageParseException("EI not found after end of image data");
     
     return bytes;
 }