Esempio n. 1
0
        /**
         * Displays a summary of the entries in the XObject dictionary for the stream
         * @param resourceDic the resource dictionary for the stream
         * @return a string with the summary of the entries
         * @throws IOException
         * @since 5.0.2
         */
        public static String GetXObjectDetail(PdfDictionary resourceDic)
        {
            StringBuilder sb = new StringBuilder();

            PdfDictionary xobjects = resourceDic.GetAsDict(PdfName.XOBJECT);

            if (xobjects == null)
            {
                return("No XObjects");
            }
            foreach (PdfName entryName in xobjects.Keys)
            {
                PdfStream xobjectStream = xobjects.GetAsStream(entryName);

                sb.Append("------ " + entryName + " - subtype = " + xobjectStream.Get(PdfName.SUBTYPE) + " = " + xobjectStream.GetAsNumber(PdfName.LENGTH) + " bytes ------\n");

                if (!xobjectStream.Get(PdfName.SUBTYPE).Equals(PdfName.IMAGE))
                {
                    byte[] contentBytes = ContentByteUtils.GetContentBytesFromContentObject(xobjectStream);

                    foreach (byte b in contentBytes)
                    {
                        sb.Append((char)b);
                    }

                    sb.Append("------ " + entryName + " - subtype = " + xobjectStream.Get(PdfName.SUBTYPE) + "End of Content" + "------\n");
                }
            }

            return(sb.ToString());
        }
            public void HandleXObject(PdfContentStreamProcessor processor, PdfStream stream, PdfIndirectReference refi)
            {
                PdfDictionary resources = stream.GetAsDict(PdfName.RESOURCES);

                // we read the content bytes up here so if it fails we don't leave the graphics state stack corrupted
                // this is probably not necessary (if we fail on this, probably the entire content stream processing
                // operation should be rejected
                byte[] contentBytes;
                contentBytes = ContentByteUtils.GetContentBytesFromContentObject(stream);
                PdfArray matrix = stream.GetAsArray(PdfName.MATRIX);

                new PushGraphicsState().Invoke(processor, null, null);

                if (matrix != null)
                {
                    float  a          = matrix.GetAsNumber(0).FloatValue;
                    float  b          = matrix.GetAsNumber(1).FloatValue;
                    float  c          = matrix.GetAsNumber(2).FloatValue;
                    float  d          = matrix.GetAsNumber(3).FloatValue;
                    float  e          = matrix.GetAsNumber(4).FloatValue;
                    float  f          = matrix.GetAsNumber(5).FloatValue;
                    Matrix formMatrix = new Matrix(a, b, c, d, e, f);

                    processor.Gs().ctm = formMatrix.Multiply(processor.Gs().ctm);
                }

                processor.ProcessContent(contentBytes, resources);

                new PopGraphicsState().Invoke(processor, null, null);
            }
        /**
         * Processes content from the specified page number using the specified listener
         * @param <E> the type of the renderListener - this makes it easy to chain calls
         * @param pageNumber the page number to process
         * @param renderListener the listener that will receive render callbacks
         * @return the provided renderListener
         * @throws IOException if operations on the reader fail
         */

        public E ProcessContent <E>(int pageNumber, E renderListener) where E : IRenderListener
        {
            PdfDictionary pageDic      = reader.GetPageN(pageNumber);
            PdfDictionary resourcesDic = pageDic.GetAsDict(PdfName.RESOURCES);

            PdfContentStreamProcessor processor = new PdfContentStreamProcessor(renderListener);

            processor.ProcessContent(ContentByteUtils.GetContentBytesForPage(reader, pageNumber), resourcesDic);
            return(renderListener);
        }
        /**
         * Gets the content bytes of a page from a reader
         * @param reader  the reader to get content bytes from
         * @param pageNum   the page number of page you want get the content stream from
         * @return  a byte array with the effective content stream of a page
         * @throws IOException
         * @since 5.0.1
         */
        public static byte[] GetContentBytesForPage(PdfReader reader, int pageNum)
        {
            PdfDictionary pageDictionary = reader.GetPageN(pageNum);
            PdfObject     contentObject  = pageDictionary.Get(PdfName.CONTENTS);

            if (contentObject == null)
            {
                return(new byte[0]);
            }

            byte[] contentBytes = ContentByteUtils.GetContentBytesFromContentObject(contentObject);
            return(contentBytes);
        }
        /**
         * Processes content from the specified page number using the specified listener.
         * Also allows registration of custom ContentOperators
         * @param <E> the type of the renderListener - this makes it easy to chain calls
         * @param pageNumber the page number to process
         * @param renderListener the listener that will receive render callbacks
         * @param additionalContentOperators an optional dictionary of custom IContentOperators for rendering instructions
         * @return the provided renderListener
         * @throws IOException if operations on the reader fail
         */
        public virtual E ProcessContent <E>(int pageNumber, E renderListener, IDictionary <string, IContentOperator> additionalContentOperators) where E : IRenderListener
        {
            PdfDictionary pageDic      = reader.GetPageN(pageNumber);
            PdfDictionary resourcesDic = pageDic.GetAsDict(PdfName.RESOURCES);

            PdfContentStreamProcessor processor = new PdfContentStreamProcessor(renderListener);

            foreach (KeyValuePair <string, IContentOperator> entry in additionalContentOperators)
            {
                processor.RegisterContentOperator(entry.Key, entry.Value);
            }
            processor.ProcessContent(ContentByteUtils.GetContentBytesForPage(reader, pageNumber), resourcesDic);
            return(renderListener);
        }