【发布时间】:2013-04-15 17:28:22
【问题描述】:
我制作了一个可以制作 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();
}
【问题讨论】:
-
这些图片有多大?你放了多少?
-
如何将图像添加到 PDF 中?在将全尺寸图像添加到 PDF 但框架要小得多时,我遇到了类似的问题。 PDF 内部仍然包含完整尺寸的图像。我必须先将图像按比例缩放,然后将较小的图像添加到 PDF。
-
PDF 需要为 11" x 8.5"。背景图像为 11 x 8.5,大小为 13 MB。我尝试了一个较小的版本,但打印出来的 PDF 很模糊。查看原始问题以查看代码
-
demo.png 有多大(宽度和高度,以像素为单位)?如果它大于 792x612,那么您应该缩放图像(创建一个较小尺寸的新 UIImage)并将较小的 UIImage 添加到 PDF。
-
它比 792 x 612 大,因为当我有较小的 792 x 612 图像时,它在 pdf 中变得模糊
标签: ios pdf compression