예제 #1
0
        /**
         * If the child of a structured element is a dictionary, we inspect the
         * child; we may also draw a tag.
         *
         * @param k
         *            the child dictionary to inspect
         */
        public void InspectChildDictionary(PdfDictionary k)
        {
            if (k == null)
            {
                return;
            }
            PdfName s = k.GetAsName(PdfName.S);

            if (s != null)
            {
                String tagN = PdfName.DecodeName(s.ToString());
                String tag  = FixTagName(tagN);
                outp.Write("<");
                outp.Write(tag);
                outp.Write(">");
                PdfDictionary dict = k.GetAsDict(PdfName.PG);
                if (dict != null)
                {
                    ParseTag(tagN, k.GetDirectObject(PdfName.K), dict);
                }
                InspectChild(k.GetDirectObject(PdfName.K));
                outp.Write("</");
                outp.Write(tag);
                outp.WriteLine(">");
            }
            else
            {
                InspectChild(k.GetDirectObject(PdfName.K));
            }
        }
예제 #2
0
        /**
         * If the child of a structured element is a dictionary, we inspect the
         * child; we may also draw a tag.
         *
         * @param k
         *            the child dictionary to inspect
         */
        virtual public void InspectChildDictionary(PdfDictionary k, bool inspectAttributes)
        {
            if (k == null)
            {
                return;
            }
            PdfName s = k.GetAsName(PdfName.S);

            if (s != null)
            {
                String tagN = PdfName.DecodeName(s.ToString());
                String tag  = FixTagName(tagN);
                outp.Write("<");
                outp.Write(tag);
                if (inspectAttributes)
                {
                    PdfDictionary a = k.GetAsDict(PdfName.A);
                    if (a != null)
                    {
                        Dictionary <PdfName, PdfObject> .KeyCollection keys = a.Keys;
                        foreach (PdfName key in keys)
                        {
                            outp.Write(' ');
                            PdfObject value = a.Get(key);
                            value = PdfReader.GetPdfObject(value);
                            outp.Write(XmlName(key));
                            outp.Write("=\"");
                            outp.Write(value.ToString());
                            outp.Write("\"");
                        }
                    }
                }
                outp.Write(">");
                PdfObject alt = k.Get(PdfName.ALT);
                if (alt != null && alt.ToString() != null)
                {
                    outp.Write("<alt><![CDATA[");
                    outp.Write(Regex.Replace(alt.ToString(), "[\\000]*", ""));
                    outp.Write("]]></alt>");
                }
                PdfDictionary dict = k.GetAsDict(PdfName.PG);
                if (dict != null)
                {
                    ParseTag(tagN, k.GetDirectObject(PdfName.K), dict);
                }
                InspectChild(k.GetDirectObject(PdfName.K));
                outp.Write("</");
                outp.Write(tag);
                outp.WriteLine(">");
            }
            else
            {
                InspectChild(k.GetDirectObject(PdfName.K));
            }
        }
 private bool ObjectsIsEquals(PdfDictionary outDict, PdfDictionary cmpDict)
 {
     foreach (PdfName key in cmpDict.Keys)
     {
         if (key.CompareTo(PdfName.PARENT) == 0)
         {
             continue;
         }
         if (key.CompareTo(PdfName.BASEFONT) == 0 || key.CompareTo(PdfName.FONTNAME) == 0)
         {
             PdfObject cmpObj = cmpDict.GetDirectObject(key);
             if (cmpObj.IsName() && cmpObj.ToString().IndexOf('+') > 0)
             {
                 PdfObject outObj = outDict.GetDirectObject(key);
                 if (!outObj.IsName())
                 {
                     return(false);
                 }
                 String cmpName = cmpObj.ToString().Substring(cmpObj.ToString().IndexOf('+'));
                 String outName = outObj.ToString().Substring(outObj.ToString().IndexOf('+'));
                 if (!cmpName.Equals(outName))
                 {
                     return(false);
                 }
                 continue;
             }
         }
         if (!ObjectsIsEquals(outDict.Get(key), cmpDict.Get(key)))
         {
             return(false);
         }
     }
     return(true);
 }
예제 #4
0
        /**
         * Shows the detail of a dictionary.
         * @param dic   the dictionary of which you want the detail
         * @param depth the depth of the current dictionary (for nested dictionaries)
         * @return  a String representation of the dictionary
         */
        public static String GetDictionaryDetail(PdfDictionary dic, int depth)
        {
            StringBuilder builder = new StringBuilder();

            builder.Append('(');
            IList <PdfName> subDictionaries = new List <PdfName>();

            foreach (PdfName key in dic.Keys)
            {
                PdfObject val = dic.GetDirectObject(key);
                if (val.IsDictionary())
                {
                    subDictionaries.Add(key);
                }
                builder.Append(key);
                builder.Append('=');
                builder.Append(val);
                builder.Append(", ");
            }
            builder.Length = builder.Length - 2;
            builder.Append(')');
            foreach (PdfName pdfSubDictionaryName in subDictionaries)
            {
                builder.Append('\n');
                for (int i = 0; i < depth + 1; i++)
                {
                    builder.Append('\t');
                }
                builder.Append("Subdictionary ");
                builder.Append(pdfSubDictionaryName);
                builder.Append(" = ");
                builder.Append(GetDictionaryDetail(dic.GetAsDict(pdfSubDictionaryName), depth + 1));
            }
            return(builder.ToString());
        }
