2012-07-03 120 views
2

我需要使用Apple的Objective C SDK將一些自定義元數據嵌入到PDF文件中。有沒有任何工作示例代碼可以做到這一點?iOS:示例代碼或示例CGPDFContextAddDocumentMetadata

我需要的是非常基本的。打開一個現有的PDF並添加一些元數據,然後保存。

存儲信息的例子就像....客戶的姓名,聯繫電話,訂購的物品。

我看到這個「CGPDFContextAddDocumentMetadata」支持元數據,但我不知道元數據是怎麼樣的,它是如何傳遞給這個函數的。

任何幫助,將不勝感激... :)

回答

1

元數據是元數據(當然,它是由Adobe您使用XMP XML推薦)。只要你創建一個有效的CFDataRef並將它傳遞給參數2,你就可以隨意使用任何東西。例如,下面是如何將字符串「Hello World」傳遞到一個PDF的元數據:

void MakeAPDF() 
{ 

    CGRect mediaRect = CGRectMake(0, 0, 400, 600); 
    // use your own rect instead 

    CFMutableDataRef result = CFDataCreateMutable(kCFAllocatorDefault, 0); 
    CGDataConsumerRef PDFDataConsumer = CGDataConsumerCreateWithCFData(result); 

    // mark the PDF as coming from your program 
    CFMutableDictionaryRef auxInfo = CFDictionaryCreateMutable(kCFAllocatorDefault, 1, NULL, NULL); 
    CFDictionaryAddValue(auxInfo, kCGPDFContextCreator, CFSTR("Your Programs Name")); 
    CFDictionaryRef auxillaryInformation = CFDictionaryCreateCopy(kCFAllocatorDefault, auxInfo); 
    CFRelease(auxInfo); 

    // create a context to draw into 
    CGContextRef graphicContext = CGPDFContextCreate(PDFDataConsumer, &mediaRect, auxillaryInformation); 
    CFRelease(auxillaryInformation); 
    CGDataConsumerRelease(PDFDataConsumer); 

    // actually make the call to embed your String 
    NSString* str= @"Hello World"; 
    NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding]; 
    CFDataRef cfdata = CFDataCreate(NULL, [data bytes], [data length]); 

    CGPDFContextAddDocumentMetadata(graphicContext, cfdata); 

    CGContextBeginPage(graphicContext, &mediaRect); 
    // do your drawing, like this grey rectangle 
    CGContextSetGrayFillColor(graphicContext, 0.5, 0.5); 
    CGContextAddRect(graphicContext, mediaRect); 
    CGContextFillPath(graphicContext); 
    // end your drawing 

    CGContextEndPage(graphicContext); 
    CGContextFlush(graphicContext); 
    CGPDFContextClose(graphicContext);  
} 

XMP文件可能是這樣的:

<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?> 
<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.1-c041"> 
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" > 
<rdf:Description rdf:about="" xmlns:dc="http://purl.org/dc/elements/1.1/"> 
    <dc:format>application/pdf</dc:format> 
    <dc:title> 
    <rdf:Alt> 
     <rdf:li /> 
    </rdf:Alt> 
    </dc:title> 
    <dc:description> 
    <rdf:Alt> 
     <rdf:li /> 
    </rdf:Alt> 
    </dc:description> 
    <dc:creator> 
    <rdf:Seq> 
     <rdf:li /> 
    </rdf:Seq> 
    </dc:creator> 
</rdf:Description> 
<rdf:Description rdf:about="" xmlns:pdf="http://ns.adobe.com/pdf/1.3/"> 
    <pdf:Keywords /> 
    <pdf:Producer /> 
</rdf:Description> 
<rdf:Description rdf:about="" xmlns:xmp="http://ns.adobe.com/xap/1.0/"> 
    <xmp:CreatorTool /> 
    <xmp:CreateDate>2011-02-10T11:41:05+02:00</xmp:CreateDate> 
    <xmp:ModifyDate>2011-02-10T11:41:06+02:00</xmp:ModifyDate> 
</rdf:Description></rdf:RDF> 
</x:xmpmeta> 
<?xpacket end="w"?> 
+0

謝謝你的幫助! :) 我的PDF目前由UIGraphicsBeginPDFContextToFile創建,生成的PDF爲「firstDoc.pdf」。我怎樣才能讓它傳遞這個文件並添加元數據? 您的代碼創建一個新的圖形上下文並用它來生成pdf。 –

+0

當您生成上下文時,只需使用從字符串向下到元數據添加調用的所有內容並傳入上下文。另外,不要忘記upvote和接受! – CodaFi

+0

我的問題是,我在PDF文檔中有多個頁面。我不確定你的方法,但最後一次生成PDF時,我無法繼續輸入字符串。當我估計我的頁面空間不足時,我必須調用一些「創建新頁面功能」。 –

1

UIKit中有一些方法,使這個簡單的:

// use your own rect instead 
CGRect pageRect = CGRectMake(0, 0, 400, 600); 

// mark the PDF as coming from your program 
NSDictionary *auxillaryInformation = @{(NSString *)kCGPDFContextCreator: @"Koedal, Inc."}; 
UIGraphicsBeginPDFContextToFile([[self URLOfPDF] path], pageRect, auxillaryInformation); 


// Embed metadata into PDF, I'm pulling it from a textView 
NSString *PDFMetadata = self.metadataTextView.text; 
NSData *data = [PDFMetadata dataUsingEncoding:NSUTF8StringEncoding]; 

CGContextRef PDFContext = UIGraphicsGetCurrentContext(); 
CGPDFContextAddDocumentMetadata(PDFContext, (CFDataRef)data); 

NSDictionary *textAttributes = @{ NSFontAttributeName : [UIFont boldSystemFontOfSize:20], 
            NSForegroundColorAttributeName : [UIColor blackColor] 
            }; 

// do your drawing, like drawing this string 
UIGraphicsBeginPDFPage(); 
NSString *content = self.contentTextField.text; 
[content drawAtPoint:CGPointMake(150, 280) withAttributes:textAttributes]; 

// end your drawing 
UIGraphicsEndPDFContext(); 
+0

Hi Collin,do你弄清楚,如何讀取添加的元數據?我試圖使用CGPDFDocumentGetInfo - > CGPDFDictionaryGetObject方法,但沒有結果。 – alc77