Share My Creation b4i : View PDF pages in an imageView

This code snippet shows how to render PDF page to an image then display it in an imageView
b4i code:
'Code module
#Region  Project Attributes
    #ApplicationLabel: B4i Example
    #Version: 1.0.0
    'Orientation possible values: Portrait, LandscapeLeft, LandscapeRight and PortraitUpsideDown
    #iPhoneOrientations: Portrait, LandscapeLeft, LandscapeRight
    #iPadOrientations: Portrait, LandscapeLeft, LandscapeRight, PortraitUpsideDown
    #Target: iPhone, iPad
    #ATSEnabled: True
    #MinVersion: 8
#End Region

Sub Process_Globals
    
    Public App As Application
    Public NavControl As NavigationController
    Private Page1 As Page
    Private xui As XUI
    Private imageView1 As ImageView
    
End Sub

Private Sub Application_Start (Nav As NavigationController)
    NavControl = Nav
    Page1.Initialize("Page1")
    Page1.RootPanel.LoadLayout("Page1")
    NavControl.ShowPage(Page1)
    imageView1.Initialize("iv")
    
    
End Sub
Sub Page1_Resize(Width As Int, Height As Int)
    Dim bmp As Bitmap
    Page1.RootPanel.AddView(imageView1,0, 0, 100%x, 90%y)
    imageView1.SetLayoutAnimated (0, 1, 0.02 * Width, 0.02 * Height, 0.96 * Width, 0.9 * Height)
    bmp = GetVectorPDF(File.Combine(File.DirAssets, any.pdf"),3,3)
    imageView1.Bitmap=bmp
    

End Sub

Sub GetVectorPDF(FilePath As String, Page As Int, Scale As Float) As Bitmap
    Dim jo As NativeObject
    jo = jo.Initialize("VectorImageBridge")

    Dim img As Object = jo.RunMethod("getVectorPDFAtPath:page:scale:", _
        Array(FilePath, Page, Scale))

    Dim bmp As Bitmap
    bmp = img
    Return bmp
End Sub

Sub TestVector()
    Dim p As String = File.Combine(File.DirAssets, "cv.pdf")
    Log("PATH = " & p)
    imageView1.Initialize("iv")
    Page1.RootPanel.AddView(imageView1,0, 0, 100%x, 100%y)
    Dim jo As NativeObject
    jo = jo.Initialize("VectorImageBridge")

    Dim result As Object = jo.RunMethod("getVectorPDFAtPath:scale:", Array(p, 3))

    If result = Null Then
        Log("⚠ Objective-C returned NIL")
    Else
        Dim bmp As Bitmap = result
        imageView1.Bitmap = bmp
    End If
End Sub

#If OBJC
@end
#import <UIKit/UIKit.h>
#import <CoreGraphics/CoreGraphics.h>

@interface VectorImageBridge : NSObject
+ (UIImage *)getVectorPDFAtPath:(NSString *)path page:(int)page scale:(CGFloat)scale;
@end
@implementation VectorImageBridge

+ (UIImage *)getVectorPDFAtPath:(NSString *)path page:(int)page scale:(CGFloat)scale {

    NSLog(@"[VectorPDF] Path: %@  Page: %d", path, page);

    NSURL *url = [NSURL fileURLWithPath:path];
    CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)url);
    if (!pdf) {
        NSLog(@"[VectorPDF] Cannot open PDF");
        return nil;
    }

    CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf, page);
    if (!pdfPage) {
        NSLog(@"[VectorPDF] Page %d does not exist", page);
        CGPDFDocumentRelease(pdf);
        return nil;
    }

    CGRect rect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
    CGSize size = CGSizeMake(rect.size.width * scale, rect.size.height * scale);

    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    CGContextRef ctx = UIGraphicsGetCurrentContext();

    // Flip + scale
    CGContextTranslateCTM(ctx, 0, size.height);
    CGContextScaleCTM(ctx, scale, -scale);

    CGContextDrawPDFPage(ctx, pdfPage);

    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGPDFDocumentRelease(pdf);

    NSLog(@"[VectorPDF] Render OK: %@", img);
    return img;
}

#End If
 
Top