什么尺寸的UiView用于绘制A4尺寸纸张的pdf文件?

时间:2012-09-25 12:01:25

标签: iphone ios5 cocos2d-iphone pdf-generation ios6

任何人都可以帮我找出用于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 enter image description here

这是绘制后的.pdf文件

enter image description here

2 个答案:

答案 0 :(得分:3)

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

如果您更换

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:的结尾。

答案 1 :(得分: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;
}