微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – Cocoa PDF页面拆分

在我正在创建的应用程序中,我将一长页HTML加载到webView中,然后使用以下内容将其打印为PDF:

-(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拆分为多个合理大小的页面.

有谁知道怎么做到这一点?

我试图在nsprintInfo * newInfo = [nsprintInfo sharedPrintInfo]之后插入以下内容;

[newInfo setVerticalPagination:NSAutopagination];
[newInfo setHorizontalPagination:NSAutopagination];

NSAutopagination在文档中描述如下:

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没有影响.

解决方法

你得到一个包含一个页面文件,因为PDFOperationWithView:方法根本不支持分页.出于这个原因,调用 – setVerticalPagination:或 – setHoriziontalPagination:不会改变任何东西.

您可以尝试使用“经典”printOperationWithView:printInfo:方法,将其配置为将PDF保存到临时位置,然后使用获取文件内容创建PDFDocument.我希望下面的代码片段能够提供帮助.

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.
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