我製作了一個可以製作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();
}
這些圖像有多大?你有多少人在那裏? –
如何將圖像添加到PDF?在將完整大小的圖像添加到PDF時,遇到了類似的問題,但框架更小。 PDF在內部仍包含完整大小的圖像。我不得不首先縮放圖像的大小,然後將較小的圖像添加到PDF。 – rmaddy
PDF需要是11「x 8.5」。背景圖像爲11 x 8.5,爲13 MB。我嘗試了一個較小的版本,但PDF在打印時出現模糊。請參閱原始問題以查看代碼 – user717452