我们可以将pdf上绘制的内容保存为新的pdf吗? Xcode中

时间:2012-07-03 15:21:07

标签: uikit xcode4.2 pdf-generation cgpdfdocument

我们可以保存通过pdf绘制的内容,再次作为pdf,我试图用内容保存pdf但没有运气,它保存为空白pdf

代码:

- (void)drawRect:(CGRect)rect
{
NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"];
NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
document = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdfUrl);
size_t count = CGPDFDocumentGetNumberOfPages (document);// 3

if (count == 0)
{
    NSLog(@"PDF needs at least one page");
    return;
}

CGRect paperSize = CGRectMake(0.0,0.0,595.28,841.89);

CGContextRef currentContext = UIGraphicsGetCurrentContext();

// flip context so page is right way up
CGContextTranslateCTM(currentContext, 0, paperSize.size.height);
CGContextScaleCTM(currentContext, 1.0, -1.0); 

CGPDFPageRef page = CGPDFDocumentGetPage (document, 1); // grab page 1 of the PDF 

CGContextDrawPDFPage (currentContext, page); // draw page 1 into graphics context

// flip context so annotations are right way up
CGContextScaleCTM(currentContext, 1.0, -1.0);
CGContextTranslateCTM(currentContext, 0, -paperSize.size.height);

////////////////////////////////

[curImage drawAtPoint:CGPointMake(0, 0)];
CGPoint mid1 = midPoint(previousPoint1, previousPoint2); 
CGPoint mid2 = midPoint(currentPoint, previousPoint1);

CGContextRef context = UIGraphicsGetCurrentContext(); 

[self.layer renderInContext:context];

CGContextMoveToPoint(context, mid1.x, mid1.y);
CGContextAddQuadCurveToPoint(context, previousPoint1.x, previousPoint1.y, mid2.x, mid2.y); 
CGContextSetLineCap(context, kCGLineCapRound);
CGContextSetLineWidth(context, self.lineWidth);
CGContextSetStrokeColorWithColor(context, self.lineColor.CGColor);

CGContextStrokePath(context);

[super drawRect:rect];

// get a temprorary filename for this PDF

NSString* path = [[NSBundle mainBundle] pathForResource:@"sampleData" ofType:@"plist"];
path = NSTemporaryDirectory();
self.pdfFilePath = [path stringByAppendingPathComponent:[NSString stringWithFormat:@"%d.pdf", [[NSDate date] timeIntervalSince1970] ]];

UIGraphicsBeginPDFContextToFile(@"test.pdf", paperSize, nil);

[@"annotation" drawInRect:CGRectMake(100.0, 100.0, 200.0, 40.0) withFont:[UIFont systemFontOfSize:18.0]];
}

我尝试在按钮上单击

关闭PDF
- (IBAction)buttonPressed:(UIButton *)button {
 UIGraphicsEndPDFContext();
 return;
}

但它不起作用。

任何帮助将不胜感激,提前感谢

1 个答案:

答案 0 :(得分:0)

想通了,这里是创建pdf的功能,内容是在其上绘制或注释的

-(void)createPDFfromUIView:(UIView*)aView saveToDocumentsWithFileName: (NSString*)aFilename


{
// Creates a mutable data object for updating with binary data, like a byte array
NSMutableData *pdfData = [NSMutableData data];

// Points the pdf converter to the mutable data object and to the UIView to be converted
UIGraphicsBeginPDFContextToData(pdfData, aView.bounds, nil);
UIGraphicsBeginPDFPage();
CGContextRef pdfContext = UIGraphicsGetCurrentContext();


// draws rect to the view and thus this is captured by UIGraphicsBeginPDFContextToData


[aView.layer renderInContext:pdfContext];

// remove PDF rendering context
UIGraphicsEndPDFContext();

// Retrieves the document directories from the iOS device
NSArray* documentDirectories = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);

NSString* documentDirectory = [documentDirectories objectAtIndex:0];
NSString* documentDirectoryFilename = [documentDirectory stringByAppendingPathComponent:aFilename];

// instructs the mutable data object to write its context to a file on disk
[pdfData writeToFile:documentDirectoryFilename atomically:YES];
NSLog(@"documentDirectoryFileName: %@",documentDirectory);
}

并按下按钮调用该功能。