2013-04-15 140 views
1

我製作了一個可以製作PDF文件的應用程序。我只有一個圖像作爲基礎,並在其頂部創建圖層和其他圖像以製作PDF。問題是PDF約爲9 MB,只有一頁。有沒有辦法壓縮PDF,所以它不那麼大?壓縮iOS PDF文件?

- (IBAction)generatePdfButtonPressed:(id)sender 
{ 
    pageSize = CGSizeMake(792, 612); 
    NSString *fileName = @"Demo.pdf"; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; 
    NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName]; 

    [self generatePdfWithFilePath:pdfFileName]; 
} 

- (void) drawText 
{ 
    CGContextRef currentContext = UIGraphicsGetCurrentContext(); 
    CGContextSetRGBFillColor(currentContext, 0.0, 0.0, 0.0, 1.0); 

    NSString *textToDraw = @"Text goes here"; 

    UIFont *font = [UIFont systemFontOfSize:22.0]; 



    CGRect renderingRect = CGRectMake(160, 177.82, 590, 95); 

    [textToDraw drawInRect:renderingRect 
        withFont:font 
      lineBreakMode:NSLineBreakByWordWrapping 
       alignment:NSTextAlignmentLeft]; 

} 

- (void) drawImage 
{ 
    UIImage * demoImage = [UIImage imageNamed:@"demo.png"]; 
    [demoImage drawInRect:CGRectMake(0, 0, 792, 612)]; 
} 

- (void) generatePdfWithFilePath: (NSString *)thefilePath 
{ 
    UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil); 


    BOOL done = NO; 
    do 
    { 

     UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil); 


     [self drawImage]; 

     [self drawText]; 


     done = YES; 
    } 
    while (!done); 

    // Close the PDF context and write the contents out. 
    UIGraphicsEndPDFContext(); 

} 
+1

這些圖像有多大?你有多少人在那裏? –

+1

如何將圖像添加到PDF?在將完整大小的圖像添加到PDF時,遇到了類似的問題,但框架更小。 PDF在內部仍包含完整大小的圖像。我不得不首先縮放圖像的大小,然後將較小的圖像添加到PDF。 – rmaddy

+0

PDF需要是11「x 8.5」。背景圖像爲11 x 8.5,爲13 MB。我嘗試了一個較小的版本,但PDF在打印時出現模糊。請參閱原始問題以查看代碼 – user717452

回答

0

當包含圖像時縮小PDF的方式是使用該圖像的JPEG表示。 PDF的大小取決於JPEG的壓縮程度。我知道這不是在iOS中製作PDF而是在現實生活中製作PDF。

你可能會被這個問題有所幫助:Can I use JPEG compression for images in pdf files generated on iOS?

的使用注意事項的ImageIO框架,所以你可以設置你的壓縮級別。當然,您需要進行實驗才能獲得最佳折中價值。

+0

我不確定這一點到底是什麼意思。我是否將demo.png保存在項目中,並將其壓縮到應用程序中,然後將THAT圖像用於PDF? – user717452