Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 如何在不丢失任何信息的情况下组合两个PDF?_Objective C_Pdf_Nsdata_Nsmutabledata - Fatal编程技术网

Objective c 如何在不丢失任何信息的情况下组合两个PDF?

Objective c 如何在不丢失任何信息的情况下组合两个PDF?,objective-c,pdf,nsdata,nsmutabledata,Objective C,Pdf,Nsdata,Nsmutabledata,我的目标是合并两个PDF。一个有10页,另一个有6页,所以输出应该是16页。我的方法是将两个PDF加载到存储在NSMutableArray中的两个NSData中 以下是我的保存方法: NSMutableData *toSave = [NSMutableData data]; for(NSData *pdf in PDFArray){ [toSave appendData:pdf]; } [toSave writeToFile:path atomically:YES]; 然而,输出PDF

我的目标是合并两个PDF。一个有10页,另一个有6页,所以输出应该是16页。我的方法是将两个PDF加载到存储在
NSMutableArray
中的两个
NSData

以下是我的保存方法:

NSMutableData *toSave = [NSMutableData data];
for(NSData *pdf in PDFArray){
    [toSave appendData:pdf];
}
[toSave writeToFile:path atomically:YES];

然而,输出PDF只有第二部分,它只包含6页。所以我不知道我错过了什么。有人能给我一些提示吗?

PDF是一种描述单个文档的文件格式。无法连接到PDF文件以获取连接的文档

但可以通过以下方式实现:

  • 使用创建两个文档
  • 将第二个文档的所有页面插入到第一个文档中
  • 这应该是这样的:

    PDFDocument *theDocument = [[PDFDocument alloc] initWithData:PDFArray[0]]
    PDFDocument *theSecondDocument = [[PDFDocument alloc] initWithData:PDFArray[1]]
    NSInteger theCount = theDocument.pageCount;
    NSInteger theSecondCount = theSecondDocument.pageCount;
    
    for(NSInteger i = 0; i < theSecondCount; ++i) {
        PDFPage *thePage = [theSecondDocument pageAtIndex:i];
    
        [theDocument insertPage:thePage atIndex:theCount + i];
    }
    [theDocument writeToURL:theTargetURL];
    
    PDFDocument*theDocument=[[PDFDocument alloc]initWithData:PDFArray[0]]
    PDFDocument*theSecondDocument=[[PDFDocument alloc]initWithData:PDFArray[1]]
    NSInteger theCount=document.pageCount;
    NSInteger theSecondCount=theSecondDocument.pageCount;
    对于(NSInteger i=0;i

    您必须添加
    #import
    @import PDFKit
    添加到源文件中,您应该将
    PDFKit.framework
    添加到Xcode中构建目标的链接框架和库中

    PDF是一种描述单个文档的文件格式。无法连接到PDF文件以获取连接的文档

    但可以通过以下方式实现:

  • 使用创建两个文档
  • 将第二个文档的所有页面插入到第一个文档中
  • 这应该是这样的:

    PDFDocument *theDocument = [[PDFDocument alloc] initWithData:PDFArray[0]]
    PDFDocument *theSecondDocument = [[PDFDocument alloc] initWithData:PDFArray[1]]
    NSInteger theCount = theDocument.pageCount;
    NSInteger theSecondCount = theSecondDocument.pageCount;
    
    for(NSInteger i = 0; i < theSecondCount; ++i) {
        PDFPage *thePage = [theSecondDocument pageAtIndex:i];
    
        [theDocument insertPage:thePage atIndex:theCount + i];
    }
    [theDocument writeToURL:theTargetURL];
    
    PDFDocument*theDocument=[[PDFDocument alloc]initWithData:PDFArray[0]]
    PDFDocument*theSecondDocument=[[PDFDocument alloc]initWithData:PDFArray[1]]
    NSInteger theCount=document.pageCount;
    NSInteger theSecondCount=theSecondDocument.pageCount;
    对于(NSInteger i=0;i

    您必须添加
    #import
    @import PDFKit
    添加到源文件中,您应该将
    PDFKit.framework
    添加到Xcode中构建目标的链接框架和库中

    我制作了一个Swift命令行工具,可以组合任意数量的PDF文件。它将输出路径作为第一个参数,将输入PDF文件作为其他参数。没有任何错误处理,所以如果您愿意,可以添加它。以下是完整的代码:

    import PDFKit
    
    let args = CommandLine.arguments.map { URL(fileURLWithPath: $0) }
    let doc = PDFDocument(url: args[2])!
    
    for i in 3..<args.count {
        let docAdd = PDFDocument(url: args[i])!
        for i in 0..<docAdd.pageCount {
            let page = docAdd.page(at: i)!
            doc.insert(page, at: doc.pageCount)
        }
    }
    doc.write(to: args[1])
    
    导入PDFKit
    让args=CommandLine.arguments.map{URL(fileURLWithPath:$0)}
    让doc=PDFDocument(url:args[2])!
    
    对于iIn 3..我制作了一个Swift命令行工具来组合任意数量的PDF文件。它将输出路径作为第一个参数,将输入PDF文件作为其他参数。没有任何错误处理,所以如果您愿意,可以添加它。以下是完整的代码:

    import PDFKit
    
    let args = CommandLine.arguments.map { URL(fileURLWithPath: $0) }
    let doc = PDFDocument(url: args[2])!
    
    for i in 3..<args.count {
        let docAdd = PDFDocument(url: args[i])!
        for i in 0..<docAdd.pageCount {
            let page = docAdd.page(at: i)!
            doc.insert(page, at: doc.pageCount)
        }
    }
    doc.write(to: args[1])
    
    导入PDFKit
    让args=CommandLine.arguments.map{URL(fileURLWithPath:$0)}
    让doc=PDFDocument(url:args[2])!
    
    因为我在3号…@GiddensA:我已经扩展了我的职位。是的,这就是我要找的!谢谢但我还有一个问题。我的应用程序是Cocoa应用程序(我意识到我以前应该提到这个),我尝试添加PDFKit.framework,但找不到框架。我读了PDFKit的文档,它说PDFKit在OSX 10.4之后可用。那么我错过了什么?@GiddensA:我已经扩展了我的帖子。是的,这就是我要找的!谢谢但我还有一个问题。我的应用程序是Cocoa应用程序(我意识到我以前应该提到这个),我尝试添加PDFKit.framework,但找不到框架。我读了PDFKit的文档,它说PDFKit在OSX 10.4之后可用。那么我错过了什么呢?斯威夫特5斯威夫特5------斯威夫特5斯威夫特5-----------