示例#1
0
 private static void ValidateRemoteDestination(PdfDestination destination)
 {
     if (destination is PdfExplicitDestination)
     {
         // No page object can be specified for a destination associated with a remote go-to action because the
         // destination page is in a different PDF document. In this case, the page parameter specifies an integer
         // page number within the remote document instead of a page object in the current document.
         PdfObject firstObj = ((PdfArray)destination.GetPdfObject()).Get(0);
         if (firstObj.IsDictionary())
         {
             throw new ArgumentException("Explicit destinations shall specify page number in remote go-to actions instead of page dictionary"
                                         );
         }
     }
     else
     {
         if (destination is PdfStructureDestination)
         {
             // No structure element dictionary can be specified for a structure destination associated with a remote
             // go-to action because the destination structure element is in a
             // different PDF document. In this case, the indirect reference to the structure element dictionary shall be
             // replaced by a byte string representing a structure element ID
             PdfObject firstObj = ((PdfArray)destination.GetPdfObject()).Get(0);
             if (firstObj.IsDictionary())
             {
                 PdfDictionary structElemObj = (PdfDictionary)firstObj;
                 PdfString     id            = structElemObj.GetAsString(PdfName.ID);
                 if (id == null)
                 {
                     throw new ArgumentException("Structure destinations shall specify structure element ID in remote go-to actions. Structure element that has no ID is specified instead"
                                                 );
                 }
                 else
                 {
                     LogManager.GetLogger(typeof(iText.Kernel.Pdf.Action.PdfAction)).Warn(iText.IO.LogMessageConstant.STRUCTURE_ELEMENT_REPLACED_BY_ITS_ID_IN_STRUCTURE_DESTINATION
                                                                                          );
                     ((PdfArray)destination.GetPdfObject()).Set(0, id);
                     destination.GetPdfObject().SetModified();
                 }
             }
         }
     }
 }
示例#2
0
        protected override void CheckEmbeddedFile(PdfDictionary embeddedFile)
        {
            PdfObject _params = GetDirectObject(embeddedFile.Get(PdfName.PARAMS));

            if (_params == null)
            {
                throw new PdfAConformanceException(embeddedFile,
                                                   MessageLocalization.GetComposedMessage("embedded.file.shall.contain.valid.params.key"));
            }
            else if (_params.IsDictionary())
            {
                PdfObject modDate = ((PdfDictionary)_params).Get(PdfName.MODDATE);
                if (modDate == null || !(modDate is PdfString))
                {
                    throw new PdfAConformanceException(embeddedFile,
                                                       MessageLocalization.GetComposedMessage("embedded.file.shall.contain.params.key.with.valid.moddate.key"));
                }
            }
        }
        /// <exception cref="System.IO.IOException"/>
        internal static void ValidateTemplateForSignedDeferredResult(String output, String sigFieldName, PdfName filter
                                                                     , PdfName subFilter, int estimatedSize)
        {
            PdfDocument outDocument   = new PdfDocument(new PdfReader(output));
            PdfObject   outSigDictObj = PdfAcroForm.GetAcroForm(outDocument, false).GetField(sigFieldName).GetValue();

            NUnit.Framework.Assert.IsTrue(outSigDictObj.IsDictionary());
            PdfDictionary outSigDict = (PdfDictionary)outSigDictObj;
            PdfArray      byteRange  = outSigDict.GetAsArray(PdfName.ByteRange);

            NUnit.Framework.Assert.IsNotNull(byteRange);
            NUnit.Framework.Assert.IsTrue(byteRange.Size() == 4);
            NUnit.Framework.Assert.AreEqual(filter, outSigDict.GetAsName(PdfName.Filter));
            NUnit.Framework.Assert.AreEqual(subFilter, outSigDict.GetAsName(PdfName.SubFilter));
            PdfString outSigContents = outSigDict.GetAsString(PdfName.Contents);

            NUnit.Framework.Assert.IsTrue(outSigContents.IsHexWriting());
            NUnit.Framework.Assert.AreEqual(new byte[estimatedSize], outSigContents.GetValueBytes());
        }
 private static PdfObject CopyObjectKid(PdfObject kid, PdfDictionary copiedParent, PdfDictionary destPage, 
     bool parentChangePg, StructureTreeCopier.StructElemCopyingParams copyingParams) {
     if (kid.IsNumber()) {
         if (!parentChangePg) {
             copyingParams.GetToDocument().GetStructTreeRoot().GetParentTreeHandler().RegisterMcr(new PdfMcrNumber((PdfNumber
                 )kid, new PdfStructElem(copiedParent)));
             return kid;
         }
     }
     else {
         if (kid.IsDictionary()) {
             PdfDictionary kidAsDict = (PdfDictionary)kid;
             if (copyingParams.GetObjectsToCopy().Contains(kidAsDict)) {
                 bool hasParent = kidAsDict.ContainsKey(PdfName.P);
                 PdfDictionary copiedKid = CopyObject(kidAsDict, destPage, parentChangePg, copyingParams);
                 if (hasParent) {
                     copiedKid.Put(PdfName.P, copiedParent);
                 }
                 else {
                     PdfMcr mcr;
                     if (copiedKid.ContainsKey(PdfName.Obj)) {
                         mcr = new PdfObjRef(copiedKid, new PdfStructElem(copiedParent));
                         PdfDictionary contentItemObject = copiedKid.GetAsDictionary(PdfName.Obj);
                         if (PdfName.Link.Equals(contentItemObject.GetAsName(PdfName.Subtype)) && !contentItemObject.ContainsKey(PdfName
                             .P)) {
                             // Some link annotations may be not copied, because their destination page is not copied.
                             return null;
                         }
                         contentItemObject.Put(PdfName.StructParent, new PdfNumber((int)copyingParams.GetToDocument().GetNextStructParentIndex
                             ()));
                     }
                     else {
                         mcr = new PdfMcrDictionary(copiedKid, new PdfStructElem(copiedParent));
                     }
                     copyingParams.GetToDocument().GetStructTreeRoot().GetParentTreeHandler().RegisterMcr(mcr);
                 }
                 return copiedKid;
             }
         }
     }
     return null;
 }
