Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove PDFDocument margin

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

like image 949
Spanix Dev Avatar asked Oct 19 '25 01:10

Spanix Dev


2 Answers

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)
like image 171
jyce1492 Avatar answered Oct 21 '25 16:10

jyce1492


  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

like image 31
Razib Mollick Avatar answered Oct 21 '25 15:10

Razib Mollick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!