简体   繁体   中英

Objective C - Create PDF (Mac OSX / Cocoa)

I'm following this guide to create PDF but I don't understand what I have to code on myDrawnContent function : https://developer.apple.com/library/archive/documentation/GraphicsImaging/Conceptual/drawingwithquartz2d/dq_pdf/dq_pdf.html#//apple_ref/doc/uid/TP30001066-CH214-CJBFFHHA

Using this guide I'm able to create a PDF with the function:

-(void) MyCreatePDFFile:(CGRect)pageRect filename:(const char *)filename;

I need to code the -(void)myDrawContent:(CGContextRef) pdfContext; I want to set a Tittle on the top, a NSImage and a NSString after the image, how i do that?

Adiccionaly: for that I found this code:

NSString * path = @"/Users/admin/Downloads/prueba.pdf";
PDFDocument * pdf = [[PDFDocument alloc]init];
NSImage *image = [self getRepImage];
PDFPage * page = [[PDFPage alloc ] initWithImage:image];
[pdf insertPage:page atIndex: [pdf pageCount]];
[pdf writeToFile:path];

It creates a pdf with a NSImage but I how do i add text ? Thank you

The PDFKit classes like PDFDocument and PDFPage do not let you add text. If you need to create a PDF with text, use the data structure CGContext in the Quartz framework to create a PDF context to create a PDF file and use Core Text to draw text in the PDF context.

To create a PDF context create a CGContext object. You must supply a URL for the PDF file. The second argument is a rectangle that specifies the page size. If you want a standard 8.5 by 11 inch page, you can pass NULL. The third and final argument is an optional dictionary of auxiliary information. You can pass NULL for this option.

Call the CGContext function beginPDFPage to create a PDF page that you can draw in. Draw your image. For small amounts of text, call the Core Text function CTLineCreateWithAttributedString to create a line for the text. Call the Core Text function CTLineDraw to draw the text in the PDF context. If you have large amounts of text to draw, you will need to create a Core Text framesetter and use that to create and draw frames of text.

Call the CGContext function endPDFPage to finish drawing the page. Repeat the calls to beginPDFPage and endPDFPage to draw additional pages. When you are finished, call the CGContext function closePDF to close the PDF context and save the PDF file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM