How to remove PDFDocument margin

2k Views Asked by At

I add pdfdocument to the PDFView . but it is showing margin and also showing in the canter. How to remove this margin . And Is there any way to set pdfdocument to the top-left position.

enter image description here

2

There are 2 best solutions below

0
On

I think this would help with the problem.

https://developer.apple.com/documentation/pdfkit/pdfview/2881210-pagebreakmargins

Just set a value to this variable pageBreakMargins by passing a UIEdgeInsets value.

Something like this

self.pdfView.pageBreakMargins = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
0
On
  1. To remove the margin: you need to set pageShadowsEnabled to false.
  2. Change the position: You need to adjust height of the PdfView.

Here is the sample code:

private func openPdf() {
        let pdfView = PDFView(frame: CGRect(x: 0, y: 20, width: self.view.frame.width, height: self.view.frame.height))
        view.addSubview(pdfView)

        guard let path = Bundle.main.url(forResource: "test", withExtension: "pdf") else { return }

        if let document = PDFDocument(url: path) {
            pdfView.document = document
            pdfView.pageShadowsEnabled = false
            pdfView.displayMode = .singlePage
            pdfView.autoScales = true
            pdfView.frame.size.height = pdfView.documentView?.frame.height ?? self.view.frame.height
            self.view.layoutIfNeeded()
            pdfView.minScaleFactor = pdfView.scaleFactorForSizeToFit
        }
    } 

Output: With margin and without margin. enter image description here