예제 #1
0
        internal virtual PdfDestination CopyDestination(PdfObject dest, IDictionary <PdfPage, PdfPage> page2page, PdfDocument
                                                        toDocument)
        {
            PdfDestination d = null;

            if (dest.IsArray())
            {
                PdfObject pageObject = ((PdfArray)dest).Get(0);
                foreach (PdfPage oldPage in page2page.Keys)
                {
                    if (oldPage.GetPdfObject() == pageObject)
                    {
                        // in the copiedArray old page ref will be correctly replaced by the new page ref as this page is already copied
                        PdfArray copiedArray = (PdfArray)dest.CopyTo(toDocument, false);
                        d = new PdfExplicitDestination(copiedArray);
                        break;
                    }
                }
            }
            else
            {
                if (dest.IsString() || dest.IsName())
                {
                    PdfNameTree destsTree = GetNameTree(PdfName.Dests);
                    IDictionary <String, PdfObject> dests = destsTree.GetNames();
                    String   srcDestName  = dest.IsString() ? ((PdfString)dest).ToUnicodeString() : ((PdfName)dest).GetValue();
                    PdfArray srcDestArray = (PdfArray)dests.Get(srcDestName);
                    if (srcDestArray != null)
                    {
                        PdfObject pageObject = srcDestArray.Get(0);
                        if (pageObject is PdfNumber)
                        {
                            pageObject = GetDocument().GetPage(((PdfNumber)pageObject).IntValue() + 1).GetPdfObject();
                        }
                        foreach (PdfPage oldPage in page2page.Keys)
                        {
                            if (oldPage.GetPdfObject() == pageObject)
                            {
                                d = new PdfStringDestination(srcDestName);
                                if (!IsEqualSameNameDestExist(page2page, toDocument, srcDestName, srcDestArray, oldPage))
                                {
                                    // in the copiedArray old page ref will be correctly replaced by the new page ref as this page is already copied
                                    PdfArray copiedArray = (PdfArray)srcDestArray.CopyTo(toDocument, false);
                                    // here we can safely replace first item of the array because array of NamedDestination or StringDestination
                                    // never refers to page in another document via PdfNumber, but should always refer to page within current document
                                    // via page object reference.
                                    copiedArray.Set(0, page2page.Get(oldPage).GetPdfObject());
                                    toDocument.AddNamedDestination(srcDestName, copiedArray);
                                }
                                break;
                            }
                        }
                    }
                }
            }
            return(d);
        }
예제 #2
0
        internal virtual PdfDestination CopyDestination(PdfObject dest, IDictionary <PdfPage, PdfPage> page2page, PdfDocument
                                                        toDocument)
        {
            PdfDestination d = null;

            if (dest.IsArray())
            {
                PdfObject pageObject = ((PdfArray)dest).Get(0);
                foreach (PdfPage oldPage in page2page.Keys)
                {
                    if (oldPage.GetPdfObject() == pageObject)
                    {
                        // in the copiedArray old page ref will be correctly replaced by the new page ref as this page is already copied
                        PdfArray copiedArray = (PdfArray)dest.CopyTo(toDocument, false);
                        d = new PdfExplicitDestination(copiedArray);
                        break;
                    }
                }
            }
            else
            {
                if (dest.IsString())
                {
                    PdfNameTree destsTree = GetNameTree(PdfName.Dests);
                    IDictionary <String, PdfObject> dests = destsTree.GetNames();
                    String   srcDestName  = ((PdfString)dest).ToUnicodeString();
                    PdfArray srcDestArray = (PdfArray)dests.Get(srcDestName);
                    if (srcDestArray != null)
                    {
                        PdfObject pageObject = srcDestArray.Get(0);
                        foreach (PdfPage oldPage in page2page.Keys)
                        {
                            if (oldPage.GetPdfObject() == pageObject)
                            {
                                d = new PdfStringDestination(srcDestName);
                                if (!IsEqualSameNameDestExist(page2page, toDocument, srcDestName, srcDestArray, oldPage))
                                {
                                    // in the copiedArray old page ref will be correctly replaced by the new page ref as this page is already copied
                                    PdfArray copiedArray = (PdfArray)srcDestArray.CopyTo(toDocument, false);
                                    toDocument.AddNamedDestination(srcDestName, copiedArray);
                                }
                                break;
                            }
                        }
                    }
                }
            }
            return(d);
        }
예제 #3
0
 private static bool OrderBranchContainsSetElements(PdfObject arrayObj, PdfArray array, int currentIndex, ICollection
     <PdfIndirectReference> ocgs, PdfArray toOcgs, PdfDocument toDocument) {
     if (arrayObj.IsDictionary()) {
         if (ocgs.Contains(arrayObj.GetIndirectReference())) {
             return true;
         }
         else {
             if (currentIndex < (array.Size() - 1) && array.Get(currentIndex + 1).IsArray()) {
                 PdfArray nextArray = array.GetAsArray(currentIndex + 1);
                 if (!nextArray.Get(0).IsString()) {
                     bool result = iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(nextArray, array, currentIndex
                          + 1, ocgs, toOcgs, toDocument);
                     if (result && toOcgs != null && !ocgs.Contains(arrayObj.GetIndirectReference())) {
                         // Add the OCG to the OCGs array to register the OCG in document, since it is not used
                         // directly in the document, but is used as a parent for the order group. If it is not added
                         // to the OCGs array, then the OCG will be deleted at the 3rd stage of the /Order entry coping.
                         toOcgs.Add(arrayObj.CopyTo(toDocument, false));
                     }
                     return result;
                 }
             }
         }
     }
     else {
         if (arrayObj.IsArray()) {
             PdfArray arrayItem = (PdfArray)arrayObj;
             for (int i = 0; i < arrayItem.Size(); i++) {
                 PdfObject obj = arrayItem.Get(i);
                 if (iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(obj, arrayItem, i, ocgs, toOcgs, toDocument
                     )) {
                     return true;
                 }
             }
             if (!arrayItem.IsEmpty() && !arrayItem.Get(0).IsString()) {
                 if (currentIndex > 0 && array.Get(currentIndex - 1).IsDictionary()) {
                     PdfDictionary previousDict = (PdfDictionary)array.Get(currentIndex - 1);
                     return ocgs.Contains(previousDict.GetIndirectReference());
                 }
             }
         }
     }
     return false;
 }
