我使用NSPrintOperation方法在PDF中转换了NSView(PDFContentView:NSView)。此NSView包含2个子视图(Drawing:NSView)。这些子视图是使用drawRect方法绘制的。
当我在NSWindow中显示视图时,预期结果是正确的:
但是在PDF文件中,第二个子视图似乎包含在第一个子视图中:
这似乎是分层的问题,但是当我将setWantsLayer:更改为NO时,结果是相同的。
注意:我发现了一些建议,建议将使用drawRect方法绘制的子视图转换为NSImage,然后将其添加到视图中。但是此解决方案不适合我的问题,因为我需要保持放大pdf的可能性而不会使子视图像素化。
谢谢您的帮助。
这是我的代码:
将我的pdfContentView转换为PDF的方法:
- (void)createPDFWithPages
{
NSPrintInfo *printInfo;
NSPrintInfo *sharedInfo;
NSPrintOperation *printOp;
NSMutableDictionary *printInfoDict;
NSMutableDictionary *sharedDict;
sharedInfo = [NSPrintInfo sharedPrintInfo];
sharedDict = [sharedInfo dictionary];
printInfoDict = [NSMutableDictionary dictionaryWithDictionary:sharedDict];
[printInfoDict setObject:NSPrintSaveJob forKey:NSPrintJobDisposition];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *desktopURLArray = [fileManager URLsForDirectory:NSDesktopDirectory inDomains:NSUserDomainMask];
NSURL *desktopURL = [desktopURLArray objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"/%@", @"myPDF.pdf"];
NSURL *fileURL = [NSURL fileURLWithPath:[[desktopURL path] stringByAppendingString:fileName]];
[printInfoDict setObject:[fileURL path] forKey:NSPrintSavePath];
printInfo = [[NSPrintInfo alloc] initWithDictionary: printInfoDict];
[printInfo setTopMargin:30.0];
[printInfo setBottomMargin:30.0];
[printInfo setLeftMargin:30.0];
[printInfo setRightMargin:30.0];
[printInfo setHorizontalPagination: NSAutoPagination];
[printInfo setVerticalPagination: NSAutoPagination];
[printInfo setVerticallyCentered:NO];
printOp = [NSPrintOperation printOperationWithView:pdfContentView printInfo:printInfo];
[printOp setShowsPrintPanel:NO];
[printOp runOperation];
}
PDFContentView.m:
#import "PDFContentView.h"
#import "Drawing.h"
@interface PDFContentView ()
{
Drawing *drawing1;
Drawing *drawing2;
}
@end
@implementation PDFContentView
- (id)init
{
if(self = [super init])
{
self.frame = NSMakeRect(10, 10, 520, 780); //page size = (520, 780)
[self setWantsLayer:YES];
self.layer.backgroundColor = [NSColor whiteColor].CGColor;
drawing1 = [[Drawing alloc] initWithFrame:NSMakeRect(0, 20, 50, 50)];
[drawing1 setBackgroundColor:[NSColor greenColor]];
[self addSubview:drawing1];
drawing2 = [[Drawing alloc] initWithFrame:NSMakeRect(30, 0, 50, 50)];
[drawing2 setBackgroundColor:[NSColor yellowColor]];
[self addSubview:drawing2];
}
return self;
}
- (BOOL)isFlipped
{
return YES;
}
@end
Drawing.m:
#import "Drawing.h"
@interface Drawing ()
{
NSColor *backgrounColor;
}
@end
@implementation Drawing
- (id)initWithFrame:(NSRect)contentRect
{
if(self = [super initWithFrame:(NSRect)contentRect])
{
self.frame = contentRect;
[self setWantsLayer:YES];
backgrounColor = [[NSColor alloc] init];
backgrounColor = [NSColor colorWithCalibratedRed:255.0/255.0 green:255.0/255.0 blue:255.0/255.0 alpha:255.0/255.0];
}
return self;
}
- (BOOL)isFlipped
{
return YES;
}
- (void)setBackgroundColor:(NSColor*)color;
{
backgrounColor = color;
[self setNeedsDisplay:YES];
}
- (void)drawRect:(CGRect)rect
{
CGContextRef context = (CGContextRef) [[NSGraphicsContext currentContext] graphicsPort];
CGMutablePathRef path00 = CGPathCreateMutable();
CGPathMoveToPoint(path00, NULL, 0, 0);
CGPathAddLineToPoint(path00, NULL, self.frame.size.width, 0);
CGPathAddLineToPoint(path00, NULL, self.frame.size.width, self.frame.size.height);
CGPathAddLineToPoint(path00, NULL, 0, self.frame.size.height);
CGPathCloseSubpath(path00);
CGContextSaveGState(context);
CGContextAddPath(context, path00);
CGContextSetFillColorWithColor(context, backgrounColor.CGColor);
CGContextFillPath(context);
CGContextRestoreGState(context);
CGRect borderRect = NSMakeRect(0.5, 0.5, 24, 24);
CGMutablePathRef borderPath = CGPathCreateMutable();
CGPathAddRect(borderPath, NULL, borderRect);
CGPathCloseSubpath(borderPath);
CGContextSaveGState(context);
CGContextAddPath(context, borderPath);
CGContextSetStrokeColorWithColor(context, [NSColor redColor].CGColor);
CGContextSetLineWidth(context, 1);
CGContextStrokePath(context);
CGContextRestoreGState(context); // => Missing funtion
CGMutablePathRef path2 = CGPathCreateMutable();
CGRect aRect = NSMakeRect(10, 11, 4, 4);
CGPathAddRect(path2, NULL, aRect);
CGPathCloseSubpath(path2);
CGContextSaveGState(context);
CGContextAddPath(context, path2);
CGContextSetShouldAntialias(context, NO);
CGContextSetStrokeColorWithColor(context, [NSColor blueColor].CGColor);
CGContextSetLineWidth(context, 1);
CGContextStrokePath(context);
CGContextRestoreGState(context); // => Missing funtion
}
@end