How can I display full screen PDF on iPad?

I have attached a piece of code that I use to display a PDF. The following code displays the PDF, but it seems to be either compressed or not using the full screen size of the iPad, making the page too small.

How can I display a PDF file that is on the edge of the iPad screen or in an enlarged state? I tried to use a different approach (approach-2), but this creates a problem with the appearance of a PDF file at an angle of 90 degrees.

Approach-1:

CGContextSaveGState(ctx); CGContextTranslateCTM(ctx, 0.0, [self.view bounds].size.height); CGContextScaleCTM(ctx, 1.0, -1.0); CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, [self.view bounds], 0, true)); CGContextDrawPDFPage(ctx, page); CGContextRestoreGState(ctx); 

Approach-2:

 CGPDFPageRef page = CGPDFDocumentGetPage(pdfdocument, PageNo+1); if(page){ CFRetain(page); } CGRect pageRect =CGPDFPageGetBoxRect(page, kCGPDFMediaBox); int angle= CGPDFPageGetRotationAngle(page); float pdfScale = self.bounds.size.width/pageRect.size.width; CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); CGContextFillRect(context,self.bounds); CGContextSaveGState(context); // Flip the context so that the PDF page is rendered // right side up. CGContextTranslateCTM(context, 0.0, self.bounds.size.height); CGContextScaleCTM(context, 1.0, -1.0); // Scale the context so that the PDF page is rendered // at the correct size for the zoom level. CGContextScaleCTM(context, pdfScale,pdfScale); CGContextDrawPDFPage(context, page); CGContextRestoreGState(context); 

Can someone suggest me a solution that allows any PDF file of any size and any angle to be displayed in full screen on the iPad in both orientations? It would be great if you provided me with a code snippet or pseudo code. Thanks

+4
source share
2 answers

Hope this helps. It shows how to display the first PDF page that the URL links to.

This code is a collection of fragments of my own code base, so don't just copy them into 1 file and expect it to be created and launched. I added a few comments so that you understand what belongs, and you will need to declare some ivars for it to work.

 // helper function CGRect CGRectScaleAspectFit(CGRect sourceRect,CGRect fitRect) { if ( sourceRect.size.width > fitRect.size.width) { float scale = fitRect.size.width / sourceRect.size.width; sourceRect.size.width = fitRect.size.width; sourceRect.size.height = (int)(sourceRect.size.height * scale); } if ( sourceRect.size.height > fitRect.size.height) { float scale = fitRect.size.height / sourceRect.size.height; sourceRect.size.height = fitRect.size.height; sourceRect.size.width = (int)(sourceRect.size.width * scale); } return sourceRect; } // in your UIView subclass init method CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL); pdfPage = CGPDFDocumentGetPage(pdf, 1); CGRect fitrect = CGRectMake(0, 0, self.frame.size.width,self.frame.size.height); CGRect pageRect = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox); CGRect f; f.origin.x=0; f.origin.y=0; f.size.height = self.frame.size.height; f.size.width = self.frame.size.height * pageRect.size.width/pageRect.size.height; f = CGRectScaleAspectFit(f,fitrect); // this is the actual pdf frame rectangle to fill the screen as much as possible pdfScale = f.size.height/pageRect.size.height; // in your UIView subclass drawRect method CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); CGContextFillRect(context,self.bounds); CGContextSaveGState(context); CGContextTranslateCTM(context, 0.0, self.bounds.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGContextScaleCTM(context, pdfScale,pdfScale); CGContextDrawPDFPage(context, pdfPage); CGContextRestoreGState(context); 
+1
source

This will display your complete PDF file: not sure if this is the best way to handle PDF, but

 - (void)drawRect:(CGRect)rect { CGContextRef ctx = UIGraphicsGetCurrentContext(); //PDF might be transparent, assume white paper - set White Background [[UIColor whiteColor] set]; CGContextFillRect(ctx, rect); //Flip coordinates CGContextGetCTM(ctx); CGContextScaleCTM(ctx, 1, -1); CGContextTranslateCTM(ctx, 0, -rect.size.height); //PDF File Path NSURL *pdfURL = [[NSBundle mainBundle] URLForResource:@"TEST" withExtension:@"pdf"]; CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfURL); CGPDFPageRef page1 = CGPDFDocumentGetPage(pdf, 1); //Get the rectangle of the cropped inside CGRect mediaRect = CGPDFPageGetBoxRect(page1, kCGPDFCropBox); CGContextScaleCTM(ctx, rect.size.width / mediaRect.size.width, rect.size.height / mediaRect.size.height); //Draw PDF CGContextDrawPDFPage(ctx, page1); CGPDFDocumentRelease(pdf); } 

Copy the PDF file you want to display into your project library, in the example above the PDF file name is "TEST": you want to name your file name with your file name

This allows me to display PDF in full screen in a UIView,

I'm not sure if this is your best option: there are problems: for example, you need to handle scaling, and if you do, you need to handle panning. Also Orientation is a big mess (when you turn the device into landscape mode), the file loses its Aspect Ration (and gets spread)

Play a round with him.

Although switching to PDF processing on iOS is nothing but a pain.,.

Hope this helps you a little

0
source

Source: https://habr.com/ru/post/1333462/


All Articles