示例#5
0
// ---------------------------------------------------------------------------

        /**
         * Manipulates a PDF file src with the file dest as result
         * @param src the original PDF
         */
        public byte[] ManipulatePdf(byte[] src)
        {
            // the font file
            byte[] fontfile = null;
            using (FileStream fs = new FileStream(
                       FONT, FileMode.Open, FileAccess.Read))
            {
                fontfile = new byte[fs.Length];
                fs.Read(fontfile, 0, (int)fs.Length);
            }
            // create a new stream for the font file
            PdfStream stream = new PdfStream(fontfile);

            stream.FlateCompress();
            stream.Put(PdfName.LENGTH1, new PdfNumber(fontfile.Length));
            // create a reader object
            PdfReader     reader = new PdfReader(src);
            int           n      = reader.XrefSize;
            PdfDictionary font;

            using (MemoryStream ms = new MemoryStream()) {
                using (PdfStamper stamper = new PdfStamper(reader, ms)) {
                    PdfName fontname = new PdfName(FONTNAME);
                    for (int i = 0; i < n; i++)
                    {
                        PdfObject objectPdf = reader.GetPdfObject(i);
                        if (objectPdf == null || !objectPdf.IsDictionary())
                        {
                            continue;
                        }
                        font = (PdfDictionary)objectPdf;
                        if (PdfName.FONTDESCRIPTOR.Equals(font.Get(PdfName.TYPE)) &&
                            fontname.Equals(font.Get(PdfName.FONTNAME)))
                        {
                            PdfIndirectObject objref = stamper.Writer.AddToBody(stream);
                            font.Put(PdfName.FONTFILE2, objref.IndirectReference);
                        }
                    }
                }
                return(ms.ToArray());
            }
        }
示例#6
0
文件: xpdf.cs 项目: 24/source_04
        public static String GetDictionaryDetail(PdfDictionary dic, int depth)
        {
            StringBuilder builder = new StringBuilder();

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

            foreach (PdfName key in dic.Keys)
            {
                if (!first)
                {
                    builder.Append(", ");
                    first = false;
                }
                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());
        }
示例#7
0
        protected void EmbedFont(String merged, String fontfile, String result)
        {
            // The font file
            FileStream raf = new FileStream(fontfile, FileMode.Open, FileAccess.Read);

            byte[] fontbytes = new byte[(int)raf.Length];
            raf.Read(fontbytes, 0, fontbytes.Length);
            raf.Close();

            // Create a new stream for the font file
            PdfStream stream = new PdfStream(fontbytes);

            stream.SetCompressionLevel(CompressionConstants.DEFAULT_COMPRESSION);
            stream.Put(PdfName.Length1, new PdfNumber(fontbytes.Length));

            PdfDocument pdfDoc             = new PdfDocument(new PdfReader(merged), new PdfWriter(result));
            int         numberOfPdfObjects = pdfDoc.GetNumberOfPdfObjects();

            // Search for the font dictionary
            for (int i = 0; i < numberOfPdfObjects; i++)
            {
                PdfObject pdfObject = pdfDoc.GetPdfObject(i);
                if (pdfObject == null || !pdfObject.IsDictionary())
                {
                    continue;
                }

                PdfDictionary fontDictionary = (PdfDictionary)pdfObject;
                PdfFont       font           = PdfFontFactory.CreateFont(fontfile, PdfEncodings.WINANSI);
                PdfName       fontname       = new PdfName(font.GetFontProgram().GetFontNames().GetFontName());
                if (PdfName.FontDescriptor.Equals(fontDictionary.Get(PdfName.Type)) &&
                    fontname.Equals(fontDictionary.Get(PdfName.FontName)))
                {
                    // Embed the passed font to the pdf document
                    fontDictionary.Put(PdfName.FontFile2, stream.MakeIndirect(pdfDoc).GetIndirectReference());
                }
            }

            pdfDoc.Close();
        }
