Links within PDF getting removed after processing using PDFKit

I'm using the following code to draw a text over PDF page.Everything works fine, but if you process a PDF with internal links eg: a Book with Content links to pages, the resultant PDF file seems to have the links stripped off.Why does this happen?

 let data = NSMutableData()
                    let consumer = CGDataConsumer(data: data as CFMutableData)!
                    let context = CGContext(consumer: consumer, mediaBox: nil, nil)!
                    let pdffile=PDFDocument(url: input)                   
                    for y in stride(from: 0, to: pagecount, by: 1)
                    {
                    let page: PDFPage = pdffile!.page(at: y)!
                    var mediaBox = page.bounds(for: PDFDisplayBox.mediaBox)
                    NSGraphicsContext.current = NSGraphicsContext(cgContext: context, flipped: false)
                    
                    let bounds = page.bounds(for: PDFDisplayBox.mediaBox)
                    let size = bounds.size                   
                    page.draw(with: .mediaBox, to: context)
                    text.draw(in:drawrect,withAttributes:textFontAttributes);
                    context.endPDFPage()
                    }
                    context.closePDF()
                    try! data!.write(to: GetOutputFileName(inputfile: x), options[.atomicWrite])