Displaying a PDF page through Quartz lazarus / 33-macOS - Mac OS X ---------------------------------------------------------------------- WildMan · 2012-06-27 10:41 I'm trying to create a procedure to draw a PDF page to a bitmap (or a Graphics32 tBitmap32), given the document name and page number.  I purchased a copy of "Programming with Quartz" and I've been digging through the unit MacOSAll.  I've managed to open the PDF file, apparently, as the code below does correctly display the number of pages in the PDF document.  I can't make any sense of the C/Xcode examples in the book order to get a Graphics context so that I can draw the page to a bitmap.  Can anyone share some pointers so that I can get moving in the right direction again? procedure TForm1.ShowPDFPage(PDFpath: string; PageNo: Integer); var  MyPDFref: CGPDFdocumentRef;        MyCFURL: CFURLref;        x: integer; begin   MyCFStringRef:= CFStringCreateWithPascalString(nil,PDFpath,kCFStringEncodingUTF8);   MyCFURL:=CFURLCreateWithFileSystemPath(nil,MyCFStringRef,kCFURLPOSIX PathStyle,false);   MyPDFref:=CGPDFDocumentCreateWithURL(MyCFURL);   x:=CGPDFDocumentGetNumberOfPages(MyPDFref);   Showmessage(IntToStr(x)+' pages'); end; ---------------------------------------------------------------------- havezet · 2012-06-28 10:46 Because I need this too, I took your code as a starter and completed it with the help of Google Code: [Select] uses   CarbonCanvas, MacOSAll; procedure TForm1.Button1Click(Sender: TObject);   function CreatePDFPageBitmap(APDFpath: String; APageNo: Integer; ASize: Integer): TBitmap;   var     MyCFStringRef: CFStringRef;     MyPDFref: CGPDFDocumentRef;     MyCFURL: CFURLref;     x: integer;     MyPDFPage: CGPDFPageRef;     context: CGContextRef;     mediarect: CGRect;     ARatio: Single;   begin     result := TBitmap.Create;     MyCFStringRef := CFStringCreateWithPascalString(nil, APDFpath, kCFStringEncodingUTF8);     MyCFURL := CFURLCreateWithFileSystemPath(nil, MyCFStringRef, kCFURLPOSIXPathStyle, false);     MyPDFref := CGPDFDocumentCreateWithURL(MyCFURL);     try       x := CGPDFDocumentGetNumberOfPages(MyPDFref);       if x < APageNo then exit;       //Showmessage(IntToStr(x)+' pages');       MyPDFPage := CGPDFDocumentGetPage(MyPDFref, APageNo);       mediarect := CGPDFPageGetBoxRect(MyPDFPage, kCGPDFCropBox);       ARatio := mediarect.size.width / mediarect.size.height;       if ARatio >= 1 then  // landscape         result.SetSize(ASize, Round(ASize/ARatio))       else         result.SetSize(Round(ASize*ARatio), ASize);  // portrait       result.Canvas.Brush.Color := clWhite;       result.Canvas.FillRect(0, 0, result.Width-1, result.Height-1);       context := TCarbonBitmapContext(result.Canvas.Handle).CGContext;       // flip coordinates       CGContextScaleCTM(context, 1, -1);       CGContextTranslateCTM(context, 0, -result.Height);       // get the rectangle of the cropped inside       CGContextScaleCTM(context, result.Width / mediarect.size.width, result.Height / mediarect.size.height);       CGContextTranslateCTM(context, -mediarect.origin.x, -mediarect.origin.y);       // draw it       CGContextDrawPDFPage(context, MyPDFPage);     finally       CFRelease(MyPDFref);       CFRelease(MyCFURL);       CFRelease(MyCFStringRef);     end;   end; var   ABitmap: TBitmap; begin   ABitmap := CreatePDFPageBitmap('/Users/havezet/desktop/test.pdf', 1, 500);   Image1.Picture.Bitmap := ABitmap;   ABitmap.Free; end; Hope this gets you started... ---------------------------------------------------------------------- WildMan · 2012-06-29 06:00 Thank you!  I was banging my head against the wall because I couldn't get the context; I see now how you did that.  I really appreciate the help; I can finally start moving forward again. ---------------------------------------------------------------------- WildMan · 2019-10-11 00:45 Years later now I'm trying to update this code from carbon to Cocoa to make it 64-bit  but I'm stuck on this line  context := TCarbonBitmapContext(result.Canvas.Handle).CGContext; How can I get that context when compiling for Cocoa rather than Carbon? ---------------------------------------------------------------------- trev · 2019-10-11 05:42 Beware the release notes for Catalina... QuoteQuartz Composer Deprecations     Starting in macOS 10.15, the Quartz Composer framework is deprecated and remains present for compatibility purposes. Transition to frameworks such as Core Image, SceneKit, or Metal. (50911608) ---------------------------------------------------------------------- WildMan · 2019-10-11 18:32 Thanks for the heads-up; I didn't realize that.  All I'm trying to do is grab a page from a multi-page PDF file and display it on-screen.  Is there a simple way to accomplish that without having to take on delving deep into one of those frameworks?