예제 #5
0
        /// <summary>
        /// Updates the pagination in the footer.
        /// </summary>
        /// <param name="inputPdf">The pdf to modify.</param>
        /// <param name="outputPdf">The pdf created with updated pagination.</param>
        public static void UpdateFooterPagination(string inputPdf, string outputPdf)
        {
            PdfReader  reader = new PdfReader(inputPdf);
            FileStream fs     = new FileStream(outputPdf, FileMode.Create, FileAccess.Write);
            int        n      = reader.NumberOfPages;

            for (int i = 1; i <= n; i++)
            {
                PdfDictionary dict = reader.GetPageN(i);
                PdfObject     obj  = dict.GetDirectObject(PdfName.CONTENTS);
                if (obj.GetType() == typeof(PRStream))
                {
                    PRStream stream = (PRStream)obj;
                    byte[]   data   = PdfReader.GetStreamBytes(stream);
                    String   oldStr = System.Text.Encoding.UTF8.GetString(data);

                    //Get the string matching the pagination
                    String pageString = CommonUtils.MatchRegex(oldStr, @"\[\(Seite \)\]TJ.*\[\(");

                    //Regex replacement of page string with updated page number
                    String updatedPageString = Regex.Replace(pageString, @"\[\(\d+\)\]", "[(" + i + ")]");
                    String newString         = Regex.Replace(oldStr, @"\[\(Seite \)\]TJ.*\[\(", updatedPageString, RegexOptions.Singleline);
                    stream.SetData(System.Text.Encoding.UTF8.GetBytes(newString));
                }
            }
            PdfStamper stamper = new PdfStamper(reader, fs);

            stamper.Close();
            reader.Close();
        }
예제 #6
0
        protected void TestXObject(bool shouldExist, int page, String xObjectName)
        {
            PdfReader reader            = null;
            RandomAccessFileOrArray raf = null;

            raf    = new RandomAccessFileOrArray(pdfContent[output]);
            reader = new PdfReader(raf, null);
            try
            {
                PdfDictionary dictionary = reader.GetPageN(page);

                PdfDictionary resources       = (PdfDictionary)dictionary.Get(PdfName.RESOURCES);
                PdfDictionary xobject         = (PdfDictionary)resources.Get(PdfName.XOBJECT);
                PdfObject     directXObject   = xobject.GetDirectObject(new PdfName(xObjectName));
                PdfObject     indirectXObject = xobject.Get(new PdfName(xObjectName));

                if (shouldExist)
                {
                    Assert.NotNull(indirectXObject);
                    Assert.NotNull(directXObject);
                }
                else
                {
                    Assert.IsNull(indirectXObject);
                    Assert.IsNull(directXObject);
                }
            }
            finally
            {
                reader.Close();
            }
        }
 /**
  * Shows the detail of a dictionary.
  * @param dic   the dictionary of which you want the detail
  * @param depth the depth of the current dictionary (for nested dictionaries)
  * @return  a String representation of the dictionary
  */
 public static  String GetDictionaryDetail(PdfDictionary dic, int depth){
     StringBuilder builder = new StringBuilder();
     builder.Append('(');
     IList<PdfName> subDictionaries = new List<PdfName>();
     foreach (PdfName key in dic.Keys) {
         PdfObject val = dic.GetDirectObject(key);
         if (val.IsDictionary())
             subDictionaries.Add(key);
         builder.Append(key);
         builder.Append('=');
         builder.Append(val);
         builder.Append(", ");
     }
     builder.Length = builder.Length-2;
     builder.Append(')');
     foreach (PdfName pdfSubDictionaryName in subDictionaries) {
         builder.Append('\n');
         for (int i = 0; i < depth+1; i++){
             builder.Append('\t');
         }
         builder.Append("Subdictionary ");
         builder.Append(pdfSubDictionaryName);
         builder.Append(" = ");
         builder.Append(GetDictionaryDetail(dic.GetAsDict(pdfSubDictionaryName), depth+1));
     }
     return builder.ToString();
 }
예제 #8
0
        /**
         * In case an object is an array, a dictionary or a stream,
         * we need to loop over the entries and process them one by one.
         * @param object    the object to examine
         */
        protected void LoopOver(PdfObject @object)
        {
            switch (@object.Type)
            {
            case PdfObject.ARRAY:
                PdfArray array = (PdfArray)@object;
                for (int i = 0; i < array.Size; i++)
                {
                    Process(array.GetDirectObject(i));
                }
                break;

            case PdfObject.DICTIONARY:
            case PdfObject.STREAM:
                PdfDictionary dict = (PdfDictionary)@object;
                if (dict.IsPages())
                {
                    break;
                }
                foreach (PdfName name in dict.Keys)
                {
                    Process(dict.GetDirectObject(name));
                }
                break;
            }
        }
예제 #9
0
 /**
  * Looks at a StructElem dictionary, and processes it.
  * @param dict	the StructElem dictionary that needs to be examined
  * @param ref	the reference to the StructElem dictionary
  * @throws DocumentException
  */
 protected void ProcessStructElems(PdfDictionary structElem, PdfIndirectReference refa)
 {
     LOGGER.Info(String.Format("addStructureItems({0}, {1})", structElem, refa));
     if (structElem == null)
     {
         return;
     }
     ProcessStructElemKids(structElem, refa, structElem.GetDirectObject(PdfName.K));
 }
