IOS: creating PDF from UIView

I am trying to create a PDF file from a UIView that can contain images, tags, etc. I checked the link ( GeneratingPDF ) and I also know the renderInContext: method, which can be used to draw a UIView in a PDF context.

I used renderInContext: to create a PDF file. However, the images and text have lost their loyalty. those. Images are not individually selected, and text cannot be selected / copied, etc. in the resulting PDF file. It was as good as a PDF created from a UIView snapshot.

My question is, do I need to individually draw texts and images to achieve what I want (using CGContextShowTextAtPoint and CGContextDrawImage )?

If so, how do I start scanning the UIView for texts and images? Suppose I get a UIView from outside and don’t know its contents.

+3
ios objective-c iphone pdf uiview
source share
3 answers

You really need to draw the texts / images individually in CGContextRef (see CGPDFContextCreateWithURL ) using functions like CGContextShowTextAtPoint . Although you could theoretically scan your UIView for images / shortcuts, it would be better if you just drew the layout from scratch. You should have some way to find out what elements should be in the UIView. Although the code will be completely different, logically you should approach it the same way as if you were going to draw a UIView programmatically, rather than loading it from the bottom.

If you really want to scan your UIView, you can do something like this:

 for(UIView *subview in parentView.subviews) { if([subview isKindOfClass:[UIImageView class]]) { ... } else if([subview isKindOfClass:[UITextView class]]) { ... } else if([subview isKindOfClass:[UILabel class]]) { ... } } 
+1
source share

I had moderate luck, just strolling through my hierarchy of views and calling -drawRect: me. There, it’s a little configured what you do before each, for example, translating ctm and clearing the text matrix, but when you have done this for each view, you can do this:

 UIGraphicsPushContext(ctx); [self drawRect:self.frame]; UIGraphicsPopContext(); 

The PDF that it creates is based on a vector, not just a flat bitmap, so it scales well and you can select text in the viewer / browser. This only works on the usual vanilla graphic design and very simple things like UILabels. I doubt that this will work with more complex UIKit objects (I think of table views and scrolling). In addition, you will need additional work for UIImageViews (which, I assume, sets the layer content property with CGImageRef), and if you have other attributes and transformations at the layer level, even more work.

I hope Apple provides us with a better solution than maintaining parallel drawing code!

+2
source share

to try

 { //read the pdf file CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("iPhoneAppProgrammingGuide.pdf"), NULL, NULL); PDFfile = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); CFRelease(pdfURL) CGPDFPageRef page = CGPDFDocumentGetPage(PDFfile,currentpage); context = UIGraphicsGetCurrentContext(); CGContextSaveGState(context); CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); CGContextFillRect(context,self.bounds); CGContextTranslateCTM(context, -1.0, [self bounds].size.height); CGContextScaleCTM(context, 1.0, -1.0); CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFArtBox, [self bounds], 0, true)); CGContextDrawPDFPage(context, page); CGContextRestoreGState(context); //CGAffineTransform transform = aspectFit(CGPDFPageGetBoxRect(page, kCGPDFMediaBox), CGContextGetClipBoundingBox(context)); // CGContextConcatCTM(context, transform); UIGraphicsBeginImageContext(CGSizeMake(self.bounds.size.width, self.bounds.size.height)); } 
0
source share

All Articles