예제 #4
0
        public virtual void CopyObject8()
        {
            String      exceptionMessage = null;
            PdfDocument pdfDoc           = new PdfDocument(new PdfWriter(destinationFolder + "copyObject6_1.pdf"));

            try {
                PdfPage       page1        = pdfDoc.AddNewPage();
                PdfDictionary directDict   = new PdfDictionary();
                PdfObject     indirectDict = ((PdfDictionary) new PdfDictionary().MakeIndirect(pdfDoc));
                page1.GetPdfObject().Put(new PdfName("HelloWorldDirect"), directDict);
                page1.GetPdfObject().Put(new PdfName("HelloWorldIndirect"), indirectDict);
                indirectDict.CopyTo(null);
            }
            catch (PdfException ex) {
                exceptionMessage = ex.Message;
            }
            finally {
                pdfDoc.Close();
            }
            NUnit.Framework.Assert.AreEqual(exceptionMessage, PdfException.DocumentForCopyToCannotBeNull);
        }
예제 #5
0
 private static void CopyDArrayField(PdfName fieldToCopy, ICollection<PdfIndirectReference> fromOcgsToCopy, 
     PdfDictionary fromDict, PdfDictionary toDict, PdfDocument toDocument) {
     if (fromDict.GetAsArray(fieldToCopy) == null) {
         return;
     }
     PdfArray fromArray = fromDict.GetAsArray(fieldToCopy);
     if (toDict.GetAsArray(fieldToCopy) == null) {
         toDict.Put(fieldToCopy, new PdfArray());
     }
     PdfArray toArray = toDict.GetAsArray(fieldToCopy);
     ICollection<PdfIndirectReference> toOcgsToCopy = new HashSet<PdfIndirectReference>();
     foreach (PdfIndirectReference fromRef in fromOcgsToCopy) {
         toOcgsToCopy.Add(fromRef.GetRefersTo().CopyTo(toDocument, false).GetIndirectReference());
     }
     if (PdfName.Order.Equals(fieldToCopy)) {
         // Stage 1: delete all Order the entire branches from the output document in which the copied OCGs were
         IList<int> removeIndex = new List<int>();
         for (int i = 0; i < toArray.Size(); i++) {
             PdfObject toOrderItem = toArray.Get(i);
             if (iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(toOrderItem, toArray, i, toOcgsToCopy
                 , null, null)) {
                 removeIndex.Add(i);
             }
         }
         for (int i = removeIndex.Count - 1; i > -1; i--) {
             toArray.Remove(removeIndex[i]);
         }
         PdfArray toOcgs = toDocument.GetCatalog().GetPdfObject().GetAsDictionary(PdfName.OCProperties).GetAsArray(
             PdfName.OCGs);
         // Stage 2: copy all the Order the entire branches in which the copied OСGs were
         for (int i = 0; i < fromArray.Size(); i++) {
             PdfObject fromOrderItem = fromArray.Get(i);
             if (iText.Kernel.Pdf.OcgPropertiesCopier.OrderBranchContainsSetElements(fromOrderItem, fromArray, i, fromOcgsToCopy
                 , toOcgs, toDocument)) {
                 toArray.Add(fromOrderItem.CopyTo(toDocument, false));
             }
         }
     }
     else {
         // Stage 3: remove from Order OCGs not presented in the output document. When forming
         // the Order dictionary in the PdfOcProperties constructor, only those OCGs that are
         // in the OCProperties/OCGs array will be taken into account
         if (PdfName.RBGroups.Equals(fieldToCopy)) {
             // Stage 1: delete all RBGroups from the output document in which the copied OCGs were
             for (int i = toArray.Size() - 1; i > -1; i--) {
                 PdfArray toRbGroup = (PdfArray)toArray.Get(i);
                 foreach (PdfObject toRbGroupItemObj in toRbGroup) {
                     if (toOcgsToCopy.Contains(toRbGroupItemObj.GetIndirectReference())) {
                         toArray.Remove(i);
                         break;
                     }
                 }
             }
             // Stage 2: copy all the RBGroups in which the copied OCGs were
             foreach (PdfObject fromRbGroupObj in fromArray) {
                 PdfArray fromRbGroup = (PdfArray)fromRbGroupObj;
                 foreach (PdfObject fromRbGroupItemObj in fromRbGroup) {
                     if (fromOcgsToCopy.Contains(fromRbGroupItemObj.GetIndirectReference())) {
                         toArray.Add(fromRbGroup.CopyTo(toDocument, false));
                         break;
                     }
                 }
             }
         }
         else {
             // Stage 3: remove from RBGroups OCGs not presented in the output
             // document (is in the PdfOcProperties#fillDictionary method)
             foreach (PdfObject fromObj in fromArray) {
                 iText.Kernel.Pdf.OcgPropertiesCopier.AttemptToAddObjectToArray(fromOcgsToCopy, fromObj, toArray, toDocument
                     );
             }
         }
     }
     if (toArray.IsEmpty()) {
         toDict.Remove(fieldToCopy);
     }
 }