0

我将几个单独的 pdf 页面合并到一个文件中。合并本身效果很好。所有页面都在正确的位置,并且看起来正确。

合并代码如下所示:

CGContextRef writeContext = CGPDFContextCreateWithURL((CFURLRef)originalURL, NULL, NULL);
for(NSURL * pdfCacheURL in pdfURLs) {
    singlePDFDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)pdfCacheURL);
    pdfPage = CGPDFDocumentGetPage(singlePDFDocumentRef, 1);
    mediaBox = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox);
    CGContextBeginPage(writeContext, &mediaBox);
    CGContextDrawPDFPage(writeContext, pdfPage);
    CGContextEndPage(writeContext);
    CGPDFPageRelease(pdfPage);
}
CGPDFContextClose(writeContext);
CFRelease(writeContext);

合并之后发生的奇怪的事情是,最终的 Document 的文件大小比所有单个页面的文件大小组合起来要大得多。

这是我的调试输出的一部分

Processing Page: 1
File Size before merge:: 0.000000 mb
Single page length: 0.758951 mb
File Size after merge: 6.172294 mb

Processing Page: 2
File Size before merge: 6.172294 mb
Single page length: 0.262792 mb
File Size after merge: 6.722573 mb

Processing Page: 3
File Size before merge:: 6.722573 mb
Single page length: 0.215380 mb
File Size after merge: 8.150043 mb

Processing Page: 4
File Size before merge:: 8.150043 mb
Single page length: 0.346910 mb
File Size after merge: 10.788255 mb

如您所见,在 4 页之后,文件大小超过 10 兆字节,但合并后的文件大小为 1.58 兆。您可以想象合并 100 个页面时会发生什么。

pdf包含大量图像,但我不确定这是否会导致文件大小增加

4

1 回答 1

0

This is a result of the way you are initializing CGPDFContextCreateWithURL((CFURLRef)originalURL, NULL, NULL);

The second parameter is mediaBox, which according to Apple, is

A rectangle that specifies the bounds of the PDF. The origin of the rectangle should typically be (0,0). The CGPDFContextCreateWithURL function uses this rectangle as the default page media bounding box. If you pass NULL, CGPDFContextCreateWithURL uses a default page size of 8.5 by 11 inches (612 by 792 points).

If your original file is small than this size, it will get increased because it get drawn on a bigger canvas. Pass in a smaller size instead.

Here is the link to the reference: CGPDFContext Reference

于 2016-06-08T21:55:33.857 回答