可可PDF页面拆分 [英] Cocoa PDF page splitting
问题描述
在我创建的应用程序中,我将一长段HTML加载到webView中,然后使用以下命令将其打印为PDF:
In the application I'm creating, I load a long page of HTML into a webView and then print it to a PDF using the following:
-(void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
if ([frame isEqual:[[self doc] mainFrame]])
{
NSMutableData *newData = [[NSMutableData alloc] init];
NSPrintInfo *newInfo = [NSPrintInfo sharedPrintInfo];
NSView *docView = [[[[self doc] mainFrame] frameView] documentView];
NSPrintOperation *newPrintOp = [NSPrintOperation PDFOperationWithView:docView insideRect:docView.bounds toData:newData printInfo:newInfo];
BOOL runPrint = [newPrintOp runOperation];
if (!runPrint)
{
NSLog(@"Print Failed");
}
PDFDocument *newDoc = [[PDFDocument alloc] initWithData:newData];
[newData release];
[self setPdf:newDoc];
//Other code here
}
}
问题是当我查看newDoc
时,它是单页的巨大PDF.我希望打印的行为与另存为PDF ..."对话框中的打印行为相同-也就是说,将PDF分成多个合理大小的页面.
The problem is that when I look at newDoc
, it is a huge PDF of a single page. What I would prefer would be the printing acting the same as it does from the "save as PDF..." dialog - that is, splitting the PDF into multiple reasonably-sized pages.
有人知道如何做到这一点吗?
Does anyone know how to accomplish this?
我尝试在NSPrintInfo *newInfo = [NSPrintInfo sharedPrintInfo];
[newInfo setVerticalPagination:NSAutoPagination];
[newInfo setHorizontalPagination:NSAutoPagination];
NSAutoPagination在文档中的描述如下:
NSAutoPagination is described in the docs as the following:
NSAutoPagination 图像被分成大小相等的矩形,并放置在页面的一列中. 在Mac OS X v10.0和更高版本中可用. 在NSPrintInfo.h中声明.
NSAutoPagination The image is divided into equal-sized rectangles and placed in one column of pages. Available in Mac OS X v10.0 and later. Declared in NSPrintInfo.h.
这对打印的PDF没有影响.
This had no effect on the printed PDF.
推荐答案
您将获得一个大页面的文件,因为+ PDFOperationWithView:
方法根本不支持分页.因此,调用- setVerticalPagination:
或- setHoriziontalPagination:
不会改变任何内容.
You get a file with one large page because + PDFOperationWithView:
method doesn't support pagination at all. For that reason calling - setVerticalPagination:
or - setHoriziontalPagination:
doesn't change anything.
您可以尝试使用经典" + printOperationWithView:printInfo:
方法,将其配置为将PDF保存到临时位置,然后使用获取的文件内容创建PDFDocument
.我希望下面的代码片段会有所帮助.
You could try use "classical" + printOperationWithView:printInfo:
method, configure it to save PDF to temporary location and then create PDFDocument
with contents of obtained file. I hope that fragment of code below will help.
NSMutableDictionary *dict = [[NSPrintInfo sharedPrintInfo] dictionary];
[dict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
[dict setObject:temporaryFilePath forKey:NSPrintSavePath];
NSPrintInfo *pi = [[NSPrintInfo alloc] initWithDictionary:dict];
[pi setHorizontalPagination:NSAutoPagination];
[pi setVerticalPagination:NSAutoPagination];
NSPrintOperation *op = [NSPrintOperation printOperationWithView:[[[webView mainFrame] frameView] documentView] printInfo:pi];
[pi release];
[op setShowsPrintPanel:NO];
[op setShowsProgressPanel:NO];
if ([op runOperation] ){
PDFDocument *doc = [[[PDFDocument alloc] initWithURL:[NSURL fileURLWithPath: temporaryFilePath]] autorelease];
// do with doc what you want, remove file, etc.
}
这篇关于可可PDF页面拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!