Bạn có thể thực hiện chú thích bằng cách đọc trong trang PDF, vẽ chú thích lên ngữ cảnh đồ họa PDF mới, sau đó vẽ thêm nội dung vào ngữ cảnh đồ họa đó. Dưới đây là một số mã thêm từ 'Chú thích mẫu' tại vị trí (100.0,100.0) vào tệp PDF hiện có. Phương thức getPDFFileName trả về đường dẫn của PD gốc. getTempPDFFileName trả về đường dẫn của tệp PDF mới, một trong đó là bản gốc cộng với chú thích.
Để thay đổi chú thích, chỉ cần thêm mã vẽ khác thay cho phương thức drawInRect: withFont :. Xem Hướng dẫn vẽ và in cho iOS để biết thêm về cách thực hiện điều đó.
- (void) exampleAnnotation;
{
NSURL* url = [NSURL fileURLWithPath:[self getPDFFileName]];
CGPDFDocumentRef document = CGPDFDocumentCreateWithURL ((CFURLRef) url);// 2
size_t count = CGPDFDocumentGetNumberOfPages (document);// 3
if (count == 0)
{
NSLog(@"PDF needs at least one page");
return;
}
CGRect paperSize = CGRectMake(0.0,0.0,595.28,841.89);
UIGraphicsBeginPDFContextToFile([self getTempPDFFileName], paperSize, nil);
UIGraphicsBeginPDFPageWithInfo(paperSize, nil);
CGContextRef currentContext = UIGraphicsGetCurrentContext();
// flip context so page is right way up
CGContextTranslateCTM(currentContext, 0, paperSize.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0);
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 up
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, -paperSize.size.height);
[@"Example annotation" drawInRect:CGRectMake(100.0, 100.0, 200.0, 40.0) withFont:[UIFont systemFontOfSize:18.0]];
UIGraphicsEndPDFContext();
CGPDFDocumentRelease (document);
}
Xin chào Jack, bạn có chú thích không? –