Exemplo n.º 1
0
        private static List <long> GetObjectNumbers(PdfRawStream stream)
        {
            var indexArray = (COSArray)stream.Dictionary.GetDictionaryObject(CosName.INDEX);

            // If Index doesn't exist, we will use the default values.
            if (indexArray == null)
            {
                indexArray = new COSArray();
                indexArray.add(CosInt.Zero);
                indexArray.add(stream.Dictionary.GetDictionaryObject(CosName.SIZE));
            }

            List <long> objNums = new List <long>();

            // Populates objNums with all object numbers available

            for (int i = 0; i < indexArray.Count; i += 2)
            {
                var longId = ((CosInt)indexArray.get(i)).AsLong();
                var size   = ((CosInt)indexArray.get(i + 1)).AsInt();

                for (int j = 0; j < size; j++)
                {
                    objNums.Add(longId + j);
                }
            }

            return(objNums);
        }
Exemplo n.º 2
0
        public COSArray Parse(IRandomAccessRead reader, CosBaseParser baseParser, CosObjectPool pool)
        {
            ReadHelper.ReadExpectedChar(reader, '[');
            var     po = new COSArray();
            CosBase pbo;

            ReadHelper.SkipSpaces(reader);
            int i;

            while (((i = reader.Peek()) > 0) && ((char)i != ']'))
            {
                pbo = baseParser.Parse(reader, pool);
                if (pbo is CosObject)
                {
                    // We have to check if the expected values are there or not PDFBOX-385
                    if (po.get(po.size() - 1) is CosInt)
                    {
                        var genNumber = (CosInt)po.remove(po.size() - 1);
                        if (po.get(po.size() - 1) is CosInt)
                        {
                            var          number = (CosInt)po.remove(po.size() - 1);
                            CosObjectKey key    = new CosObjectKey(number.AsLong(), genNumber.AsInt());
                            pbo = pool.Get(key);
                        }
                        else
                        {
                            // the object reference is somehow wrong
                            pbo = null;
                        }
                    }
                    else
                    {
                        pbo = null;
                    }
                }
                if (pbo != null)
                {
                    po.add(pbo);
                }
                else
                {
                    //it could be a bad object in the array which is just skipped
                    // LOG.warn("Corrupt object reference at offset " + seqSource.getPosition());

                    // This could also be an "endobj" or "endstream" which means we can assume that
                    // the array has ended.
                    string isThisTheEnd = ReadHelper.ReadString(reader);
                    reader.Unread(OtherEncodings.StringAsLatin1Bytes(isThisTheEnd));
                    if (string.Equals(isThisTheEnd, "endobj") || string.Equals(isThisTheEnd, "endstream"))
                    {
                        return(po);
                    }
                }

                ReadHelper.SkipSpaces(reader);
            }
            // read ']'
            reader.Read();
            ReadHelper.SkipSpaces(reader);
            return(po);
        }
        /**
         * Embeds an external file (generic - any type allowed) in the PDF.
         *
         * @param doc
         *            PDDocument to attach the file to.
         * @param filename
         *            name of the file that will become attachment name in the PDF
         * @param relationship
         *            how the file relates to the content, e.g. "Alternative"
         * @param description
         *            Human-readable description of the file content
         * @param subType
         *            type of the data e.g. could be "text/xml" - mime like
         * @param data
         *            the binary data of the file/attachment
         */
        public void PDFAttachGenericFile(PDDocument doc, String filename,
                                         String relationship, String description, String subType, byte[] data)
        {
            PDComplexFileSpecification fs = new PDComplexFileSpecification();

            fs.setFile(filename);

            COSDictionary dict = fs.getCOSDictionary();

            dict.setName("AFRelationship", relationship);
            dict.setString("UF", filename);
            dict.setString("Desc", description);

            ByteArrayInputStream fakeFile = new ByteArrayInputStream(data);
            PDEmbeddedFile       ef       = new PDEmbeddedFile(doc, fakeFile);

            ef.setSubtype(subType);
            ef.setSize(data.Length);
            ef.setCreationDate(new GregorianCalendar());

            ef.setModDate(GregorianCalendar.getInstance());

            fs.setEmbeddedFile(ef);

            // In addition make sure the embedded file is set under /UF
            dict = fs.getCOSDictionary();
            COSDictionary efDict = (COSDictionary)dict
                                   .getDictionaryObject(COSName.EF);
            COSBase lowerLevelFile = efDict.getItem(COSName.F);

            efDict.setItem(COSName.UF, lowerLevelFile);

            // now add the entry to the embedded file tree and set in the document.
            PDDocumentNameDictionary names = new PDDocumentNameDictionary(
                doc.getDocumentCatalog());
            PDEmbeddedFilesNameTreeNode efTree = names.getEmbeddedFiles();

            if (efTree == null)
            {
                efTree = new PDEmbeddedFilesNameTreeNode();
            }

            //Map<String, COSObjectable> namesMap = new HashMap<String, COSObjectable>();
            //      Map<String, COSObjectable> oldNamesMap = efTree.getNames();
            //if (oldNamesMap != null) {
            // for (String key : oldNamesMap.keySet()) {
            //  namesMap.put(key, oldNamesMap.get(key));
            // }
            //}
            //efTree.setNames(namesMap);
            //should be ported more exactly...
            efTree.setNames(Collections.singletonMap(filename, fs));

            names.setEmbeddedFiles(efTree);
            doc.getDocumentCatalog().setNames(names);

            // AF entry (Array) in catalog with the FileSpec
            COSArray cosArray = (COSArray)doc.getDocumentCatalog()
                                .getCOSDictionary().getItem("AF");

            if (cosArray == null)
            {
                cosArray = new COSArray();
            }
            cosArray.add(fs);
            COSDictionary dict2 = doc.getDocumentCatalog().getCOSDictionary();
            COSArray      array = new COSArray();

            array.add(fs.getCOSDictionary()); // see below
            dict2.setItem("AF", array);
            doc.getDocumentCatalog().getCOSDictionary().setItem("AF", cosArray);
        }