示例#8
0
        protected void ManipulatePdf(String dest)
        {
            // Create a pdf file with an embedded font in memory.
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(new MemoryStream(CreatePdf().ToArray())),
                                                 new PdfWriter(dest));

            for (int i = 0; i < pdfDoc.GetNumberOfPdfObjects(); i++)
            {
                PdfObject obj = pdfDoc.GetPdfObject(i);

                // Skip all objects that aren't a dictionary
                if (obj == null || !obj.IsDictionary())
                {
                    continue;
                }

                // Process all dictionaries
                UnembedTTF((PdfDictionary)obj);
            }

            pdfDoc.Close();
        }
示例#9
0
 private void CheckOpenAction(PdfObject openAction)
 {
     if (openAction == null)
     {
         return;
     }
     if (openAction.IsDictionary())
     {
         CheckAction((PdfDictionary)openAction);
     }
     else
     {
         if (openAction.IsArray())
         {
             PdfArray actions = (PdfArray)openAction;
             foreach (PdfObject action in actions)
             {
                 CheckAction((PdfDictionary)action);
             }
         }
     }
 }
示例#10
0
        /// <summary>Adds a chained action.</summary>
        /// <param name="nextAction">the next action or sequence of actions that shall be performed after the current action
        ///     </param>
        public virtual void Next(iText.Kernel.Pdf.Action.PdfAction nextAction)
        {
            PdfObject currentNextAction = GetPdfObject().Get(PdfName.Next);

            if (currentNextAction == null)
            {
                Put(PdfName.Next, nextAction.GetPdfObject());
            }
            else
            {
                if (currentNextAction.IsDictionary())
                {
                    PdfArray array = new PdfArray(currentNextAction);
                    array.Add(nextAction.GetPdfObject());
                    Put(PdfName.Next, array);
                }
                else
                {
                    ((PdfArray)currentNextAction).Add(nextAction.GetPdfObject());
                }
            }
        }
示例#11
0
        protected internal virtual void ParseTag(PdfMcr kid)
        {
            int           mcid       = kid.GetMcid();
            PdfDictionary pageDic    = kid.GetPageObject();
            String        tagContent = "";

            if (mcid != -1)
            {
                if (!parsedTags.ContainsKey(pageDic))
                {
                    TaggedPdfReaderTool.MarkedContentEventListener listener = new TaggedPdfReaderTool.MarkedContentEventListener
                                                                                  (this);
                    PdfCanvasProcessor processor = new PdfCanvasProcessor(listener);
                    PdfPage            page      = document.GetPage(pageDic);
                    processor.ProcessContent(page.GetContentBytes(), page.GetResources());
                    parsedTags.Put(pageDic, listener.GetMcidContent());
                }
                if (parsedTags.Get(pageDic).ContainsKey(mcid))
                {
                    tagContent = parsedTags.Get(pageDic).Get(mcid);
                }
            }
            else
            {
                PdfObjRef objRef  = (PdfObjRef)kid;
                PdfObject @object = objRef.GetReferencedObject();
                if (@object.IsDictionary())
                {
                    PdfName subtype = ((PdfDictionary)@object).GetAsName(PdfName.Subtype);
                    tagContent = subtype.ToString();
                }
            }
            try {
                @out.Write(EscapeXML(tagContent, true));
            }
            catch (System.IO.IOException e) {
                throw new iText.IO.IOException(iText.IO.IOException.UnknownIOException, e);
            }
        }
示例#12
0
        /// <summary>
        /// Adds an additional action to the provided
        /// <see cref="iText.Kernel.Pdf.PdfObjectWrapper{T}"/>
        /// &lt;
        /// <see cref="iText.Kernel.Pdf.PdfDictionary"/>
        /// &gt; wrapper.
        /// </summary>
        /// <param name="wrapper">the wrapper to add an additional action to</param>
        /// <param name="key">
        /// a
        /// <see cref="iText.Kernel.Pdf.PdfName"/>
        /// specifying the name of an additional action
        /// </param>
        /// <param name="action">
        /// the
        /// <see cref="PdfAction"/>
        /// to add as an additional action
        /// </param>
        public static void SetAdditionalAction(PdfObjectWrapper <PdfDictionary> wrapper, PdfName key, iText.Kernel.Pdf.Action.PdfAction
                                               action)
        {
            PdfDictionary dic;
            PdfObject     obj      = wrapper.GetPdfObject().Get(PdfName.AA);
            bool          aaExists = obj != null && obj.IsDictionary();

            if (aaExists)
            {
                dic = (PdfDictionary)obj;
            }
            else
            {
                dic = new PdfDictionary();
            }
            dic.Put(key, action.GetPdfObject());
            dic.SetModified();
            wrapper.GetPdfObject().Put(PdfName.AA, dic);
            if (!aaExists || !dic.IsIndirect())
            {
                wrapper.GetPdfObject().SetModified();
            }
        }
