首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在PDFDocument和CGPDFContext之间移动数据?

在PDFDocument和CGPDFContext之间移动数据?
EN

Stack Overflow用户
提问于 2018-04-01 11:04:06
回答 1查看 163关注 0票数 2

我想在PDFKit的PDFDocument和Core的CGPDFContext之间移动数据,但是我不知道怎么做。我正在使用python,但是任何显示要使用的方法和对象的代码都是受欢迎的。

使用CGPDFDocument很容易,但我需要使用PDFDocument是有原因的。你会觉得他们是免费搭桥的。

  1. 从PDFDocument创建上下文。 pdfDoc = PDFDocument.alloc().initWithURL_(pdfURL) pdfData = pdfDoc.dataRepresentation() CGPDFContextCreate(pdfData,None,metaDict)

但我得到了:'deflateEnd: error -3:(Null)‘。

  1. 我可以用PDFPage ToContext将一个drawWithBox添加到CGPDFContext中,但只有10.12以上。我试过这个: page = pdfDoc.pageAtIndex_(0) writeContext = CGPDFContextCreateWithURL(myURL,None,dictarray) CGContextBeginPage(writeContext,mbox) CGContextDrawPDFPage(writeContext,page.pageRef)

这给了我一个分割错误。(这是通过注释掉drawPDF行,或者用CGPDFPage对象代替CGPDFPage对象而不是PDFPage来修复的。

  1. 使用来自PDFDocument的数据初始化CGPDFContext对象。

我可能不得不以某种方式使用CGDataProviders和消费者,但却毫无头绪。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-05 10:43:34

我已经把你的代码和我一直在做的一些PDF代码混在一起,并想出了把一个PDF页面绘制到CGContext中的方法。

我还使用了我的“参数”类,它只为我处理命令行选项。应该是不言而喻的。

我在几个不同的PDF上测试了这一点,并在一个新的PDF中得到了一个页面。

代码语言:javascript
复制
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
#import <Quartz/Quartz.h>


#import "ArgumentsObj.h"

int main (int argc,  char*const* argv)
{

    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    Arguments* aa = [Arguments argumentsWithCount:argc values:argv];
    
    if ([aa countPositionalArguments] == 2) {
        NSURL* furl = [NSURL fileURLWithPath:[aa positionalArgumentAt:0]];
        PDFDocument* pdfd = [[PDFDocument alloc] initWithURL:furl];

        PDFPage* pp = [pdfd pageAtIndex:0];
        PDFRect mediabox = [pp boundsForBox:kPDFDisplayBoxMediaBox];

        NSURL* nsfn = [NSURL fileURLWithPath:[aa positionalArgumentAt:1]];
        NSDictionary* aux = [NSDictionary dictionaryWithObjectsAndKeys:@"main",kCGPDFContextCreator, nil];

        CGContextRef cgpdf = CGPDFContextCreateWithURL((CFURLRef)nsfn,&mediabox,(CFDictionaryRef)aux);
    
        CGPDFContextBeginPage(cgpdf, NULL);

        CGContextDrawPDFPage(cgpdf, [pp pageRef]);
                       
        CGPDFContextEndPage(cgpdf);
        CGPDFContextClose(cgpdf);

    }
    [pool release];
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49597379

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档