gpt4 book ai didi

iphone - 将注释和 Pdf 保存到 IOS 5 中的新 Pdf 文件

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:22:11 24 4
gpt4 key购买 nike

我正在开发一个 pdf 注释应用程序,我需要保存注释,所以当我尝试将注释和 pdf 内容保存到新的 pdf 时,注释被保存但 pdf 的内容不是?这是我的功能:

-(void)save_doc_as_pdf:(ReaderMainToolbar *)toolbar BackButton:(UIButton *)button
{
NSError *error;
NSString *PdfPath = [document.fileURL absoluteString];
NSString *newPath = [PdfPath stringByReplacingOccurrencesOfString:@"file://localhost" withString:@""];

NSString *NewPdfPath = [thenewPath stringByReplacingOccurrencesOfString:@".pdf" withString:@"_1.pdf"];
NSData *pdfData = [NSData dataWithContentsOfFile:[document.fileURL path] options:NSDataReadingUncached error:&error];
if (pdfData == nil) {
NSLog(@"data is nil !!!!");

}

if (error)
{
NSLog(@"%@", [error localizedDescription]);
return;
}
else
NSLog(@"Data has loaded successfully.");
//If fails to create the new file, returns
if (![[NSFileManager defaultManager] createFileAtPath:NewPdfPath contents:pdfData attributes:nil])
{
return;
}

NSURL *url = [NSURL fileURLWithPath:newPath];
CGPDFDocumentRef pdf_document = CGPDFDocumentCreateWithURL ((__bridge_retained CFURLRef) url);
size_t count = CGPDFDocumentGetNumberOfPages(pdf_document);

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

CGRect paperSize = CGRectMake(0, 0,842, 1190);

UIGraphicsBeginPDFContextToFile(NewPdfPath , paperSize, nil);
// CGPDFPageRef page = CGPDFDocumentGetPage(document, 1);
UIGraphicsBeginPDFPageWithInfo(paperSize, nil);
CGContextRef currentContext = UIGraphicsGetCurrentContext();

// flip context so page is right way (landscape app)
CGContextScaleCTM(currentContext, 1, -1);
// Rotate the coordinate system (rotation = M_PI or -M_PI for landscape)
//CGContextRotateCTM(currentContext, rotation/ 2);

CGPDFPageRef page = CGPDFDocumentGetPage (pdf_document, 1); // grab page 1 of the PDF
CGContextDrawPDFPage (currentContext, page); // draw page 1 into graphics context

//flip context so annotations are right way
CGContextScaleCTM(currentContext, 1, -1);
//CGContextRotateCTM(currentContext, rotation / 2);
//Render the layer of the annotations view in the context
[startdraw.layer renderInContext:currentContext];
UIGraphicsEndPDFContext();
CGPDFDocumentRelease (pdf_document);
}

最佳答案

你好,我找到了一个在 pdf 文档上保存注释的解决方案,我想与你分享它,所以这里是代码:

-(void)save_the_pdf:(ReaderMainToolbar *)toolbar BackButton:(UIButton *)button{
NSMutableArray *URLsTableObject=[[NSMutableArray alloc] init];
NSMutableArray *URLsTable=[[NSMutableArray alloc] init];
NSUserDefaults *DataStoreUrls;
NSString *OriginalPdfName = [document.fileName stringByReplacingOccurrencesOfString:@".pdf" withString:@""];


NSString *OriginalPdfPath = [document.fileName stringByReplacingOccurrencesOfString:@".pdf" withString:@""];
NSString* thenewPath = [[NSBundle mainBundle] pathForResource:OriginalPdfPath ofType:@"pdf"];
NSString *NewPdfPath = [thenewPath stringByReplacingOccurrencesOfString:@".pdf" withString:@"_1.pdf"];

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((__bridge CFURLRef)[[NSBundle mainBundle] URLForResource:OriginalPdfName withExtension:@"pdf"]);

const size_t numberOfPages = CGPDFDocumentGetNumberOfPages(pdf);

NSMutableData* data = [NSMutableData data];
UIGraphicsBeginPDFContextToData(data, CGRectZero, nil);

for(size_t page = 1; page <= numberOfPages; page++)
{


// Get the current page and page frame
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdf, page);
const CGRect pageFrame = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);

UIGraphicsBeginPDFPageWithInfo(pageFrame, nil);

// Draw the page (flipped)
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSaveGState(ctx);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -pageFrame.size.height);
CGContextDrawPDFPage(ctx, pdfPage);
CGContextRestoreGState(ctx);
if (page == currentPage) {
[startdraw.layer renderInContext:ctx];
}
}

UIGraphicsEndPDFContext();

CGPDFDocumentRelease(pdf);
//pdf = nil;
if (![[NSFileManager defaultManager] createFileAtPath:NewPdfPath contents:data attributes:nil])
{
return;
}

// Do something with the 'data'...

DataStoreUrls=[NSUserDefaults standardUserDefaults];

if([DataStoreUrls objectForKey:@"ALLURLS"]!=NULL){
URLsTableObject=[DataStoreUrls objectForKey:@"ALLURLS"];
int index=[URLsTableObject count];
URLsTable=[[NSMutableArray alloc] initWithArray:URLsTableObject];
[URLsTable insertObject:NewPdfPath atIndex:index];
[DataStoreUrls setObject:URLsTable forKey:@"ALLURLS"];
}else{
[URLsTable addObject:NewPdfPath];
[DataStoreUrls setObject:URLsTable forKey:@"ALLURLS"];
}
[DataStoreUrls synchronize];
}

关于iphone - 将注释和 Pdf 保存到 IOS 5 中的新 Pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16397126/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com