【问题标题】:Contex Drawing + Pagination上下文绘图+分页
【发布时间】:2011-11-28 06:16:00
【问题描述】:

我正在尝试将 scrollview 的内容绘制到 PDF 上下文中,我遇到了 pagination 的问题。

我使用的以下代码:

- (void)renderTheView:(UIView *)view inPDFContext:(CGContextRef)pdfContext
{
    // Creating frame.
    CGFloat heightOfPdf = [[[self attributes] objectForKey:SRCPdfHeight] floatValue];
    CGFloat widthOfPdf  = [[[self attributes] objectForKey:SRCPdfWidth] floatValue];
    CGRect pdfFrame = CGRectMake(0, 0, widthOfPdf, heightOfPdf);   
    CGRect viewFrame = [view frame];

    if ([view isKindOfClass:[UIScrollView class]])
    {
        viewFrame.size.height = ((UIScrollView *)view).contentSize.height;
        [view setFrame:viewFrame];
    }
    // Calculates number of pages.
    NSUInteger totalNumberOfPages = ceil(viewFrame.size.height/heightOfPdf);

    // Start rendering.
    for (NSUInteger pageNumber = 0; pageNumber<totalNumberOfPages; pageNumber++)
    {
       // Starts our first page.
       CGContextBeginPage (pdfContext, &pdfFrame);  
       // Turn PDF upsidedown
       CGAffineTransform transform = CGAffineTransformIdentity;
       transform = CGAffineTransformMakeTranslation(0,view.bounds.size.height);
       transform = CGAffineTransformScale(transform, 1.0, -1.0);
       CGContextConcatCTM(pdfContext, transform);

       // Calculate amount of y to be displace.
       CGFloat ty = (heightOfPdf*(pageNumber));

       CGContextTranslateCTM(pdfContext,0,-ty);
       [view.layer renderInContext:pdfContext];

       // We are done drawing to this page, let's end it.
       CGContextEndPage (pdfContext);
   }    
}

它创建了所需数量的页面,但错误地放置了内容。下图说明。

我的代码有什么问题吗?

【问题讨论】:

  • 好奇如果您将子视图从 ScrollView 中取出,事情是如何工作的?
  • 您需要添加到 pdf 页面的部分放在滚动内的单独视图中,并将视图呈现到 pdf 上下文
  • 假设一个答案解决了这个问题或者您找到了解决方案,您可以接受或输入它以关闭未解决的问题吗?

标签: iphone ios pdf quartz-2d cgcontext


【解决方案1】:

我认为您不需要翻转 PDF 的代码。我以前做过(手动分页,但没有滚动视图),并且从来不需要垂直翻转上下文。我主要担心的是你在每次迭代中都这样做——如果你有充分的理由翻转它,你可能在循环中不需要它,但在循环开始之前只需要一次。此外,代码CGContextTranslateCTM(pdfContext,0,-ty); 可能需要替换为CGContextTranslateCTM(pdfContext,0,heightOfPdf);

如果这不起作用,请尝试 UIGraphicsBeginPDFPage(); 而不是 CGContextBeginPage(),这是您的代码和我的代码之间的唯一主要区别。

【讨论】:

    【解决方案2】:

    Thisgithub 项目绝对可以帮到你。

    - (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx {
    CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index + 1);
    CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),
                                            CGContextGetClipBoundingBox(ctx));
    CGContextConcatCTM(ctx, transform);
    CGContextDrawPDFPage(ctx, page);
    

    }

    【讨论】:

    • 在添加到上下文时是否可以更改 pdf 的字体和字体大小?...或者有没有其他方法可以更改 pdf 中的字体或字体大小?..
    【解决方案3】:

    找到这个链接来呈现 PDF,它说的是多个页面,但没有显示实现。它不会立即回答您的问题,但它提供了一种更简单的方法来呈现 pdf 且翻译和转换更少。

    Render PDF

    Generate Multipage PDF -anoop

    【讨论】:

      【解决方案4】:

      您可以考虑使用UIScrollView 来布置单独的 PDF 页面。以下方法将每个 PDF 页面加载到视图 (PDFView) 中,并以居中的方式将其添加到滚动视图内容中:

      - (void) loadPDFAtPath:(NSString *)path
      {
          NSURL *pdfUrl = [NSURL fileURLWithPath:path];
          CGPDFDocumentRef document = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);
      
          float height = 0.0;
      
          for(int i = 0; i < CGPDFDocumentGetNumberOfPages(document); i++) {
              CGPDFPageRef page = CGPDFDocumentGetPage(document, i + 1);
              PDFView *pdfView = [[PDFView alloc] initPdfViewWithPageRef:page];
      
              CGRect pageSize = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
              pv.frame = CGRectMake((self.frame.size.width / 2.0) - pageSize.size.width / 2.0, height, pageSize.size.width, pageSize.size.height);
              height += pageSize.size.height + SOME_SPACE_IN_BETWEEN_PAGES;
      
              // self is a subclass of UIScrollView
              [self addSubview:pdfView];
              [pdfView setNeedsDisplay];
          }
      
          CGPDFDocumentRelease(document);
          [self setContentSize:CGSizeMake(self.frame.size.width, height)];
      }
      

      在这种情况下,PDFView 负责在其 drawRect 或 drawLayer 实现中呈现单个 CGPDFPageRef

      【讨论】:

        【解决方案5】:

        您需要添加到 pdf 页面的部分放在滚动内的单独视图中,并将视图呈现到 pdf 上下文

        只需找到正确的尺寸并使用 CG 方法绘制:

                UIImage *Logo=[UIImage imageNamed:@"small-logo-left-top130_133.png"];
            CGPoint drawingLogoOrgin = CGPointMake(5,5);
        
            UIGraphicsBeginPDFContextToData(pdfData, CGRectZero, nil);
            UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);
            CGContextRef pdfContext = UIGraphicsGetCurrentContext();
            [Logo drawAtPoint:drawingLogoOrgin];
        

        画pdf有很多画法。您可以使用它来绘制所有内容。

        【讨论】:

          【解决方案6】:

          这可能会对你有所帮助。

          - (void) renderPageAtIndex:(NSUInteger)index inContext:(CGContextRef)ctx 
          {
          CGPDFPageRef page = CGPDFDocumentGetPage(pdf, index + 1);
          CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox),
                                              CGContextGetClipBoundingBox(ctx));
          CGContextConcatCTM(ctx, transform);
          CGContextDrawPDFPage(ctx, page);
          } 
          

          【讨论】:

          • 和 Rakesh 的评论有什么不同?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-30
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-05-04
          相关资源
          最近更新 更多