【问题标题】:Get graphics context after saving in PDF - Objective C保存为 PDF 后获取图形上下文 - Objective C
【发布时间】:2014-12-05 04:02:13
【问题描述】:

保存后如何从PDF中获取核心图形上下文?例如,我在 PDF 页面内创建一个矩形,保存后我需要在 PDF 页面内编辑、更新或删除该矩形。

这是我用来在目标 c 中保存 PDF 页面内的矩形的代码。

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)[[NSBundle mainBundle] URLForResource:@"tutorial" withExtension:@"pdf"]);

const size_t numberOfPages = CGPDFDocumentGetNumberOfPages(pdf);

NSMutableData* data = [NSMutableData data];
UIGraphicsBeginPDFContextToData(data, CGRectZero, nil);

for(size_t page = 1; page <= numberOfPages; page++)
{
    //  Get the current page and page frame
    CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf, page);
    const CGRect pageFrame = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);

    UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);

    //  Draw the page (flipped)
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGContextSaveGState(ctx);
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
    CGContextDrawPDFPage(ctx, pdfPage);
    CGContextRestoreGState(ctx);

    //  Draw a red box
    [[UIColor redColor] set];
    UIRectFill(CGRectMake(20, 20, 100, 100));
}

UIGraphicsEndPDFContext();

CGPDFDocumentRelease(pdf);
pdf = nil;

现在我需要编辑或删除上面创建的矩形。请指教!!!

【问题讨论】:

    标签: objective-c annotations pdf-generation core-graphics quartz-2d


    【解决方案1】:

    无法使用 CoreGraphics API 从 PDF 文件中删除内容。
    您需要根据某些条件重新设计代码以在 PDF 文件中绘制或不绘制矩形。

    【讨论】:

    • 您能否通过任何示例或示例代码就如何实现这一目标提供一些建议?谢谢 :) 我看到你的博客真是太棒了:)
    • 将“绘制红框”代码放在“if”中,因为我认为这是您要添加/删除的矩形。 'if' 语句的条件完全取决于你的应用程序,你知道什么时候应该绘制矩形,什么时候不应该绘制。
    猜你喜欢
    • 2012-02-24
    • 1970-01-01
    • 2013-12-28
    • 2016-12-20
    • 1970-01-01
    • 2018-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多