【问题标题】:What is the best way to display a single-paged pdf as an image?将单页 pdf 显示为图像的最佳方法是什么?
【发布时间】:2014-05-16 14:51:32
【问题描述】:

我想在NSView 中显示单页 PDF。

到目前为止,我有两种解决方案,但它们都有缺点。任何人都可以帮助我解决这些缺点吗?

第一个解决方案:使用 NSImage 和 NSImageView

   NSString *path= [[NSBundle mainBundle] pathForResource:name ofType:@"pdf"];
   NSImage * image = [[NSImage alloc] initWithContentsOfFile:path] ;

   NSImageView * imageView = [[NSImageView alloc] init] ;
   imageView.frame = NSMakeRect(0, 0, 2*image.size.width, 2*image.size.height) ;
   imageView.image = image ;
   imageView.imageScaling = NSImageScaleAxesIndependently ;

   return imageView

缺点

  • 图像未消除锯齿
  • 我不明白为什么需要2 因子。为什么我的 PDF 在 NSView 中显示的比在 Finder 中显示的要小?

第二种解决方案:使用 PDFDocument 和 PDFView

    NSString *path= [[NSBundle mainBundle] pathForResource:name ofType:@"pdf"];
    NSURL *urlPDF = [NSURL fileURLWithPath:path] ;

    PDFDocument * myPDFDocument = [[PDFDocument alloc] initWithURL:urlPDF] ;

    PDFView *myPDFView = [[PDFView alloc] init] ;
    myPDFView.document = myPDFDocument ;

    PDFPage * firstPage = [myPDFDocument pageAtIndex:0] ;
    NSRect myBounds = [firstPage boundsForBox:kPDFDisplayBoxMediaBox] ;
    NSRect myNewBounds = NSMakeRect(0, 0, myBounds.size.width*2, myBounds.size.height*2+5) ;

    myPDFView.frame = myNewBounds ;
    myPDFView.autoScales = YES ;

    return myPDFView ;

缺点

  • 我可以选择我的 pdf 的文本,我可以放大或缩小。但我希望我的 PDF 文档显示为图像,没有这些可能性
  • 我不明白为什么需要2 因子。为什么我的 PDF 在 NSView 中显示的比在 Finder 中显示的要小?
  • 我的图片周围有一些边距

【问题讨论】:

    标签: cocoa pdf nsview nsimage pdfview


    【解决方案1】:

    我没有看到你用 NSImageView 描述的问题。我实现了一个基于 nib 的窗口和 NSImageView。就我而言,我有一个重叠的兄弟视图,所以我在笔尖中打开了 CALayers。我在 10.9.2。大小是正常的(1x),我的 PDF 中的文本是抗锯齿的(我认为是亚像素,因为当我放大它时我会看到颜色)。我确实有缩放 NONE - 也许缩放会阻止抗锯齿文本?

    否则我的猜测是您的视图或 PDF 内容有所不同。尝试更简单的 PDF 和/或基于 nib 的视图,如果可行,您可以寻找差异。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-29
      • 1970-01-01
      • 2011-11-13
      • 2011-12-24
      • 1970-01-01
      • 1970-01-01
      • 2018-08-20
      相关资源
      最近更新 更多