2011-05-20 85 views
2

我從iPad應用程序中的UIView創建PDF。它的大小爲768 * 2000。當我創建pdf時,它會創建相同的大小,並在一個頁面上顯示所有內容。所以我在iPad上打印時遇到問題。我使用下面的代碼來創建PDF: -從UIView在iPad中創建pdf的問題應用程序

-(void)drawPdf:(UIView *)previewView{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Waypoint Data.pdf"]; 
    //CGRect tempRect = CGRectMake(0, 0, 768, 1068); 
    CGContextRef pdfContext = [self createPDFContext:previewView.bounds path:(CFStringRef)writableDBPath]; 
    CGContextBeginPage (pdfContext,nil); // 6 

    //turn PDF upsidedown 

    CGAffineTransform transform = CGAffineTransformIdentity;  
    transform = CGAffineTransformMakeTranslation(0, previewView.bounds.size.height); 
    transform = CGAffineTransformScale(transform, 1.0, -1.0); 
    CGContextConcatCTM(pdfContext, transform); 

    //Draw view into PDF 
    [previewView.layer renderInContext:pdfContext]; 
    CGContextEndPage (pdfContext);// 8 
    CGContextRelease (pdfContext); 
} 

//Create empty PDF context on iPhone for later randering in it 

-(CGContextRef) createPDFContext:(CGRect)inMediaBox path:(CFStringRef) path{ 

    CGContextRef myOutContext = NULL; 

    CFURLRef url; 

    url = CFURLCreateWithFileSystemPath (NULL, // 1 

            path, 

            kCFURLPOSIXPathStyle, 

            false); 

    if (url != NULL) { 

     myOutContext = CGPDFContextCreateWithURL (url,// 2 

               &inMediaBox,            NULL);   
     CFRelease(url);// 3  
    } 
    return myOutContext;// 4  
} 

任何人都可以建議我如何縮小pdf大小,它有多個頁面?

在此先感謝。

+0

你發現瞭解決方案嗎? – 2012-04-08 17:17:22

回答

0

見例如「繪圖和打印指南適用於iOS」在

https://developer.apple.com/library/ios/#documentation/2DDrawing/Conceptual/DrawingPrintingiOS/GeneratingPDF/GeneratingPDF.html#//apple_ref/doc/uid/TP40010156-CH10-SW1

基本上在清單4-1的代碼示例,他們有一個do while循環,並採取通知它是如何開始一個新的PDF在循環頁:

...

// Mark the beginning of a new page. 
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil); 

...

在你當前的方法中,你只調用一次begin頁面方法,這就是爲什麼你只會有一個頁面。

0

您需要爲每個要創建的新PDF頁面調用UIGraphicsBeginPDFPage。假設你有一個可變高度的UIView,這裏是你如何在運行時將其分解成儘可能多的PDF頁面:

NSInteger pageHeight = 792; // Standard page height - adjust as needed 
NSInteger pageWidth = 612; // Standard page width - adjust as needed 

/* CREATE PDF */ 
NSMutableData *pdfData = [NSMutableData data]; 
UIGraphicsBeginPDFContextToData(pdfData, CGRectMake(0,0,pageWidth,pageHeight), nil); 
CGContextRef pdfContext = UIGraphicsGetCurrentContext(); 
for (int page=0; pageHeight * page < theView.frame.size.height; page++) 
{ 
    UIGraphicsBeginPDFPage(); 
    CGContextTranslateCTM(pdfContext, 0, -pageHeight * page); 
    [theView.layer renderInContext:pdfContext]; 
} 

UIGraphicsEndPDFContext();