Is there any way to find the current pdf content size.
For example:-
When user open the pdf file and fill some information in the pdf and click on submit button. Then how to check pdf content size means the content fill fully or not. If user not fully added the details, pending some TextFiled in the pdf, then we need to show alert "Please fill all the information."
Code:-
override func viewDidLoad() {
super.viewDidLoad()
if let documentURL = Bundle.main.url(forResource: pdfName, withExtension: "pdf") {
if let document = PDFDocument(url: documentURL) {
pdfView.autoScales = true
pdfView.backgroundColor = UIColor.lightGray
document.delegate = self
pdfView.document = document
pdfView.autoScales = true
}
}
}
Can someone please explain to me is it possible in swift if yes please guide me how to achieve this, I've tried lot of search but no results yet.
Any help would be greatly appreciated.
Thanks in advance.
I found solution to check PDF Content.
Code:-
func checkPDFTextFiledDataBlankOrNot() {
if let page = pdfVw.document?.page(at: 0){
let annotations = page.annotations
for annotation in annotations{
if annotation.widgetStringValue == " "{
annotation.widgetStringValue = "N/A"
emptyTxtFields += 1
}else {
fillTxtfields += 1
}
}
}
}
Related
In my app users will enter information on one screen that gets put into a database. On another screen I want to display a pdf with their information used to fill out the edit text fields (annotations). In the example below the user can click "John Smith" currently and type a new name.
Using ios PDFKit I am able to display the pdf and allow the user to edit. I am unable to update text in the text boxes or change the status of checkboxes. It looks as though I do have read/write privileges for the pdf.
With my current code I was able to get the number of pages of my pdf and print off the annotation names of every annotation so I can get the names of the fields I want to set values for but the setvalue function does not work and checking the contents of a filled textbox shows nil.
override func viewDidLoad() {
super.viewDidLoad()
displayPDF()
updatePDFText()
}
func displayPDF() {
let pdfView = PDFView()
pdfView.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(pdfView)
pdfView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
pdfView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
pdfView.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
pdfView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor).isActive = true
guard let path = Bundle.main.url(forResource: MY_PDF, withExtension: "pdf") else { return }
if let document = PDFDocument(url: path) {
pdfView.document = document
}
}
func updatePDFText() {
guard let path = Bundle.main.url(forResource: MY_PDF, withExtension: "pdf") else { return }
if let document = PDFDocument(url: path) {
for index in 0..<document.pageCount {
if let page = document.page(at: index) {
let annotations = page.annotations
for annotation in annotations {
//print("Annotation Name :: \(annotation.fieldName ?? "")")
if annotation.fieldName == "SSN1" {
annotation.setValue("TEST", forAnnotationKey: .widgetValue)
page.removeAnnotation(annotation)
page.addAnnotation(annotation)
} else if annotation.fieldName == "District" {
annotation.buttonWidgetState = .onState
page.removeAnnotation(annotation)
page.addAnnotation(annotation)
}
}
}
}
}
}
Is there an issue with my implementation or is there another method of programmatically updating the pdf annotations?
The Apple API isn't working on iOS 13.1, does anyone have the same issue or I did wrong way to use it.
I tried to get a PDFPage from PDFDocument.page(at: validPageIndex), the outcome page is with right index, and I set this page to PDFView.go(to: page) and the navigation isn't working.
let validPageIndex: Int = 11
guard let targetPage = document.page(at: validPageIndex) else { return }
print(targetPage.index)
// Print 11
pdfView.go(to: targetPage)
the line pdfView.go(to: targetPage) was executed but the page in PDF file stay at first page
Thanks for Usama Azam, my PDFView display direction is horizontal, seems it works on vertical.
I tried this sample piece of code and it works for me to move page next and previous. You can follow this link to add a PDF view in Storyboard.
import UIKit
import PDFKit
class ViewController: UIViewController {
#IBOutlet weak var pdfView: PDFView!
var currentPage = 0
override func viewDidLoad() {
super.viewDidLoad()
if let path = Bundle.main.path(forResource: "sample", ofType: "pdf") {
if let pdfDocument = PDFDocument(url: URL(fileURLWithPath: path)) {
pdfView.displayMode = .singlePageContinuous
pdfView.autoScales = true
pdfView.displayDirection = .vertical
pdfView.document = pdfDocument
}
}
}
#IBAction func previousPage(_ sender: UIButton) {
let validPageIndex: Int = currentPage - 1
guard let targetPage = pdfView.document!.page(at: validPageIndex) else { return }
print(targetPage.index)
currentPage = currentPage - 1
pdfView.go(to: targetPage)
}
#IBAction func nextPage(_ sender: UIButton) {
let validPageIndex: Int = currentPage + 1
guard let targetPage = pdfView.document!.page(at: validPageIndex) else { return }
print(targetPage.index)
currentPage = currentPage + 1
pdfView.go(to: targetPage)
}
}
Try this code, it solves the problem in the SwiftUI UIViewRepresentable.
if let doc = pdfView.document,
let page = doc.page(at: pageNumber)
{
Task { #MainActor in
pdfView.go(to: page )
}
}
I want to share a post on facebook with Image and text. I am using Facebook SDK and I am able to share Image, Now I want to share text with this image. How I can?
Here is the source code of sharing the image,
let photo:FBSDKSharePhoto = FBSDKSharePhoto()
var message:String?
let imageName:String = "Star.png"
photo.image = UIImage(named: imageName)
photo.isUserGenerated = true
let content = FBSDKSharePhotoContent()
content.photos = [photo];
let dialog = FBSDKShareDialog()
dialog.fromViewController = self
dialog.shareContent = content
dialog.mode = .native
dialog.show()
Due to the abuse from some applications, Facebook has explicitly disabled the pre-fill option for the text by simply ignoring it.
"You must not pre-fill any of the fields associated with the
following products, unless the user manually generated the content
earlier in the workflow: Stream stories (user_message parameter for
Facebook.streamPublish and FB.Connect.streamPublish, and message
parameter for stream.publish), Photos (caption), Videos (description),
Notes (title and content), Links (comment), and Jabber/XMPP."
More info here.
Facebook wont allow user to add text with image.
Integrate facebookshare sdk
#IBAction func facebookBtn(sender: AnyObject) {
let shareImage = SharePhoto()
shareImage.image = imageView.image //Image from your imageview
shareImage.isUserGenerated = true
let content = SharePhotoContent()
content.photos = [shareImage]
let sharedDialoge = ShareDialog()
sharedDialoge.shareContent = content
sharedDialoge.fromViewController = self
sharedDialoge.mode = .automatic
if(sharedDialoge.canShow)
{
sharedDialoge.show()
}
else
{
print("Install Facebook client app to share image")
}
}
Try this :
let sharePhoto = FBSDKSharePhoto()
photo.image = photo
photo.caption = "you title"
let content = FBSDKShareMediaContent()
content.media = [photo];
I'm trying to add highlight annotations to documents using PDFKit on iOS.
let highlight = PDFAnnotation(bounds: selection.bounds(for: page),
forType: PDFAnnotationSubtype.highlight,
withProperties: nil)
highlight.color = color
page.addAnnotation(highlight)
page.displaysAnnotations = true
When adding them using the code above, they appear as two differently shaped layers. When saving them to the PDF file and reopening it they display correctly.
In this screen capture
The top and bottom highlights have been added the same way using the snippet provided here. The top one has been saved to the pdf document and appears as expected when reopening it, the bottom one has just been added.
Does anyone know how to have them display correctly (i.e. like the top one) without resorting to saving and reopening the file?
So this is a know bug in 10.13. There is a workaround by scrolling the page away and then back to the highlight
You can create the highlight using this code:
let page = self.pdfDocument?.page(at: 10)
let bounds = CGRect(x: 85.8660965, y: 786.8891167, width: 298.41, height: 12.1485)
let annotation = PDFAnnotation(bounds: bounds, forType: .highlight, withProperties: nil)
annotation.color = NSColor.blue
page?.addAnnotation(annotation)
Then you need to scroll away from the page and back to the highlight
func annotationScrollHack(page: PDFPage) {
guard let pdfDocument = self.pdfDocument else { return }
//When adding highlights to macOS 10.13 it seems like 2 highlights are added.
//If you scroll to a different page and back the "extra" highlight is removed
//This function scrolls to the first/last page in the book and then back to the current page
//rdar://34784917
let bookScrollView = self.pdfView.documentView?.enclosingScrollView
let currentVisibleRect = bookScrollView?.contentView.documentVisibleRect
if (0 ... 3).contains(pdfDocument.index(for: page)) {
if self.pdfView.canGoToLastPage {
self.pdfView.goToLastPage(self)
}
} else {
if self.pdfView.canGoToFirstPage {
self.pdfView.goToFirstPage(self)
}
}
if let currentVisibleRect = currentVisibleRect {
bookScrollView?.contentView.scroll(to: CGPoint(x: currentVisibleRect.origin.x, y: currentVisibleRect.origin.y))
}
}
Response from Apple:
There is no workaround other than the “hack" they described: scrolling
away then back is the best option. Changing zoom factor and reverting
it might fix it in the same way, but not guaranteed.
I've ended up with this hack method which takes into consideration all cases in my opinion. Hope that will help.
private func hackScroll(to page: PDFPage) {
switch (pdfView.canGoToFirstPage(), pdfView.canGoToLastPage()) {
case (true, false):
pdfView.goToFirstPage(nil)
pdfView.go(to: page)
case (false, true):
pdfView.goToLastPage(nil)
pdfView.go(to: page)
case (false, false):
guard let dir = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else { return }
let file = "temp.pdf"
/// Save to temp file
let fileURL = dir.appendingPathComponent(file)
pdfView.document?.write(to: fileURL)
/// Read from temp file
guard let document = PDFDocument(url: fileURL) else { return }
pdfView.document = document
pdfView.autoScales = true
default:
pdfView.goToFirstPage(nil)
pdfView.go(to: page)
}
}
By default "no-profile-image" is set to imageview, User should upload the profile picture mandatory while registration, otherwise user should not allow to navigate to next screen.Any help is appreciable.
Here is my code
lazy var profileLogo:UIImageView = {
let profileLogo = UIComponentFactory.appImageView(UIImage(named: "no-profile-image")!, contentMode: .ScaleToFill, cornerRadius: LayoutService.width(15))
profileLogo.userInteractionEnabled = false
return profileLogo
}()
func nextButtonPressed() {
let noImage = UIImage(named: "no-profile-image")
if self.mainView.profileLogo.image == nil && self.mainView.profileLogo.image == noImage{
displayAlert("", message: "profile picture is mandatory, please upload it")
}
}