コード例 #1
0
        /**
         * Test to make sure that the following issue is fixed: http://sourceforge.net/mailarchive/message.php?msg_id=30891213
         */
        virtual public void TestDecodeParmsArrayWithNullItems()
        {
            Document     document     = new Document();
            MemoryStream byteStream   = new MemoryStream();
            PdfSmartCopy pdfSmartCopy = new PdfSmartCopy(document, byteStream);

            document.Open();

            PdfReader reader = TestResourceUtils.GetResourceAsPdfReader(RESOURCES, "imgWithDecodeParms.pdf");

            pdfSmartCopy.AddPage(pdfSmartCopy.GetImportedPage(reader, 1));

            document.Close();
            reader.Close();

            reader = new PdfReader(byteStream.ToArray());
            PdfDictionary page        = reader.GetPageN(1);
            PdfDictionary resources   = page.GetAsDict(PdfName.RESOURCES);
            PdfDictionary xObject     = resources.GetAsDict(PdfName.XOBJECT);
            PdfStream     img         = xObject.GetAsStream(new PdfName("Im0"));
            PdfArray      decodeParms = img.GetAsArray(PdfName.DECODEPARMS);

            Assert.AreEqual(2, decodeParms.Size);
            Assert.IsTrue(decodeParms[0] is PdfNull);

            reader.Close();
        }
コード例 #2
0
        /// <summary>
        /// Uses the OCGParser on a page </summary>
        /// <param name="parser">	the OCGParser </param>
        /// <param name="page">		the page dictionary of the page that needs to be parsed. </param>
        /// <exception cref="IOException"> </exception>
        private void Parse(OCGParser parser, PdfDictionary page)
        {
            PRStream      stream    = (PRStream)page.GetAsStream(PdfName.CONTENTS);
            PdfDictionary resources = page.GetAsDict(PdfName.RESOURCES);

            parser.Parse(stream, resources);
        }
コード例 #3
0
ファイル: ReuseImage.cs プロジェクト: anqin0725/i7ns-samples
        protected void ManipulatePdf(String dest)
        {
            PdfDocument srcDoc    = new PdfDocument(new PdfReader(SRC));
            PdfDocument resultDoc = new PdfDocument(new PdfWriter(dest));
            Document    doc       = new Document(resultDoc, PageSize.A4.Rotate());

            // Assume that there is a single XObject in the source document
            // and this single object is an image.
            PdfDictionary   pageDict      = srcDoc.GetFirstPage().GetPdfObject();
            PdfDictionary   pageResources = pageDict.GetAsDictionary(PdfName.Resources);
            PdfDictionary   pageXObjects  = pageResources.GetAsDictionary(PdfName.XObject);
            PdfName         imgRef        = pageXObjects.KeySet().First();
            PdfStream       imgStream     = pageXObjects.GetAsStream(imgRef);
            PdfImageXObject imgObject     = new PdfImageXObject((PdfStream)imgStream.CopyTo(resultDoc));

            srcDoc.Close();

            Image image = new Image(imgObject);

            image.ScaleToFit(PageSize.A4.GetHeight(), PageSize.A4.GetWidth());
            image.SetFixedPosition((PageSize.A4.GetHeight() - image.GetImageScaledWidth()) / 2,
                                   (PageSize.A4.GetWidth() - image.GetImageScaledHeight()) / 2);
            doc.Add(image);

            doc.Close();
        }
コード例 #4
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());
        }
コード例 #5
0
        virtual public void CopyTaggedPdf21()
        {
            InitializeDocument("21");
            copy.SetMergeFields();

            PdfReader reader1 = new PdfReader(SOURCE73);

            copy.AddDocument(reader1);
            document.Close();
            reader1.Close();

            PdfReader     reader      = new PdfReader(output);
            PdfDictionary page        = reader.GetPageN(1);
            PdfDictionary resources   = page.GetAsDict(PdfName.RESOURCES);
            PdfDictionary xObject     = resources.GetAsDict(PdfName.XOBJECT);
            PdfStream     img         = xObject.GetAsStream(new PdfName("Im0"));
            PdfArray      decodeParms = img.GetAsArray(PdfName.DECODEPARMS);

            Assert.AreEqual(2, decodeParms.Size);
            PdfObject iref = decodeParms[0];

            Assert.IsTrue(iref is PdfIndirectReference);
            Assert.IsTrue(reader.GetPdfObjectRelease(((PdfIndirectReference)iref).Number) is PdfNull);

            reader.Close();
        }
コード例 #6
0
        // ---------------------------------------------------------------------------

        /**
         * Extracts attachments from an existing PDF.
         * @param src the path to the existing PDF
         * @param zip the ZipFile object to add the extracted images
         */
        public void ExtractAttachments(byte[] src, ZipFile zip)
        {
            PdfReader reader = new PdfReader(src);

            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                PdfArray array = reader.GetPageN(i).GetAsArray(PdfName.ANNOTS);
                if (array == null)
                {
                    continue;
                }
                for (int j = 0; j < array.Size; j++)
                {
                    PdfDictionary annot = array.GetAsDict(j);
                    if (PdfName.FILEATTACHMENT.Equals(
                            annot.GetAsName(PdfName.SUBTYPE)))
                    {
                        PdfDictionary fs   = annot.GetAsDict(PdfName.FS);
                        PdfDictionary refs = fs.GetAsDict(PdfName.EF);
                        foreach (PdfName name in refs.Keys)
                        {
                            zip.AddEntry(
                                fs.GetAsString(name).ToString(),
                                PdfReader.GetStreamBytes((PRStream)refs.GetAsStream(name))
                                );
                        }
                    }
                }
            }
        }
コード例 #7
0
    public override bool AllowText(TextRenderInfo renderInfo)
    {
        DocumentFont  font     = renderInfo.GetFont();
        PdfDictionary fontDict = font.FontDictionary;
        PdfName       subType  = fontDict.GetAsName(PdfName.SUBTYPE);

        if (PdfName.TYPE0.Equals(subType))
        {
            PdfArray      descendantFonts = fontDict.GetAsArray(PdfName.DESCENDANTFONTS);
            PdfDictionary descendantFont  = descendantFonts[0] as PdfDictionary;
            PdfDictionary fontDescriptor  = descendantFont.GetAsDict(PdfName.FONTDESCRIPTOR);
            PdfStream     fontStream      = fontDescriptor.GetAsStream(PdfName.FONTFILE2);
            byte[]        fontData        = PdfReader.GetStreamBytes((PRStream)fontStream);
            MemoryStream  dataStream      = new MemoryStream(fontData);
            dataStream.Position = 0;
            MemoryPackage        memoryPackage = new MemoryPackage();
            Uri                  uri           = memoryPackage.CreatePart(dataStream);
            GlyphTypeface        glyphTypeface = new GlyphTypeface(uri);
            ICollection <string> names         = glyphTypeface.FamilyNames.Values;
            return(names.Where(name => name.Contains("Arial")).Count() > 0);
        }
        else
        {
            // analogous code for other font subtypes
            return(false);
        }
    }