示例#13
0
        private string PdfObjectContents(PdfObject obj)
        {
            if (obj.IsStream())
            {
                PRStream stream = (PRStream)obj;
                byte[]   b;
                try
                {
                    b = PdfReader.GetStreamBytes(stream);
                }
                catch (UnsupportedPdfException)
                {
                    b = PdfReader.GetStreamBytesRaw(stream);
                }
                return(new string(b.ToCharArray()));
            }

            if (obj.IsDictionary())
            {
                PdfDictionary dict = (PdfDictionary)obj;
            }
            return("yet unsupported:(");
        }
示例#14
0
 public static FontEncoding CreateDocFontEncoding(PdfObject encoding, CMapToUnicode toUnicode, bool fillBaseEncoding
                                                  )
 {
     if (encoding != null)
     {
         if (encoding.IsName())
         {
             return(FontEncoding.CreateFontEncoding(((PdfName)encoding).GetValue()));
         }
         else
         {
             if (encoding.IsDictionary())
             {
                 iText.Kernel.Font.DocFontEncoding fontEncoding = new iText.Kernel.Font.DocFontEncoding();
                 fontEncoding.differences = new String[256];
                 if (fillBaseEncoding)
                 {
                     FillBaseEncoding(fontEncoding, ((PdfDictionary)encoding).GetAsName(PdfName.BaseEncoding));
                 }
                 FillDifferences(fontEncoding, ((PdfDictionary)encoding).GetAsArray(PdfName.Differences), toUnicode);
                 return(fontEncoding);
             }
         }
     }
     if (toUnicode != null)
     {
         iText.Kernel.Font.DocFontEncoding fontEncoding = new iText.Kernel.Font.DocFontEncoding();
         fontEncoding.differences = new String[256];
         FillDifferences(fontEncoding, toUnicode);
         return(fontEncoding);
     }
     else
     {
         return(FontEncoding.CreateFontSpecificEncoding());
     }
 }
示例#15
0
        public static PdfViewerPreferencesImp GetViewerPreferences(PdfDictionary catalog)
        {
            PdfViewerPreferencesImp preferences = new PdfViewerPreferencesImp();
            int     prefs = 0;
            PdfName name  = null;
            // page layout
            PdfObject obj = PdfReader.GetPdfObjectRelease(catalog.Get(PdfName.PAGELAYOUT));

            if (obj != null && obj.IsName())
            {
                name = (PdfName)obj;
                if (name.Equals(PdfName.SINGLEPAGE))
                {
                    prefs |= PdfWriter.PageLayoutSinglePage;
                }
                else if (name.Equals(PdfName.ONECOLUMN))
                {
                    prefs |= PdfWriter.PageLayoutOneColumn;
                }
                else if (name.Equals(PdfName.TWOCOLUMNLEFT))
                {
                    prefs |= PdfWriter.PageLayoutTwoColumnLeft;
                }
                else if (name.Equals(PdfName.TWOCOLUMNRIGHT))
                {
                    prefs |= PdfWriter.PageLayoutTwoColumnRight;
                }
                else if (name.Equals(PdfName.TWOPAGELEFT))
                {
                    prefs |= PdfWriter.PageLayoutTwoPageLeft;
                }
                else if (name.Equals(PdfName.TWOPAGERIGHT))
                {
                    prefs |= PdfWriter.PageLayoutTwoPageRight;
                }
            }
            // page mode
            obj = PdfReader.GetPdfObjectRelease(catalog.Get(PdfName.PAGEMODE));
            if (obj != null && obj.IsName())
            {
                name = (PdfName)obj;
                if (name.Equals(PdfName.USENONE))
                {
                    prefs |= PdfWriter.PageModeUseNone;
                }
                else if (name.Equals(PdfName.USEOUTLINES))
                {
                    prefs |= PdfWriter.PageModeUseOutlines;
                }
                else if (name.Equals(PdfName.USETHUMBS))
                {
                    prefs |= PdfWriter.PageModeUseThumbs;
                }
                else if (name.Equals(PdfName.FULLSCREEN))
                {
                    prefs |= PdfWriter.PageModeFullScreen;
                }
                else if (name.Equals(PdfName.USEOC))
                {
                    prefs |= PdfWriter.PageModeUseOC;
                }
                else if (name.Equals(PdfName.USEATTACHMENTS))
                {
                    prefs |= PdfWriter.PageModeUseAttachments;
                }
            }
            // set page layout and page mode preferences
            preferences.ViewerPreferences = prefs;
            // other preferences
            obj = PdfReader.GetPdfObjectRelease(catalog
                                                .Get(PdfName.VIEWERPREFERENCES));
            if (obj != null && obj.IsDictionary())
            {
                PdfDictionary vp = (PdfDictionary)obj;
                for (int i = 0; i < VIEWER_PREFERENCES.Length; i++)
                {
                    obj = PdfReader.GetPdfObjectRelease(vp.Get(VIEWER_PREFERENCES[i]));
                    preferences.AddViewerPreference(VIEWER_PREFERENCES[i], obj);
                }
            }
            return(preferences);
        }
