private void TestFile(String filename, String objectid, String expectedImageFormat)
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfReader(sourceFolder + filename));

            try {
                PdfResources  resources = pdfDocument.GetPage(1).GetResources();
                PdfDictionary xobjects  = resources.GetResource(PdfName.XObject);
                PdfObject     obj       = xobjects.Get(new PdfName(objectid));
                if (obj == null)
                {
                    throw new ArgumentException("Reference " + objectid + " not found - Available keys are " + xobjects.KeySet
                                                    ());
                }
                PdfImageXObject img = new PdfImageXObject((PdfStream)(obj.IsIndirectReference() ? ((PdfIndirectReference)obj
                                                                                                   ).GetRefersTo() : obj));
                NUnit.Framework.Assert.AreEqual(expectedImageFormat, img.IdentifyImageFileExtension());
                byte[] result   = img.GetImageBytes(true);
                byte[] cmpBytes = File.ReadAllBytes(Path.Combine(sourceFolder, filename.JSubstring(0, filename.Length - 4)
                                                                 + "." + expectedImageFormat));
                if (img.IdentifyImageFileExtension().Equals("tif"))
                {
                    CompareTiffImages(cmpBytes, result);
                }
                else
                {
                    NUnit.Framework.Assert.AreEqual(cmpBytes, result);
                }
            }
            finally {
                pdfDocument.Close();
            }
        }
        private IPdfStructElem ConvertPdfObjectToIPdfStructElem(PdfObject obj)
        {
            if (obj.IsIndirectReference())
            {
                obj = ((PdfIndirectReference)obj).GetRefersTo();
            }
            IPdfStructElem elem = null;

            switch (obj.GetObjectType())
            {
            case PdfObject.DICTIONARY: {
                PdfDictionary d = (PdfDictionary)obj;
                if (IsStructElem(d))
                {
                    elem = new iText.Kernel.Pdf.Tagging.PdfStructElem(d);
                }
                else
                {
                    if (PdfName.MCR.Equals(d.GetAsName(PdfName.Type)))
                    {
                        elem = new PdfMcrDictionary(d, this);
                    }
                    else
                    {
                        if (PdfName.OBJR.Equals(d.GetAsName(PdfName.Type)))
                        {
                            elem = new PdfObjRef(d, this);
                        }
                    }
                }
                break;
            }

            case PdfObject.NUMBER: {
                elem = new PdfMcrNumber((PdfNumber)obj, this);
                break;
            }

            default: {
                break;
            }
            }
            return(elem);
        }
Beispiel #3
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);
 }
Beispiel #4
0
        /// <summary>Reads the layers from the document to be able to modify them in the future.</summary>
        private void ReadLayersFromDictionary()
        {
            PdfArray ocgs = GetPdfObject().GetAsArray(PdfName.OCGs);

            if (ocgs == null || ocgs.IsEmpty())
            {
                return;
            }
            IDictionary <PdfIndirectReference, PdfLayer> layerMap = new SortedDictionary <PdfIndirectReference, PdfLayer
                                                                                          >();

            for (int ind = 0; ind < ocgs.Size(); ind++)
            {
                PdfLayer currentLayer = new PdfLayer((PdfDictionary)ocgs.GetAsDictionary(ind).MakeIndirect(GetDocument()));
                // We will set onPanel to true later for the objects present in /D->/Order entry.
                currentLayer.onPanel = false;
                layerMap.Put(currentLayer.GetIndirectReference(), currentLayer);
            }
            PdfDictionary d = GetPdfObject().GetAsDictionary(PdfName.D);

            if (d != null && !d.IsEmpty())
            {
                PdfArray off = d.GetAsArray(PdfName.OFF);
                if (off != null)
                {
                    for (int i = 0; i < off.Size(); i++)
                    {
                        PdfObject offLayer = off.Get(i, false);
                        if (offLayer.IsIndirectReference())
                        {
                            layerMap.Get((PdfIndirectReference)offLayer).on = false;
                        }
                        else
                        {
                            layerMap.Get(offLayer.GetIndirectReference()).on = false;
                        }
                    }
                }
                PdfArray locked = d.GetAsArray(PdfName.Locked);
                if (locked != null)
                {
                    for (int i = 0; i < locked.Size(); i++)
                    {
                        PdfObject lockedLayer = locked.Get(i, false);
                        if (lockedLayer.IsIndirectReference())
                        {
                            layerMap.Get((PdfIndirectReference)lockedLayer).locked = true;
                        }
                        else
                        {
                            layerMap.Get(lockedLayer.GetIndirectReference()).locked = true;
                        }
                    }
                }
                PdfArray orderArray = d.GetAsArray(PdfName.Order);
                if (orderArray != null && !orderArray.IsEmpty())
                {
                    ReadOrderFromDictionary(null, orderArray, layerMap);
                }
            }
            // Add the layers which should not be displayed on the panel to the order list
            foreach (PdfLayer layer in layerMap.Values)
            {
                if (!layer.IsOnPanel())
                {
                    layers.Add(layer);
                }
            }
        }
Beispiel #5
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());
        }