コード例 #8
0
        /* (non-Javadoc)
         * @see com.itextpdf.pdfa.checker.PdfA1Checker#checkMetaData(com.itextpdf.kernel.pdf.PdfDictionary)
         */
        protected override void CheckMetaData(PdfDictionary catalog)
        {
            base.CheckMetaData(catalog);
            XMPMeta xmpMeta;

            try {
                xmpMeta = XMPMetaFactory.ParseFromBuffer(catalog.GetAsStream(PdfName.Metadata).GetBytes());
                String docFileName = xmpMeta.GetPropertyString(ZugferdXMPUtil.ZUGFERD_SCHEMA_NS, ZugferdXMPUtil.ZUGFERD_DOCUMENT_FILE_NAME
                                                               );
                foreach (PdfDictionary attachment in attachments)
                {
                    if ((attachment.GetAsString(PdfName.UF) != null && docFileName.Equals(attachment.GetAsString(PdfName.UF).ToString
                                                                                              ())) || (attachment.GetAsString(PdfName.F) != null && docFileName.Equals(attachment.GetAsString(PdfName
                                                                                                                                                                                              .F).ToString())))
                    {
                        PdfName relationship = attachment.GetAsName(PdfName.AFRelationship);
                        if (!PdfName.Alternative.Equals(relationship))
                        {
                            throw new ZugferdConformanceException(ZugferdConformanceException.AFRelationshipValueShallbeAlternative);
                        }
                        break;
                    }
                }
            }
            catch (XMPException e) {
                throw new PdfException(e);
            }
        }
コード例 #9
0
        /**
         * Deletes redact annotations from the page and substitutes them with either OverlayText or RO object if it's needed.
         */
        private void DeleteRedactAnnots(int pageNum)
        {
            HashSet2 <String> indirRefs;

            redactAnnotIndirRefs.TryGetValue(pageNum, out indirRefs);

            if (indirRefs == null || indirRefs.Count == 0)
            {
                return;
            }

            PdfReader      reader      = pdfStamper.Reader;
            PdfContentByte canvas      = pdfStamper.GetOverContent(pageNum);
            PdfDictionary  pageDict    = reader.GetPageN(pageNum);
            PdfArray       annotsArray = pageDict.GetAsArray(PdfName.ANNOTS);

            // j is for access annotRect (i can be decreased, so we need to store additional index,
            // indicating current position in ANNOTS array in case if we don't remove anything
            for (int i = 0, j = 0; i < annotsArray.Size; ++i, ++j)
            {
                PdfIndirectReference annotIndRef = annotsArray.GetAsIndirectObject(i);
                PdfDictionary        annotDict   = annotsArray.GetAsDict(i);

                if (indirRefs.Contains(annotIndRef.ToString()) || indirRefs.Contains(GetParentIndRefStr(annotDict)))
                {
                    PdfStream formXObj    = annotDict.GetAsStream(PdfName.RO);
                    PdfString overlayText = annotDict.GetAsString(PdfName.OVERLAYTEXT);

                    if (FillCleanedArea && formXObj != null)
                    {
                        PdfArray  rectArray = annotDict.GetAsArray(PdfName.RECT);
                        Rectangle annotRect = new Rectangle(rectArray.GetAsNumber(0).FloatValue,
                                                            rectArray.GetAsNumber(1).FloatValue,
                                                            rectArray.GetAsNumber(2).FloatValue,
                                                            rectArray.GetAsNumber(3).FloatValue);

                        InsertFormXObj(canvas, pageDict, formXObj, clippingRects[j], annotRect);
                    }
                    else if (FillCleanedArea && overlayText != null && overlayText.ToUnicodeString().Length > 0)
                    {
                        DrawOverlayText(canvas, clippingRects[j], overlayText,
                                        annotDict.GetAsString(PdfName.DA),
                                        annotDict.GetAsNumber(PdfName.Q),
                                        annotDict.GetAsBoolean(PdfName.REPEAT));
                    }

                    annotsArray.Remove(i--); // array size is changed, so we need to decrease i
                }
            }

            if (annotsArray.Size == 0)
            {
                pageDict.Remove(PdfName.ANNOTS);
            }
        }
コード例 #10
0
        public ImageInfo(PdfObject pdfObject, Matrix matrix, float pageUnits, int pageNumber, ImageRenderInfo renderInfo)
        {
            IsImage = false;
            ID      = Guid.NewGuid();

            PageNumber = pageNumber;

            _matrix     = matrix;
            _pageUnits  = pageUnits;
            _renderInfo = renderInfo;

            _imgObject = (PdfDictionary)PdfReader.GetPdfObject(pdfObject);

            PdfObject subType = _imgObject.Get(PdfName.SUBTYPE);

            if (subType == null)
            {
                return;
            }

            _stream     = _imgObject as PRStream;
            _maskStream = (PRStream)_imgObject.GetAsStream(PdfName.MASK) ?? (PRStream)_imgObject.GetAsStream(PdfName.SMASK);

            IsMask = _maskStream != null;

            _fltr = _imgObject.Get(PdfName.FILTER);

            WidthPx      = _imgObject.GetAsNumber(PdfName.WIDTH).IntValue;
            HeightPx     = _imgObject.GetAsNumber(PdfName.HEIGHT).IntValue;
            BitsPerPixel = _imgObject.GetAsNumber(PdfName.BITSPERCOMPONENT).IntValue;

            PixelFormat   = _DetectPixelFormat();
            ImageFilter   = _DetectImageFilter();
            ImageFileType = _GetFileType();

            ImageBytes = _GetImageBytes();

            var ctmWidth  = matrix[Matrix.I11];
            var ctmHeight = matrix[Matrix.I22];

            var ImgSize        = new SizeF(WidthPx, HeightPx);
            var CtmSize        = new SizeF(ctmWidth, ctmHeight);
            var ImgWidthScale  = ImgSize.Width / CtmSize.Width;
            var ImgHeightScale = ImgSize.Height / CtmSize.Height;

            Dpix = ImgWidthScale * pageUnits;
            DpiY = ImgHeightScale * pageUnits;

            WidthMm  = Utilities.PointsToMillimeters(ctmWidth);
            HeightMm = Utilities.PointsToMillimeters(ctmHeight);

            IsImage = true;
        }
コード例 #11
0
 private void SetCheckerOutputIntent(PdfDictionary outputIntent)
 {
     if (outputIntent != null)
     {
         PdfStream destOutputProfile = outputIntent.GetAsStream(PdfName.DestOutputProfile);
         if (destOutputProfile != null)
         {
             String intentCS = IccProfile.GetIccColorSpaceName(destOutputProfile.GetBytes());
             this.pdfAOutputIntentColorSpace = intentCS;
         }
     }
 }
コード例 #12
0
    public PdfImageObject extractImage(String signatureName)
    {
        MyImageRenderListener     listener         = new MyImageRenderListener();
        PdfDictionary             sigFieldDic      = reader.AcroFields.GetFieldItem(signatureName).GetMerged(0);
        PdfDictionary             appearancesDic   = sigFieldDic.GetAsDict(PdfName.AP);
        PdfStream                 normalAppearance = appearancesDic.GetAsStream(PdfName.N);
        PdfDictionary             resourcesDic     = normalAppearance.GetAsDict(PdfName.RESOURCES);
        PdfContentStreamProcessor processor        = new PdfContentStreamProcessor(listener);

        processor.ProcessContent(ContentByteUtils.GetContentBytesFromContentObject(normalAppearance), resourcesDic);
        return(listener.image);
    }
