簡體   English   中英

iPhone從PDF頁面創建PDF文檔

[英]iphone create pdf document from pdf pages

我想從現有的pdf文檔中創建一個新的PDF文件。也就是說,我有一個名為“ old.pdf”的pdf文件,並且該用戶可以選擇一些頁面,然后從那些選定的頁面中創建一個新的pdf文件,並說“ temp1”。 .pdf”。 是否可以創建“ temp1.pdf”? 請幫助我,並提供一些代碼示例來做到這一點。 謝謝。

這是我的演示代碼,您可以嘗試。 但是這段代碼有一些問題。 新生成的PDF出現亂碼,我仍然無法解決此問題(有人可以幫忙嗎?)。

//old pdf
NSArray* old_PDF_paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* old_PDF_documentsDirectory = [old_PDF_paths objectAtIndex:0];
NSString* old_PDF_ABSOLUTE_PATH = [old_PDF_documentsDirectory stringByAppendingPathComponent:@"old.pdf"];

//new generate pdf
NSArray* output_PDF_paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* output_PDF_documentsDirectory = [output_PDF_paths objectAtIndex:0];
NSString* output_PDF_ABSOLUTE_PATH = [output_PDF_documentsDirectory stringByAppendingPathComponent:@"temp1.pdf"];
NSString* outpath = output_PDF_ABSOLUTE_PATH;

NSURL *url = [[NSURL alloc] initFileURLWithPath:old_PDF_ABSOLUTE_PATH];
CGPDFDocumentRef originalPDF = CGPDFDocumentCreateWithURL((CFURLRef)url);

// query for the number of pages
int ct = CGPDFDocumentGetNumberOfPages(originalPDF);

CGFloat width = 0.0f;
CGFloat height = 0.0f;

CGRect outRect = CGRectMake(0, 0, width, height);
CFURLRef outurl = CFURLCreateWithFileSystemPath (NULL, (CFStringRef)outpath,  kCFURLPOSIXPathStyle, 0);
CFMutableDictionaryRef myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks); 

// create a context for drawing
CGContextRef context = CGPDFContextCreateWithURL ((CFURLRef)outurl, &outRect, myDictionary); // 5
CFRelease(myDictionary);

for(int i = 1; i <= ct; ++i) {
    //page render
    CGPDFPageRef pdfPage =  CGPDFDocumentGetPage(originalPDF, i);
    CGRect pageMediaBox = CGPDFPageGetBoxRect(pdfPage, kCGPDFMediaBox); 
    CGContextBeginPage (context, &pageMediaBox); 
    CGContextDrawPDFPage(context, pdfPage);
    CGContextEndPage (context); 
}

CGContextClosePath(context);
CGContextRelease(context);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM