Calling toolManager.pageAdded method is causing freezing for a few seconds

Hello all,

Product Version:
9.1.0.78529

Please give a brief summary of your issue:
Calling toolManager.pageAdded after adding page programmatically makes my app freeze

Please describe your issue and provide steps to reproduce it:

Problem occurs when one of page in the document contains images, other than that it works ok.
lets say my first page has 5 images ( which I added as using stampImage method )
if I want to create a new page, I call toolManager.pageAdded after, pushing the new page back.
but it makes my app freeze and turning to really poor user experience.

without calling this method I cant set the left page number indicator, ( “current page” of “total page” )
I added my image like below

     let imageStampTool = PTImageStampCreate(pdfViewCtrl: pdfViewCtrl)
        imageStampTool.stampImage(image,
                                  at: CGPoint(x: pdfViewCtrl.pagingScrollView.frame.width / 2,
                                              y: pdfViewCtrl.pagingScrollView.frame.height / 2))

I need your help thanks

Hi @ogito,

Thank you for getting in touch with us about this and apologies for the delay in response.
We are investigating this behaviour.
Meanwhile are you able to share some more complete sample code showing how you are adding the pages to the document?

Could you also describe in some more detail the workflow you are trying to create in your app?

Thank you,
Jamie

Hi @ogito,

Just following up again.
In general the PTImageStampCreate tool (and particularly the stampImage() method) is better suited as a user-facing tool to stamp images on existing pages and documents rather than when adding new pages.

A better approach would be to use the PTStamper class to stamp the pages. Internally we do this by:

  1. creating a temporary document (PTPDFDoc)
  2. adding a page to the temporary document
  3. stamp the page
  4. add the new page to the document from the temporary document
do{
    try pdfViewCtrl.docLock(true) { doc in // lock the document
        guard let document = doc,
                let image = UIImage(), // image to stamp
                let tempDoc = PTPDFDoc(),
                let stamper = PTStamper(size_type: e_ptabsolute_size, a: image.size.width, b: image.size.height),
                let imageData = image.pngData(), // image data
                let ptImage = PTImage.create(withDataSimple: tempDoc.getSDFDoc(), buf: imageData, buf_size: UInt(imageData.count), encoder_hints: PTObj()), // PTImage
                let pageSet = PTPageSet.init(one_page: 1) // pages to stamp (single page in this case)
        else {return}

        let pageMediaBox = PTPDFRect(x1: 0, y1: 0, x2: 612, y2: 792) // whichever page size you want
        tempDoc.lock()
        let newPage = tempDoc.pageCreate(pageMediaBox) // create a new page
        tempDoc.pageInsert(tempDoc.getPageIterator(1), page: newPage) // insert it in the temp doc
        stamper.stampImage(tempDoc, src_img: ptImage, dest_pages: pageSet) // stamp the image onto the page
        tempDoc.unlock()
        document.insertPages(document.getPageCount()+1, src_doc: tempDoc, start_page: 1, end_page: tempDoc.getPageCount(), flag: e_ptinsert_none) // insert the page into the existing doc from the temp doc
        toolManager.pageAdded(forPageNumber: document.getPageCount()) // call `toolManager`method
        pdfViewCtrl.updatePageLayout() // needs to be called for the pdfViewCtrl to update its layout
    }
} catch {
    // catch error
}

Would that approach work for you?

Hi @Jamie_Dassoulas

Q: Meanwhile are you able to share some more complete sample code showing how you are adding the pages to the document?
A: we have a template pdf in bundle so we get document from there and add its page to the actual document.

extension PTPDFViewCtrl {
    func addNewPage(backgroundIndex: Int) {
        guard let document = self.getDoc() else { return }
        let newPageDocument = PTPDFDoc(filepath: UIConstantsImages.writingPadBackgroundsPDF[backgroundIndex])
        let newPage = newPageDocument?.getPage(1)
        document.pagePushBack(newPage)
        let insertedPage = document.getPage(UInt32(document.getPageCount()))
        insertedPage?.setPKAnnotation(drawing: PKDrawing())
        insertedPage?.setCreationDate(date: Date())
        self.updatePageLayout()
    }
}

I guess I expressed myself wrong, I am not trying to add an image to new page, In my case let’s say any page in the current document consists of more than 10 images. creating new page and calling toolManager.pageAdded function causes to freeze.

I looked it up in the Tools.framework and found that the long taking part is related with this function

  [self.undoRedoManager pageAddedAtPageNumber:pageNumber];

I want to remind you again that if there is no page with image the function works in a perfect speed. the problem only occurs if there is a page with images.

thank you again.

Hi @ogito,

Are you able to share a working sample project which we can use to reproduce this process and investigate the slowdown?