gpt4 book ai didi

objective-c - cocoa PDF 页面拆分

转载 作者:太空狗 更新时间:2023-10-30 03:41:21 25 4
gpt4 key购买 nike

在我正在创建的应用程序中,我将一长页 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com