0

我有一个非常奇怪和特殊的情况,我想在屏幕上显示 PDF。但是,问题是我的文档显示模糊的线条,而不是在 Acrobat Pro 程序中显示。

看这张图片:

http://imgur.com/ws4Qi

这是我可以用 CGContext 修复(模糊的线条)还是分辨率问题。这是我的代码:

- (id)initWithFrame:(CGRect)frame {

self = [super initWithFrame:(CGRect)frame];
if (self) {
    self.backgroundColor = [UIColor clearColor];
    NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"];
    NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
    document = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);
    currentPage = 1;

}

return self;
}

+ (void) renderPage: (CGPDFPageRef) page inContext: (CGContextRef) context{
[viewPDF renderPage:page inContext:context atPoint:CGPointMake(0, 0)];
}

+ (void) renderPage: (CGPDFPageRef) page inContext: (CGContextRef) context atPoint:(CGPoint) point{
[viewPDF renderPage:page inContext:context atPoint:point withZoom:100];
}

+ (void) renderPage: (CGPDFPageRef) page inContext: (CGContextRef) context atPoint: (CGPoint) point withZoom: (float) zoom{

CGRect cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
int rotate = CGPDFPageGetRotationAngle(page);

CGContextSaveGState(context);

// Setup the coordinate system.
// Top left corner of the displayed page must be located at the point specified by the 'point' parameter.
CGContextTranslateCTM(context, point.x, point.y);

// Scale the page to desired zoom level.
CGContextScaleCTM(context, zoom / 100, zoom / 100);

// The coordinate system must be set to match the PDF coordinate system.
switch (rotate) {
    case 0:
        CGContextTranslateCTM(context, 0, cropBox.size.height);
        CGContextScaleCTM(context, 1, -1);
        break;
    case 90:
        CGContextScaleCTM(context, 1, -1);
        CGContextRotateCTM(context, -M_PI / 2);
        break;
    case 180:
    case -180:
        CGContextScaleCTM(context, 1, -1);
        CGContextTranslateCTM(context, cropBox.size.width, 0);
        CGContextRotateCTM(context, M_PI);
        break;
    case 270:
    case -90:
        CGContextTranslateCTM(context, cropBox.size.height, cropBox.size.width);
        CGContextRotateCTM(context, M_PI / 2);
        CGContextScaleCTM(context, -1, 1);
        break;
}

// The CropBox defines the page visible area, clip everything outside it.
CGRect clipRect = CGRectMake(0, 0, cropBox.size.width, cropBox.size.height);
CGContextAddRect(context, clipRect);
CGContextClip(context);

CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0);


CGContextSetLineJoin(context, kCGLineJoinRound);

CGContextFillRect(context, clipRect);

CGContextTranslateCTM(context, -cropBox.origin.x, -cropBox.origin.y);

CGContextDrawPDFPage(context, page);

CGContextRestoreGState(context);


}

 + (void) renderPage: (CGPDFPageRef) page inContext: (CGContextRef) context inRectangle: (CGRect) displayRectangle {
if ((displayRectangle.size.width == 0) || (displayRectangle.size.height == 0)) {
    return;
}

CGRect cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
int pageRotation = CGPDFPageGetRotationAngle(page);

CGSize pageVisibleSize = CGSizeMake(cropBox.size.width, cropBox.size.height);
if ((pageRotation == 90) || (pageRotation == 270) ||(pageRotation == -90)) {
    pageVisibleSize = CGSizeMake(cropBox.size.height, cropBox.size.width);
}

float scaleX = displayRectangle.size.width / pageVisibleSize.width;
float scaleY = displayRectangle.size.height / pageVisibleSize.height;
float scale = scaleX < scaleY ? scaleX : scaleY;

// Offset relative to top left corner of rectangle where the page will be displayed
float offsetX = 0;
float offsetY = 0;

float rectangleAspectRatio = displayRectangle.size.width / displayRectangle.size.height;
float pageAspectRatio = pageVisibleSize.width / pageVisibleSize.height;

if (pageAspectRatio < rectangleAspectRatio) {
    // The page is narrower than the rectangle, we place it at center on the horizontal
    offsetX = (displayRectangle.size.width - pageVisibleSize.width * scale) / 2;
}
else { 
    // The page is wider than the rectangle, we place it at center on the vertical
    offsetY = (displayRectangle.size.height - pageVisibleSize.height * scale) / 2;
}

CGPoint topLeftPage = CGPointMake(displayRectangle.origin.x + offsetX, displayRectangle.origin.y + offsetY);

[viewPDF renderPage:page inContext:context atPoint:topLeftPage withZoom:scale * 100];
}



-(void)drawRect:(CGRect)inRect{                 
if(document) 

{
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGPDFPageRef page = CGPDFDocumentGetPage(document, currentPage);
    [viewPDF renderPage:page inContext:ctx  atPoint:CGPointMake(10,10) withZoom:125];


}
4

1 回答 1

0

好的。该问题已通过 PDF 的源文件 Finale 解决。2012 版现在可以导出为 PDF 并按原样导出行。代码留在这里,以帮助任何想要在所有屏幕上显示 PDF 的人

于 2012-08-01T14:48:12.533 回答