예제 #10
0
 /**
  * Creates a StructureObject for an OBJR dictionary.
  * @param structElem	the parent structure element
  * @param ref			the reference of the parent structure element
  * @param dict			the object reference dictionary
  */
 public StructureObject(PdfDictionary structElem, PdfIndirectReference refa, PdfDictionary dict) {
     this.structElem = structElem;
     this.refa = refa;
     this.obj = dict.GetDirectObject(PdfName.OBJ);
     this.objref = dict.GetAsIndirectObject(PdfName.OBJ);
     this.structParent = ((PdfDictionary) obj).GetAsNumber(PdfName.STRUCTPARENT).IntValue;
     PdfIndirectReference pg = dict.GetAsIndirectObject(PdfName.PG);
     if (pg == null)
         pg = structElem.GetAsIndirectObject(PdfName.PG);
     this.pageref = pg.Number;
 }
예제 #11
0
        public byte[] GetImageAsBytes()
        {
            if (streamBytes == null)
            {
                return(null);
            }
            if (!decoded)
            {
                PdfName filter = dictionary.GetAsName(PdfName.FILTER);
                if (PdfName.DCTDECODE.Equals(filter))
                {
                    fileType = TYPE_JPG;
                    return(streamBytes);
                }
                else if (PdfName.JPXDECODE.Equals(filter))
                {
                    fileType = TYPE_JP2;
                    return(streamBytes);
                }
                return(null);
            }
            pngColorType = -1;
            width        = dictionary.GetAsNumber(PdfName.WIDTH).IntValue;
            height       = dictionary.GetAsNumber(PdfName.HEIGHT).IntValue;
            bpc          = dictionary.GetAsNumber(PdfName.BITSPERCOMPONENT).IntValue;
            pngBitDepth  = bpc;
            PdfObject colorspace = dictionary.GetDirectObject(PdfName.COLORSPACE);

            palette = null;
            icc     = null;
            stride  = 0;
            FindColorspace(colorspace, true);
            if (pngColorType < 0)
            {
                return(null);
            }
            MemoryStream ms  = new MemoryStream();
            PngWriter    png = new PngWriter(ms);

            png.WriteHeader(width, height, pngBitDepth, pngColorType);
            if (icc != null)
            {
                png.WriteIccProfile(icc);
            }
            if (palette != null)
            {
                png.WritePalette(palette);
            }
            png.WriteData(streamBytes, stride);
            png.WriteEnd();
            fileType = TYPE_PNG;
            return(ms.ToArray());
        }
예제 #12
0
        /**
         * Parses a string with structured content.
         *
         * @param reader
         *            the PdfReader that has access to the PDF file
         * @param os
         *            the Stream to which the resulting xml will be written
         * @param charset
         *            the charset to encode the data
         * @since 5.0.5
         */
        public void ConvertToXml(PdfReader reader, Stream os, Encoding encoding)
        {
            this.reader = reader;
            outp        = new StreamWriter(os, encoding);
            // get the StructTreeRoot from the root obj
            PdfDictionary catalog = reader.Catalog;
            PdfDictionary struc   = catalog.GetAsDict(PdfName.STRUCTTREEROOT);

            // Inspect the child or children of the StructTreeRoot
            InspectChild(struc.GetDirectObject(PdfName.K));
            outp.Flush();
            outp.Close();
        }
예제 #13
0
        /**
         * Creates a StructureObject for an OBJR dictionary.
         * @param structElem	the parent structure element
         * @param ref			the reference of the parent structure element
         * @param dict			the object reference dictionary
         */
        public StructureObject(PdfDictionary structElem, PdfIndirectReference refa, PdfDictionary dict)
        {
            this.structElem   = structElem;
            this.refa         = refa;
            this.obj          = dict.GetDirectObject(PdfName.OBJ);
            this.objref       = dict.GetAsIndirectObject(PdfName.OBJ);
            this.structParent = ((PdfDictionary)obj).GetAsNumber(PdfName.STRUCTPARENT).IntValue;
            PdfIndirectReference pg = dict.GetAsIndirectObject(PdfName.PG);

            if (pg == null)
            {
                pg = structElem.GetAsIndirectObject(PdfName.PG);
            }
            this.pageref = pg.Number;
        }
예제 #14
0
        public static void Test(string inputFile, string outputFile)
        {
            Console.WriteLine("Test");
            String formFile = inputFile;
            String newFile  = outputFile;

            PdfReader     reader = new PdfReader(formFile);
            PdfDictionary dict   = reader.GetPageN(1);

            foreach (var v in dict)
            {
                Console.WriteLine("{0}", v);
            }
            PdfObject obj = dict.GetDirectObject(PdfName.CONTENTS);

            if (obj is PRStream stream)
            {
                byte[] data = PdfReader.GetStreamBytes(stream);

                string dd = System.Text.Encoding.ASCII.GetString(data);

                Console.WriteLine(dd);

                stream.SetData(Encoding.ASCII.GetBytes(dd));
            }

            //PdfStamper stamper = new PdfStamper(reader, new FileStream(newFile, FileMode.Create));

            //AcroFields fields = reader.AcroFields;   //  stamper.AcroFields;



            //foreach(var v in fields.Fields)
            //{
            //    Console.WriteLine("{0}", v);
            //}
            // set form fields

            //fields.SetField("{TO}", "John Doe");

            //fields.SetField("{FROM}", "2 Milky Way, London");

            //stamper.FormFlattening = true;

            //stamper.Close();

            reader.Close();
        }
 public override PdfObject GetDirectObject(PdfName key)
 {
     for (int i = resourcesStack.Count - 1; i >= 0; i--)
     {
         PdfDictionary subResource = resourcesStack[i];
         if (subResource != null)
         {
             PdfObject obj = subResource.GetDirectObject(key);
             if (obj != null)
             {
                 return(obj);
             }
         }
     }
     return(base.GetDirectObject(key)); // shouldn't be necessary, but just in case we've done something crazy
 }
