在PDF文件中的内容之后绘制图像

Phi*_*lip 9 pdf objective-c ios

我最近一直在寻找一个很好的PDF教程,文档等等.

我最终使用了这段代码,但问题很少.

脚本

我有一个包含标签,textView和imageView的视图.现在,我们将调用标签name,textView description和imageView image.

该名称作为标题.

描述非常非常可变,它可以是从2行到某些页面.

图像应该放在描述文本的末尾.

我正在使用此代码:

- (void)generatePDF{
    NSString *fileName = [NSString stringWithFormat:@"%@.pdf",nameString];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];

    CFAttributedStringRef currentText = CFAttributedStringCreate(NULL,
                                                                 (CFStringRef)descriptionString, NULL);
    if (currentText) {
        CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
        if (framesetter) {

            // Create the PDF context using the default page: currently constants at the size
            // of 612 x 792.
            UIGraphicsBeginPDFContextToFile(pdfFileName, CGRectZero, nil);

            CFRange currentRange = CFRangeMake(0, 0);
            NSInteger currentPage = 0;
            BOOL done = NO;

            do {
                // Mark the beginning of a new page.
                UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, kDefaultPageWidth,
                                                          kDefaultPageHeight), nil);
                [self drawHeader]
                // Draw a page number at the bottom of each page
                currentPage++;
                [self drawPageNumber:currentPage];

                // Render the current page and update the current range to
                // point to the beginning of the next page.
                currentRange = [self renderPage:currentPage withTextRange:
                                currentRange andFramesetter:framesetter];

                // If we're at the end of the text, exit the loop.
                if (currentRange.location == CFAttributedStringGetLength
                    ((CFAttributedStringRef)currentText))
                    done = YES;
            } while (!done);

            // Close the PDF context and write the contents out.
            UIGraphicsEndPDFContext();

            // Release the framewetter.
            CFRelease(framesetter);

        } else {
            NSLog(@"Could not create the framesetter needed to lay out the atrributed string.");
        }
        // Release the attributed string.
        CFRelease(currentText);
    } else {
        NSLog(@"Could not create the attributed string for the framesetter");
    }

}

- (CFRange)renderPage:(NSInteger)pageNum withTextRange:(CFRange)currentRange
       andFramesetter:(CTFramesetterRef)framesetter
{
    // Get the graphics context.
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();

    // Put the text matrix into a known state. This ensures
    // that no old scaling factors are left in place.
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

    // Create a path object to enclose the text. Use 72 point
    // margins all around the text.
    CGRect    frameRect = CGRectMake(22,72, 468, 648);
    CGMutablePathRef framePath = CGPathCreateMutable();
    CGPathAddRect(framePath, NULL, frameRect);

    // Get the frame that will do the rendering.
    // The currentRange variable specifies only the starting point. The framesetter
    // lays out as much text as will fit into the frame.
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
    CGPathRelease(framePath);

    // Core Text draws from the bottom-left corner up, so flip
    // the current transform prior to drawing.
    CGContextTranslateCTM(currentContext, 0, kDefaultPageHeight);
    CGContextScaleCTM(currentContext, 1.0, -1.0);

    // Draw the frame.
    CTFrameDraw(frameRef, currentContext);

    // Update the current range based on what was drawn.
    currentRange = CTFrameGetVisibleStringRange(frameRef);
    currentRange.location += currentRange.length;
    currentRange.length = 0;
    CFRelease(frameRef);

    return currentRange;
}


- (void)drawPageNumber:(NSInteger)pageNum
{
    NSString* pageString = [NSString stringWithFormat:NSLocalizedString(@"Page", nil), pageNum];
    UIFont* theFont = [UIFont systemFontOfSize:12];
    CGSize maxSize = CGSizeMake(kDefaultPageWidth, 72);

    CGSize pageStringSize = [pageString sizeWithFont:theFont
                                   constrainedToSize:maxSize
                                       lineBreakMode:UILineBreakModeClip];
    CGRect stringRect = CGRectMake(((kDefaultPageWidth - pageStringSize.width) / 2.0),
                                   720.0 + ((72.0 - pageStringSize.height) / 2.0) ,
                                   pageStringSize.width,
                                   pageStringSize.height);

    [pageString drawInRect:stringRect withFont:theFont];
}
Run Code Online (Sandbox Code Playgroud)

我想知道如何在描述结束后立即在页面的末尾绘制图像.

我用这种方式绘制了标题:

-(void)drawHeader{
    NSString *headerString = nameString;
    UIFont* theFont = [UIFont boldSystemFontOfSize:15];
    CGSize maxSize = CGSizeMake(kDefaultPageWidth, 72);

    CGSize pageStringSize = [headerString sizeWithFont:theFont constrainedToSize:maxSize lineBreakMode:UILineBreakModeClip];
    CGRect stringRect = CGRectMake(22,22,pageStringSize.width,pageStringSize.height);

    [headerString drawInRect:stringRect withFont:theFont];
}
Run Code Online (Sandbox Code Playgroud)

并且它显示在每个页面的开头.

现在我不知道如何在内容之后绘制图像(描述)!

任何帮助高度赞赏!

Lit*_*T.V 5

在上下文中绘制 pdf 就像在画布中绘制一样。

绘制本质上是动态的文本和图像的最佳方法是创建和使用绘制方法,该方法返回用于在页面中绘制的高度,并简单地保留一个变量以通过返回的递增来计算下一个绘制起点 y 位置值。还放了一个单独的偏移变量来检查页框。

要将图像绘制到 pdf 上下文中,您可以使用以下示例。您也可以CGImage通过 Core Graphics 进行渲染,但您必须考虑画布(CGContext)的旋转,因为这些将自坐标以来以颠倒的方式出现(翻转) Core Graphics 中的原点位于右下角。

UIImage *gymLogo=[UIImage imageNamed:@"logo.png"];
CGPoint drawingLogoOrigin = CGPointMake(5,5);
[gymLogo drawAtPoint:drawingLogoOrigin];
Run Code Online (Sandbox Code Playgroud)

快乐编码:)