예제 #1
0
        private static void TestSetModified(bool isAppendMode)
        {
            String[]     expectedKeys = new String[] { "new_key1", "new_key2", "new_key3" };
            MemoryStream sourceFile   = CreateDocumentInMemory();
            MemoryStream modifiedFile = new MemoryStream();
            PdfReader    reader       = new PdfReader(new MemoryStream(sourceFile.ToArray()));
            PdfDocument  pdfDoc       = isAppendMode ? new PdfDocument(reader, new PdfWriter(modifiedFile), new StampingProperties
                                                                           ().UseAppendMode()) : new PdfDocument(reader, new PdfWriter(modifiedFile));
            PdfNameTree nameTree = pdfDoc.GetCatalog().GetNameTree(PdfName.Dests);
            IDictionary <String, PdfObject> names = nameTree.GetNames();
            List <String> keys = new List <String>(names.Keys);

            for (int i = 0; i < keys.Count; i++)
            {
                names.Put(expectedKeys[i], names.Get(keys[i]));
                names.JRemove(keys[i]);
            }
            nameTree.SetModified();
            pdfDoc.Close();
            reader   = new PdfReader(new MemoryStream(modifiedFile.ToArray()));
            pdfDoc   = new PdfDocument(reader);
            nameTree = pdfDoc.GetCatalog().GetNameTree(PdfName.Dests);
            ICollection <String> actualKeys = nameTree.GetNames().Keys;

            NUnit.Framework.Assert.AreEqual(expectedKeys, actualKeys.ToArray());
        }
예제 #2
0
        public virtual void AnnotationAppearanceTest()
        {
            PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "AnnotationAppearanceTest.pdf"
                                                                    ));
            PdfPage   page   = pdfDocument.AddNewPage();
            PdfCanvas canvas = new PdfCanvas(page);

            canvas.SetFillColor(ColorConstants.MAGENTA).BeginText().SetFontAndSize(PdfFontFactory.CreateFont(StandardFonts
                                                                                                             .TIMES_ROMAN), 30).SetTextMatrix(25, 500).ShowText("This file has AP key in Names dictionary").EndText
                ();
            PdfArray array = new PdfArray();

            array.Add(new PdfString("normalAppearance"));
            array.Add(new PdfAnnotationAppearance().SetState(PdfName.N, new PdfFormXObject(new Rectangle(50, 50, 50, 50
                                                                                                         ))).GetPdfObject());
            PdfDictionary dict = new PdfDictionary();

            dict.Put(PdfName.Names, array);
            PdfDictionary dictionary = new PdfDictionary();

            dictionary.Put(PdfName.AP, dict);
            pdfDocument.GetCatalog().GetPdfObject().Put(PdfName.Names, dictionary);
            PdfNameTree appearance = pdfDocument.GetCatalog().GetNameTree(PdfName.AP);
            IDictionary <String, PdfObject> objs = appearance.GetNames();

            pdfDocument.Close();
            NUnit.Framework.Assert.AreEqual(1, objs.Count);
        }
예제 #3
0
        /// <summary>This method returns a complete outline tree of the whole document.</summary>
        /// <param name="updateOutlines">
        /// if the flag is true, the method read the whole document and creates outline tree.
        /// If false the method gets cached outline tree (if it was cached via calling getOutlines method before).
        /// </param>
        /// <returns>
        /// fully initialized
        /// <see cref="PdfOutline"/>
        /// object.
        /// </returns>
        internal virtual PdfOutline GetOutlines(bool updateOutlines)
        {
            if (outlines != null && !updateOutlines)
            {
                return(outlines);
            }
            if (outlines != null)
            {
                outlines.Clear();
                pagesWithOutlines.Clear();
            }
            outlineMode = true;
            PdfNameTree   destsTree   = GetNameTree(PdfName.Dests);
            PdfDictionary outlineRoot = GetPdfObject().GetAsDictionary(PdfName.Outlines);

            if (outlineRoot == null)
            {
                if (null == GetDocument().GetWriter())
                {
                    return(null);
                }
                outlines = new PdfOutline(GetDocument());
            }
            else
            {
                ConstructOutlines(outlineRoot, destsTree.GetNames());
            }
            return(outlines);
        }