예제 #16
0
        /**
         * Parses a string with structured content.
         *
         * @param reader
         *            the PdfReader that has access to the PDF file
         * @param os
         *            the Stream to which the resulting xml will be written
         * @param charset
         *            the charset to encode the data
         * @since 5.0.5
         */
        public virtual void ConvertToXml(PdfReader reader, Stream os, Encoding encoding)
        {
            this.reader = reader;
            outp        = new StreamWriter(os, encoding);
            // get the StructTreeRoot from the root obj
            PdfDictionary catalog = reader.Catalog;
            PdfDictionary struc   = catalog.GetAsDict(PdfName.STRUCTTREEROOT);

            if (struc == null)
            {
                throw new IOException(MessageLocalization.GetComposedMessage("no.structtreeroot.found"));
            }
            // Inspect the child or children of the StructTreeRoot
            InspectChild(struc.GetDirectObject(PdfName.K));
            outp.Flush();
            outp.Close();
        }
예제 #17
0
        public override PdfObject GetDirectObject(PdfName key)
        {
            for (int index = _resourceStack.Count - 1; index >= 0; index--)
            {
                PdfDictionary subResource = _resourceStack[index];

                if (subResource != null)
                {
                    PdfObject obj = subResource.GetDirectObject(key);
                    if (obj != null)
                    {
                        return(obj);
                    }
                }
            }
            return(base.GetDirectObject(key));
        }
        public static void FixPageNumberOnPage(String src, String dest, int pageIndex, string newPageLabel)
        {
            using (PdfReader reader = new PdfReader(src))
            {
                PdfDictionary dict    = reader.GetPageN(pageIndex);
                PdfObject     o       = dict.GetDirectObject(PdfName.CONTENTS);
                var           streams = SearchAllStreams(reader, o);
                foreach (var stream in streams)
                {
                    FixPageNumberOnStream(stream as PRStream, pageIndex.ToString(), newPageLabel);
                }

                using (PdfStamper stamper = new PdfStamper(reader, new FileStream(dest, FileMode.Create)))
                    stamper.Close();

                reader.Close();
            }
        }
예제 #19
0
            public override void ConvertToXml(PdfReader reader, Stream os, Encoding encoding)
            {
                this.reader = reader;
                outp        = new StreamWriter(os, encoding);
                outp.Write("<root>");
                // get the StructTreeRoot from the root object
                PdfDictionary catalog = reader.Catalog;
                PdfDictionary str     = catalog.GetAsDict(PdfName.STRUCTTREEROOT);

                if (str == null)
                {
                    throw new IOException("No StructTreeRoot");
                }
                // Inspect the child or children of the StructTreeRoot
                InspectChild(str.GetDirectObject(PdfName.K));
                outp.Write("</root>");
                outp.Close();
            }
예제 #20
0
        /**
         * Searches for a tag in a page.
         *
         * @param tag
         *            the name of the tag
         * @param obj
         *            an identifier to find the marked content
         * @param page
         *            a page dictionary
         * @throws IOException
         */
        public void ParseTag(String tag, PdfObject obj, PdfDictionary page)
        {
            PRStream stream = (PRStream)page.GetAsStream(PdfName.CONTENTS);

            // if the identifier is a number, we can extract the content right away
            if (obj is PdfNumber)
            {
                PdfNumber                  mcid      = (PdfNumber)obj;
                RenderFilter               filter    = new MarkedContentRenderFilter(mcid.IntValue);
                ITextExtractionStrategy    strategy  = new SimpleTextExtractionStrategy();
                FilteredTextRenderListener listener  = new FilteredTextRenderListener(strategy, new RenderFilter[] { filter });
                PdfContentStreamProcessor  processor = new PdfContentStreamProcessor(
                    listener);
                processor.ProcessContent(PdfReader.GetStreamBytes(stream), page
                                         .GetAsDict(PdfName.RESOURCES));
                outp.Write(SimpleXMLParser.EscapeXML(listener.GetResultantText(), true));
            }
            // if the identifier is an array, we call the parseTag method
            // recursively
            else if (obj is PdfArray)
            {
                PdfArray arr = (PdfArray)obj;
                int      n   = arr.Size;
                for (int i = 0; i < n; i++)
                {
                    ParseTag(tag, arr[i], page);
                    if (i < n - 1)
                    {
                        outp.WriteLine();
                    }
                }
            }
            // if the identifier is a dictionary, we get the resources from the
            // dictionary
            else if (obj is PdfDictionary)
            {
                PdfDictionary mcr = (PdfDictionary)obj;
                ParseTag(tag, mcr.GetDirectObject(PdfName.MCID), mcr
                         .GetAsDict(PdfName.PG));
            }
        }