コード例 #13
0
        protected internal override void CheckFileSpec(PdfDictionary fileSpec)
        {
            PdfName relationship = fileSpec.GetAsName(PdfName.AFRelationship);

            if (relationship == null || !allowedAFRelationships.Contains(relationship))
            {
                throw new PdfAConformanceException(PdfAConformanceException.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_ONE_OF_THE_PREDEFINED_AFRELATIONSHIP_KEYS
                                                   );
            }
            if (fileSpec.ContainsKey(PdfName.EF))
            {
                if (!fileSpec.ContainsKey(PdfName.F) || !fileSpec.ContainsKey(PdfName.UF) || !fileSpec.ContainsKey(PdfName
                                                                                                                   .Desc))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_F_KEY_AND_UF_KEY
                                                       );
                }
                PdfDictionary ef           = fileSpec.GetAsDictionary(PdfName.EF);
                PdfStream     embeddedFile = ef.GetAsStream(PdfName.F);
                if (embeddedFile == null)
                {
                    throw new PdfAConformanceException(PdfAConformanceException.EF_KEY_OF_FILE_SPECIFICATION_DICTIONARY_SHALL_CONTAIN_DICTIONARY_WITH_VALID_F_KEY
                                                       );
                }
                if (!embeddedFile.ContainsKey(PdfName.Subtype))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.MIME_TYPE_SHALL_BE_SPECIFIED_USING_THE_SUBTYPE_KEY_OF_THE_FILE_SPECIFICATION_STREAM_DICTIONARY
                                                       );
                }
                if (embeddedFile.ContainsKey(PdfName.Params))
                {
                    PdfObject @params = embeddedFile.Get(PdfName.Params);
                    if ([email protected]())
                    {
                        throw new PdfAConformanceException(PdfAConformanceException.EMBEDDED_FILE_SHALL_CONTAIN_PARAMS_KEY_WITH_DICTIONARY_AS_VALUE
                                                           );
                    }
                    if (((PdfDictionary)@params).GetAsString(PdfName.ModDate) == null)
                    {
                        throw new PdfAConformanceException(PdfAConformanceException.EMBEDDED_FILE_SHALL_CONTAIN_PARAMS_KEY_WITH_VALID_MODDATE_KEY
                                                           );
                    }
                }
                else
                {
                    ILog logger = LogManager.GetLogger(typeof(PdfAChecker));
                    logger.Warn(PdfAConformanceLogMessageConstant.EMBEDDED_FILE_SHOULD_CONTAIN_PARAMS_KEY);
                }
            }
        }
コード例 #14
0
        /**
         * Extracts locations from the concrete annotation.
         * Note: annotation can consist not only of one area specified by the RECT entry, but also of multiple areas specified
         * by the QuadPoints entry in the annotation dictionary.
         */
        private IList <PdfCleanUpLocation> ExtractLocationsFromRedactAnnot(int page, int annotIndex, PdfDictionary annotDict)
        {
            IList <PdfCleanUpLocation> locations        = new List <PdfCleanUpLocation>();
            List <Rectangle>           markedRectangles = new List <Rectangle>();
            PdfArray quadPoints = annotDict.GetAsArray(PdfName.QUADPOINTS);

            if (quadPoints.Size != 0)
            {
                markedRectangles.AddRange(TranslateQuadPointsToRectangles(quadPoints));
            }
            else
            {
                PdfArray annotRect = annotDict.GetAsArray(PdfName.RECT);
                markedRectangles.Add(new Rectangle(annotRect.GetAsNumber(0).FloatValue,
                                                   annotRect.GetAsNumber(1).FloatValue,
                                                   annotRect.GetAsNumber(2).FloatValue,
                                                   annotRect.GetAsNumber(3).FloatValue));
            }

            clippingRects.Add(annotIndex, markedRectangles);

            BaseColor cleanUpColor = null;
            PdfArray  ic           = annotDict.GetAsArray(PdfName.IC);

            if (ic != null)
            {
                cleanUpColor = new BaseColor(
                    ic.GetAsNumber(0).FloatValue,
                    ic.GetAsNumber(1).FloatValue,
                    ic.GetAsNumber(2).FloatValue
                    );
            }


            PdfStream ro = annotDict.GetAsStream(PdfName.RO);

            if (ro != null)
            {
                cleanUpColor = null;
            }

            foreach (Rectangle rect in markedRectangles)
            {
                locations.Add(new PdfCleanUpLocation(page, rect, cleanUpColor));
            }

            return(locations);
        }
コード例 #15
0
        /// <summary>
        /// The assert only image and image size.
        /// </summary>
        /// <param name="reader">
        /// The reader.
        /// </param>
        /// <param name="pageNum">
        /// The page number.
        /// </param>
        /// <param name="width">
        /// The width.
        /// </param>
        /// <param name="height">
        /// The height.
        /// </param>
        private static void AssertOnlyImageAndImageSize(PdfReader reader, int pageNum, int width, int height)
        {
            PdfDictionary page       = reader.GetPageNRelease(pageNum);
            PdfDictionary resources  = page.GetAsDict(PdfName.RESOURCES);
            PdfDictionary dictionary = resources.GetAsDict(PdfName.XOBJECT);

            Assert.AreEqual(1, dictionary.Keys.Count);
            dictionary.GetEnumerator().MoveNext();
            Dictionary <PdfName, PdfObject> .Enumerator e = dictionary.GetEnumerator();
            e.MoveNext();
            PdfName   key = e.Current.Key;
            PdfStream str = dictionary.GetAsStream(key);

            Assert.AreEqual(width, str.GetAsNumber(PdfName.WIDTH).IntValue);
            Assert.AreEqual(height, str.GetAsNumber(PdfName.HEIGHT).IntValue);
        }
コード例 #16
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));

            // Assume that there is a single XObject in the source document
            // and this single object is an image.
            PdfDictionary pageDict  = pdfDoc.GetFirstPage().GetPdfObject();
            PdfDictionary resources = pageDict.GetAsDictionary(PdfName.Resources);
            PdfDictionary xObjects  = resources.GetAsDictionary(PdfName.XObject);
            PdfName       imgRef    = xObjects.KeySet().First();
            PdfStream     stream    = xObjects.GetAsStream(imgRef);
            Image         img       = ConvertToBlackAndWhitePng(new PdfImageXObject(stream));

            // Replace the original image with the grayscale image
            xObjects.Put(imgRef, img.GetXObject().GetPdfObject());

            pdfDoc.Close();
        }
コード例 #17
0
ファイル: OCGParser.cs プロジェクト: tbrion/itextsharp
        /// <summary>
        /// Parses a stream object and removes OCGs. </summary>
        /// <param name="stream">	a stream object </param>
        /// <param name="resources">	the resources dictionary of that object (containing info about the OCGs) </param>
        public virtual void Parse(PRStream stream, PdfDictionary resources)
        {
            baos       = new MemoryStream();
            properties = resources.GetAsDict(PdfName.PROPERTIES);
            xobj       = new HashSet2 <PdfName>();
            PdfDictionary xobjects = resources.GetAsDict(PdfName.XOBJECT);

            if (xobjects != null)
            {
                // remove XObject (form or image) that belong to an OCG that needs to be removed
                foreach (PdfName name in xobjects.Keys)
                {
                    PRStream      xobject = (PRStream)xobjects.GetAsStream(name);
                    PdfDictionary oc      = xobject.GetAsDict(PdfName.OC);
                    if (oc != null)
                    {
                        PdfString ocname = oc.GetAsString(PdfName.NAME);
                        if (ocname != null && ocgs.Contains(ocname.ToString()))
                        {
                            xobj.Add(name);
                        }
                    }
                }
                foreach (PdfName name in xobj)
                {
                    xobjects.Remove(name);
                }
            }
            // parse the content stream
            byte[]           contentBytes = PdfReader.GetStreamBytes(stream);
            PRTokeniser      tokeniser    = new PRTokeniser(new RandomAccessFileOrArray(contentBytes));
            PdfContentParser ps           = new PdfContentParser(tokeniser);
            List <PdfObject> operands     = new List <PdfObject>();

            while (ps.Parse(operands).Count > 0)
            {
                PdfLiteral @operator = (PdfLiteral)operands[operands.Count - 1];
                ProcessOperator(this, @operator, operands);
            }
            baos.Flush();
            baos.Close();
            stream.SetData(baos.GetBuffer());
        }
