public static PdfFileSpec Wrap(PdfDictionary dictionary)
        {
            if (!PdfName.EncryptedPayload.Equals(dictionary.GetAsName(PdfName.AFRelationship)))
            {
                LogManager.GetLogger(typeof(PdfEncryptedPayloadFileSpecFactory)).Error(iText.IO.LogMessageConstant.ENCRYPTED_PAYLOAD_FILE_SPEC_SHALL_HAVE_AFRELATIONSHIP_FILED_EQUAL_TO_ENCRYPTED_PAYLOAD
                                                                                       );
            }
            PdfDictionary ef = dictionary.GetAsDictionary(PdfName.EF);

            if (ef == null || (ef.GetAsStream(PdfName.F) == null) && (ef.GetAsStream(PdfName.UF) == null))
            {
                throw new PdfException(PdfException.EncryptedPayloadFileSpecShallHaveEFDictionary);
            }
            if (!PdfName.Filespec.Equals(dictionary.GetAsName(PdfName.Type)))
            {
                throw new PdfException(PdfException.EncryptedPayloadFileSpecShallHaveTypeEqualToFilespec);
            }
            if (!dictionary.IsIndirect())
            {
                throw new PdfException(PdfException.EncryptedPayloadFileSpecShallBeIndirect);
            }
            PdfFileSpec fileSpec = PdfFileSpec.WrapFileSpecObject(dictionary);

            if (PdfEncryptedPayload.ExtractFrom(fileSpec) == null)
            {
                throw new PdfException(PdfException.EncryptedPayloadFileSpecDoesntHaveEncryptedPayloadDictionary);
            }
            return(fileSpec);
        }
        private bool UpdateStructParentTreeForContentStreamEntries(IDictionary <int, PdfMcr> mcrsOfContentStream, int
                                                                   pageStructParentIndex)
        {
            // element indices in parentsOfMcrs shall be the same as mcid of one of their kids.
            // See "Finding Structure Elements from Content Items" in pdf spec.
            PdfArray parentsOfMcrs = new PdfArray();
            int      currentMcid   = 0;

            foreach (KeyValuePair <int, PdfMcr> entry in mcrsOfContentStream)
            {
                PdfMcr        mcr       = entry.Value;
                PdfDictionary parentObj = ((PdfStructElem)mcr.GetParent()).GetPdfObject();
                if (!parentObj.IsIndirect())
                {
                    continue;
                }
                // if for some reason some mcrs were not registered or don't exist, we ensure that the rest
                // of the parent objects were placed at correct index
                while (currentMcid++ < mcr.GetMcid())
                {
                    parentsOfMcrs.Add(PdfNull.PDF_NULL);
                }
                parentsOfMcrs.Add(parentObj);
            }
            if (!parentsOfMcrs.IsEmpty())
            {
                parentsOfMcrs.MakeIndirect(structTreeRoot.GetDocument());
                parentTree.AddEntry(pageStructParentIndex, parentsOfMcrs);
                structTreeRoot.GetDocument().CheckIsoConformance(parentsOfMcrs, IsoKey.TAG_STRUCTURE_ELEMENT);
                parentsOfMcrs.Flush();
                return(true);
            }
            return(false);
        }
