如何使用PDFKit在pdf页面上绘图

时间:2018-06-13 11:54:04

标签: ios swift pdfkit

我目前正在开展一个项目,我需要根据用户输入的大量信息生成PDF。

我正在使用PDFKit,我设法创建了一个PDFView并向其添加了PDF文档。我的问题是我无法真正找到使用PDFKit在文档页面上绘图的方法。我不想添加注释,我想在该表的单元格中绘制表格和文本。

我已经找到了一些例子,但是所有这些都不完整,你需要有一些知识来真正理解它们。我也发现了一些使用Quartz和Core Graphics的例子,但我不知道是否可以将它应用于PDFKit。

我只需要一个使用PDFKit绘制线条的例子。

感谢。

1 个答案:

答案 0 :(得分:1)

  1. 您只需要创建pdfData:

    -(NSMutableData *)createPDFData {
     // Creates a mutable data object for updating with binary data, like a byte   array
        NSMutableData *pdfData = [NSMutableData data];
    
    // Points the pdf converter to the mutable data object and to the UIView to be converted
    CGRect bounds = self.bounds;
    
    UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
    UIGraphicsBeginPDFPageWithInfo(bounds, nil);
    
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    
    [self drawBottomRect];
    
    [self drawImageView:self.ivBackground];
    
    // draw labels by section
    
    [self drawLabel:self.walletName];
    
    currentContext = UIGraphicsGetCurrentContext();
    
    // remove PDF rendering context
    UIGraphicsEndPDFContext();
    return pdfData;
    }
    
  2. 如何绘制图像视图的示例:

     -(void) drawImageView: (UIImageView*) imageView
     {
      CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    
      CGRect frameRect =  [imageView convertRect:imageView.bounds  toView:self];
    
      CGContextTranslateCTM(currentContext, frameRect.origin.x,frameRect.origin.y);
      [imageView.layer renderInContext:currentContext];
       CGContextTranslateCTM(currentContext, -frameRect.origin.x,-   frameRect.origin.y);
     }
    
  3. 将PDF数据另存为文件。