2

所以我在这里尝试了几条路线,但似乎总是需要权衡取舍。

我拥有的是一个垂直长的 PDF(包含在 的子类中NSDocument),例如,我将使用 a16z 的屏幕截图。这是我当前的打印结果,然后选择“保存为 PDF”选项。

https://www.dropbox.com/s/02u3j8pbw1kne91/Untitled.pdf?dl=0

这有两个问题:
1)图像似乎垂直拉伸了一点点
2)当我实际进行物理打印时,它只抓住了左下角,而且也只有部分pdf: 在此处输入图像描述

所以这里是相关的代码(至少我相信,其他层可能还有更多,但我大部分时间都在这里玩):

   EditorPrintView *offscreenView = [[EditorPrintView alloc] initWithFrame:[self canvasRect] withImageDoc:self withBackgroundColor:[self backgroundColor]];

   NSPrintInfo *printInfo = [self printInfo];
   [printInfo setVerticalPagination:NSAutoPagination];
   [printInfo setPaperSize:NSMakeSize[self canvasRect].size.width , ([self canvasRect].size.height+[[self printInfo] topMargin] + [[self printInfo] bottomMargin]))];
   [self setPrintInfo:printInfo];

   NSPrintOperation *op = [NSPrintOperation printOperationWithView:offscreenView
                                                         printInfo:printInfo];
   [op runOperationModalForWindow:[[EditorWindowController sharedWindowController] window]
                                 delegate:self
                         didRunSelector:@selector(printOperationDidRun:success:contextInfo:)
                             contextInfo:nil];

现在这就是事情变得时髦的地方。

我一直在对paperSizewith进行更改NSMakeSize[self canvasRect].size.width , ([self canvasRect].size.height+[[self printInfo] topMargin] + [[self printInfo] bottomMargin]))以产生不同的打印结果。

这是我的实验:
1)如果我根本不设置paperSize,pdf实际上是正确分页的: 在此处输入图像描述
页数是正确的,但是,内容不正确。每一页都将整个图像压缩成一页。
2)如果我将 设置paperSize为图像的大小,则会生成 2 页。一页大小正确(页面大小很大) 在此处输入图像描述
:,然后另一页将整个图像压缩成页面边缘的垂直大小:
在此处输入图像描述

3)很自然,我决定使用我现在拥有的代码,并在设置时添加边距paperSize

这导致我最初链接的一页pdf - 但同样,它似乎被垂直拉伸,并且实际上也没有正确打印

知道了这一切,我相信我不想乱了,paperSize所以我可以保持正确的分页,并保持正常的纸张大小。

我相信也许我对我提供的东西做错了NSView。那些有敏锐眼光的人会看到我有一个子类,NSView叫做EditorPrintView. 这是-drawRect方法:

- (void)drawRect:(NSRect)rect 
{
   if ( (_editorDoc) && (_backgroundColor) )
   {
      NSGraphicsContext* theContext = [NSGraphicsContext currentContext];
      if ( [theContext isDrawingToScreen] == NO ) 
      {
         [theContext saveGraphicsState];

         ImageDocumentRenderer* renderer = [ImageDocumentRenderer pixelRenderForDocumentData:_editorDoc.docData andDownsample:NO];
         CGLayerRef cgLayer = [renderer createPrinterFriendlyCGLayerWithContext:(CGContextRef)[theContext graphicsPort] andBackgroundColor:_backgroundColor];

         CGContextDrawLayerInRect((CGContextRef)[theContext graphicsPort], rect, cgLayer);
         [theContext restoreGraphicsState];
         CGLayerRelease(cgLayer);
      }
      else
      {
         NSLog(@"EditorPrintView drawRect - called for drawing onpo [ screen, should only be used for printing");
      }
   }
   else
   {
      NSLog(@"EditorPrintView drawRect - editordocument and/or background color never set, NOT DRAWING ANYTHING");
   }
}

这是真正的墙出现的地方,因为我不知道如何调试 CGContext,而且最初的工程师已经离开了公司,所以我不能问任何人。任何人都可以看到我如何绘制我的任何危险信号NSView吗?

我知道这是一篇很长的文章,所以提前为你的阅读时间欢呼:)

4

0 回答 0