【问题标题】:How to draw on pdf page with PDFKit如何使用 PDFKit 在 pdf 页面上绘图
【发布时间】:2018-11-22 23:59:40
【问题描述】:

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

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

我找到了一些例子来做到这一点,但它们都不完整,你需要有一些知识才能真正理解它们。我还找到了一些使用 Quartz 和 Core Graphics 的示例,但我不知道是否可以将其应用于 PDFKit。

我只需要一个使用 PDFKit 画线的例子。

谢谢。

【问题讨论】:

    标签: ios swift pdfkit


    【解决方案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 数据另存为文件。

    【讨论】:

      猜你喜欢
      • 2022-08-23
      • 2021-05-05
      • 2015-06-25
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-28
      相关资源
      最近更新 更多