示例#16
0
        private static void SeparateKids(PdfDictionary structElem, ICollection <PdfObject> firstPartElems, StructureTreeCopier.LastClonedAncestor
                                         lastCloned)
        {
            PdfObject k = structElem.Get(PdfName.K);

            // If /K entry is not a PdfArray - it would be a kid which we won't clone at the moment, because it won't contain
            // kids from both parts at the same time. It would either be cloned as an ancestor later, or not cloned at all.
            // If it's kid is struct elem - it would definitely be structElem from the first part, so we simply call separateKids for it.
            if (!k.IsArray())
            {
                if (k.IsDictionary() && PdfStructElem.IsStructElem((PdfDictionary)k))
                {
                    SeparateKids((PdfDictionary)k, firstPartElems, lastCloned);
                }
            }
            else
            {
                PdfDocument document = structElem.GetIndirectReference().GetDocument();
                PdfArray    kids     = (PdfArray)k;
                for (int i = 0; i < kids.Size(); ++i)
                {
                    PdfObject     kid     = kids.Get(i);
                    PdfDictionary dictKid = null;
                    if (kid.IsDictionary())
                    {
                        dictKid = (PdfDictionary)kid;
                    }
                    if (dictKid != null && PdfStructElem.IsStructElem(dictKid))
                    {
                        if (firstPartElems.Contains(kid))
                        {
                            SeparateKids((PdfDictionary)kid, firstPartElems, lastCloned);
                        }
                        else
                        {
                            if (dictKid.IsFlushed())
                            {
                                throw new PdfException(PdfException.TagFromTheExistingTagStructureIsFlushedCannotAddCopiedPageTags);
                            }
                            // elems with no kids will not be marked as from the first part,
                            // but nonetheless we don't want to move all of them to the second part; we just leave them as is
                            if (dictKid.ContainsKey(PdfName.K))
                            {
                                CloneParents(structElem, lastCloned, document);
                                kids.Remove(i--);
                                PdfStructElem.AddKidObject(lastCloned.clone, -1, kid);
                            }
                        }
                    }
                    else
                    {
                        if (!firstPartElems.Contains(kid))
                        {
                            CloneParents(structElem, lastCloned, document);
                            PdfMcr mcr;
                            if (dictKid != null)
                            {
                                if (dictKid.Get(PdfName.Type).Equals(PdfName.MCR))
                                {
                                    mcr = new PdfMcrDictionary(dictKid, new PdfStructElem(lastCloned.clone));
                                }
                                else
                                {
                                    mcr = new PdfObjRef(dictKid, new PdfStructElem(lastCloned.clone));
                                }
                            }
                            else
                            {
                                mcr = new PdfMcrNumber((PdfNumber)kid, new PdfStructElem(lastCloned.clone));
                            }
                            kids.Remove(i--);
                            PdfStructElem.AddKidObject(lastCloned.clone, -1, kid);
                            document.GetStructTreeRoot().GetParentTreeHandler().RegisterMcr(mcr);
                        }
                    }
                }
            }
            // re-register mcr
            if (lastCloned.ancestor == structElem)
            {
                lastCloned.ancestor = lastCloned.ancestor.GetAsDictionary(PdfName.P);
                lastCloned.clone    = lastCloned.clone.GetAsDictionary(PdfName.P);
            }
        }
        private bool ObjectsIsEquals(PdfObject outObj, PdfObject cmpObj)
        {
            PdfObject outDirectObj = PdfReader.GetPdfObject(outObj);
            PdfObject cmpDirectObj = PdfReader.GetPdfObject(cmpObj);

            if (outDirectObj == null || cmpDirectObj.Type != outDirectObj.Type)
            {
                return(false);
            }
            if (cmpDirectObj.IsDictionary())
            {
                PdfDictionary cmpDict = (PdfDictionary)cmpDirectObj;
                PdfDictionary outDict = (PdfDictionary)outDirectObj;
                if (cmpDict.IsPage())
                {
                    if (!outDict.IsPage())
                    {
                        return(false);
                    }
                    RefKey cmpRefKey = new RefKey((PRIndirectReference)cmpObj);
                    RefKey outRefKey = new RefKey((PRIndirectReference)outObj);
                    if (cmpPagesRef.Contains(cmpRefKey) && cmpPagesRef.IndexOf(cmpRefKey) == outPagesRef.IndexOf(outRefKey))
                    {
                        return(true);
                    }
                    return(false);
                }
                if (!ObjectsIsEquals(outDict, cmpDict))
                {
                    return(false);
                }
            }
            else if (cmpDirectObj.IsStream())
            {
                if (!ObjectsIsEquals((PRStream)outDirectObj, (PRStream)cmpDirectObj))
                {
                    return(false);
                }
            }
            else if (cmpDirectObj.IsArray())
            {
                if (!ObjectsIsEquals((PdfArray)outDirectObj, (PdfArray)cmpDirectObj))
                {
                    return(false);
                }
            }
            else if (cmpDirectObj.IsName())
            {
                if (!ObjectsIsEquals((PdfName)outDirectObj, (PdfName)cmpDirectObj))
                {
                    return(false);
                }
            }
            else if (cmpDirectObj.IsNumber())
            {
                if (!ObjectsIsEquals((PdfNumber)outDirectObj, (PdfNumber)cmpDirectObj))
                {
                    return(false);
                }
            }
            else if (cmpDirectObj.IsString())
            {
                if (!ObjectsIsEquals((PdfString)outDirectObj, (PdfString)cmpDirectObj))
                {
                    return(false);
                }
            }
            else if (cmpDirectObj.IsBoolean())
            {
                if (!ObjectsIsEquals((PdfBoolean)outDirectObj, (PdfBoolean)cmpDirectObj))
                {
                    return(false);
                }
            }
            else
            {
                throw new InvalidOperationException();
            }
            return(true);
        }
            private PdfDictionary GetPropertiesDictionary(PdfObject operand1, ResourceDictionary resources){
                if (operand1.IsDictionary())
                    return (PdfDictionary)operand1;

                PdfName dictionaryName = ((PdfName)operand1);
                return resources.GetAsDict(dictionaryName);
            }