예제 #21
0
        /**
         * Creates a StructureObject for an OBJR dictionary.
         * @param structElem	the parent structure element
         * @param ref			the reference of the parent structure element
         * @param dict			the object reference dictionary
         */
        public StructureObject(PdfDictionary structElem, PdfIndirectReference refa, PdfDictionary dict)
        {
            this.structElem = structElem;
            this.refa       = refa;
            this.obj        = dict.GetDirectObject(PdfName.OBJ);
            this.objref     = dict.GetAsIndirectObject(PdfName.OBJ);
            PdfNumber sp = ((PdfDictionary)obj).GetAsNumber(PdfName.STRUCTPARENT);

            if (sp == null)
            {
                throw new InvalidPdfException("structparentid.not.found");
            }
            this.structParent = sp.IntValue;
            PdfIndirectReference pg = dict.GetAsIndirectObject(PdfName.PG);

            if (pg == null)
            {
                pg = structElem.GetAsIndirectObject(PdfName.PG);
            }
            this.pageref = pg.Number;
        }
        private List <PdfObject> Do(PdfLiteral oper, List <PdfObject> operands)
        {
            PdfName xobjectName = (PdfName)operands[0];

            //System.Diagnostics.Debug.WriteLine("Opr: " + xobjectName.ToString());

            PdfDictionary xobjects = _modifier.ResourceDictionary.GetAsDict(PdfName.XOBJECT);
            PdfObject     po       = xobjects.Get(xobjectName);

            int n = (po as PdfIndirectReference).Number;

            if (_convertedIndirectList.Contains(n))
            {
                return(operands);
            }
            else
            {
                _convertedIndirectList.Add(n);
            }

            PdfObject xobject = xobjects.GetDirectObject(xobjectName);

            if (xobject.IsStream())
            {
                PdfStream xobjectStream = (PdfStream)xobject;
                PdfName   subType       = xobjectStream.GetAsName(PdfName.SUBTYPE);

                if (subType == PdfName.FORM)
                {
                    this.Do_Form(xobjectStream);
                }
                else if (subType == PdfName.IMAGE)
                {
                    this.Do_Image(xobjectStream);
                }
            }

            return(operands);
        }
        /**
         * Displays an XObject using the registered handler for this XObject's subtype
         * @param xobjectName the name of the XObject to retrieve from the resource dictionary
         */
        private void DisplayXObject(PdfName xobjectName)
        {
            PdfDictionary xobjects      = resources.GetAsDict(PdfName.XOBJECT);
            PdfObject     xobject       = xobjects.GetDirectObject(xobjectName);
            PdfStream     xobjectStream = (PdfStream)xobject;

            PdfName subType = xobjectStream.GetAsName(PdfName.SUBTYPE);

            if (xobject.IsStream())
            {
                IXObjectDoHandler handler;
                xobjectDoHandlers.TryGetValue(subType, out handler);
                if (handler == null)
                {
                    handler = xobjectDoHandlers[PdfName.DEFAULT];
                }
                handler.HandleXObject(this, xobjectStream, xobjects.GetAsIndirectObject(xobjectName));
            }
            else
            {
                throw new InvalidOperationException(MessageLocalization.GetComposedMessage("XObject.1.is.not.a.stream", xobjectName));
            }
        }
예제 #24
0
 /// <summary>
 /// Replace texts.
 /// TODO: Los textos con formato creado en word introducen caracteres entre las palabras y no lo localiza.
 /// </summary>
 public void Replace()
 {
     using (FileStream stream = new FileStream(_pathSource, FileMode.Open))
     {
         using (PdfReader pdfReader = new PdfReader(stream))
         {
             for (int x = 1; x <= pdfReader.NumberOfPages; x++)
             {
                 using (FileStream streamDest = new FileStream(_pathDest, FileMode.Create))
                 {
                     PdfDictionary dict = pdfReader.GetPageN(x);
                     PdfObject     obj  = dict.GetDirectObject(PdfName.CONTENTS);
                     if (obj.GetType() == typeof(PRStream))
                     {
                         ReplacePRStream(obj);
                     }
                     if (obj.GetType() == typeof(PdfArray))
                     {
                         foreach (var r in (PdfArray)obj)
                         {
                             PRIndirectReference ir      = (PRIndirectReference)r;
                             PdfObject           refdObj = pdfReader.GetPdfObject(ir.Number);
                             if (refdObj.IsStream())
                             {
                                 ReplacePRStream(refdObj);
                             }
                         }
                     }
                     using (PdfStamper stamper = new PdfStamper(pdfReader, streamDest))
                     {
                     }
                 }
             }
         }
     }
 }
예제 #25
0
        /**
         * Creates a list of StructuredItem objects.
         * @param reader the reader holding the PDF to examine
         */
        public StructureItems(PdfReader reader)
        {
            PdfDictionary catalog = reader.Catalog;

            structTreeRoot = catalog.GetAsDict(PdfName.STRUCTTREEROOT);
            if (structTreeRoot == null)
            {
                throw new DocumentException(MessageLocalization.GetComposedMessage("can.t.read.document.structure"));
            }
            // Storing the parent tree
            parentTree = PdfNumberTree.ReadTree(structTreeRoot.GetAsDict(PdfName.PARENTTREE));
            structTreeRoot.Remove(PdfName.STRUCTPARENTS);
            // Examining the StructTreeRoot
            PdfObject objecta = structTreeRoot.GetDirectObject(PdfName.K);

            if (objecta == null)
            {
                return;
            }
            switch (objecta.Type)
            {
            case PdfObject.DICTIONARY:
                LOGGER.Info("StructTreeRoot refers to dictionary");
                ProcessStructElems((PdfDictionary)objecta, structTreeRoot.GetAsIndirectObject(PdfName.K));
                break;

            case PdfObject.ARRAY:
                LOGGER.Info("StructTreeRoot refers to array");
                PdfArray array = (PdfArray)objecta;
                for (int i = 0; i < array.Size; i++)
                {
                    ProcessStructElems(array.GetAsDict(i), array.GetAsIndirectObject(i));
                }
                break;
            }
        }
