我做了一个可以制作PDF文件的应用程序。我只有一张图像作为基础,然后在其上叠加文本和其他图像以制作 PDF。问题是 PDF 大约 9 MB,只有一页。有没有办法将 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();
}