ConsolidateNamedDestinations() public method

public ConsolidateNamedDestinations ( ) : void
return void
示例#1
0
        public void testGetLink2()
        {
            string testFile = TestResourceUtils.GetResourceAsTempFile(TEST_RESOURCES_PATH, "getLinkTest2.pdf");
            string filename = testFile;
            PdfReader rdr = new PdfReader(new RandomAccessFileOrArray(filename), new byte[0]);
            // this one works: PdfReader rdr = new PdfReader(filename);
            rdr.ConsolidateNamedDestinations(); // does not help
            rdr.GetLinks(1);

            rdr.Close();
        }
        void ProcessPdf(string path)
        {
            using (var reader = new PdfReader(path))
            {
                // need this call to parse page numbers
                reader.ConsolidateNamedDestinations();

                var bookmarks = ParseBookMarks(SimpleBookmark.GetBookmark(reader));
                for (int i = 0; i < bookmarks.Count; ++i)
                {
                    int page = bookmarks[i].PageNumberInteger;
                    int nextPage = i + 1 < bookmarks.Count
                        // if not top of page will be missing content
                        ? bookmarks[i + 1].PageNumberInteger - 1 

                        /* alternative is to potentially add redundant content:
                        ? bookmarks[i + 1].PageNumberInteger
                        */

                        : reader.NumberOfPages;
                    string range = string.Format("{0}-{1}", page, nextPage);

                    // DEMO!
                    if (i < 1000)
                    {
                        var outputPath = Path.Combine(OUTPUT_DIR, bookmarks[i].GetFileName());
                        using (var readerCopy = new PdfReader(reader))
                        {
                            var number = bookmarks[i].Number;
                            readerCopy.SelectPages(range);
                            using (FileStream stream = new FileStream(outputPath, FileMode.Create))
                            {
                                using (var document = new Document())
                                {
                                    using (var copy = new PdfCopy(document, stream))
                                    {
                                        document.Open();
                                        int n = readerCopy.NumberOfPages;
                                        for (int j = 0; j < n; )
                                        {
                                            copy.AddPage(copy.GetImportedPage(readerCopy, ++j));
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        void DumpResults(string path)
        {
            using (var reader = new PdfReader(path))
            {
                // need this call to parse page numbers
                reader.ConsolidateNamedDestinations();

                var bookmarks = ParseBookMarks(SimpleBookmark.GetBookmark(reader));
                var sb = new StringBuilder();
                foreach (var bookmark in bookmarks)
                {
                    sb.AppendLine(string.Format(
                        "{0, -4}{1, -100}{2, -25}{3}",
                        bookmark.Number, bookmark.Title,
                        bookmark.PageNumberString, bookmark.PageNumberInteger
                    ));
                }
                File.WriteAllText(outputTextFile, sb.ToString());
            }
        }
示例#4
0
        public void CombineMultiplePDFs(string[] fileNames, string outFile)
        {
            // step 1: creation of a document-object
            Document document = new Document();

            // step 2: we create a writer that listens to the document
            PdfCopy writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
            if (writer == null)
            {
                return;
            }

            // step 3: we open the document
            document.Open();

            foreach (string fileName in fileNames)
            {
                // we create a reader for a certain document
                PdfReader reader = new PdfReader(fileName);
                reader.ConsolidateNamedDestinations();

                // step 4: we add content
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    PdfImportedPage page = writer.GetImportedPage(reader, i);
                    writer.AddPage(page);
                }

                PRAcroForm form = reader.AcroForm;
                if (form != null)
                {
                    writer.CopyAcroForm(reader);
                }

                reader.Close();
            }

            // step 5: we close the document and writer
            writer.Close();
            document.Close();
        }
示例#5
0
 public void AddDocument(PdfReader reader) {
     if (indirectMap.ContainsKey(reader)) {
         throw new ArgumentException(MessageLocalization.GetComposedMessage("document.1.has.already.been.added", reader.ToString()));
     }
     if (!reader.IsOpenedWithFullPermissions)
         throw new BadPasswordException(MessageLocalization.GetComposedMessage("pdfreader.not.opened.with.owner.password"));
     if (mergeFields) {
         reader.ConsolidateNamedDestinations();
         reader.ShuffleSubsetNames();
         for (int i = 1; i <= reader.NumberOfPages; i++) {
             PdfDictionary page = reader.GetPageNRelease(i);
             if (page != null && page.Contains(PdfName.ANNOTS)) {
                 PdfArray annots = page.GetAsArray(PdfName.ANNOTS);
                 if (annots != null) {
                     for (int j = 0; j < annots.Size; j++) {
                         PdfDictionary annot = annots.GetAsDict(j);
                         if (annot != null)
                             annot.Put(annotId, new PdfNumber(++annotIdCnt));
                     }
                 }
             }
         }
         fields.Add(reader.AcroFields);
         UpdateCalculationOrder(reader);
     }
     bool tagged = PdfStructTreeController.CheckTagged(reader);
     mergeFieldsInternalCall = true;
     for (int i = 1; i <= reader.NumberOfPages; i++) {
         AddPage(GetImportedPage(reader, i, tagged && this.tagged));
     }
     mergeFieldsInternalCall = false;
 }
		public static void CombineMultiplePDFs(string[] fileNames, string outFile)
		{
			try
			{
				iTextSharp.text.Document document = new iTextSharp.text.Document();
				PdfCopy writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
				if (writer == null)
				{
					return;
				}
				document.Open();

				foreach (string fileName in fileNames)
				{
					if (System.IO.File.Exists(fileName))
					{
						PdfReader reader = new PdfReader(fileName);
						reader.ConsolidateNamedDestinations();
						for (int i = 1; i <= reader.NumberOfPages; i++)
						{
							PdfImportedPage page = writer.GetImportedPage(reader, i);
							writer.AddPage(page);
						}

						PRAcroForm form = reader.AcroForm;
						if (form != null)
						{
							writer.CopyDocumentFields(reader);
						}
						reader.Close();
					}
				}
				writer.Close();
				document.Close();

			}
			catch
			{
				MessageBox.Show("Close the pdf file and try again.");
			}

		}
        private void addFilePages(Stream fileStream)
        {
            PdfReader reader = null;
            try
            {
                _fileNumber++;
                _attachmentsCount += copyAttachments(fileStream);
                fileStream = fileStream.ReopenForReading();
                reader = new PdfReader(fileStream);
                reader.ConsolidateNamedDestinations();

                addBookmark(reader);

                int numberOfPages = reader.NumberOfPages;
                for (int pageNumber = 1; pageNumber <= numberOfPages; pageNumber++)
                {
                    var size = reader.GetPageSizeWithRotation(pageNumber);
                    _document.SetPageSize(size);
                    _document.NewPage();
                    _overallPageNumber++;

                    var page = _writer.GetImportedPage(reader, pageNumber);
                    addContentToPage(reader, size, page);
                    _writer.AddPage(page);
                }

                var form = reader.AcroForm;
                if (form != null)
                    _writer.CopyAcroForm(reader);
            }
            finally
            {
                if (reader != null)
                    _writer.FreeReader(reader);
            }
        }
示例#8
0
        public byte[] PdfMerger(List<byte[]> files)
        {
            int pageOffset = 0;
            int f = 0;

            Document document = null;
            PdfCopy writer = null;
            var ms = new MemoryStream();

            foreach (var file in files)
            {
                var reader = new PdfReader(file);
                reader.ConsolidateNamedDestinations();
                // we retrieve the total number of pages
                int n = reader.NumberOfPages;

                pageOffset += n;

                if (f == 0)
                {
                    // step 1: creation of a document-object
                    document = new Document(reader.GetPageSizeWithRotation(1));
                    // step 2: we create a writer that listens to the document
                    writer = new PdfCopy(document, ms);
                    // step 3: we open the document
                    document.Open();
                }
                // step 4: we add content
                for (int i = 0; i < n; )
                {
                    ++i;
                    if (writer != null)
                    {
                        PdfImportedPage page = writer.GetImportedPage(reader, i);
                        writer.AddPage(page);
                    }
                }
                PRAcroForm form = reader.AcroForm;
                if (form != null && writer != null)
                {
                    writer.CopyAcroForm(reader);
                }

                f++;
            }

            // step 5: we close the document
            if (document != null)
            {
                document.Close();
            }

            return ms.ToArray();
        }
示例#9
0
        public static byte[] Merge(string[] documentPaths) {
            byte[] mergedDocument;

            using (MemoryStream memoryStream = new MemoryStream())
            using (Document document = new Document())
            {
                PdfSmartCopy pdfSmartCopy = new PdfSmartCopy(document, memoryStream);
                document.Open();

                foreach (string docPath in documentPaths)
                {
                    PdfReader reader = new PdfReader(docPath);
                    try
                    {
                        reader.ConsolidateNamedDestinations();
                        int numberOfPages = reader.NumberOfPages;
                        for (int page = 0; page < numberOfPages; )
                        {
                            PdfImportedPage pdfImportedPage = pdfSmartCopy.GetImportedPage(reader, ++page);
                            pdfSmartCopy.AddPage(pdfImportedPage);
                        }
                    }
                    finally
                    {
                        reader.Close();
                    }
                }

                document.Close();
                mergedDocument = memoryStream.ToArray();
            }

            return mergedDocument;
        }
示例#10
0
 virtual public void AddDocument(PdfReader reader) {
     if (!document.IsOpen()) {
         throw new DocumentException(MessageLocalization.GetComposedMessage("the.document.is.not.open.yet.you.can.only.add.meta.information"));
     }
     if (indirectMap.ContainsKey(reader)) {
         throw new ArgumentException(MessageLocalization.GetComposedMessage("document.1.has.already.been.added", reader.ToString()));
     }
     if (!reader.IsOpenedWithFullPermissions)
         throw new BadPasswordException(MessageLocalization.GetComposedMessage("pdfreader.not.opened.with.owner.password"));
     if (mergeFields) {
         reader.ConsolidateNamedDestinations();
         reader.ShuffleSubsetNames();
         for (int i = 1; i <= reader.NumberOfPages; i++) {
             PdfDictionary page = reader.GetPageNRelease(i);
             if (page != null && page.Contains(PdfName.ANNOTS)) {
                 PdfArray annots = page.GetAsArray(PdfName.ANNOTS);
                 if (annots != null) {
                     for (int j = 0; j < annots.Size; j++) {
                         PdfDictionary annot = annots.GetAsDict(j);
                         if (annot != null)
                             annot.Put(annotId, new PdfNumber(++annotIdCnt));
                     }
                 }
             }
         }
         AcroFields acro = reader.AcroFields;
         // when a document with NeedAppearances is encountered, the flag is set
         // in the resulting document.
         bool needapp = !acro.GenerateAppearances;
         if (needapp)
             needAppearances = true;
         fields.Add(reader.AcroFields);
         UpdateCalculationOrder(reader);
     }
     bool tagged = this.tagged && PdfStructTreeController.CheckTagged(reader);
     mergeFieldsInternalCall = true;
     for (int i = 1; i <= reader.NumberOfPages; i++) {
         AddPage(GetImportedPage(reader, i, tagged));
     }
     mergeFieldsInternalCall = false;
 }
示例#11
0
        /**
         * Copy document fields to a destination document.
         * @param reader a document where fields are copied from.
         * @throws DocumentException
         * @throws IOException
         */
        public virtual void CopyDocumentFields(PdfReader reader) {
            if (!document.IsOpen()) {
                throw new DocumentException(
                    MessageLocalization.GetComposedMessage("the.document.is.not.open.yet.you.can.only.add.meta.information"));
            }

            if (indirectMap.ContainsKey(reader)) {
                throw new ArgumentException(MessageLocalization.GetComposedMessage("document.1.has.already.been.added",
                    reader.ToString()));
            }

            if (!reader.IsOpenedWithFullPermissions)
                throw new BadPasswordException(MessageLocalization.GetComposedMessage("pdfreader.not.opened.with.owner.password"));

            if (!mergeFields)
                throw new ArgumentException(
                    MessageLocalization.GetComposedMessage(
                        "1.method.can.be.only.used.in.mergeFields.mode.please.use.addDocument", "copyDocumentFields"));

            indirects = new Dictionary<RefKey, IndirectReferences>();
            indirectMap[reader] = indirects;

            reader.ConsolidateNamedDestinations();
            reader.ShuffleSubsetNames();
            if (tagged && PdfStructTreeController.CheckTagged(reader)) {
                structTreeRootReference = (PRIndirectReference) reader.Catalog.Get(PdfName.STRUCTTREEROOT);
                if (structTreeController != null) {
                    if (reader != structTreeController.reader)
                        structTreeController.SetReader(reader);
                } else {
                    structTreeController = new PdfStructTreeController(reader, this);
                }
            }

            IList<PdfObject> annotationsToBeCopied = new List<PdfObject>();

            for (int i = 1; i <= reader.NumberOfPages; i++) {
                PdfDictionary page = reader.GetPageNRelease(i);
                if (page != null && page.Contains(PdfName.ANNOTS)) {
                    PdfArray annots = page.GetAsArray(PdfName.ANNOTS);
                    if (annots != null && annots.Size > 0) {
                        if (importedPages.Count < i)
                            throw new DocumentException(
                                MessageLocalization.GetComposedMessage("there.are.not.enough.imported.pages.for.copied.fields"));
                        indirectMap[reader][new RefKey(reader.pageRefs.GetPageOrigRef(i))] = new IndirectReferences(pageReferences[i - 1]);
                        for (int j = 0; j < annots.Size; j++) {
                            PdfDictionary annot = annots.GetAsDict(j);
                            if (annot != null) {
                                annot.Put(annotId, new PdfNumber(++annotIdCnt));
                                annotationsToBeCopied.Add(annots[j]);
                            }
                        }
                    }
                }
            }

            foreach (PdfObject annot in annotationsToBeCopied) {
                CopyObject(annot);
            }

            if (tagged && structTreeController != null)
                structTreeController.AttachStructTreeRootKids(null);

            AcroFields acro = reader.AcroFields;
            bool needapp = !acro.GenerateAppearances;
            if (needapp)
                needAppearances = true;
            fields.Add(acro);
            UpdateCalculationOrder(reader);
            structTreeRootReference = null;
        }
        // http://stackoverflow.com/questions/566899/is-there-a-straight-forward-way-to-append-one-pdf-doc-to-another-using-itextsharp
        public static bool CombineMultiplePDFs(string outFile, string[] fileNames)
        {
            int pageOffset = 0;
            int f = 0;

            Document document = null;
            PdfCopy writer = null;
            IList<Dictionary<string, object>> master = new List<Dictionary<string, object>>();
            while (f < fileNames.Length)
            {
                // we create a reader for a certain document
                PdfReader reader = new PdfReader(fileNames[f]);
                reader.ConsolidateNamedDestinations();
                // we retrieve the total number of pages
                int n = reader.NumberOfPages;
                IList<Dictionary<string, object>> bookmarks = SimpleBookmark.GetBookmark(reader);

                if (bookmarks != null)
                {
                    if (pageOffset != 0)
                    {
                        SimpleBookmark.ShiftPageNumbers(bookmarks, pageOffset, null);
                    }
                    foreach (Dictionary<string, object> dictionary in bookmarks)
                    {
                        master.Add(dictionary);
                    }
                }
                pageOffset += n;

                if (f == 0)
                {
                    // step 1: creation of a document-object
                    document = new Document(reader.GetPageSizeWithRotation(1));
                    // step 2: we create a writer that listens to the document
                    writer = new PdfCopy(document, new FileStream(outFile, FileMode.Create));
                    writer.ViewerPreferences = PdfWriter.PageModeUseOutlines;
                    // step 3: we open the document
                    document.Open();
                }
                // step 4: we add content
                for (int i = 0; i < n; )
                {
                    ++i;
                    if (writer != null)
                    {
                        PdfImportedPage page = writer.GetImportedPage(reader, i);
                        writer.AddPage(page);
                    }
                }
                PRAcroForm form = reader.AcroForm;
                if (form != null && writer != null)
                {
                    writer.CopyAcroForm(reader);
                }
                f++;
            }
            if (master.Count > 0 && writer != null)
            {
                writer.Outlines = master;
            }
            // step 5: we close the document
            if (document != null)
            {
                document.Close();
            }
            return true;
        }
示例#13
0
        private void SaveAsPDFA(ref string tempPDF, string pdfPath, MetaData md)
        {
            string tempdir = Path.Combine(Path.GetTempPath(), "GhostScriptWrapper");

            if (!Directory.Exists(tempdir))
            {
                Directory.CreateDirectory(tempdir);
            }

            tempPDF = Path.Combine(tempdir, string.Format("{0}_tmp.pdf", Guid.NewGuid()));

            File.Copy(pdfPath, tempPDF);

            GhostScriptWrapper.CallAPI(GetArgs(tempPDF, pdfPath));

            var document = new it.Document();

            using (var fs = new FileStream(tempPDF, FileMode.Create))
            {
                // step 2: we create a writer that listens to the document
                //PdfCopy writer = new PdfCopy(document, fs);
                var pdfaWriter = ip.PdfAWriter.GetInstance(document, fs, ip.PdfAConformanceLevel.PDF_A_1B);

                pdfaWriter.SetTagged();
                pdfaWriter.CreateXmpMetadata();
                // step 3: we open the document
                document.Open();

                document.AddAuthor(md.Author);
                document.AddCreator(md.Creator);
                document.AddLanguage(md.Language);
                document.AddProducer();
                document.AddTitle(Path.GetFileNameWithoutExtension(pdfPath));

                // we create a reader for a certain document
                var reader = new ip.PdfReader(pdfPath);
                reader.ConsolidateNamedDestinations();

                document.NewPage();

                var icc = ip.ICC_Profile.GetInstance(Environment.GetEnvironmentVariable("SystemRoot") + @"\System32\spool\drivers\color\sRGB Color Space Profile.icm");
                pdfaWriter.SetOutputIntents("sRGB", null, "http://www.color.org", "sRGB IEC61966-2.1", icc.Data);

                // step 4: we add content
                for (var i = 1; i <= reader.NumberOfPages; i++)
                {
                    var page = pdfaWriter.GetImportedPage(reader, i);
                    pdfaWriter.DirectContentUnder.AddTemplate(page, 0, 0);

                    document.NewPage();
                }

                // step 5: we close the document and writer

                document.AddCreationDate();
                pdfaWriter.Flush();

                try
                {
                    pdfaWriter.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                reader.Close();
                try
                {
                    document.Close();
                }
                catch
                {
                }
            }

            ManipulatePdf(tempPDF, pdfPath, md);
        }