Exemplo n.º 3
0
        private bool UpdateStructParentTreeEntries(PdfPage page, IDictionary <int, PdfMcr> mcrs)
        {
            bool res = false;
            // element indexes in parentsOfPageMcrs shall be the same as mcid of one of their kids.
            // See "Finding Structure Elements from Content Items" in pdf spec.
            PdfArray parentsOfPageMcrs = new PdfArray();
            int      currentMcid       = 0;

            foreach (KeyValuePair <int, PdfMcr> entry in mcrs)
            {
                PdfMcr        mcr       = entry.Value;
                PdfDictionary parentObj = ((PdfStructElem)mcr.GetParent()).GetPdfObject();
                if (!parentObj.IsIndirect())
                {
                    continue;
                }
                if (mcr is PdfObjRef)
                {
                    int structParent = KeyIntoStructParentIndex((int)entry.Key);
                    parentTree.AddEntry(structParent, parentObj);
                    res = true;
                }
                else
                {
                    // if for some reason some mcr where not registered or don't exist, we ensure that the rest
                    // of the parent objects were placed at correct index
                    while (currentMcid++ < mcr.GetMcid())
                    {
                        parentsOfPageMcrs.Add(PdfNull.PDF_NULL);
                    }
                    parentsOfPageMcrs.Add(parentObj);
                }
            }
            if (!parentsOfPageMcrs.IsEmpty())
            {
                int pageStructParentIndex;
                if (page.IsFlushed())
                {
                    PdfIndirectReference pageRef = page.GetPdfObject().GetIndirectReference();
                    if (!pageToStructParentsInd.ContainsKey(pageRef))
                    {
                        return(res);
                    }
                    pageStructParentIndex = (int)pageToStructParentsInd.JRemove(pageRef);
                }
                else
                {
                    pageStructParentIndex = GetOrCreatePageStructParentIndex(page);
                }
                parentsOfPageMcrs.MakeIndirect(structTreeRoot.GetDocument());
                parentTree.AddEntry(pageStructParentIndex, parentsOfPageMcrs);
                res = true;
                structTreeRoot.GetDocument().CheckIsoConformance(parentsOfPageMcrs, IsoKey.TAG_STRUCTURE_ELEMENT);
                parentsOfPageMcrs.Flush();
            }
            return(res);
        }
        private bool UpdateStructParentTreeEntries(PdfPage page, ParentTreeHandler.PageMcrsContainer mcrs)
        {
            bool res = false;

            foreach (KeyValuePair <int, PdfMcr> entry in mcrs.GetObjRefs())
            {
                PdfMcr        mcr       = entry.Value;
                PdfDictionary parentObj = ((PdfStructElem)mcr.GetParent()).GetPdfObject();
                if (!parentObj.IsIndirect())
                {
                    continue;
                }
                int structParent = entry.Key;
                parentTree.AddEntry(structParent, parentObj);
                res = true;
            }
            int pageStructParentIndex;

            foreach (KeyValuePair <PdfIndirectReference, SortedDictionary <int, PdfMcr> > entry in mcrs.GetPageResourceXObjects
                         ())
            {
                PdfIndirectReference xObjectRef = entry.Key;
                if (xObjectToStructParentsInd.ContainsKey(xObjectRef))
                {
                    pageStructParentIndex = (int)xObjectToStructParentsInd.JRemove(xObjectRef);
                    if (UpdateStructParentTreeForContentStreamEntries(entry.Value, pageStructParentIndex))
                    {
                        res = true;
                    }
                }
            }
            if (page.IsFlushed())
            {
                PdfIndirectReference pageRef = page.GetPdfObject().GetIndirectReference();
                if (!pageToStructParentsInd.ContainsKey(pageRef))
                {
                    return(res);
                }
                pageStructParentIndex = (int)pageToStructParentsInd.JRemove(pageRef);
            }
            else
            {
                pageStructParentIndex = GetOrCreatePageStructParentIndex(page);
            }
            if (UpdateStructParentTreeForContentStreamEntries(mcrs.GetPageContentStreamsMcrs(), pageStructParentIndex)
                )
            {
                res = true;
            }
            return(res);
        }
Exemplo n.º 5
0
        private void UpdateStructParentTreeEntries(PdfPage page, IDictionary <int, PdfMcr> mcrs)
        {
            // element indexes in parentsOfPageMcrs shall be the same as mcid of one of their kids.
            // See "Finding Structure Elements from Content Items" in pdf spec.
            PdfArray parentsOfPageMcrs = new PdfArray();
            int      currentMcid       = 0;

            foreach (KeyValuePair <int, PdfMcr> entry in mcrs)
            {
                PdfMcr        mcr       = entry.Value;
                PdfDictionary parentObj = ((PdfStructElem)mcr.GetParent()).GetPdfObject();
                if (!parentObj.IsIndirect())
                {
                    continue;
                }
                if (mcr is PdfObjRef)
                {
                    int structParent = KeyIntoStructParentIndex((int)entry.Key);
                    parentTree.AddEntry(structParent, parentObj);
                }
                else
                {
                    // if for some reason some mcr where not registered or don't exist, we ensure that the rest
                    // of the parent objects were placed at correct index
                    while (currentMcid++ < mcr.GetMcid())
                    {
                        parentsOfPageMcrs.Add(PdfNull.PDF_NULL);
                    }
                    parentsOfPageMcrs.Add(parentObj);
                }
            }
            if (parentsOfPageMcrs.Size() > 0)
            {
                parentsOfPageMcrs.MakeIndirect(structTreeRoot.GetDocument());
                int structParents = page.GetStructParentIndex() != -1 ? page.GetStructParentIndex() : page.GetDocument().GetNextStructParentIndex
                                        ();
                page.GetPdfObject().Put(PdfName.StructParents, new PdfNumber(structParents));
                parentTree.AddEntry(structParents, parentsOfPageMcrs);
                structTreeRoot.GetDocument().CheckIsoConformance(parentsOfPageMcrs, IsoKey.TAG_STRUCTURE_ELEMENT);
                parentsOfPageMcrs.Flush();
            }
        }
