PDF打印透明背景
PDF Printing Transparent Backgrounds
我的应用程序创建 PDF。作为其中的一部分,我已经成功地允许用户添加图像,并且图像被裁剪成圆形并添加。当您在屏幕上或计算机屏幕上查看随后创建的 PDF 时,它看起来很棒。但是,当您将其打印出来时,它会打印掉部分透明背景,如图像中所见与屏幕上的外观。这是 Xcode 中 PDFKit 的问题还是我可以做些什么来解决这个问题?
- (void) generatePdfWithFilePath2: (NSString *)thefilePath
{
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
BOOL done = NO;
do
{
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
[self drawImage];
[self drawImage2];
[self drawText];
[self drawText2];
[self drawText4];
[self drawText5];
done = YES;
}
while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
并绘制图像:
- (void) drawImage2
{
UIImage * demoImage = self.imageCopy;
NSData *pngData = UIImagePNGRepresentation(demoImage);
CGDataProviderRef dp = CGDataProviderCreateWithCFData(( CFDataRef)pngData);
CGImageRef cgImage = CGImageCreateWithPNGDataProvider(dp, NULL, true, kCGRenderingIntentDefault);
[[UIImage imageWithCGImage:cgImage] drawInRect:CGRectMake(100, 371.82, 190, 190)];
}
两件事...
- 您转换为
CGImage
并返回的原因是什么?
- 你确定背景是完全透明的吗?
- 如果您确定,那么您可能想尝试使用剪切路径。
这是我的快速测试结果...
将其用作“背景”图像:
并且这两张图片与背景重叠,具有透明度 - 第一张图片 有 alpha/透明区域,第二张图片没有:
使用此代码:
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)didTap:(id)sender {
NSFileManager *fileManager = [NSFileManager defaultManager];
//Get documents directory
NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0];
NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:@"SomeFileName.pdf"];
[self generatePdfWithFilePath2:path];
}
- (void) generatePdfWithFilePath2: (NSString *)thefilePath
{
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
CGSize pageSize = CGSizeMake(612, 792);
BOOL done = NO;
do
{
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
[self drawImage1];
[self drawImage2];
[self drawImage3];
[self drawImage4];
done = YES;
}
while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
- (void) drawImage1
{
UIImage * demoImage = [UIImage imageNamed:@"bkgGradient"];
[demoImage drawInRect:CGRectMake(50, 50, 200, 200)];
}
- (void) drawImage2
{
UIImage * demoImage = [UIImage imageNamed:@"pro1"];
[demoImage drawInRect:CGRectMake(150, 150, 200, 200)];
}
- (void) drawImage3
{
UIImage * demoImage = [UIImage imageNamed:@"bkgGradient"];
[demoImage drawInRect:CGRectMake(50, 400, 200, 200)];
}
- (void) drawImage4
{
UIImage * demoImage = [UIImage imageNamed:@"pro2"];
CGRect r = CGRectMake(150, 500, 200, 200);
// create a bezier path defining rounded corners
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:r cornerRadius:r.size.height];
// use this path for clipping in the implicit context
[path addClip];
[demoImage drawInRect:r];
}
这是 on-screen 预览的样子:
和完整打印页面的扫描(按比例缩小):
和non-scaled打印页面的 300-dpi 扫描:
如您所见,“pro1”的 alpha 区域和“pro2”的裁剪区域都是完全透明的,on-screen 和打印。
我的应用程序创建 PDF。作为其中的一部分,我已经成功地允许用户添加图像,并且图像被裁剪成圆形并添加。当您在屏幕上或计算机屏幕上查看随后创建的 PDF 时,它看起来很棒。但是,当您将其打印出来时,它会打印掉部分透明背景,如图像中所见与屏幕上的外观。这是 Xcode 中 PDFKit 的问题还是我可以做些什么来解决这个问题?
- (void) generatePdfWithFilePath2: (NSString *)thefilePath
{
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
BOOL done = NO;
do
{
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
[self drawImage];
[self drawImage2];
[self drawText];
[self drawText2];
[self drawText4];
[self drawText5];
done = YES;
}
while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
并绘制图像:
- (void) drawImage2
{
UIImage * demoImage = self.imageCopy;
NSData *pngData = UIImagePNGRepresentation(demoImage);
CGDataProviderRef dp = CGDataProviderCreateWithCFData(( CFDataRef)pngData);
CGImageRef cgImage = CGImageCreateWithPNGDataProvider(dp, NULL, true, kCGRenderingIntentDefault);
[[UIImage imageWithCGImage:cgImage] drawInRect:CGRectMake(100, 371.82, 190, 190)];
}
两件事...
- 您转换为
CGImage
并返回的原因是什么? - 你确定背景是完全透明的吗?
- 如果您确定,那么您可能想尝试使用剪切路径。
这是我的快速测试结果...
将其用作“背景”图像:
并且这两张图片与背景重叠,具有透明度 - 第一张图片 有 alpha/透明区域,第二张图片没有:
使用此代码:
- (void)viewDidLoad {
[super viewDidLoad];
}
- (IBAction)didTap:(id)sender {
NSFileManager *fileManager = [NSFileManager defaultManager];
//Get documents directory
NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [directoryPaths objectAtIndex:0];
NSString *path = [documentsDirectoryPath stringByAppendingPathComponent:@"SomeFileName.pdf"];
[self generatePdfWithFilePath2:path];
}
- (void) generatePdfWithFilePath2: (NSString *)thefilePath
{
UIGraphicsBeginPDFContextToFile(thefilePath, CGRectZero, nil);
CGSize pageSize = CGSizeMake(612, 792);
BOOL done = NO;
do
{
UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, pageSize.width, pageSize.height), nil);
[self drawImage1];
[self drawImage2];
[self drawImage3];
[self drawImage4];
done = YES;
}
while (!done);
// Close the PDF context and write the contents out.
UIGraphicsEndPDFContext();
}
- (void) drawImage1
{
UIImage * demoImage = [UIImage imageNamed:@"bkgGradient"];
[demoImage drawInRect:CGRectMake(50, 50, 200, 200)];
}
- (void) drawImage2
{
UIImage * demoImage = [UIImage imageNamed:@"pro1"];
[demoImage drawInRect:CGRectMake(150, 150, 200, 200)];
}
- (void) drawImage3
{
UIImage * demoImage = [UIImage imageNamed:@"bkgGradient"];
[demoImage drawInRect:CGRectMake(50, 400, 200, 200)];
}
- (void) drawImage4
{
UIImage * demoImage = [UIImage imageNamed:@"pro2"];
CGRect r = CGRectMake(150, 500, 200, 200);
// create a bezier path defining rounded corners
UIBezierPath * path = [UIBezierPath bezierPathWithRoundedRect:r cornerRadius:r.size.height];
// use this path for clipping in the implicit context
[path addClip];
[demoImage drawInRect:r];
}
这是 on-screen 预览的样子:
和完整打印页面的扫描(按比例缩小):
和non-scaled打印页面的 300-dpi 扫描:
如您所见,“pro1”的 alpha 区域和“pro2”的裁剪区域都是完全透明的,on-screen 和打印。