1

任何人都可以帮我解决用于 A4 尺寸纸张的 UIView For Drawing pdf 的大小。

我使用的是以下尺寸,但我的 pdfView 对象(如标签、图像)不是我为绘图提供的尺寸。

我创建 .xib 并手动绘制 UILabels、图像和线条 然后使用标签属性我提供了用于绘制的框架,但它们根据 .xib 绘制不同的位置我提供了与 .xib 中相同的大小(0、0、792、 1122)。

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 792, 1122), nil);

+(void)drawLabels:(NSMutableArray*)arr isData:(BOOL)isdata
{

    NSArray* objects = [[NSBundle mainBundle] loadNibNamed:@"PDFView" owner:nil options:nil];

    UIView* mainView = [objects objectAtIndex:0];


    if (isdata) 
    {
        for (int i=1;i<=[arr count];i++)
        {
            UILabel *lbl=(UILabel*)[mainView viewWithTag:i];
            [self drawText:[arr objectAtIndex:i-1] inFrame:lbl.frame isData:isdata];
        }
    }
}

//这是我的绘制方法

+(void)drawText:(NSString*)textToDraw inFrame:(CGRect)frameRect isData:(BOOL)isdata
{
    int length=[textToDraw length];
    CFStringRef string = (__bridge CFStringRef) textToDraw; 
    CFMutableAttributedStringRef currentText = CFAttributedStringCreateMutable(kCFAllocatorDefault, 0); 
    CTFontRef font = CTFontCreateWithName((CFStringRef)@"Helvetica Neue Bold", 12.0f, nil);
    if (isdata) 
    {
        font = CTFontCreateWithName((CFStringRef)@"Helvetica Neue ", 12.0f, nil);
    }
    CFAttributedStringReplaceString (currentText,CFRangeMake(0, 0), string);
    CFAttributedStringSetAttribute(currentText,CFRangeMake(0, length),kCTFontAttributeName,font);
    CTFramesetterRef framesetter = CTFramesetterCreateWithAttributedString(currentText);
    CGMutablePathRef framePath = CGPathCreateMutable();
    CGPathAddRect(framePath, NULL, frameRect);
    CFRange currentRange = CFRangeMake(0, 0);
    CTFrameRef frameRef = CTFramesetterCreateFrame(framesetter, currentRange, framePath, NULL);
    CGPathRelease(framePath);
    CGContextRef    currentContext = UIGraphicsGetCurrentContext();
    CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);
    CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
    CGContextScaleCTM(currentContext, 1.0, -1.0);
    CTFrameDraw(frameRef, currentContext);
    CGContextScaleCTM(currentContext, 1.0, -1.0);
    CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);
    CFRelease(frameRef);
    CFRelease(framesetter);
}

// 对于线条图,我使用下面的代码

for (int j=1001;j<=1003;j++)
        {
            UILabel *lbl=(UILabel*)[mainView viewWithTag:j];

            CGPoint lblPoint=CGPointMake(lbl.frame.origin.x, lbl.frame.origin.y);
            CGPoint lblPoint2=CGPointMake(lbl.frame.origin.x+lbl.frame.size.width, lbl.frame.origin.y);
            [self drawLineFromPoint:lblPoint toPoint:lblPoint2];
        }

+(void)drawLineFromPoint:(CGPoint)from toPoint:(CGPoint)to
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 2.0);
    CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
    CGFloat components[] = {0.2, 0.3, 0.2, 0.4};
    CGColorRef color = CGColorCreate(colorspace, components);
    CGContextSetStrokeColorWithColor(context, color);
    CGContextMoveToPoint(context, from.x, from.y);
    CGContextAddLineToPoint(context, to.x, to.y);
    CGContextStrokePath(context);
    CGColorSpaceRelease(colorspace);
    CGColorRelease(color);
}

这是我的 .xib 在此处输入图像描述

这是我绘制后的 .pdf 文件

在此处输入图像描述

4

2 回答 2

3

如果从http://www.raywenderlich.com/6818/how-to-create-a-pdf-with-quartz-2d-in-ios-5-tutorial-part-2下载了项目,似乎该drawText:方法中的 CTM 计算可以得到改进。问题是在修改变换矩阵时没有考虑到矩形的高度。

如果你更换

CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2);
...
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2);

CGContextTranslateCTM(currentContext, 0, frameRect.origin.y*2 + frameRect.size.height);
...
CGContextTranslateCTM(currentContext, 0, (-1)*frameRect.origin.y*2 - frameRect.size.height);

那么结果似乎更准确。

此外,Xcode 静态分析器报告了currentText该方法中的内存泄漏,因此您应该添加

CFRelease(currentText);

添加结尾drawText:

于 2012-09-26T18:17:33.390 回答
1

做这个:

NSArray *aArrPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) ;

NSString *aStrPrintPdfPath = [[aArrPaths objectAtIndex:0] stringByAppendingPathComponent:@"MyDocument.pdf"];
CGContextRef aCgPDFContextRef = [self createPDFContext:CGRectMake(0, 0, 612, 892) path:(__bridge_retained CFStringRef)aStrPrintPdfPath];

CGContextBeginPage(aCgPDFContextRef,nil);
  //turn PDF upsidedown
    CGAffineTransform aCgAffTrans = CGAffineTransformIdentity;
    aCgAffTrans = CGAffineTransformMakeTranslation(0,892);
    aCgAffTrans = CGAffineTransformScale(aCgAffTrans, 1.0, -1.0);
    CGContextConcatCTM(aCgPDFContextRef, aCgAffTrans);

//capture whole view screenshot
[self.view.layer renderInContext:aCgPDFContextRef]; //add QuartzCore framework and import it
    [aPageNote release]; 
    CGContextEndPage (aCgPDFContextRef);

CGContextRelease (aCgPDFContextRef);
NSLog(@"Pdf Successfully Created");

还要添加这个方法:

-(CGContextRef) createPDFContext:(CGRect)aCgRectinMediaBox path:(CFStringRef) aCfStrPath
{

  CGContextRef aCgContextRefNewPDF = NULL;
  CFURLRef aCfurlRefPDF;
  aCfurlRefPDF = CFURLCreateWithFileSystemPath (NULL,aCfStrPath,kCFURLPOSIXPathStyle,false);
  if (aCfurlRefPDF != NULL) {
    aCgContextRefNewPDF = CGPDFContextCreateWithURL (aCfurlRefPDF,&aCgRectinMediaBox,NULL);
    CFRelease(aCfurlRefPDF); 
  }
  return aCgContextRefNewPDF;
}
于 2012-09-25T12:23:08.593 回答