Welcome to the documentation for OhPDFImage. OhPDFImage is a powerful utility for extracting images from PDF files in iOS. With OhPDFImage, you can easily extract high-quality images from PDFs and use them in your iOS applications.
Installation
To install OhPDFImage, you need to use CocoaPods. If you haven’t installed CocoaPods yet, you can follow the official installation guide:
- Open a terminal and navigate to your project’s directory.
- Run the command
sudo gem install cocoapods
to install CocoaPods. - Once installed, run the command
pod init
to create a Podfile for your project. - Edit the Podfile and add the following line:
pod 'OhPDFImage'
- Save the Podfile and run the command
pod install
to install OhPDFImage. - From now on, open the newly created
.xcworkspace
file instead of the.xcodeproj
file to work on your project.
Usage
Using OhPDFImage to extract images from a PDF is straightforward. Here’s a basic example:
// Import the OhPDFImage module
import OhPDFImage
// Load the PDF
guard let pdfURL = Bundle.main.url(forResource: "example", withExtension: "pdf") else {
print("PDF not found!")
return
}
guard let pdfDocument = PDFDocument(url: pdfURL) else {
print("Failed to initialize PDF document!")
return
}
// Load the first page
guard let pdfPage = pdfDocument.page(at: 0) else {
print("Failed to load page!")
return
}
// Get the images from the page
let images = OhPDFImage.extractImages(from: pdfPage)
// Loop through the images
for (index, image) in images.enumerated() {
if let imageData = image.jpegRepresentation {
// Do something with the image data
}
}
In the example above, we first import the OhPDFImage module. Then, we load the PDF document from the URL provided. We then load the first page of the PDF and extract the images from it using the OhPDFImage.extractImages() method. Finally, we loop through the extracted images and convert them to JPEG data for further processing.
Image Extraction Options
OhPDFImage provides some options for fine-tuning the image extraction process. By default, all images from a PDF page are extracted. However, you can specify additional options using the `options` parameter of the `extractImages()` method. Here are the available options:
- scale: The scale factor of the extracted images. The default value is 1.0.
- maxSize: The maximum size (in points) of the extracted images. Images larger than this size will be downscaled. The default value is CGSize.zero.
- minSize: The minimum size (in points) of the extracted images. Images smaller than this size will be ignored. The default value is CGSize.zero.
- colorSpace: The color space of the extracted images. The default value is CGColorSpaceCreateDeviceRGB().
- interpolationQuality: The interpolation quality used when scaling the images. The default value is .default.
- extractionMethod: The method used for extracting images. The default value is .default.
To use these options, simply pass a dictionary of options as the `options` parameter of the `extractImages()` method. Here’s an example:
let options: [OhPDFImage.ExtractionOption: Any] = [
.scale: 2.0,
.maxSize: CGSize(width: 1024, height: 1024),
.colorSpace: CGColorSpaceCreateDeviceGray(),
.extractionMethod: .all
]
let images = OhPDFImage.extractImages(from: pdfPage, options: options)
In the example above, we set the scale factor to 2.0, the maximum size to 1024×1024 points, the color space to grayscale, and the extraction method to extract all images. You can adjust these options based on your specific requirements.
Conclusion
That’s it! You’ve learned how to install OhPDFImage and extract images from PDFs using it. If you have any further questions or need assistance, feel free to reach out to our support team. Happy coding!