0

I want insert company logo image into pdf of every page. So we are referring following site Tutorial Link

I want to insert logo right bottom page of pdf, I am unable do to that process.

Please find the tried both way code:

let documentURL = url

// Create a `CGPDFDocument` object for accessing the PDF pages.
// We need these pages in order to draw the original/existing content, because `UIGraphicsBeginPDFContextToFile` creates a file with a clean slate.
// We will have the original file contents in memory as long as the `CGPDFDocument` object is around, even after we have started rewriting the file at the path.
guard let originalDocument = CGPDFDocument(documentURL as CFURL) else {
    print("Unable to create read document.")
    return
}

// Create a new PDF at the same path to draw the contents into.
UIGraphicsBeginPDFContextToFile(documentURL.path, CGRect.zero, nil)

let image = UIImage(named: "inactive")!


guard let pdfContext = UIGraphicsGetCurrentContext() else {
    print("Unable to access PDF Context.")
    return
}

let pageSize = UIGraphicsGetPDFContextBounds().size

for pageIndex in 0..<originalDocument.numberOfPages {
    
    // Mark the beginning of the page.
    pdfContext.beginPDFPage(nil)
    
    // Pages are numbered starting from 1.
    // Access the `CGPDFPage` object with the original contents.
    guard let currentPage = originalDocument.page(at: pageIndex + 1) else {
        return
    }
    
    // Draw the existing page contents.
    pdfContext.drawPDFPage(currentPage)
    
    // Save the context state to restore after we are done drawing the image.
    pdfContext.saveGState()
    
    // Change the PDF context to match the UIKit coordinate system.
    pdfContext.translateBy(x: 0, y: pageSize.height)
    pdfContext.scaleBy(x: 1, y: -1)
    
    
    // Location of the image to be drawn in UIKit coordinates.
    let imagePosition = CGRect(x: 100, y: 0, width: 50, height: 50)
    image.draw(in: imagePosition)
    //                                            UIColor.orange.set()
    //                                            UIRectFill(CGRect(x: 100, y: 0, width: 50, height: 50))
    // Restoring the context back to its original state.
    pdfContext.restoreGState()
    
    // Mark the end of the current page.
    pdfContext.endPDFPage()
}

// End the PDF context, essentially closing the PDF document context.
UIGraphicsEndPDFContext()

Updated Code:

    let documentURL = url
                                        guard let originalDocument = CGPDFDocument(documentURL as CFURL) else {
                                            print("Unable to create read document.")
                                            return
                                        }
                                        UIGraphicsBeginPDFContextToFile(documentURL.path, CGRect.zero, nil)
//                                        let image = UIImage(named: "verified_kuwy")
                                        let image = UIImage(named: "watermark")
                                        guard let pdfContext = UIGraphicsGetCurrentContext() else {
                                            print("Unable to access PDF Context.")
                                            return
                                        }
                                        let pageSize = UIGraphicsGetPDFContextBounds().size
                                        for pageIndex in 0..<originalDocument.numberOfPages {
                                            pdfContext.beginPDFPage(nil)
                                            guard let currentPage = originalDocument.page(at: pageIndex + 1) else {
                                                return
                                            }
                                            pdfContext.drawPDFPage(currentPage)
                                            pdfContext.saveGState()
                                            pdfContext.translateBy(x: 0, y: pageSize.height)
                                            pdfContext.scaleBy(x: 1, y: -1)
                                            let imagePosition = CGRect(x: pageSize.width - 150, y: pageSize.height - 150, width: 100, height: 100)
                                            image!.draw(in: imagePosition)
                                            pdfContext.restoreGState()
                                            pdfContext.endPDFPage()
                                        }

                                        // End the PDF context, essentially closing the PDF document context.
                                        UIGraphicsEndPDFContext()

Output some

karthikeyan
  • 3,821
  • 3
  • 22
  • 45
  • I dont know Swift. But i see 2 problems in your code. On One side after pdfContext.drawPDFPage(currentPage) you never know, which transformation matrix is active on the page. To be on the save side you would need to analyze it or make a SaveGraphicState before it and restore afterwards, On the other side, if you insert the Draw Operator for an image, you need to add an image xobject in the resource directory of the page. Do you do this? – PatrickF Feb 08 '22 at 15:15
  • Thanks for reply, My image is in asset folder only. – karthikeyan Feb 09 '22 at 04:34
  • What is the issue or question ? Is the image not showing up or showing up in the wrong location ? Also can you confirm this is a valid image `UIImage(named: "inactive")` ? – Shawn Frank Feb 09 '22 at 08:05
  • My image is valid and location also fixed.. How to add watermark image to PDF? – karthikeyan Feb 09 '22 at 08:14
  • How are you checking if this is added or not ? Where are you previewing the PDF ? Are you checking the PDF file or do you display the PDF in your app ? If you display it in your app, please show the code you are using to display the PDF and when do you call it - before or after the above function ? – Shawn Frank Feb 09 '22 at 09:21
  • Check updated questions, sometimes drawing my image upside down – karthikeyan Feb 09 '22 at 09:36
  • I tried the tutorial, everything works as expected. I suggest to clean up the question and the code formatting a bit as the issue is no longer about the image not showing up it seems ? And also the example you set, the image looks correct, not upside down. – Shawn Frank Feb 09 '22 at 10:00
  • Yes sometimes coming upside down like https://stackoverflow.com/questions/64536888/using-cgcontext-of-uigraphicimagerenderer-to-redraw-an-image-flipped-the-image/64538344#64538344 – karthikeyan Feb 09 '22 at 10:04
  • It seems like the above code already takes care of that with these lines `pdfContext.translateBy(x: 0, y: pageSize.height); pdfContext.scaleBy(x: 1, y: -1)` – Shawn Frank Feb 09 '22 at 10:10

0 Answers0