gpt4 book ai didi

objective-c - 使用 Quartz 将 PDF 文档保存到磁盘

转载 作者:搜寻专家 更新时间:2023-10-30 19:53:02 25 4
gpt4 key购买 nike

我正在尝试将 pdf 页面绘制到 pdf 上下文中,然后将其保存到磁盘。我似乎无法弄清楚出了什么问题。有人可以给我一些指示。谢谢。

- (void)testQuartz:(NSData *)pdfDocumentData
{
//Create the pdf document reference
CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)pdfDocumentData);
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider);
CGDataProviderRelease(dataProvider); //Release the data provider

//Create the pdf context
CGPDFPageRef page = CGPDFDocumentGetPage(document, 0);
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0);

CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData);
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL);

if (CGPDFDocumentGetNumberOfPages(document) > 0)
{
//Draw the page onto the new context
page = CGPDFDocumentGetPage(document, 0);
CGContextDrawPDFPage(pdfContext, page);
}
else
{
NSLog(@"Failed to create the document");
}

//Write to disk
[(NSMutableData *)mutableData writeToFile:@"/Users/David/Desktop/test.pdf" atomically:YES];

//Clean up
CGDataConsumerRelease(dataConsumer);
CFRelease(mutableData);
CGPDFDocumentRelease(document);
}

最佳答案

所以我发现它有 3 个问题。

  1. PDF 页面从 1 而不是 0 开始。
  2. 需要在页面开始和结束时告知 pdf 上下文。
  3. 在将数据写入磁盘之前释放 pdf 上下文。出于某种原因,这是关键。

这里是更正后的有效代码。

- (void)testQuartz:(NSData *)pdfDocumentData
{
//Create the pdf document reference
CGDataProviderRef dataProvider = CGDataProviderCreateWithCFData((CFDataRef)pdfDocumentData);
CGPDFDocumentRef document = CGPDFDocumentCreateWithProvider(dataProvider);

//Create the pdf context
CGPDFPageRef page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1
CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);
CFMutableDataRef mutableData = CFDataCreateMutable(NULL, 0);

//NSLog(@"w:%2.2f, h:%2.2f",pageRect.size.width, pageRect.size.height);
CGDataConsumerRef dataConsumer = CGDataConsumerCreateWithCFData(mutableData);
CGContextRef pdfContext = CGPDFContextCreate(dataConsumer, &pageRect, NULL);


if (CGPDFDocumentGetNumberOfPages(document) > 0)
{
//Draw the page onto the new context
//page = CGPDFDocumentGetPage(document, 1); //Pages are numbered starting at 1

CGPDFContextBeginPage(pdfContext, NULL);
CGContextDrawPDFPage(pdfContext, page);
CGPDFContextEndPage(pdfContext);
}
else
{
NSLog(@"Failed to create the document");
}

CGContextRelease(pdfContext); //Release before writing data to disk.

//Write to disk
[(NSData *)mutableData writeToFile:@"/Users/David/Desktop/test.pdf" atomically:YES];

//Clean up
CGDataProviderRelease(dataProvider); //Release the data provider
CGDataConsumerRelease(dataConsumer);
CGPDFDocumentRelease(document);
CFRelease(mutableData);
}

关于objective-c - 使用 Quartz 将 PDF 文档保存到磁盘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3780745/

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