public bool FindPage(DictionaryToken currentPageDictionary, int soughtPageNumber, List <int> pageNumbersObserved, PageTreeMembers pageTreeMembers) { var type = currentPageDictionary.GetNameOrDefault(NameToken.Type); if (type?.Equals(NameToken.Page) == true) { var pageNumber = GetNextPageNumber(pageNumbersObserved); bool found = pageNumber == soughtPageNumber; locatedPages[pageNumber] = currentPageDictionary; pageNumbersObserved.Add(pageNumber); return(found); } if (type?.Equals(NameToken.Pages) != true) { log.Warn("Did not find the expected type (Page or Pages) in dictionary: " + currentPageDictionary); return(false); } if (currentPageDictionary.TryGet(NameToken.MediaBox, out var token)) { var mediaBox = DirectObjectFinder.Get <ArrayToken>(token, pdfScanner); pageTreeMembers.MediaBox = new MediaBox(new PdfRectangle(mediaBox.GetNumeric(0).Data, mediaBox.GetNumeric(1).Data, mediaBox.GetNumeric(2).Data, mediaBox.GetNumeric(3).Data)); } if (!currentPageDictionary.TryGet(NameToken.Kids, out var kids) || !(kids is ArrayToken kidsArray)) { return(false); } pageFactory.LoadResources(currentPageDictionary, isLenientParsing); bool childFound = false; foreach (var kid in kidsArray.Data) { // todo: exit early var child = DirectObjectFinder.Get <DictionaryToken>(kid, pdfScanner); var thisPageMatches = FindPage(child, soughtPageNumber, pageNumbersObserved, pageTreeMembers); if (thisPageMatches) { childFound = true; break; } } return(childFound); }
public bool FindPage(PdfDictionary currentPageDictionary, int soughtPageNumber, List <int> pageNumbersObserved) { var type = currentPageDictionary.GetName(CosName.TYPE); if (type.Equals(CosName.PAGE)) { var pageNumber = GetNextPageNumber(pageNumbersObserved); bool found = pageNumber == soughtPageNumber; locatedPages[pageNumber] = currentPageDictionary; pageNumbersObserved.Add(pageNumber); return(found); } if (!type.Equals(CosName.PAGES)) { log.Warn("Did not find the expected type (Page or Pages) in dictionary: " + currentPageDictionary); return(false); } var kids = currentPageDictionary.GetDictionaryObject(CosName.KIDS) as COSArray; pageFactory.LoadResources(currentPageDictionary, reader, isLenientParsing); bool childFound = false; foreach (var kid in kids.OfType <CosObject>()) { // todo: exit early var child = pdfObjectParser.Parse(kid.ToIndirectReference(), reader, isLenientParsing) as PdfDictionary; var thisPageMatches = FindPage(child, soughtPageNumber, pageNumbersObserved); if (thisPageMatches) { childFound = true; break; } } return(childFound); }
public Page GetPage(int pageNumber) { if (pageNumber <= 0 || pageNumber > Count) { throw new ArgumentOutOfRangeException(nameof(pageNumber), $"Page number {pageNumber} invalid, must be between 1 and {Count}."); } var pageNode = catalog.GetPageNode(pageNumber); var pageStack = new Stack <PageTreeNode>(); var currentNode = pageNode; while (currentNode != null) { pageStack.Push(currentNode); currentNode = currentNode.Parent; } var pageTreeMembers = new PageTreeMembers(); while (pageStack.Count > 0) { currentNode = pageStack.Pop(); pageFactory.LoadResources(currentNode.NodeDictionary, isLenientParsing); if (currentNode.NodeDictionary.TryGet(NameToken.MediaBox, pdfScanner, out ArrayToken mediaBox)) { pageTreeMembers.MediaBox = new MediaBox(mediaBox.ToRectangle()); } if (currentNode.NodeDictionary.TryGet(NameToken.Rotate, pdfScanner, out NumericToken rotateToken)) { pageTreeMembers.Rotation = rotateToken.Int; } } var page = pageFactory.Create(pageNumber, pageNode.NodeDictionary, pageTreeMembers, isLenientParsing); return(page); }