コード例 #18
0
        protected internal override void CheckFileSpec(PdfDictionary fileSpec)
        {
            PdfName relationship = fileSpec.GetAsName(PdfName.AFRelationship);

            if (relationship == null || !allowedAFRelationships.Contains(relationship))
            {
                throw new PdfAConformanceException(PdfAConformanceException.FileSpecificationDictionaryShallContainOneOfThePredefinedAFRelationshipKeys
                                                   );
            }
            if (fileSpec.ContainsKey(PdfName.EF))
            {
                if (!fileSpec.ContainsKey(PdfName.F) || !fileSpec.ContainsKey(PdfName.UF) || !fileSpec.ContainsKey(PdfName
                                                                                                                   .Desc))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.FileSpecificationDictionaryShallContainFKeyUFKeyAndDescKey
                                                       );
                }
                PdfDictionary ef           = fileSpec.GetAsDictionary(PdfName.EF);
                PdfStream     embeddedFile = ef.GetAsStream(PdfName.F);
                if (embeddedFile == null)
                {
                    throw new PdfAConformanceException(PdfAConformanceException.EFKeyOfFileSpecificationDictionaryShallContainDictionaryWithValidFKey
                                                       );
                }
                if (!embeddedFile.ContainsKey(PdfName.Subtype))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.MimeTypeShallBeSpecifiedUsingTheSubtypeKeyOfTheFileSpecificationStreamDictionary
                                                       );
                }
                PdfDictionary @params = embeddedFile.GetAsDictionary(PdfName.Params);
                if (@params == null)
                {
                    throw new PdfAConformanceException(PdfAConformanceException.EmbeddedFileShallContainParamsKeyWithDictionaryAsValue
                                                       );
                }
                if (@params.GetAsString(PdfName.ModDate) == null)
                {
                    throw new PdfAConformanceException(PdfAConformanceException.EmbeddedFileShallContainParamsKeyWithValidModdateKey
                                                       );
                }
            }
        }
コード例 #19
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));
            }
        }
コード例 #20
0
        private static void ProcessStructElem(PdfStructElem elem, StringBuilder builder)
        {
            PdfDictionary page = elem.GetPdfObject().GetAsDictionary(PdfName.Pg);

            if (page == null)
            {
                return;
            }

            PdfStream contents = page.GetAsStream(PdfName.Contents);

            if (contents != null)
            {
                builder.Append("Content: \n" + Encoding.UTF8.GetString(contents.GetBytes()) + "\n");
            }
            else
            {
                PdfArray array = page.GetAsArray(PdfName.Contents);
                builder.Append("Contents array: " + array + "\n");
            }
        }
コード例 #21
0
        protected void ManipulatePdf(String dest)
        {
            PdfReader   reader = new PdfReader(SRC);
            PdfDocument pdfDoc = new PdfDocument(reader, new PdfWriter(dest));

            // Assume that there is a single XObject in the source document
            // and this single object is an image.
            PdfDictionary pageDict      = pdfDoc.GetFirstPage().GetPdfObject();
            PdfDictionary pageResources = pageDict.GetAsDictionary(PdfName.Resources);
            PdfDictionary pageXObjects  = pageResources.GetAsDictionary(PdfName.XObject);
            PdfName       imgName       = pageXObjects.KeySet().First();
            PdfStream     imgStream     = pageXObjects.GetAsStream(imgName);

            imgStream.SetData(reader.ReadStreamBytesRaw(imgStream));

            PdfArray array = new PdfArray();

            array.Add(PdfName.FlateDecode);
            array.Add(PdfName.DCTDecode);
            imgStream.Put(PdfName.Filter, array);

            pdfDoc.Close();
        }
コード例 #22
0
ファイル: LargeImage2.cs プロジェクト: anqin0725/i7ns-samples
        protected void ManipulatePdf(String dest)
        {
            PdfDocument  resultDoc = new PdfDocument(new PdfWriter(dest));
            MemoryStream tempFile  = new ByteArrayOutputStream();

            // The source pdf document's page size is expected to be huge: more than 14400 in width in height
            PdfDocument tempDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(tempFile));

            // Assume that there is a single XObject in the source document
            // and this single object is an image.
            PdfDictionary   pageDict      = tempDoc.GetFirstPage().GetPdfObject();
            PdfDictionary   pageResources = pageDict.GetAsDictionary(PdfName.Resources);
            PdfDictionary   pageXObjects  = pageResources.GetAsDictionary(PdfName.XObject);
            PdfName         imgRef        = pageXObjects.KeySet().First();
            PdfStream       imgStream     = pageXObjects.GetAsStream(imgRef);
            PdfImageXObject imgObject     = new PdfImageXObject(imgStream);
            Image           img           = new Image(imgObject);

            img.ScaleToFit(14400, 14400);
            img.SetFixedPosition(0, 0);

            tempDoc.AddNewPage(1, new PageSize(img.GetImageScaledWidth(), img.GetImageScaledHeight()));
            PdfPage page = tempDoc.GetFirstPage();

            new Canvas(page, page.GetPageSize())
            .Add(img)
            .Close();
            tempDoc.Close();

            PdfDocument docToCopy = new PdfDocument(new PdfReader(new MemoryStream(tempFile.ToArray())));

            docToCopy.CopyPagesTo(1, 1, resultDoc);

            docToCopy.Close();
            resultDoc.Close();
        }
コード例 #23
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc    = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            Document    doc       = new Document(pdfDoc);
            PdfPage     firstPage = pdfDoc.GetFirstPage();
            Rectangle   pageSize  = firstPage.GetPageSize();

            // Assume that there is a single XObject in the source document
            // and this single object is an image.
            PdfDictionary   pageDict      = firstPage.GetPdfObject();
            PdfDictionary   pageResources = pageDict.GetAsDictionary(PdfName.Resources);
            PdfDictionary   pageXObjects  = pageResources.GetAsDictionary(PdfName.XObject);
            PdfName         imgRef        = pageXObjects.KeySet().First();
            PdfStream       imgStream     = pageXObjects.GetAsStream(imgRef);
            PdfImageXObject imgObject     = new PdfImageXObject(imgStream);
            Image           image         = new Image(imgObject);

            image.SetFixedPosition(0, 0);
            image.SetBorder(new SolidBorder(ColorConstants.BLACK, 5));
            image.ScaleAbsolute(pageSize.GetWidth(), pageSize.GetHeight());
            doc.Add(image);

            doc.Close();
        }
