cf23aa
Last Updated: December 26, 2018
·
8.44K
· jfsagasti

How to save a pdf file with its annotations in Objective-C

Here is the way I saved a pdf with all the drawings or annotations made on a UIView layer. I show this UIView layer on top of the pdf reader view, so it lets the user to draw things on it. This code is about how to save both layers (pdf and annotations) in a new pdf file.

NSError *error;
//Load original pdf data:
NSData *pdfData = [NSData dataWithContentsOfFile:OriginalPdfPath    options:NSDataReadingUncached error:&error];

if (error) 
{
    NSLog(@"%@", [error localizedDescription]);
    return;
} 
else 
    NSLog(@"Data has loaded successfully.");

//If fails to create the new file, returns
if (![[NSFileManager defaultManager] createFileAtPath:NewPdfPath contents:pdfData attributes:nil])
{
    return;
}

NSURL *url = [NSURL fileURLWithPath:OriginalPdfPath];
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL ((__bridge_retained CFURLRef) url);
size_t count = CGPDFDocumentGetNumberOfPages(document);

if (count == 0)
{
    NSLog(@"PDF needs at least one page");
    return;
}

CGRect paperSize = CGRectMake(0, 0, 1190, 842);

 UIGraphicsBeginPDFContextToFile(NewPdfPath , paperSize, nil);
// CGPDFPageRef page = CGPDFDocumentGetPage(document, 1); 
 UIGraphicsBeginPDFPageWithInfo(paperSize, nil);
 CGContextRef currentContext = UIGraphicsGetCurrentContext();

// flip context so page is right way (landscape app)
CGContextScaleCTM(currentContext, 1, -1);
// Rotate the coordinate system (rotation = M_PI or -M_PI for landscape)
CGContextRotateCTM(currentContext, rotation/ 2);

CGPDFPageRef page = CGPDFDocumentGetPage (document, 1); // grab page 1 of the PDF 
CGContextDrawPDFPage (currentContext, page); // draw page 1 into graphics context

 //flip context so annotations are right way
CGContextScaleCTM(currentContext, 1, -1);
CGContextRotateCTM(currentContext, rotation / 2);
//Render the layer of the annotations view in the context
[drawingView.layer renderInContext:currentContext];
UIGraphicsEndPDFContext();
CGPDFDocumentRelease (document);

4 Responses
Add your response

12550

Thank you for the utility function.
But can you tell where are you exactly saving the layer that you draw back to pdf? Is it just displayed on the context or being drawn on the pdf an saved in File Manager?

Thank you for clarifying!!

over 1 year ago ·
12554

Draw the original pdf page in a graphic context and then draw the drawings layer in the same context ([drawingView.layer renderInContext:currentContext]). Then you can save the context as a new pdf document and you are done :)

over 1 year ago ·
12577

Yes it worked , great renderInContext works like magic, perhaps I gave a call to drawAnnots before as to draw in that view context before rendering it on pdf context. If there some other it would be great to hear from you!!

over 1 year ago ·
30892

It’s not something you can check https://emus4u.co/emus4u-android-apk/ for after but I’m guessing that some of the PDF files you work in have transparency or layers. If this is http://movieboxhdapp.org/cartoon-hd-app-apk-downloadthe case then when you edit the PDF content in Illustrator you’ll first see a prompt before it opens. And, you’ll get the behavior you mentioned https://tutuappapk.vip/tutuapp-pc-windows/
as well.

3 months ago ·