示例#19
0
        protected override PdfIndirectReference CopyIndirect(PRIndirectReference @in)
        {
            PdfObject srcObj = PdfReader.GetPdfObjectRelease(@in);

            PdfSmartCopy.ByteStore streamKey = null;
            bool validStream = false;

            if (srcObj.IsStream())
            {
                streamKey   = new PdfSmartCopy.ByteStore((PRStream)srcObj, serialized);
                validStream = true;
                PdfIndirectReference streamRef;
                if (streamMap.TryGetValue(streamKey, out streamRef))
                {
                    return(streamRef);
                }
            }
            else if (srcObj.IsDictionary())
            {
                streamKey   = new PdfSmartCopy.ByteStore((PdfDictionary)srcObj, serialized);
                validStream = true;
                PdfIndirectReference streamRef;
                if (streamMap.TryGetValue(streamKey, out streamRef))
                {
                    return(streamRef);
                }
            }

            PdfIndirectReference theRef;
            RefKey             key = new RefKey(@in);
            IndirectReferences iRef;

            if (indirects.TryGetValue(key, out iRef))
            {
                theRef = iRef.Ref;
                if (iRef.Copied)
                {
                    return(theRef);
                }
            }
            else
            {
                theRef         = body.PdfIndirectReference;
                iRef           = new IndirectReferences(theRef);
                indirects[key] = iRef;
            }
            if (srcObj.IsDictionary())
            {
                PdfObject type = PdfReader.GetPdfObjectRelease(((PdfDictionary)srcObj).Get(PdfName.TYPE));
                if (type != null)
                {
                    if ((PdfName.PAGE.Equals(type)))
                    {
                        return(theRef);
                    }
                    if ((PdfName.CATALOG.Equals(type)))
                    {
                        LOGGER.Warn(MessageLocalization.GetComposedMessage("make.copy.of.catalog.dictionary.is.forbidden"));
                        return(null);
                    }
                }
            }

            iRef.SetCopied();

            if (validStream)
            {
                streamMap[streamKey] = theRef;
            }

            PdfObject obj = CopyObject(srcObj);

            AddToBody(obj, theRef);
            return(theRef);
        }