コード例 #24
0
ファイル: MCParser.cs プロジェクト: w16014936/itextsharp
        /**
         * Converts an annotation structure item to a Form XObject annotation.
         * @param item the structure item
         * @throws IOException
         */
        protected void ConvertToXObject(StructureObject item)
        {
            PdfDictionary structElem = item.GetStructElem();

            if (structElem == null)
            {
                return;
            }
            PdfDictionary dict = item.GetObjAsDict();

            if (dict == null || !dict.CheckType(PdfName.ANNOT))
            {
                return;
            }
            PdfDictionary ap = dict.GetAsDict(PdfName.AP);

            if (ap == null)
            {
                return;
            }
            PdfNumber structParent = dict.GetAsNumber(PdfName.STRUCTPARENT);

            if (structParent == null)
            {
                return;
            }
            PdfStream stream = ap.GetAsStream(PdfName.N);

            if (stream == null)
            {
                return;
            }
            stream.Put(PdfName.STRUCTPARENT, structParent);
            PdfIndirectReference xobjr = ap.GetAsIndirectObject(PdfName.N);

            if (xobjr == null)
            {
                return;
            }
            // remove the annotation from the page
            for (int i = 0; i < annots.Length; i++)
            {
                PdfIndirectReference annotref = annots.GetAsIndirectObject(i);
                if (item.GetObjRef().Number == annotref.Number)
                {
                    annots.Remove(i);
                    break;
                }
            }
            // replace the existing attributes by a PrintField attribute
            PdfDictionary attribute = new PdfDictionary();

            attribute.Put(PdfName.O, PdfName.PRINTFIELD);
            PdfString description = dict.GetAsString(PdfName.TU);

            if (description == null)
            {
                description = dict.GetAsString(PdfName.T);
            }
            if (PdfName.BTN.Equals(dict.Get(PdfName.FT)))
            {
                PdfNumber fflags = dict.GetAsNumber(PdfName.FF);
                if (fflags != null)
                {
                    int ff = fflags.IntValue;
                    if ((ff & PdfFormField.FF_PUSHBUTTON) != 0)
                    {
                        attribute.Put(PdfName.ROLE, PdfName.PB);
                    }
                    // I don't think the condition below will ever be true
                    if ((ff & PdfFormField.FF_RADIO) != 0)
                    {
                        attribute.Put(PdfName.ROLE, PdfName.rb);
                    }
                    else
                    {
                        attribute.Put(PdfName.ROLE, PdfName.CB);
                    }
                }
            }
            else
            {
                attribute.Put(PdfName.ROLE, PdfName.TV);
            }
            attribute.Put(PdfName.DESC, description);
            // Updating the values of the StructElem dictionary
            PdfString t = structElem.GetAsString(PdfName.T);

            if (t == null || t.ToString().Trim().Length == 0)
            {
                structElem.Put(PdfName.T, dict.GetAsString(PdfName.T));
            }
            structElem.Put(PdfName.A, attribute);
            structElem.Put(PdfName.S, PdfName.P);
            structElem.Put(PdfName.PG, pageref);
            // Defining a new MCID
            int mcid = items.ProcessMCID(structParents, item.GetRef());

            LOGGER.Info("Using MCID " + mcid);
            structElem.Put(PdfName.K, new PdfNumber(mcid));
            // removing the annotation from the parent tree
            items.RemoveFromParentTree(structParent);
            // Adding the XObject to the page
            PdfName xobj = new PdfName("XObj" + structParent.IntValue);

            LOGGER.Info("Creating XObject with name " + xobj);
            xobjects.Put(xobj, xobjr);
            PdfArray array = dict.GetAsArray(PdfName.RECT);
            // Getting the position of the annotation
            Rectangle rect = new Rectangle(
                array.GetAsNumber(0).FloatValue, array.GetAsNumber(1).FloatValue,
                array.GetAsNumber(2).FloatValue, array.GetAsNumber(3).FloatValue);

            rect.Normalize();
            // A Do operator is forbidden inside a text block
            if (inText && !btWrite)
            {
                LOGGER.Debug("Introducing extra ET");
                byte[] bytes = Encoding.ASCII.GetBytes("ET\n");
                baos.Write(bytes, 0, bytes.Length);
                etExtra = true;
            }
            // Writing the marked-content sequence with the Do operator
            // Note that the position assumes that the CTM wasn't changed in the graphics state
            // TODO: do the math if the CTM did change!
            ByteBuffer buf = new ByteBuffer();

            buf.Append("/P <</MCID ");
            buf.Append(mcid);
            buf.Append(">> BDC\n");
            buf.Append("q 1 0 0 1 ");
            buf.Append(rect.Left.ToString(CultureInfo.InvariantCulture));
            buf.Append(" ");
            buf.Append(rect.Bottom.ToString(CultureInfo.InvariantCulture));
            buf.Append(" cm ");
            buf.Append(xobj.GetBytes());
            buf.Append(" Do Q\n");
            buf.Append("EMC\n");
            buf.Flush();
            buf.WriteTo(baos);
            // if we were inside a text block, we've introduced an ET, so we'll need to write a BT
            if (inText)
            {
                btWrite = true;
            }
        }
コード例 #25
0
ファイル: MCParser.cs プロジェクト: w16014936/itextsharp
        /**
         * Parses the content of a page, replacing appearances of annotations
         * with Form XObjects.
         * @param page a page dictionary
         * @throws IOException
         */
        public void Parse(PdfDictionary page, PdfIndirectReference pageref)
        {
            LOGGER.Info("Parsing page with reference " + pageref);
            // initializing member variables
            baos         = new MemoryStream();
            this.page    = page;
            this.pageref = pageref;

            structParents = page.GetAsNumber(PdfName.STRUCTPARENTS);
            if (structParents == null)
            {
                throw new DocumentException(MessageLocalization.GetComposedMessage("can.t.read.document.structure"));
            }
            annots = page.GetAsArray(PdfName.ANNOTS);
            if (annots == null)
            {
                annots = new PdfArray();
            }
            PdfDictionary resources = page.GetAsDict(PdfName.RESOURCES);

            xobjects = resources.GetAsDict(PdfName.XOBJECT);
            if (xobjects == null)
            {
                xobjects = new PdfDictionary();
                resources.Put(PdfName.XOBJECT, xobjects);
            }
            // parsing the content stream of the page
            PRStream stream = (PRStream)page.GetAsStream(PdfName.CONTENTS);

            byte[]           contentBytes = PdfReader.GetStreamBytes(stream);
            PRTokeniser      tokeniser    = new PRTokeniser(new RandomAccessFileOrArray(RASFACTORY.CreateSource(contentBytes)));
            PdfContentParser ps           = new PdfContentParser(tokeniser);
            List <PdfObject> operands     = new List <PdfObject>();

            while (ps.Parse(operands).Count > 0)
            {
                PdfLiteral opr = (PdfLiteral)operands[operands.Count - 1];
                ProcessOperator(opr, operands);
            }
            // dealing with orphans
            while (items.Count > 0 && items[0].GetPageref() == pageref.Number)
            {
                StructureItem item = items[0];
                if (item is StructureObject)
                {
                    ConvertToXObject((StructureObject)item);
                    items.RemoveAt(0);
                }
            }
            if (annots.Length == 0)
            {
                page.Remove(PdfName.ANNOTS);
            }
            else
            {
                PdfDictionary annot;
                for (int i = 0; i < annots.Size; i++)
                {
                    annot = annots.GetAsDict(i);
                    if (annot.GetAsNumber(PdfName.STRUCTPARENT) == null)
                    {
                        throw new DocumentException(MessageLocalization.GetComposedMessage("could.not.flatten.file.untagged.annotations.found"));
                    }
                }
            }
            // replacing the content stream
            baos.Flush();
            baos.Close();
            stream.SetData(baos.ToArray());
            // showing how many items are left
            LOGGER.Info(String.Format("There are {0} items left for processing", items.Count));
        }
