Pdfkit in component

Hi, I’d like to create on my MainComponent an NsViewComponent that create an instance of an NSView implemented in others module (called PdfView Component.h and PdfViewComponent.mm) where I’d like to use pdfkit to load pdf, change pages programmatically and programmatically getting the current page on screen and the totnum of pages of document.

Someone has any experience in mixing Objective-c(++) in juce and in particular with pdfkit? How can I do it?

Here a .h and a .mm where I tried to achieve my stuffs, now I get in .h near line 8 “Expected unqualified-id” and near line 26: “Unknown type name ‘PDFView’”

I cannot do any forward declaration?

I done like this because if I import objective-c modules in .h a lot of errors come out as "unknown Type name NSString

PDFComponent.h (581 Bytes)
PDFComponent.mm (2.5 KB)
"

It seems like pdfkit is incompatible with juce, does it be possible?

You can’t do a forward declare an objective-c type in a c++ header.
You can declare it as void* in the header and cast it constantly when you need it or even better create a pimpl and hide the implementation in there.

Btw you actually don’t need to use objective-c, you can also directly use the CoreGraphics implementation, which the objective-c version just wraps in case you want even more control.

Check out CGPDFDocument.

1 Like

Really thank you, I see that juce use CoreGraphics, it should be the right way to achieve my aims, but how to get a CGContextRef and get back again a juice context to render the current pdf page?

void renderPage(juce::Graphics& g, int pageNumber)
    {
        CGContextRef context = (CGContextRef)g.getInternalContext(); // how?
        CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdfDocument, pageNumber);

        CGContextDrawPDFPage(context, pdfPage);
    }

I would use CGBitmapContextCreate to render the pdf page to a offscreen buffer.
When done drawing you can use the data which you provided to CGBitmapContextCreate to create a juce::Image from it.

1 Like

Hi, really thank you for your reply!
Finally I solved for Mac using CGPDDocument

Do you have any Ideas for windows and linux ? (also linux and Android should be the same?)

OSX is easy. Windows, probably best to forget about it …
But maybe someone else has an idea.
Btw, can you post a snippet of the code you made for the PDF document, so others can have a look on how you did this?

1 Like

really thank you… I really hopes it will not do difficult for windows :sweat_smile: here the module, here the GitHub repo.

Don’t put a blame on me, it’s still really bugged because it’s in working, I create a public repo, anyone who wants to help with the implementation is welcome! here the link:

Super, thanks for sharing!

1 Like

really thank to you!