2012-06-15 3 views
0

데스크탑과 핸드 헬드 장치간에 문서를 보내고 있으며 다음과 같이 PDF에 메타 데이터 헤더를 추가하려고합니다. PDFDocument에 사용자 정의 메타 데이터 추가하기

<CUSTOM_HEADER>\n 
{"fileInfoEncodedInJson\": 
    {"filename":"My Print Out", 
    "filesize\":"630", 
    "filedesc":"",} 
    }\n 
</CUSTOM_HEADER>\n 
… file contents … 

은 내가 documentAttributessetDocumentAttributes 방법을 공급 PDFKit 및 PDFDocument를 사용하고 있지만, 사용자 정의 헤더이기 때문에, 내가 속성을 설정할 때 지속하는 것하고 파일을 저장하지 않습니다

NSURL *path = [NSURL fileURLWithPath:@"/Users/username/Desktop/file.pdf"]; 
PDFDocument *document = [[PDFDocument alloc] initWithURL:path]; 

NSDictionary *docAttributes = [self.document documentAttributes]; 
NSMutableDictionary *newAttributes = [[NSMutableDictionary alloc] initWithDictionary:docAttributes]; 
[newAttributes setObject: @"Custom header contents" forKey:@"Custom header"]; 
docAttributes = [[NSDictionary alloc] initWithDictionary: newAttributes]; 

[document setDocumentAttributes: docAttributes]; 

//Here Custom Header is an entry in the dictionary 

[self.document writeToFile:@"/Users/username/Desktop/UPDATEDfile.pdf"]; 

//Here the UPDATEDfile.pdf does not contain the custom header 

나는 두루의 비슷한 질문 (예 : here, 예 : cocoadev)을 발견했지만 답이 없습니다. 누구든지 PDF 파일에 사용자 정의 (즉, 문서 속성 키 아래 제공된 8 개의 미리 정의 된 상수) 헤더를 저장하는 방법을 알고 있습니까?

답변

1

나는 실제로 기존의 헤더를 편집하지 않았고 간단히 NSMutableData 객체를 만들었으며 텍스트 데이터와 PDF 데이터를 먼저 추가 한 다음이 데이터를 원하는 경로에 저장했습니다.

NSString *header = [NSString stringWithFormat:@"<CUSTOM_HEADER>\n {\"fileInfoEncodedInJson\": {\"filename\":\"My Print Out\", \"filesize\":\"630\",\"filedesc\":\"\",} }\n </CUSTOM_HEADER>\n"]; 

// Append header to PDF data 
NSMutableData *data = [NSMutableData dataWithData:[header dataUsingEncoding:NSWindowsCP1252StringEncoding]]; 
[data appendData:[doc dataRepresentation]]; 

[data writeToFile:@"/Users/username/Desktop/UPDATEDfile.pdf" atomically:NO]; 

이로 인해 Adobe에서 열리는 PDF 파일이 생성되고 볼 때 헤더가 보이지 않습니다.

+0

이 상황에서는 개체 오프셋이 잘못되어 파일이 손상되었습니다. Adobe Reader는 파일을 열 수 있습니다 (상호 참조를 복구 함). 그러나 다른 PDF 프로세서는 이에 대해 불평 할 수 있습니다. 목표가 Adobe Reader만이면 괜찮습니다. – iPDFdev

+0

맞습니다. 필자는이 헤더가 필요한 모바일 응용 프로그램으로 파일을 보내고 Adobe Reader 응용 프로그램에 문서를 푸시합니다. 그러나 헤드 업에 감사드립니다. – Katie

관련 문제