Exemplo n.º 6
0
        private static PdfDictionary CopyObject(PdfDictionary source, ICollection <PdfObject> objectsToCopy, PdfDocument
                                                toDocument, IDictionary <PdfDictionary, PdfDictionary> page2page, bool copyFromDestDocument)
        {
            PdfDictionary copied;

            if (copyFromDestDocument)
            {
                copied = source.Clone(ignoreKeysForCopy);
                if (source.IsIndirect())
                {
                    copied.MakeIndirect(toDocument);
                }
            }
            else
            {
                copied = source.CopyTo(toDocument, ignoreKeysForCopy, true);
            }
            if (source.ContainsKey(PdfName.Obj))
            {
                PdfDictionary obj = source.GetAsDictionary(PdfName.Obj);
                if (!copyFromDestDocument && obj != null)
                {
                    // Link annotations could be not added to the toDocument, so we need to identify this case.
                    // When obj.copyTo is called, and annotation was already copied, we would get this already created copy.
                    // If it was already copied and added, /P key would be set. Otherwise /P won't be set.
                    obj = obj.CopyTo(toDocument, iText.IO.Util.JavaUtil.ArraysAsList(PdfName.P), false);
                    copied.Put(PdfName.Obj, obj);
                }
            }
            PdfDictionary pg = source.GetAsDictionary(PdfName.Pg);

            if (pg != null)
            {
                //TODO It is possible, that pg will not be present in the page2page map. Consider the situation,
                // that we want to copy structElem because it has marked content dictionary reference, which belongs to the page from page2page,
                // but the structElem itself has /Pg which value could be arbitrary page.
                copied.Put(PdfName.Pg, page2page.Get(pg));
            }
            PdfObject k = source.Get(PdfName.K);

            if (k != null)
            {
                if (k.IsArray())
                {
                    PdfArray kArr   = (PdfArray)k;
                    PdfArray newArr = new PdfArray();
                    for (int i = 0; i < kArr.Size(); i++)
                    {
                        PdfObject copiedKid = CopyObjectKid(kArr.Get(i), copied, objectsToCopy, toDocument, page2page, copyFromDestDocument
                                                            );
                        if (copiedKid != null)
                        {
                            newArr.Add(copiedKid);
                        }
                    }
                    // TODO new array may be empty or with single element
                    copied.Put(PdfName.K, newArr);
                }
                else
                {
                    PdfObject copiedKid = CopyObjectKid(k, copied, objectsToCopy, toDocument, page2page, copyFromDestDocument);
                    if (copiedKid != null)
                    {
                        copied.Put(PdfName.K, copiedKid);
                    }
                }
            }
            return(copied);
        }
