Beispiel #1
0
        protected int ParseToc()
        {
            _outline = new List <BookItem>();
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(_src_file));

            PdfOutline pdfOutline = pdfDoc.GetOutlines(false);

            PdfNameTree destsTree = pdfDoc.GetCatalog().GetNameTree(PdfName.Dests);

            GetBookmark(pdfOutline, destsTree.GetNames(), pdfDoc);

            pdfDoc.Close();
            return(0);
        }
Beispiel #2
0
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
            Dictionary <String, PdfString> renamed = new Dictionary <String, PdfString>();

            PdfNameTree nameTree = pdfDoc.GetCatalog().GetNameTree(PdfName.Dests);
            IDictionary <String, PdfObject> names = nameTree.GetNames();
            List <String> keys = new List <string>(names.Keys);

            // Loop over all named destinations and rename its string values with new names
            foreach (String key in keys)
            {
                String newName = "new" + key;

                names.Add(newName, names[key]);
                names.Remove(key);
                renamed.Add(key, new PdfString(newName));
            }

            // Specify that the name tree has been modified
            // This implies that the name tree will be rewritten on close() method.
            nameTree.SetModified();

            PdfDictionary page        = pdfDoc.GetPage(1).GetPdfObject();
            PdfArray      annotations = page.GetAsArray(PdfName.Annots);

            // Loop over all link annotations of the first page and change their destinations.
            for (int i = 0; i < annotations.Size(); i++)
            {
                PdfDictionary annotation = annotations.GetAsDictionary(i);
                PdfDictionary action     = annotation.GetAsDictionary(PdfName.A);
                if (action == null)
                {
                    continue;
                }

                PdfString n = action.GetAsString(PdfName.D);
                if (n != null && renamed.ContainsKey(n.ToString()))
                {
                    action.Put(PdfName.D, renamed[n.ToString()]);
                }
            }

            pdfDoc.Close();
        }
Beispiel #3
0
        public static List <Bookmark> GetBookmarks(string sourceFile)
        {
            PdfDocument doc = new PdfDocument(new PdfReader(sourceFile));

            PdfNameTree     destsTree = doc.GetCatalog().GetNameTree(PdfName.Dests);
            PdfOutline      outlines  = doc.GetOutlines(false);
            List <Bookmark> bookmarks = new List <Bookmark>();

            if (outlines != null)
            {
                ListBookmarks(outlines, destsTree.GetNames(), doc, bookmarks, 0, Guid.Empty);
            }

            for (int i = 0; i < bookmarks.Count; i++)
            {
                Bookmark mark    = bookmarks[i];
                Bookmark leveled = bookmarks.FirstOrDefault(x
                                                            => x.Level == mark.Level &&
                                                            bookmarks.IndexOf(x) > i &&
                                                            x.ParentId == mark.ParentId);

                if (leveled != null)
                {
                    mark.EndPage = leveled.StartPage - 1;
                }
                else
                {
                    Bookmark levelUp = bookmarks.FirstOrDefault(x => x.Level <mark.Level && bookmarks.IndexOf(x)> i);
                    if (levelUp != null)
                    {
                        mark.EndPage = levelUp.StartPage - 1;
                    }
                    else
                    {
                        mark.EndPage = doc.GetPageNumber(doc.GetLastPage());
                    }
                }
            }

            doc.Close();

            return(bookmarks);
        }
Beispiel #4
0
        /// <summary>Gets the annotation specified by /A and /P entry values.</summary>
        /// <param name="pdfDocument">specifies the corresponding document</param>
        /// <returns>the annotation specified by /A and /P entry value.</returns>
        public virtual PdfFileAttachmentAnnotation GetAnnotation(PdfDocument pdfDocument)
        {
            PdfObject pValue = GetPdfObject().Get(PdfName.P);
            PdfPage   page   = null;

            if (pValue is PdfNumber)
            {
                // zero-based index is used
                page = pdfDocument.GetPage(((PdfNumber)pValue).IntValue() + 1);
            }
            else
            {
                if (pValue is PdfString)
                {
                    PdfNameTree destsTree = pdfDocument.GetCatalog().GetNameTree(PdfName.Dests);
                    IDictionary <String, PdfObject> dests = destsTree.GetNames();
                    PdfArray pdfArray = (PdfArray)dests.Get(((PdfString)pValue).GetValue());
                    if (null != pdfArray)
                    {
                        if (pdfArray.Get(0) is PdfNumber)
                        {
                            page = pdfDocument.GetPage(((PdfNumber)pdfArray.Get(0)).IntValue());
                        }
                        else
                        {
                            page = pdfDocument.GetPage((PdfDictionary)pdfArray.Get(0));
                        }
                    }
                }
            }
            IList <PdfAnnotation> pageAnnotations = null;

            if (null != page)
            {
                pageAnnotations = page.GetAnnotations();
            }
            PdfObject aValue = GetPdfObject().Get(PdfName.A);
            PdfFileAttachmentAnnotation resultAnnotation = null;

            if (null != pageAnnotations)
            {
                if (aValue is PdfNumber)
                {
                    resultAnnotation = (PdfFileAttachmentAnnotation)pageAnnotations[((PdfNumber)aValue).IntValue()];
                }
                else
                {
                    if (aValue is PdfString)
                    {
                        foreach (PdfAnnotation annotation in pageAnnotations)
                        {
                            if (aValue.Equals(annotation.GetName()))
                            {
                                resultAnnotation = (PdfFileAttachmentAnnotation)annotation;
                                break;
                            }
                        }
                    }
                }
            }
            if (null == resultAnnotation)
            {
                ILog logger = LogManager.GetLogger(typeof(iText.Kernel.Pdf.Action.PdfTarget));
                logger.Error(iText.IO.LogMessageConstant.SOME_TARGET_FIELDS_ARE_NOT_SET_OR_INCORRECT);
            }
            return(resultAnnotation);
        }