2011-12-23 25 views
5

、私はWebViewのにHTMLの長いページをロードし、次を使用してPDFにそれを印刷:ココア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で、それは1ページの巨大なPDFです。私が好むものは、「PDFとして保存...」ダイアログからの印刷と同じように動作する印刷、つまりPDFを複数の合理的なサイズのページに分割することです。

誰でもこれを達成する方法を知っていますか?

IはNSPrintInfo *newInfo = [NSPrintInfo sharedPrintInfo];

[newInfo setVerticalPagination:NSAutoPagination]; 
[newInfo setHorizontalPagination:NSAutoPagination]; 

NSAutoPaginationは、以下のようにドキュメントに記載された後に、次を挿入する試み:

NSAutoPagination 画像は同じサイズの矩形に分割し、1列に配置されていますページの Mac OS X v10.0以降で使用できます。 NSPrintInfo.hで宣言されています。

これは印刷されたPDFには影響しませんでした。

答えて

10

+ PDFOperationWithView:の方法では改ページはまったくサポートされていないため、1つの大きなページのファイルが作成されます。そのため、- 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. 
} 
+0

優秀な回答!次の日に他の誰もより良い答えを出してくれないなら、私はあなたに賞金を授与します。ご協力いただきありがとうございます。 – Daniel

+0

さて、私は賞金終了時に目を覚ますつもりはないので、勝つ! – Daniel

+0

これは私を助けました。いい答え。 –