예제 #26
0
        /**
         * If the child of a structured element is a dictionary, we inspect the
         * child; we may also draw a tag.
         * 
         * @param k
         *            the child dictionary to inspect
         */
        virtual public void InspectChildDictionary(PdfDictionary k, bool inspectAttributes) {
            if (k == null)
                return;
            PdfName s = k.GetAsName(PdfName.S);
            if (s != null) {
                String tagN = PdfName.DecodeName(s.ToString());
			    String tag = FixTagName(tagN);
                outp.Write("<");
                outp.Write(tag);
                if (inspectAttributes) {
                    PdfDictionary a = k.GetAsDict(PdfName.A);
                    if (a != null) {
                        Dictionary<PdfName, PdfObject>.KeyCollection keys = a.Keys;
                        foreach (PdfName key in keys) {
                            outp.Write(' ');
                            PdfObject value = a.Get(key);
                            value = PdfReader.GetPdfObject(value);
                            outp.Write(XmlName(key));
                            outp.Write("=\"");
                            outp.Write(value.ToString());
                            outp.Write("\"");
                        }
                    }
                }
                outp.Write(">");
                PdfDictionary dict = k.GetAsDict(PdfName.PG);
                if (dict != null)
                    ParseTag(tagN, k.GetDirectObject(PdfName.K), dict);
                InspectChild(k.GetDirectObject(PdfName.K));
                outp.Write("</");
                outp.Write(tag);
                outp.WriteLine(">");
            } else
                InspectChild(k.GetDirectObject(PdfName.K));
        }
예제 #27
0
        /**
         * decodes the bytes currently captured in the streamBytes and replaces it with an image representation of the bytes
         * (this will either be a png or a tiff, depending on the color depth of the image)
         * @throws IOException
         */
        private void DecodeImageBytes()
        {
            if (streamContentType != null)
            {
                throw new ArgumentException(MessageLocalization.GetComposedMessage("Decoding.can't.happen.on.this.type.of.stream.(.1.)", streamContentType.FileExtension));
            }
            pngColorType = -1;
            PdfArray decode = dictionary.GetAsArray(PdfName.DECODE);

            width       = dictionary.GetAsNumber(PdfName.WIDTH).IntValue;
            height      = dictionary.GetAsNumber(PdfName.HEIGHT).IntValue;
            bpc         = dictionary.GetAsNumber(PdfName.BITSPERCOMPONENT).IntValue;
            pngBitDepth = bpc;
            PdfObject colorspace = dictionary.GetDirectObject(PdfName.COLORSPACE);

            if (colorspace is PdfName && colorSpaceDic != null)
            {
                PdfObject csLookup = colorSpaceDic.GetDirectObject((PdfName)colorspace);
                if (csLookup != null)
                {
                    colorspace = csLookup;
                }
            }

            palette = null;
            icc     = null;
            stride  = 0;
            FindColorspace(colorspace, true);
            MemoryStream ms = new MemoryStream();

            if (pngColorType < 0)
            {
                if (bpc != 8)
                {
                    throw new UnsupportedPdfException(MessageLocalization.GetComposedMessage("the.color.depth.1.is.not.supported", bpc));
                }
                if (PdfName.DEVICECMYK.Equals(colorspace))
                {
                }
                else if (colorspace is PdfArray)
                {
                    PdfArray  ca   = (PdfArray)colorspace;
                    PdfObject tyca = ca.GetDirectObject(0);
                    if (!PdfName.ICCBASED.Equals(tyca))
                    {
                        throw new UnsupportedPdfException(MessageLocalization.GetComposedMessage("the.color.space.1.is.not.supported", colorspace));
                    }
                    PRStream pr = (PRStream)ca.GetDirectObject(1);
                    int      n  = pr.GetAsNumber(PdfName.N).IntValue;
                    if (n != 4)
                    {
                        throw new UnsupportedPdfException(MessageLocalization.GetComposedMessage("N.value.1.is.not.supported", n));
                    }
                    icc = PdfReader.GetStreamBytes(pr);
                }
                else
                {
                    throw new UnsupportedPdfException(MessageLocalization.GetComposedMessage("the.color.space.1.is.not.supported", colorspace));
                }
                stride = 4 * width;
                TiffWriter wr = new TiffWriter();
                wr.AddField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_SAMPLESPERPIXEL, 4));
                wr.AddField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_BITSPERSAMPLE, new int[] { 8, 8, 8, 8 }));
                wr.AddField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_PHOTOMETRIC, TIFFConstants.PHOTOMETRIC_SEPARATED));
                wr.AddField(new TiffWriter.FieldLong(TIFFConstants.TIFFTAG_IMAGEWIDTH, width));
                wr.AddField(new TiffWriter.FieldLong(TIFFConstants.TIFFTAG_IMAGELENGTH, height));
                wr.AddField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_COMPRESSION, TIFFConstants.COMPRESSION_LZW));
                wr.AddField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_PREDICTOR, TIFFConstants.PREDICTOR_HORIZONTAL_DIFFERENCING));
                wr.AddField(new TiffWriter.FieldLong(TIFFConstants.TIFFTAG_ROWSPERSTRIP, height));
                wr.AddField(new TiffWriter.FieldRational(TIFFConstants.TIFFTAG_XRESOLUTION, new int[] { 300, 1 }));
                wr.AddField(new TiffWriter.FieldRational(TIFFConstants.TIFFTAG_YRESOLUTION, new int[] { 300, 1 }));
                wr.AddField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_RESOLUTIONUNIT, TIFFConstants.RESUNIT_INCH));
                wr.AddField(new TiffWriter.FieldAscii(TIFFConstants.TIFFTAG_SOFTWARE, Version.GetInstance().GetVersion));
                MemoryStream comp = new MemoryStream();
                TiffWriter.CompressLZW(comp, 2, imageBytes, height, 4, stride);
                byte[] buf = comp.ToArray();
                wr.AddField(new TiffWriter.FieldImage(buf));
                wr.AddField(new TiffWriter.FieldLong(TIFFConstants.TIFFTAG_STRIPBYTECOUNTS, buf.Length));
                if (icc != null)
                {
                    wr.AddField(new TiffWriter.FieldUndefined(TIFFConstants.TIFFTAG_ICCPROFILE, icc));
                }
                wr.WriteFile(ms);
                streamContentType = ImageBytesType.CCITT;
                imageBytes        = ms.ToArray();
                return;
            }
            else
            {
                PngWriter png = new PngWriter(ms);
                if (decode != null)
                {
                    if (pngBitDepth == 1)
                    {
                        // if the decode array is 1,0, then we need to invert the image
                        if (decode.GetAsNumber(0).IntValue == 1 && decode.GetAsNumber(1).IntValue == 0)
                        {
                            int len = imageBytes.Length;
                            for (int t = 0; t < len; ++t)
                            {
                                imageBytes[t] ^= 0xff;
                            }
                        }
                        else
                        {
                            // if the decode array is 0,1, do nothing.  It's possible that the array could be 0,0 or 1,1 - but that would be silly, so we'll just ignore that case
                        }
                    }
                    else
                    {
                        // todo: add decode transformation for other depths
                    }
                }
                png.WriteHeader(width, height, pngBitDepth, pngColorType);
                if (icc != null)
                {
                    png.WriteIccProfile(icc);
                }
                if (palette != null)
                {
                    png.WritePalette(palette);
                }
                png.WriteData(imageBytes, stride);
                png.WriteEnd();
                streamContentType = ImageBytesType.PNG;
                imageBytes        = ms.ToArray();
            }
        }
