objective-c - cocoa PDF 页面拆分

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

关于objective-c - cocoa PDF 页面拆分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8613008/

相关文章:

cocoa - 从 hitTest 返回自定义 CALayer 子类时出现问题

objective-c - Obj-C MD5 哈希与 Java/SQL 不匹配

r - 在苏门答腊 PDF 阅读器中打开编译的 .rnw 的自定义函数?

java - Itext PDF 无法正确显示缅甸 Unicode 字体

ios - 从框架加载自定义单元 xib

java - 如何在多行文本周围绘制矩形

ios - appStoreReceiptURL 有何用途?

ios - 为什么 self.splitViewController == nil?

objective-c - 崩溃并尝试从对象中插入nil对象[0]

ios - NavigationController 中的 ModalViewCOntroller