예제 #4
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);
        }
예제 #5
0
        public virtual void EmbeddedFileAndJavascriptTest()
        {
            PdfDocument pdfDocument              = new PdfDocument(new PdfReader(sourceFolder + "FileWithSingleAttachment.pdf"));
            PdfNameTree embeddedFilesNameTree    = pdfDocument.GetCatalog().GetNameTree(PdfName.EmbeddedFiles);
            IDictionary <String, PdfObject> objs = embeddedFilesNameTree.GetNames();
            PdfNameTree javascript = pdfDocument.GetCatalog().GetNameTree(PdfName.JavaScript);
            IDictionary <String, PdfObject> objs2 = javascript.GetNames();

            pdfDocument.Close();
            NUnit.Framework.Assert.AreEqual(1, objs.Count);
            NUnit.Framework.Assert.AreEqual(1, objs2.Count);
        }
예제 #6
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);
        }
예제 #7
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)
                    {
                        PdfArray array = new PdfArray((PdfArray)dest);
                        array.Set(0, page2page.Get(oldPage).GetPdfObject());
                        d = new PdfExplicitDestination(array);
                    }
                }
            }
            else
            {
                if (dest.IsString())
                {
                    PdfNameTree destsTree = GetNameTree(PdfName.Dests);
                    IDictionary <String, PdfObject> dests = destsTree.GetNames();
                    String   name  = ((PdfString)dest).ToUnicodeString();
                    PdfArray array = (PdfArray)dests.Get(name);
                    if (array != null)
                    {
                        PdfObject pageObject = array.Get(0);
                        foreach (PdfPage oldPage in page2page.Keys)
                        {
                            if (oldPage.GetPdfObject() == pageObject)
                            {
                                array.Set(0, page2page.Get(oldPage).GetPdfObject());
                                d = new PdfStringDestination(name);
                                toDocument.AddNamedDestination(name, array);
                            }
                        }
                    }
                }
            }
            return(d);
        }
예제 #8
0
        public virtual void EmbeddedFileAddedInAppendModeTest()
        {
            //Create input document
            MemoryStream boasEmpty      = new MemoryStream();
            PdfWriter    emptyDocWriter = new PdfWriter(boasEmpty);
            PdfDocument  emptyDoc       = new PdfDocument(emptyDocWriter);

            emptyDoc.AddNewPage();
            PdfDictionary emptyNamesDic = new PdfDictionary();

            emptyNamesDic.MakeIndirect(emptyDoc);
            emptyDoc.GetCatalog().GetPdfObject().Put(PdfName.Names, emptyNamesDic);
            emptyDoc.Close();
            //Create input document
            MemoryStream boasAttached    = new MemoryStream();
            PdfWriter    attachDocWriter = new PdfWriter(boasAttached);
            PdfDocument  attachDoc       = new PdfDocument(attachDocWriter);

            attachDoc.AddNewPage();
            attachDoc.Close();
            //Attach file in append mode
            PdfReader    appendReader = new PdfReader(new MemoryStream(boasEmpty.ToArray()));
            MemoryStream boasAppend   = new MemoryStream();
            PdfWriter    appendWriter = new PdfWriter(boasAppend);
            PdfDocument  appendDoc    = new PdfDocument(appendReader, appendWriter, new StampingProperties().UseAppendMode
                                                            ());

            appendDoc.AddFileAttachment("Test File", PdfFileSpec.CreateEmbeddedFileSpec(appendDoc, boasAttached.ToArray
                                                                                            (), "Append Embedded File test", "Test file", null));
            appendDoc.Close();
            //Check final result
            PdfReader   finalReader           = new PdfReader(new MemoryStream(boasAppend.ToArray()));
            PdfDocument finalDoc              = new PdfDocument(finalReader);
            PdfNameTree embeddedFilesNameTree = finalDoc.GetCatalog().GetNameTree(PdfName.EmbeddedFiles);
            IDictionary <String, PdfObject> embeddedFilesMap = embeddedFilesNameTree.GetNames();

            NUnit.Framework.Assert.IsTrue(embeddedFilesMap.Count > 0);
            NUnit.Framework.Assert.IsTrue(embeddedFilesMap.ContainsKey("Test File"));
        }