예제 #28
0
        public static MemoryStream RemoveText(MemoryStream streamInput, string watermarkText)
        {
            string    content;
            PRStream  stream;
            PdfArray  contentarray;
            PdfReader reader = new PdfReader(streamInput.ToArray());
            string    DEST   = "D:\\WebMes Projects\\NewYueWebMES\\YueWebMES.Service\\WebMES.API\\Attachments\\fa1.pdf";

            //reader.RemoveUnusedObjects();
            for (var i = 1; i <= reader.NumberOfPages; i++)
            {
                PdfDictionary dictionary = reader.GetPageN(i);//获取页

                try
                {
                    PdfObject pdfObj = dictionary.GetDirectObject(PdfName.CONTENTS);
                    if (pdfObj.GetType() == typeof(PRStream))
                    {
                        PRStream pst  = (PRStream)pdfObj;
                        byte[]   data = PdfReader.GetStreamBytes(pst);
                        content = System.Text.Encoding.ASCII.GetString(data);//获取pdf页内的文字内容
                        content = content.Replace(watermarkText, "");

                        pst.SetData(System.Text.Encoding.Default.GetBytes(content));
                    }
                }
                catch (Exception ex)
                {
                }

                //contentarray = page.GetAsArray(PdfName.CONTENTS);
                //if (contentarray != null)
                //{
                //    //Loop through content
                //    for (int j = 0; j < contentarray.Size; j++)
                //    {
                //        //Get the raw byte stream
                //        stream = (PRStream)contentarray.GetAsStream(j);
                //        //Convert to a string. NOTE, you might need a different encoding here
                //        content = System.Text.Encoding.ASCII.GetString(PdfReader.GetStreamBytes(stream));//获取pdf页内的文字内容
                //                                                                                         //Look for the OCG token in the stream as well as our watermarked text
                //        if (content.IndexOf("/OC") >= 0 || content.IndexOf(watermarkText) >= 0)//如果pdf内容包含水印文字
                //        {
                //            //Remove it by giving it zero length and zero data
                //            content = content.Replace(watermarkText, "");//替换水印文字为空
                //            byte[] byteArray = System.Text.Encoding.Default.GetBytes(content);//转换为byte[]
                //            stream.Put(PdfName.LENGTH, new PdfNumber(byteArray.Length));//重新指定大小

                //            stream.SetData(byteArray);//重新赋值
                //        }
                //    }
                //}
            }
            //PdfStamper stamper = new PdfStamper(reader, new FileStream(DEST, FileMode.Create, FileAccess.ReadWrite));
            MemoryStream mstream = new MemoryStream();
            PdfStamper   stamper = new PdfStamper(reader, mstream);

            stamper.Close();
            reader.Close();
            return(mstream);
        }
