예제 #1
0
 internal void PopulateRelatedObjects(Hashtable container)
 {
     if (!container.ContainsKey(this.ObjID))
     {
         container.Add(this.ObjID, this);
         Match m = regex1.Match(this.OriginalText);
         while (m.Success)
         {
             int  num       = int.Parse(m.Groups["id"].Value);
             bool notparent = m.Groups["parent"].Length == 0;
             if (notparent)
             {
                 if (!container.Contains(num))
                 {
                     PdfFileObject pfo = this.pdfFileParser.ReadObject(num);
                     if (pfo != null)
                     {
                         pfo.PopulateRelatedObjects(container);
                     }
                 }
             }
             m = m.NextMatch();
         }
     }
 }
예제 #2
0
        public void Add(PdfParser pdfParser, int[] PageNumbers)
        {
            if (this.CancelPending)
            {
                return;
            }

            this.PdfMergerProgress(this, new PdfMergerProgressEventArgs(this.elementCount,
                                                                        0, 0, "Analyzing PDF Structure"));


            PdfFileObject[] pages = pdfParser.GetAllPages();

            ArrayList selectedPages = new ArrayList();

            #region gets needed objects

            Hashtable relatedObjects = new Hashtable();

            if (PageNumbers == null)
            {
                List <int> ps = new List <int>();
                for (int p = 0; p < pages.Length; p++)
                {
                    ps.Add(p);
                }
                PageNumbers = ps.ToArray();
            }


            int currentPageIndex = 1, pageCount = PageNumbers.Length;

            int step = Math.Max(pageCount / 20, 10);

            foreach (int pageNumber in PageNumbers)
            {
                if (this.CancelPending)
                {
                    return;
                }

                if (currentPageIndex % step == 0 || currentPageIndex == pageCount)
                {
                    this.PdfMergerProgress(this, new PdfMergerProgressEventArgs(this.elementCount,
                                                                                currentPageIndex, pageCount, "Analyzing Page {0} of {1}"));
                }
                PdfFileObject selectedPage = pages[pageNumber];
                selectedPages.Add(selectedPage);
                selectedPage.PopulateRelatedObjects(relatedObjects);
                currentPageIndex++;
            }

            ArrayList neededObjects = new ArrayList();
            neededObjects.AddRange(relatedObjects.Values);
            neededObjects.Sort(new PdfFileObjectNumberComparer());

            #endregion

            #region creates IDs transformation table

            int objectIndex = 1;
            int objectCount = neededObjects.Count;

            step = Math.Max(objectCount / 20, 50);

            Hashtable transformationTable = new Hashtable();
            foreach (PdfFileObject pfo in neededObjects)
            {
                if (this.CancelPending)
                {
                    return;
                }

                if (objectIndex % step == 0 || objectIndex == objectCount)
                {
                    this.PdfMergerProgress(this, new PdfMergerProgressEventArgs(this.elementCount,
                                                                                objectIndex, objectCount, "Rebuilding Indexes for object {0} of {1}"));
                }

                string hash = OptimizeStreams?pfo.Hash:null;
                if (hash != null && this.alreadyUsedObjects.ContainsKey(hash))
                {
                    pfo.excludedByHashComparison = true;
                    transformationTable.Add(pfo.ObjID, (int)this.alreadyUsedObjects[hash]);
                }
                else
                {
                    number++;
                    transformationTable.Add(pfo.ObjID, number);
                    if (hash != null)
                    {
                        this.alreadyUsedObjects.Add(hash, number);
                    }
                }
                objectIndex++;
            }

            #endregion

            objectIndex = 1;

            foreach (PdfFileObject pfo in neededObjects)
            {
                if (this.CancelPending)
                {
                    return;
                }

                if (objectIndex % step == 0 || objectIndex == objectCount)
                {
                    this.PdfMergerProgress(this, new PdfMergerProgressEventArgs(this.elementCount,
                                                                                objectIndex, objectCount, "Writing object {0} of {1}"));
                }

                pfo.Transform(transformationTable);
                if (!pfo.excludedByHashComparison)
                {
                    this.xrefs.Add(pos);
                    this.pos += pfo.WriteToStream(this.target);
                }
                pfo.TransformatedTextPart = null;
                objectIndex++;
            }

            foreach (PdfFileObject selectedPage in selectedPages)
            {
                this.pageNumbers.Add(selectedPage.TransformatedObjID);
            }


            this.elementCount++;
        }