コード例 #26
0
        /// <summary>Creates a Type 3 font based on an existing font dictionary, which must be an indirect object.</summary>
        /// <param name="fontDictionary">a dictionary of type <c>/Font</c>, must have an indirect reference.</param>
        internal PdfType3Font(PdfDictionary fontDictionary)
            : base(fontDictionary)
        {
            subset       = true;
            embedded     = true;
            fontProgram  = new Type3Font(false);
            fontEncoding = DocFontEncoding.CreateDocFontEncoding(fontDictionary.Get(PdfName.Encoding), toUnicode);
            PdfDictionary charProcsDic    = GetPdfObject().GetAsDictionary(PdfName.CharProcs);
            PdfArray      fontMatrixArray = GetPdfObject().GetAsArray(PdfName.FontMatrix);

            if (GetPdfObject().ContainsKey(PdfName.FontBBox))
            {
                PdfArray fontBBox = GetPdfObject().GetAsArray(PdfName.FontBBox);
                fontProgram.GetFontMetrics().SetBbox(fontBBox.GetAsNumber(0).IntValue(), fontBBox.GetAsNumber(1).IntValue(
                                                         ), fontBBox.GetAsNumber(2).IntValue(), fontBBox.GetAsNumber(3).IntValue());
            }
            else
            {
                fontProgram.GetFontMetrics().SetBbox(0, 0, 0, 0);
            }
            int firstChar = NormalizeFirstLastChar(fontDictionary.GetAsNumber(PdfName.FirstChar), 0);
            int lastChar  = NormalizeFirstLastChar(fontDictionary.GetAsNumber(PdfName.LastChar), 255);

            for (int i = firstChar; i <= lastChar; i++)
            {
                shortTag[i] = 1;
            }
            int[]    widths     = FontUtil.ConvertSimpleWidthsArray(fontDictionary.GetAsArray(PdfName.Widths), firstChar, 0);
            double[] fontMatrix = new double[6];
            for (int i = 0; i < fontMatrixArray.Size(); i++)
            {
                fontMatrix[i] = ((PdfNumber)fontMatrixArray.Get(i)).GetValue();
            }
            SetFontMatrix(fontMatrix);
            if (toUnicode != null && toUnicode.HasByteMappings() && fontEncoding.HasDifferences())
            {
                for (int i = 0; i < 256; i++)
                {
                    int     unicode   = fontEncoding.GetUnicode(i);
                    PdfName glyphName = new PdfName(fontEncoding.GetDifference(i));
                    if (unicode != -1 && !FontEncoding.NOTDEF.Equals(glyphName.GetValue()) && charProcsDic.ContainsKey(glyphName
                                                                                                                       ))
                    {
                        ((Type3Font)GetFontProgram()).AddGlyph(i, unicode, widths[i], null, new Type3Glyph(charProcsDic.GetAsStream
                                                                                                               (glyphName), GetDocument()));
                    }
                }
            }
            IDictionary <int, int?> unicodeToCode = null;

            if (toUnicode != null)
            {
                try {
                    unicodeToCode = toUnicode.CreateReverseMapping();
                }
                catch (Exception) {
                }
            }
            foreach (PdfName glyphName in charProcsDic.KeySet())
            {
                int unicode = AdobeGlyphList.NameToUnicode(glyphName.GetValue());
                int code    = -1;
                if (fontEncoding.CanEncode(unicode))
                {
                    code = fontEncoding.ConvertToByte(unicode);
                }
                else
                {
                    if (unicodeToCode != null && unicodeToCode.ContainsKey(unicode))
                    {
                        code = (int)unicodeToCode.Get(unicode);
                    }
                }
                if (code != -1 && GetFontProgram().GetGlyphByCode(code) == null)
                {
                    ((Type3Font)GetFontProgram()).AddGlyph(code, unicode, widths[code], null, new Type3Glyph(charProcsDic.GetAsStream
                                                                                                                 (glyphName), GetDocument()));
                }
            }
            FillFontDescriptor(fontDictionary.GetAsDictionary(PdfName.FontDescriptor));
        }
コード例 #27
0
ファイル: OCGParser.cs プロジェクト: zeespogeira/itextsharp
        /// <summary>
        /// Parses a stream object and removes OCGs. </summary>
        /// <param name="stream">	a stream object </param>
        /// <param name="resources">	the resources dictionary of that object (containing info about the OCGs) </param>
        public virtual void Parse(PRStream stream, PdfDictionary resources)
        {
            baos       = new MemoryStream();
            properties = resources.GetAsDict(PdfName.PROPERTIES);
            xobj       = new HashSet2 <PdfName>();
            PdfDictionary xobjects = resources.GetAsDict(PdfName.XOBJECT);

            if (xobjects != null)
            {
                // remove XObject (form or image) that belong to an OCG that needs to be removed
                foreach (PdfName name in xobjects.Keys)
                {
                    PRStream      xobject = (PRStream)xobjects.GetAsStream(name);
                    PdfDictionary oc      = xobject.GetAsDict(PdfName.OC);
                    if (oc != null)
                    {
                        PdfString ocname = oc.GetAsString(PdfName.NAME);
                        if (ocname != null && ocgs.Contains(ocname.ToString()))
                        {
                            xobj.Add(name);
                        }
                    }
                }
                foreach (PdfName name in xobj)
                {
                    xobjects.Remove(name);
                }
            }
            // parse the content stream
            byte[]           contentBytes = PdfReader.GetStreamBytes(stream);
            PRTokeniser      tokeniser    = new PRTokeniser(new RandomAccessFileOrArray(contentBytes));
            PdfContentParser ps           = new PdfContentParser(tokeniser);
            List <PdfObject> operands     = new List <PdfObject>();

            while (ps.Parse(operands).Count > 0)
            {
                PdfLiteral @operator = (PdfLiteral)operands[operands.Count - 1];
                ProcessOperator(this, @operator, operands);
                if ("BI".Equals(@operator.ToString()))
                {
                    int  found = 0;
                    int  ch;
                    bool immediateAfterBI = true;
                    while ((ch = tokeniser.Read()) != -1)
                    {
                        if (!immediateAfterBI || !PRTokeniser.IsWhitespace(ch))
                        {
                            baos.WriteByte((byte)ch);
                        }
                        immediateAfterBI = false;
                        if (found == 0 && PRTokeniser.IsWhitespace(ch))
                        {
                            found++;
                        }
                        else if (found == 1 && ch == 'E')
                        {
                            found++;
                        }
                        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.
                        }
                        else if (found == 2 && ch == 'I')
                        {
                            found++;
                        }
                        else if (found == 3 && PRTokeniser.IsWhitespace(ch))
                        {
                            break;
                        }
                        else
                        {
                            found = 0;
                        }
                    }
                }
            }
            baos.Flush();
            baos.Close();
            stream.SetData(baos.GetBuffer());
        }