예제 #29
0
        public byte[] GetImageAsBytes()
        {
            if (streamBytes == null)
            {
                return(null);
            }
            if (!decoded)
            {
                // if the stream hasn't been decoded, check to see if it is a single stage JPG or JPX encoded stream.  If it is,
                // then we can just use stream as-is
                PdfName filter = dictionary.GetAsName(PdfName.FILTER);
                if (filter == null)
                {
                    PdfArray filterArray = dictionary.GetAsArray(PdfName.FILTER);
                    if (filterArray.Size == 1)
                    {
                        filter = filterArray.GetAsName(0);
                    }
                    else
                    {
                        throw new UnsupportedPdfException("Multi-stage filters not supported here (" + filterArray + ")");
                    }
                }
                if (PdfName.DCTDECODE.Equals(filter))
                {
                    fileType = TYPE_JPG;
                    return(streamBytes);
                }
                else if (PdfName.JPXDECODE.Equals(filter))
                {
                    fileType = TYPE_JP2;
                    return(streamBytes);
                }
                throw new UnsupportedPdfException("Unsupported stream filter " + filter);
            }
            pngColorType = -1;
            width        = dictionary.GetAsNumber(PdfName.WIDTH).IntValue;
            height       = dictionary.GetAsNumber(PdfName.HEIGHT).IntValue;
            bpc          = dictionary.GetAsNumber(PdfName.BITSPERCOMPONENT).IntValue;
            pngBitDepth  = bpc;
            PdfObject colorspace = dictionary.GetDirectObject(PdfName.COLORSPACE);

            palette = null;
            icc     = null;
            stride  = 0;
            FindColorspace(colorspace, true);
            MemoryStream ms = new MemoryStream();

            if (pngColorType < 0)
            {
                if (bpc != 8)
                {
                    return(null);
                }
                if (PdfName.DEVICECMYK.Equals(colorspace))
                {
                }
                else if (colorspace is PdfArray)
                {
                    PdfArray  ca   = (PdfArray)colorspace;
                    PdfObject tyca = ca.GetDirectObject(0);
                    if (!PdfName.ICCBASED.Equals(tyca))
                    {
                        return(null);
                    }
                    PRStream pr = (PRStream)ca.GetDirectObject(1);
                    int      n  = pr.GetAsNumber(PdfName.N).IntValue;
                    if (n != 4)
                    {
                        return(null);
                    }
                    icc = PdfReader.GetStreamBytes(pr);
                }
                else
                {
                    return(null);
                }
                stride = 4 * width;
                TiffWriter wr = new TiffWriter();
                wr.AddField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_SAMPLESPERPIXEL, 4));
                wr.AddField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_BITSPERSAMPLE, new int[] { 8, 8, 8, 8 }));
                wr.AddField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_PHOTOMETRIC, TIFFConstants.PHOTOMETRIC_SEPARATED));
                wr.AddField(new TiffWriter.FieldLong(TIFFConstants.TIFFTAG_IMAGEWIDTH, width));
                wr.AddField(new TiffWriter.FieldLong(TIFFConstants.TIFFTAG_IMAGELENGTH, height));
                wr.AddField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_COMPRESSION, TIFFConstants.COMPRESSION_LZW));
                wr.AddField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_PREDICTOR, TIFFConstants.PREDICTOR_HORIZONTAL_DIFFERENCING));
                wr.AddField(new TiffWriter.FieldLong(TIFFConstants.TIFFTAG_ROWSPERSTRIP, height));
                wr.AddField(new TiffWriter.FieldRational(TIFFConstants.TIFFTAG_XRESOLUTION, new int[] { 300, 1 }));
                wr.AddField(new TiffWriter.FieldRational(TIFFConstants.TIFFTAG_YRESOLUTION, new int[] { 300, 1 }));
                wr.AddField(new TiffWriter.FieldShort(TIFFConstants.TIFFTAG_RESOLUTIONUNIT, TIFFConstants.RESUNIT_INCH));
                wr.AddField(new TiffWriter.FieldAscii(TIFFConstants.TIFFTAG_SOFTWARE, Document.Version));
                MemoryStream comp = new MemoryStream();
                TiffWriter.CompressLZW(comp, 2, streamBytes, height, 4, stride);
                byte[] buf = comp.ToArray();
                wr.AddField(new TiffWriter.FieldImage(buf));
                wr.AddField(new TiffWriter.FieldLong(TIFFConstants.TIFFTAG_STRIPBYTECOUNTS, buf.Length));
                if (icc != null)
                {
                    wr.AddField(new TiffWriter.FieldUndefined(TIFFConstants.TIFFTAG_ICCPROFILE, icc));
                }
                wr.WriteFile(ms);
                fileType = TYPE_TIF;
                return(ms.ToArray());
            }
            PngWriter png = new PngWriter(ms);

            png.WriteHeader(width, height, pngBitDepth, pngColorType);
            if (icc != null)
            {
                png.WriteIccProfile(icc);
            }
            if (palette != null)
            {
                png.WritePalette(palette);
            }
            png.WriteData(streamBytes, stride);
            png.WriteEnd();
            fileType = TYPE_PNG;
            return(ms.ToArray());
        }