Exemplo n.º 7
0
        private static PdfDictionary CopyObject(PdfDictionary source, PdfDictionary destPage, bool parentChangePg,
                                                StructureTreeCopier.StructElemCopyingParams copyingParams)
        {
            PdfDictionary copied;

            if (copyingParams.IsCopyFromDestDocument())
            {
                //TODO: detect wether object is needed to be cloned at all
                copied = source.Clone(ignoreKeysForClone);
                if (source.IsIndirect())
                {
                    copied.MakeIndirect(copyingParams.GetToDocument());
                }
                PdfDictionary pg = source.GetAsDictionary(PdfName.Pg);
                if (pg != null)
                {
                    if (copyingParams.IsCopyFromDestDocument())
                    {
                        if (pg != destPage)
                        {
                            copied.Put(PdfName.Pg, destPage);
                            parentChangePg = true;
                        }
                        else
                        {
                            parentChangePg = false;
                        }
                    }
                }
            }
            else
            {
                copied = source.CopyTo(copyingParams.GetToDocument(), ignoreKeysForCopy, true);
                PdfDictionary obj = source.GetAsDictionary(PdfName.Obj);
                if (obj != null)
                {
                    // Link annotations could be not added to the toDocument, so we need to identify this case.
                    // When obj.copyTo is called, and annotation was already copied, we would get this already created copy.
                    // If it was already copied and added, /P key would be set. Otherwise /P won't be set.
                    obj = obj.CopyTo(copyingParams.GetToDocument(), JavaUtil.ArraysAsList(PdfName.P), false);
                    copied.Put(PdfName.Obj, obj);
                }
                PdfDictionary nsDict = source.GetAsDictionary(PdfName.NS);
                if (nsDict != null)
                {
                    PdfDictionary copiedNsDict = CopyNamespaceDict(nsDict, copyingParams);
                    copied.Put(PdfName.NS, copiedNsDict);
                }
                PdfDictionary pg = source.GetAsDictionary(PdfName.Pg);
                if (pg != null)
                {
                    PdfDictionary pageAnalog = copyingParams.GetPage2page().Get(pg);
                    if (pageAnalog == null)
                    {
                        pageAnalog     = destPage;
                        parentChangePg = true;
                    }
                    else
                    {
                        parentChangePg = false;
                    }
                    copied.Put(PdfName.Pg, pageAnalog);
                }
            }
            PdfObject k = source.Get(PdfName.K);

            if (k != null)
            {
                if (k.IsArray())
                {
                    PdfArray kArr   = (PdfArray)k;
                    PdfArray newArr = new PdfArray();
                    for (int i = 0; i < kArr.Size(); i++)
                    {
                        PdfObject copiedKid = CopyObjectKid(kArr.Get(i), copied, destPage, parentChangePg, copyingParams);
                        if (copiedKid != null)
                        {
                            newArr.Add(copiedKid);
                        }
                    }
                    if (!newArr.IsEmpty())
                    {
                        if (newArr.Size() == 1)
                        {
                            copied.Put(PdfName.K, newArr.Get(0));
                        }
                        else
                        {
                            copied.Put(PdfName.K, newArr);
                        }
                    }
                }
                else
                {
                    PdfObject copiedKid = CopyObjectKid(k, copied, destPage, parentChangePg, copyingParams);
                    if (copiedKid != null)
                    {
                        copied.Put(PdfName.K, copiedKid);
                    }
                }
            }
            return(copied);
        }
 private IDictionary <int, CleanUpImagesCompareTool.PageImageObjectsPaths> ExtractImagesFromPdf(String pdf,
                                                                                                String outputPath)
 {
     using (PdfReader readerPdf = new PdfReader(pdf)) {
         using (PdfDocument pdfDoc = new PdfDocument(readerPdf)) {
             IDictionary <int, CleanUpImagesCompareTool.PageImageObjectsPaths> imageObjectDatas = new Dictionary <int, CleanUpImagesCompareTool.PageImageObjectsPaths
                                                                                                                  >();
             for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
             {
                 PdfPage page = pdfDoc.GetPage(i);
                 CleanUpImagesCompareTool.PageImageObjectsPaths imageObjectData = new CleanUpImagesCompareTool.PageImageObjectsPaths
                                                                                      (page.GetPdfObject().GetIndirectReference());
                 Stack <LocalPathItem> baseLocalPath = new Stack <LocalPathItem>();
                 PdfResources          pdfResources  = page.GetResources();
                 if (pdfResources.GetPdfObject().IsIndirect())
                 {
                     imageObjectData.AddIndirectReference(pdfResources.GetPdfObject().GetIndirectReference());
                 }
                 else
                 {
                     baseLocalPath.Push(new DictPathItem(PdfName.Resources));
                 }
                 PdfDictionary xObjects = pdfResources.GetResource(PdfName.XObject);
                 if (xObjects == null)
                 {
                     continue;
                 }
                 if (xObjects.IsIndirect())
                 {
                     imageObjectData.AddIndirectReference(xObjects.GetIndirectReference());
                     baseLocalPath.Clear();
                 }
                 else
                 {
                     baseLocalPath.Push(new DictPathItem(PdfName.XObject));
                 }
                 bool isPageToGsExtract = false;
                 foreach (PdfName objectName in xObjects.KeySet())
                 {
                     if (!xObjects.Get(objectName).IsStream() || !PdfName.Image.Equals(xObjects.GetAsStream(objectName).GetAsName
                                                                                           (PdfName.Subtype)))
                     {
                         continue;
                     }
                     PdfImageXObject pdfObject = new PdfImageXObject(xObjects.GetAsStream(objectName));
                     baseLocalPath.Push(new DictPathItem(objectName));
                     if (!useGs)
                     {
                         String extension = pdfObject.IdentifyImageFileExtension();
                         String fileName  = outputPath + objectName + "_" + i + "." + extension;
                         CreateImageFromPdfXObject(fileName, pdfObject);
                     }
                     else
                     {
                         isPageToGsExtract = true;
                     }
                     Stack <LocalPathItem> reversedStack = new Stack <LocalPathItem>();
                     reversedStack.AddAll(baseLocalPath);
                     Stack <LocalPathItem> resultStack = new Stack <LocalPathItem>();
                     resultStack.AddAll(reversedStack);
                     imageObjectData.AddLocalPath(resultStack);
                     baseLocalPath.Pop();
                 }
                 if (useGs && isPageToGsExtract)
                 {
                     String fileName = "Page_" + i;
                     ghostscriptHelper.RunGhostScriptImageGeneration(pdf, outputPath, fileName, i.ToString());
                 }
                 CleanUpImagesCompareTool.ImageRenderListener listener = new CleanUpImagesCompareTool.ImageRenderListener();
                 PdfCanvasProcessor parser = new PdfCanvasProcessor(listener);
                 parser.ProcessPageContent(page);
                 ignoredImagesAreas.Put(i, listener.GetImageRectangles());
                 imageObjectDatas.Put(i, imageObjectData);
             }
             return(imageObjectDatas);
         }
     }
 }