コード例 #28
0
        protected internal override void CheckAnnotation(PdfDictionary annotDic)
        {
            PdfName subtype = annotDic.GetAsName(PdfName.Subtype);

            if (subtype == null)
            {
                throw new PdfAConformanceException(PdfAConformanceException.AnnotationType1IsNotPermitted).SetMessageParams
                          ("null");
            }
            if (forbiddenAnnotations.Contains(subtype))
            {
                throw new PdfAConformanceException(PdfAConformanceException.AnnotationType1IsNotPermitted).SetMessageParams
                          (subtype.GetValue());
            }
            PdfNumber ca = annotDic.GetAsNumber(PdfName.CA);

            if (ca != null && ca.FloatValue() != 1.0)
            {
                throw new PdfAConformanceException(PdfAConformanceException.AnAnnotationDictionaryShallNotContainTheCaKeyWithAValueOtherThan1
                                                   );
            }
            if (!annotDic.ContainsKey(PdfName.F))
            {
                throw new PdfAConformanceException(PdfAConformanceException.AnnotationShallContainKeyF);
            }
            int flags = (int)annotDic.GetAsInt(PdfName.F);

            if (!CheckFlag(flags, PdfAnnotation.PRINT) || CheckFlag(flags, PdfAnnotation.HIDDEN) || CheckFlag(flags, PdfAnnotation
                                                                                                              .INVISIBLE) || CheckFlag(flags, PdfAnnotation.NO_VIEW))
            {
                throw new PdfAConformanceException(PdfAConformanceException.TheFKeysPrintFlagBitShallBeSetTo1AndItsHiddenInvisibleAndNoviewFlagBitsShallBeSetTo0
                                                   );
            }
            if (subtype.Equals(PdfName.Text) && (!CheckFlag(flags, PdfAnnotation.NO_ZOOM) || !CheckFlag(flags, PdfAnnotation
                                                                                                        .NO_ROTATE)))
            {
                throw new PdfAConformanceException(PdfAConformanceException.TextAnnotationsShouldSetTheNozoomAndNorotateFlagBitsOfTheFKeyTo1
                                                   );
            }
            if (annotDic.ContainsKey(PdfName.C) || annotDic.ContainsKey(PdfName.IC))
            {
                if (!ICC_COLOR_SPACE_RGB.Equals(pdfAOutputIntentColorSpace))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.DestoutputprofileInThePdfa1OutputintentDictionaryShallBeRgb
                                                       );
                }
            }
            PdfDictionary ap = annotDic.GetAsDictionary(PdfName.AP);

            if (ap != null)
            {
                if (ap.ContainsKey(PdfName.D) || ap.ContainsKey(PdfName.R))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.AppearanceDictionaryShallContainOnlyTheNKeyWithStreamValue
                                                       );
                }
                PdfStream n = ap.GetAsStream(PdfName.N);
                if (n == null)
                {
                    throw new PdfAConformanceException(PdfAConformanceException.AppearanceDictionaryShallContainOnlyTheNKeyWithStreamValue
                                                       );
                }
                CheckResourcesOfAppearanceStreams(ap);
            }
            if (PdfName.Widget.Equals(subtype) && (annotDic.ContainsKey(PdfName.AA) || annotDic.ContainsKey(PdfName.A)
                                                   ))
            {
                throw new PdfAConformanceException(PdfAConformanceException.WidgetAnnotationDictionaryOrFieldDictionaryShallNotIncludeAOrAAEntry
                                                   );
            }
            if (annotDic.ContainsKey(PdfName.AA))
            {
                throw new PdfAConformanceException(PdfAConformanceException.AnAnnotationDictionaryShallNotContainAAKey);
            }
            if (CheckStructure(conformanceLevel))
            {
                if (contentAnnotations.Contains(subtype) && !annotDic.ContainsKey(PdfName.Contents))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.AnnotationOfType1ShouldHaveContentsKey).SetMessageParams
                              (subtype);
                }
            }
        }
コード例 #29
0
ファイル: MCParser.cs プロジェクト: newlysoft/itextsharp
 /**
  * Parses the content of a page, replacing appearances of annotations
  * with Form XObjects.
  * @param page a page dictionary
  * @throws IOException
  */
 virtual public void Parse(PdfDictionary page, PdfIndirectReference pageref) {
     LOGGER.Info("Parsing page with reference " + pageref);
     // initializing member variables
     baos = new MemoryStream();
     this.page = page;
     this.pageref = pageref;
     
     structParents = page.GetAsNumber(PdfName.STRUCTPARENTS);
     if(structParents == null)
         throw new DocumentException(MessageLocalization.GetComposedMessage("can.t.read.document.structure"));
     annots = page.GetAsArray(PdfName.ANNOTS);
     if(annots == null)
         annots = new PdfArray();
     PdfDictionary resources = page.GetAsDict(PdfName.RESOURCES);
     xobjects = resources.GetAsDict(PdfName.XOBJECT);
     if (xobjects == null) {
         xobjects = new PdfDictionary();
         resources.Put(PdfName.XOBJECT, xobjects);
     }
     // parsing the content stream of the page
     PRStream stream = (PRStream) page.GetAsStream(PdfName.CONTENTS);
     byte[] contentBytes = PdfReader.GetStreamBytes(stream);
     PRTokeniser tokeniser = new PRTokeniser(new RandomAccessFileOrArray(RASFACTORY.CreateSource(contentBytes)));
     PdfContentParser ps = new PdfContentParser(tokeniser);
     List<PdfObject> operands = new List<PdfObject>();
     while (ps.Parse(operands).Count > 0) {
         PdfLiteral opr = (PdfLiteral) operands[operands.Count - 1];
         ProcessOperator(opr, operands);
     }
     // dealing with orphans
     while (items.Count > 0 && items[0].GetPageref() == pageref.Number) {
         StructureItem item = items[0];
         if (item is StructureObject) {
             ConvertToXObject((StructureObject) item);
             items.RemoveAt(0);
         }
     }
     if(annots.Length == 0) {
         page.Remove(PdfName.ANNOTS);
     }
     else {
         PdfDictionary annot;
         for(int i = 0; i < annots.Size; i++) {
             annot = annots.GetAsDict(i);
             if(annot.GetAsNumber(PdfName.STRUCTPARENT) == null)
                 throw new DocumentException(MessageLocalization.GetComposedMessage("could.not.flatten.file.untagged.annotations.found"));
         }
     }
     // replacing the content stream
     baos.Flush();
     baos.Close();
     stream.SetData(baos.ToArray());
     // showing how many items are left
     LOGGER.Info(String.Format("There are {0} items left for processing", items.Count));
 }
コード例 #30
0
        internal static void FillFontDescriptor(iText.Kernel.Font.DocTrueTypeFont font, PdfDictionary fontDesc)
        {
            if (fontDesc == null)
            {
                ILog logger = LogManager.GetLogger(typeof(FontUtil));
                logger.Warn(iText.IO.LogMessageConstant.FONT_DICTIONARY_WITH_NO_FONT_DESCRIPTOR);
                return;
            }
            PdfNumber v = fontDesc.GetAsNumber(PdfName.Ascent);

            if (v != null)
            {
                font.SetTypoAscender(v.IntValue());
            }
            v = fontDesc.GetAsNumber(PdfName.Descent);
            if (v != null)
            {
                font.SetTypoDescender(v.IntValue());
            }
            v = fontDesc.GetAsNumber(PdfName.CapHeight);
            if (v != null)
            {
                font.SetCapHeight(v.IntValue());
            }
            v = fontDesc.GetAsNumber(PdfName.XHeight);
            if (v != null)
            {
                font.SetXHeight(v.IntValue());
            }
            v = fontDesc.GetAsNumber(PdfName.ItalicAngle);
            if (v != null)
            {
                font.SetItalicAngle(v.IntValue());
            }
            v = fontDesc.GetAsNumber(PdfName.StemV);
            if (v != null)
            {
                font.SetStemV(v.IntValue());
            }
            v = fontDesc.GetAsNumber(PdfName.StemH);
            if (v != null)
            {
                font.SetStemH(v.IntValue());
            }
            v = fontDesc.GetAsNumber(PdfName.FontWeight);
            if (v != null)
            {
                font.SetFontWeight(v.IntValue());
            }
            v = fontDesc.GetAsNumber(PdfName.MissingWidth);
            if (v != null)
            {
                font.missingWidth = v.IntValue();
            }
            PdfName fontStretch = fontDesc.GetAsName(PdfName.FontStretch);

            if (fontStretch != null)
            {
                font.SetFontStretch(fontStretch.GetValue());
            }
            PdfArray bboxValue = fontDesc.GetAsArray(PdfName.FontBBox);

            if (bboxValue != null)
            {
                int[] bbox = new int[4];
                bbox[0] = bboxValue.GetAsNumber(0).IntValue();
                //llx
                bbox[1] = bboxValue.GetAsNumber(1).IntValue();
                //lly
                bbox[2] = bboxValue.GetAsNumber(2).IntValue();
                //urx
                bbox[3] = bboxValue.GetAsNumber(3).IntValue();
                //ury
                if (bbox[0] > bbox[2])
                {
                    int t = bbox[0];
                    bbox[0] = bbox[2];
                    bbox[2] = t;
                }
                if (bbox[1] > bbox[3])
                {
                    int t = bbox[1];
                    bbox[1] = bbox[3];
                    bbox[3] = t;
                }
                font.SetBbox(bbox);
                // If ascender or descender in font descriptor are zero, we still want to get more or less correct valuee for
                // text extraction, stamping etc. Thus we rely on font bbox in this case
                if (font.GetFontMetrics().GetTypoAscender() == 0 && font.GetFontMetrics().GetTypoDescender() == 0)
                {
                    float maxAscent  = Math.Max(bbox[3], font.GetFontMetrics().GetTypoAscender());
                    float minDescent = Math.Min(bbox[1], font.GetFontMetrics().GetTypoDescender());
                    font.SetTypoAscender((int)(maxAscent * 1000 / (maxAscent - minDescent)));
                    font.SetTypoDescender((int)(minDescent * 1000 / (maxAscent - minDescent)));
                }
            }
            PdfString fontFamily = fontDesc.GetAsString(PdfName.FontFamily);

            if (fontFamily != null)
            {
                font.SetFontFamily(fontFamily.GetValue());
            }
            PdfNumber flagsValue = fontDesc.GetAsNumber(PdfName.Flags);

            if (flagsValue != null)
            {
                int flags = flagsValue.IntValue();
                if ((flags & 1) != 0)
                {
                    font.SetFixedPitch(true);
                }
                if ((flags & 262144) != 0)
                {
                    font.SetBold(true);
                }
            }
            PdfName[] fontFileNames = new PdfName[] { PdfName.FontFile, PdfName.FontFile2, PdfName.FontFile3 };
            foreach (PdfName fontFile in fontFileNames)
            {
                if (fontDesc.ContainsKey(fontFile))
                {
                    font.fontFileName = fontFile;
                    font.fontFile     = fontDesc.GetAsStream(fontFile);
                    break;
                }
            }
        }