示例#20
0
 public static iText.Kernel.Pdf.Annot.PdfAnnotation MakeAnnotation(PdfObject pdfObject, iText.Kernel.Pdf.Annot.PdfAnnotation
                                                                   parent)
 {
     iText.Kernel.Pdf.Annot.PdfAnnotation annotation = null;
     if (pdfObject.IsIndirectReference())
     {
         pdfObject = ((PdfIndirectReference)pdfObject).GetRefersTo();
     }
     if (pdfObject.IsDictionary())
     {
         PdfDictionary dictionary = (PdfDictionary)pdfObject;
         PdfName       subtype    = dictionary.GetAsName(PdfName.Subtype);
         if (PdfName.Link.Equals(subtype))
         {
             annotation = new PdfLinkAnnotation((PdfDictionary)pdfObject);
         }
         else
         {
             if (PdfName.Popup.Equals(subtype))
             {
                 annotation = new PdfPopupAnnotation((PdfDictionary)pdfObject);
             }
             else
             {
                 if (PdfName.Widget.Equals(subtype))
                 {
                     annotation = new PdfWidgetAnnotation((PdfDictionary)pdfObject);
                 }
                 else
                 {
                     if (PdfName.Screen.Equals(subtype))
                     {
                         annotation = new PdfScreenAnnotation((PdfDictionary)pdfObject);
                     }
                     else
                     {
                         if (PdfName._3D.Equals(subtype))
                         {
                             throw new NotSupportedException();
                         }
                         else
                         {
                             if (PdfName.Highlight.Equals(subtype) || PdfName.Underline.Equals(subtype) || PdfName.Squiggly.Equals(subtype
                                                                                                                                   ) || PdfName.StrikeOut.Equals(subtype))
                             {
                                 annotation = new PdfTextMarkupAnnotation((PdfDictionary)pdfObject);
                             }
                             else
                             {
                                 if (PdfName.Caret.Equals(subtype))
                                 {
                                     annotation = new PdfCaretAnnotation((PdfDictionary)pdfObject);
                                 }
                                 else
                                 {
                                     if (PdfName.Text.Equals(subtype))
                                     {
                                         annotation = new PdfTextAnnotation((PdfDictionary)pdfObject);
                                     }
                                     else
                                     {
                                         if (PdfName.Sound.Equals(subtype))
                                         {
                                             annotation = new PdfSoundAnnotation((PdfDictionary)pdfObject);
                                         }
                                         else
                                         {
                                             if (PdfName.Stamp.Equals(subtype))
                                             {
                                                 annotation = new PdfStampAnnotation((PdfDictionary)pdfObject);
                                             }
                                             else
                                             {
                                                 if (PdfName.FileAttachment.Equals(subtype))
                                                 {
                                                     annotation = new PdfFileAttachmentAnnotation((PdfDictionary)pdfObject);
                                                 }
                                                 else
                                                 {
                                                     if (PdfName.Ink.Equals(subtype))
                                                     {
                                                         annotation = new PdfInkAnnotation((PdfDictionary)pdfObject);
                                                     }
                                                     else
                                                     {
                                                         if (PdfName.PrinterMark.Equals(subtype))
                                                         {
                                                             annotation = new PdfPrinterMarkAnnotation((PdfDictionary)pdfObject);
                                                         }
                                                         else
                                                         {
                                                             if (PdfName.TrapNet.Equals(subtype))
                                                             {
                                                                 annotation = new PdfTrapNetworkAnnotation((PdfDictionary)pdfObject);
                                                             }
                                                             else
                                                             {
                                                                 if (PdfName.FreeText.Equals(subtype))
                                                                 {
                                                                     annotation = new PdfFreeTextAnnotation((PdfDictionary)pdfObject);
                                                                 }
                                                                 else
                                                                 {
                                                                     if (PdfName.Square.Equals(subtype))
                                                                     {
                                                                         annotation = new PdfSquareAnnotation((PdfDictionary)pdfObject);
                                                                     }
                                                                     else
                                                                     {
                                                                         if (PdfName.Circle.Equals(subtype))
                                                                         {
                                                                             annotation = new PdfCircleAnnotation((PdfDictionary)pdfObject);
                                                                         }
                                                                         else
                                                                         {
                                                                             if (PdfName.Line.Equals(subtype))
                                                                             {
                                                                                 annotation = new PdfLineAnnotation((PdfDictionary)pdfObject);
                                                                             }
                                                                             else
                                                                             {
                                                                                 if (PdfName.Polygon.Equals(subtype) || PdfName.PolyLine.Equals(subtype))
                                                                                 {
                                                                                     annotation = new PdfPolyGeomAnnotation((PdfDictionary)pdfObject);
                                                                                 }
                                                                                 else
                                                                                 {
                                                                                     if (PdfName.Redact.Equals(subtype))
                                                                                     {
                                                                                         annotation = new PdfRedactAnnotation((PdfDictionary)pdfObject);
                                                                                     }
                                                                                     else
                                                                                     {
                                                                                         if (PdfName.Watermark.Equals(subtype))
                                                                                         {
                                                                                             annotation = new PdfWatermarkAnnotation((PdfDictionary)pdfObject);
                                                                                         }
                                                                                     }
                                                                                 }
                                                                             }
                                                                         }
                                                                     }
                                                                 }
                                                             }
                                                         }
                                                     }
                                                 }
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     }
     if (annotation is PdfMarkupAnnotation)
     {
         PdfMarkupAnnotation markup    = (PdfMarkupAnnotation)annotation;
         PdfDictionary       inReplyTo = markup.GetInReplyToObject();
         if (inReplyTo != null)
         {
             markup.SetInReplyTo(MakeAnnotation(inReplyTo));
         }
         PdfDictionary popup = markup.GetPopupObject();
         if (popup != null)
         {
             markup.SetPopup((PdfPopupAnnotation)MakeAnnotation(popup, markup));
         }
     }
     if (annotation is PdfPopupAnnotation)
     {
         PdfPopupAnnotation popup = (PdfPopupAnnotation)annotation;
         if (parent != null)
         {
             popup.SetParent(parent);
         }
     }
     return(annotation);
 }
示例#21
0
        public static PdfViewerPreferencesImp GetViewerPreferences(PdfDictionary catalog)
        {
            PdfViewerPreferencesImp preferences = new PdfViewerPreferencesImp();
            int     prefs = 0;
            PdfName name  = null;
            // page layout
            PdfObject obj = PdfReader.GetPdfObjectRelease(catalog.Get(PdfName.Pagelayout));

            if (obj != null && obj.IsName())
            {
                name = (PdfName)obj;
                if (name.Equals(PdfName.Singlepage))
                {
                    prefs |= PdfWriter.PageLayoutSinglePage;
                }
                else if (name.Equals(PdfName.Onecolumn))
                {
                    prefs |= PdfWriter.PageLayoutOneColumn;
                }
                else if (name.Equals(PdfName.Twocolumnleft))
                {
                    prefs |= PdfWriter.PageLayoutTwoColumnLeft;
                }
                else if (name.Equals(PdfName.Twocolumnright))
                {
                    prefs |= PdfWriter.PageLayoutTwoColumnRight;
                }
                else if (name.Equals(PdfName.Twopageleft))
                {
                    prefs |= PdfWriter.PageLayoutTwoPageLeft;
                }
                else if (name.Equals(PdfName.Twopageright))
                {
                    prefs |= PdfWriter.PageLayoutTwoPageRight;
                }
            }
            // page mode
            obj = PdfReader.GetPdfObjectRelease(catalog.Get(PdfName.Pagemode));
            if (obj != null && obj.IsName())
            {
                name = (PdfName)obj;
                if (name.Equals(PdfName.Usenone))
                {
                    prefs |= PdfWriter.PageModeUseNone;
                }
                else if (name.Equals(PdfName.Useoutlines))
                {
                    prefs |= PdfWriter.PageModeUseOutlines;
                }
                else if (name.Equals(PdfName.Usethumbs))
                {
                    prefs |= PdfWriter.PageModeUseThumbs;
                }
                else if (name.Equals(PdfName.Fullscreen))
                {
                    prefs |= PdfWriter.PageModeFullScreen;
                }
                else if (name.Equals(PdfName.Useoc))
                {
                    prefs |= PdfWriter.PageModeUseOC;
                }
                else if (name.Equals(PdfName.Useattachments))
                {
                    prefs |= PdfWriter.PageModeUseAttachments;
                }
            }
            // set page layout and page mode preferences
            preferences.ViewerPreferences = prefs;
            // other preferences
            obj = PdfReader.GetPdfObjectRelease(catalog
                                                .Get(PdfName.Viewerpreferences));
            if (obj != null && obj.IsDictionary())
            {
                PdfDictionary vp = (PdfDictionary)obj;
                for (int i = 0; i < VIEWER_PREFERENCES.Length; i++)
                {
                    obj = PdfReader.GetPdfObjectRelease(vp.Get(VIEWER_PREFERENCES[i]));
                    preferences.AddViewerPreference(VIEWER_PREFERENCES[i], obj);
                }
            }
            return(preferences);
        }
示例#22
0
        public static string GetObjectInfo(PdfObject pdfObject)
        {
            StringBuilder sb = new StringBuilder();

            if (pdfObject != null)
            {
                if (pdfObject.IsBoolean())
                {
                    sb.Append(", bool");
                }
                if (pdfObject.IsNumber())
                {
                    sb.Append(", number");
                }
                if (pdfObject.IsString())
                {
                    sb.Append(", string");
                }
                if (pdfObject.IsLiteral())
                {
                    sb.Append(", literal");
                }
                if (pdfObject.IsArray())
                {
                    sb.Append(", array");
                }
                if (pdfObject.IsDictionary())
                {
                    sb.Append(", dictionary");
                }
                if (pdfObject.IsName())
                {
                    sb.Append(", name");
                }
                if (pdfObject.IsStream())
                {
                    sb.Append(", stream");
                }
                if (pdfObject.IsIndirect())
                {
                    sb.Append(", indirect");
                }
                if (pdfObject.IsIndirectReference())
                {
                    sb.Append(", indirect reference");
                }
                if (pdfObject.IsModified())
                {
                    sb.Append(", modified");
                }
                if (pdfObject.IsNull())
                {
                    sb.Append(", null");
                }
                if (sb.Length > 0)
                {
                    sb.Remove(0, 2);
                }
            }
            else
            {
                sb.Append("null");
            }
            return(sb.ToString());
        }
示例#23
0
 private void SerObject(PdfObject obj, int level, ByteBuffer bb)
 {
     if (level <= 0)
         return;
     if (obj == null) {
         bb.Append("$Lnull");
         return;
     }
     obj = PdfReader.GetPdfObject(obj);
     if (obj.IsStream()) {
         bb.Append("$B");
         SerDic((PdfDictionary)obj, level - 1, bb);
         if (level > 0) {
             md5.Initialize();
             bb.Append(md5.ComputeHash(PdfReader.GetStreamBytesRaw((PRStream)obj)));
         }
     }
     else if (obj.IsDictionary()) {
         SerDic((PdfDictionary)obj, level - 1, bb);
     }
     else if (obj.IsArray()) {
         SerArray((PdfArray)obj, level - 1, bb);
     }
     else if (obj.IsString()) {
         bb.Append("$S").Append(obj.ToString());
     }
     else if (obj.IsName()) {
         bb.Append("$N").Append(obj.ToString());
     }
     else
         bb.Append("$L").Append(obj.ToString());
 }