AI’s Greatest Moments: Celebrating Artificial Intelligence Appreciation Day
Discover the most remarkable achievements in artificial intelligence as we honor AI’s Greatest Moments on Artificial Intelligence Appreciation Day.
I would recommend you to use LibPdf, for PDF to Image conversion. This library will convert PDF file to image with PNG and BMP as supported image format.
I hope the below example will encourage you to give converting PDF to image a shot.
using (FileStream file = File.OpenRead(@”..\path\to\pdf\file.pdf”)) // in file
{
var bytes = new byte[file.Length];
file.Read(bytes, 0, bytes.Length);
using (var pdf = new LibPdf(bytes))
{
byte[] pngBytes = pdf.GetImage(0,ImageType.PNG); // image type
using (var outFile = File.Create(@”..\path\to\pdf\file.png”)) // out file
{
outFile.Write(pngBytes, 0, pngBytes.Length);
}
}
}
Code Credit: stackoverflow
If you are looking to convert PDF to an image but in an easy manner and not much interested in coding then try ImageMagick, it will simply not do your job but also provide few .NET Bindings
This is very simple.
convert file.pdf imagefile.png
Here is the function, which will convert the PDF into images. Add the following lines of code.
-(void)splitPDF:(NSURL *)sourcePDFUrl withOutputName:(NSString *)outputBaseName intoDirectory:(NSString *)directory
{
CGPDFDocumentRef SourcePDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)sourcePDFUrl);
size_t numberOfPages = CGPDFDocumentGetNumberOfPages(SourcePDFDocument);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathAndDirectory = [documentsDirectory stringByAppendingPathComponent:directory];
NSError *error;
if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
withIntermediateDirectories:NO
attributes:nil
error:&error])
{
NSLog(@”Create directory error: %@”, error);
return;
}
for(int currentPage = 1; currentPage <= numberOfPages; currentPage ++ )
{
CGPDFPageRef SourcePDFPage = CGPDFDocumentGetPage(SourcePDFDocument, currentPage);
// CoreGraphics: MUST retain the Page-Refernce manually
CGPDFPageRetain(SourcePDFPage);
NSString *relativeOutputFilePath = [NSString stringWithFormat:@”%@/%@%d.png”, directory, outputBaseName, currentPage];
NSString *ImageFileName = [documentsDirectory stringByAppendingPathComponent:relativeOutputFilePath];
CGRect sourceRect = CGPDFPageGetBoxRect(SourcePDFPage, kCGPDFMediaBox);
UIGraphicsBeginPDFContextToFile(ImageFileName, sourceRect, nil);
UIGraphicsBeginImageContext(CGSizeMake(sourceRect.size.width,sourceRect.size.height));
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, 0.0, sourceRect.size.height); //596,842 //640×960,
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextDrawPDFPage (currentContext, SourcePDFPage); // draws the page in the graphics context
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent: relativeOutputFilePath];
[UIImagePNGRepresentation(image) writeToFile: imagePath atomically:YES];
}
}
CGPDFDocumentRef SourcePDFDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)sourcePDFUrl);
size_t numberOfPages = CGPDFDocumentGetNumberOfPages(SourcePDFDocument);
Firstly it will create a document reference and then through that reference it will ake out the number of pages.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePathAndDirectory = [documentsDirectory stringByAppendingPathComponent:directory];
NSError *error;
if (![[NSFileManager defaultManager] createDirectoryAtPath:filePathAndDirectory
withIntermediateDirectories:NO
attributes:nil
error:&error])
{
NSLog(@”Create directory error: %@”, error);
return;
}
Then it will check either the PDF, which we are converting into images, exists or not. If it does not exist or there is any error it returns from the same part.
for(int currentPage = 1; currentPage <= numberOfPages; currentPage ++ )
In this it will start the loop and start fetching the each page of the PDF
CGPDFPageRef SourcePDFPage = CGPDFDocumentGetPage(SourcePDFDocument, currentPage);
// CoreGraphics: MUST retain the Page-Refernce manually
CGPDFPageRetain(SourcePDFPage);
It will fetch the current Page from PDF and then it will retain it.
NSString *relativeOutputFilePath = [NSString stringWithFormat:@”%@/%@%d.png”, directory, outputBaseName, currentPage];
NSString *ImageFileName = [documentsDirectory stringByAppendingPathComponent:relativeOutputFilePath];
It will create a path to save the Images
CGRect sourceRect = CGPDFPageGetBoxRect(SourcePDFPage, kCGPDFMediaBox);
UIGraphicsBeginPDFContextToFile(ImageFileName, sourceRect, nil);
UIGraphicsBeginImageContext(CGSizeMake(sourceRect.size.width,sourceRect.size.height));
CGContextRef currentContext = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(currentContext, 0.0, sourceRect.size.height); //596,842 //640×960,
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextDrawPDFPage (currentContext, SourcePDFPage); // draws the page in the graphics context
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Here, the Current PDF page will be converted into Image.
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent: relativeOutputFilePath];
[UIImagePNGRepresentation(image) writeToFile: imagePath atomically:YES];
It will save the file into the directory.
You may also like:
GeneralMobile AppsTechnologies
Jul 16th, 2024
Discover the most remarkable achievements in artificial intelligence as we honor AI’s Greatest Moments on Artificial Intelligence Appreciation Day.
Jul 11th, 2024
Discover the best methods for adding your blogs to your GitHub profile. Enhance your visibility and engage more followers. Click for detailed guide.
Jun 27th, 2024
On National Doctor’s Day, let’s appreciate the advancements in healthcare, especially the rise of telemedicine. This innovation has revolutionized medical care, making it more convenient and effective. India’s eSanjeevani platform stands out, providing equitable healthcare to all, especially in rural areas. Launched by the Ministry of Health and Family Welfare, eSanjeevani facilitates remote consultations and enhances the quality of care. As telemedicine continues to evolve, driven by technological advancements, it promises to play a crucial role in the future of healthcare, ensuring accessibility and efficiency for everyone.