Reputation: 602
I have multi page document and i want to find page number for specific sentence.
I've tried to find with contains method but If the last sentence splits over two pages, it does not find the page number.
My code is:
func getPageNumber(sentence: Sentence) -> Int {
let pages = document.pages
for (index, page) in pages.enumerated() {
guard let sentenceText = sentence.all else { return 0 }
if page.text.contains(sentenceText) {
return index
}
}
return 0
}
Example Usage
Page 1
Ullamcorper a lacus vestibulum sed arcu non. Nec ullamcorper sit amet risus. Donec ac odio tempor orci dapibus ultrices. Non consectetur a erat nam at lectus urna. Cursus vitae congue mauris rhoncus aenean vel elit scelerisque. Maecenas accumsan lacus vel facilisis volutpat est velit egestas dui. Justo donec enim
Page 2
diam vulputate ut. Sed augue lacus viverra vitae. Eget duis at tellus at urna condimentum mattis. Convallis posuere morbi leo urna molestie at elementum eu. Sed tempus urna et pharetra pharetra massa massa.
If i want to find "Nec ullamcorper sit amet risus." it returns page 1. But if I want to find Justo donec enim diam vulputate ut. it is not returning page1 and page2.
How can i achieve this efficently?
Thanks.
Upvotes: 0
Views: 67
Reputation: 3867
If it wasn't multi page then you could easily search all occurrences, and get back an array of matching ranges. To make it multi page you could just add page break indexes (which form ranges 0..p1..p2..end). Now, given a match range, you can see which cross which page breaks, and which are contained with a single page.
Upvotes: 1