コード例 #31
0
        protected internal override void CheckAnnotation(PdfDictionary annotDic)
        {
            PdfName subtype = annotDic.GetAsName(PdfName.Subtype);

            if (subtype == null)
            {
                throw new PdfAConformanceException(PdfAConformanceException.ANNOTATION_TYPE_0_IS_NOT_PERMITTED).SetMessageParams
                          ("null");
            }
            if (forbiddenAnnotations.Contains(subtype))
            {
                throw new PdfAConformanceException(PdfAConformanceException.ANNOTATION_TYPE_0_IS_NOT_PERMITTED).SetMessageParams
                          (subtype.GetValue());
            }
            PdfNumber ca = annotDic.GetAsNumber(PdfName.CA);

            if (ca != null && ca.FloatValue() != 1.0)
            {
                throw new PdfAConformanceException(PdfAConformanceException.AN_ANNOTATION_DICTIONARY_SHALL_NOT_CONTAIN_THE_CA_KEY_WITH_A_VALUE_OTHER_THAN_1
                                                   );
            }
            if (!annotDic.ContainsKey(PdfName.F))
            {
                throw new PdfAConformanceException(PdfAConformanceException.AN_ANNOTATION_DICTIONARY_SHALL_CONTAIN_THE_F_KEY
                                                   );
            }
            int flags = (int)annotDic.GetAsInt(PdfName.F);

            if (!CheckFlag(flags, PdfAnnotation.PRINT) || CheckFlag(flags, PdfAnnotation.HIDDEN) || CheckFlag(flags, PdfAnnotation
                                                                                                              .INVISIBLE) || CheckFlag(flags, PdfAnnotation.NO_VIEW))
            {
                throw new PdfAConformanceException(PdfAConformanceException.THE_F_KEYS_PRINT_FLAG_BIT_SHALL_BE_SET_TO_1_AND_ITS_HIDDEN_INVISIBLE_AND_NOVIEW_FLAG_BITS_SHALL_BE_SET_TO_0
                                                   );
            }
            if (subtype.Equals(PdfName.Text) && (!CheckFlag(flags, PdfAnnotation.NO_ZOOM) || !CheckFlag(flags, PdfAnnotation
                                                                                                        .NO_ROTATE)))
            {
                throw new PdfAConformanceException(PdfAConformanceLogMessageConstant.TEXT_ANNOTATIONS_SHOULD_SET_THE_NOZOOM_AND_NOROTATE_FLAG_BITS_OF_THE_F_KEY_TO_1
                                                   );
            }
            if (annotDic.ContainsKey(PdfName.C) || annotDic.ContainsKey(PdfName.IC))
            {
                if (!ICC_COLOR_SPACE_RGB.Equals(pdfAOutputIntentColorSpace))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.DESTOUTPUTPROFILE_IN_THE_PDFA1_OUTPUTINTENT_DICTIONARY_SHALL_BE_RGB
                                                       );
                }
            }
            PdfDictionary ap = annotDic.GetAsDictionary(PdfName.AP);

            if (ap != null)
            {
                if (ap.ContainsKey(PdfName.D) || ap.ContainsKey(PdfName.R))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.APPEARANCE_DICTIONARY_SHALL_CONTAIN_ONLY_THE_N_KEY_WITH_STREAM_VALUE
                                                       );
                }
                PdfStream n = ap.GetAsStream(PdfName.N);
                if (n == null)
                {
                    throw new PdfAConformanceException(PdfAConformanceException.APPEARANCE_DICTIONARY_SHALL_CONTAIN_ONLY_THE_N_KEY_WITH_STREAM_VALUE
                                                       );
                }
                CheckResourcesOfAppearanceStreams(ap);
            }
            if (PdfName.Widget.Equals(subtype) && (annotDic.ContainsKey(PdfName.AA) || annotDic.ContainsKey(PdfName.A)
                                                   ))
            {
                throw new PdfAConformanceException(PdfAConformanceException.WIDGET_ANNOTATION_DICTIONARY_OR_FIELD_DICTIONARY_SHALL_NOT_INCLUDE_A_OR_AA_ENTRY
                                                   );
            }
            if (annotDic.ContainsKey(PdfName.AA))
            {
                throw new PdfAConformanceException(PdfAConformanceException.AN_ANNOTATION_DICTIONARY_SHALL_NOT_CONTAIN_AA_KEY
                                                   );
            }
            if (CheckStructure(conformanceLevel))
            {
                if (contentAnnotations.Contains(subtype) && !annotDic.ContainsKey(PdfName.Contents))
                {
                    throw new PdfAConformanceException(PdfAConformanceException.ANNOTATION_OF_TYPE_0_SHOULD_HAVE_CONTENTS_KEY)
                          .SetMessageParams(subtype.GetValue());
                }
            }
        }
コード例 #32
0
ファイル: OCGRemover.cs プロジェクト: yu0410aries/itextsharp
 /// <summary>
 /// Uses the OCGParser on a page </summary>
 /// <param name="parser">	the OCGParser </param>
 /// <param name="page">		the page dictionary of the page that needs to be parsed. </param>
 /// <exception cref="IOException"> </exception>
 private void Parse(OCGParser parser, PdfDictionary page) {
     PRStream stream = (PRStream) page.GetAsStream(PdfName.CONTENTS);
     PdfDictionary resources = page.GetAsDict(PdfName.RESOURCES);
     parser.Parse(stream, resources);
 }