私はしばらくの間、iOSアプリでPDFを操作することを研究してきました。オペレーターのスキャンやUIWebViewでのPDFの表示など、パズルのいくつかのピースを理解しました。ただし、私が実際に行う必要があるのは、PDFドキュメント内の編集可能なフィールドを識別することです。
理想的には、フィールドと直接対話できるようにしたいと思いますが、それは非常に困難に聞こえ、明白な最初のステップではありません。私はすでに、この方法でPDFを操作できるWindowsサービスとやり取りしており、編集可能なフィールドを識別し、フォームビューでユーザーからフィールドデータを収集し、そのデータをサーバーにPOSTすることで解決できます。問題は、フィールドを識別する方法がわからないことです。私はI-9やW-4などの政府発行のPDFとやり取りしているため、PDFの作成やフィールドの名前を制御することはできません。そのため、動的に抽出する必要があります。任意のヘルプおよび/または参照をいただければ幸いです。
AppleのQuatrz2Dプログラミングガイドの[このリファレンス](https://developer.apple.com/library/mac/#documentation/graphicsimaging/conceptual/drawingwithquartz2d/dq_pdf_scan/dq_pdf_scan.html "PDF Document Parsing")を使用しています。 PDFをスキャンするときにオペレーターのコールバックをトリガーしますが、編集可能なフィールドを見つけるのに役立ちません。
また、ユーザーに表示するPDFデータをUIWebViewにロードするだけです。
[_webView loadData:decodedData MIMEType:@"application/pdf" textEncodingName:@"utf-8" baseURL:nil];
アップデート:
カタログ内のすべての可能なオブジェクトタイプをトラバースするために、PDFヘルパークラス(以下に表示)を作成しました。元々、配列内のネストされた辞書を処理していなかったため、フォームフィールドが表示されませんでした。修正すると、無限ループを開始する循環再帰呼び出しを回避するために考慮する必要のある親参照があることに気付きました。以下のコードは、ドキュメントカタログからの豊富な情報を示しています。今、私はそれを解析して、必要なフォームフィールドを分離する必要があります。
PDFHelper.h
#import <Foundation/Foundation.h>
id selfClass;
@interface PDFHelper : NSObject
@property (nonatomic, strong) NSData *pdfData;
@property (nonatomic, strong) NSMutableDictionary *pdfDict;
@property (nonatomic) int catalogLevel;
-(NSArray *) copyPDFArray:(CGPDFArrayRef)arr referencingDictionary:(CGPDFDictionaryRef)dict referencingKey:(const char *)key;
-(NSArray *) getFormFields;
-(CGPDFDictionaryRef) getDocumentCatalog;
@end
PDFHelper.m
#import "PDFHelper.h"
#import "FileHelpers.h"
#import "Log.h"
@implementation PDFHelper
@synthesize pdfData = _pdfData;
@synthesize pdfDict = _pdfDict;
@synthesize catalogLevel = _catalogLevel;
-(id)init
{
self = [super init];
if(self)
{
selfClass = self;
_pdfDict = [[NSMutableDictionary alloc] init];
_catalogLevel = 1;
}
return self;
}
-(NSArray *) getFormFields
{
CGPDFDictionaryRef acroForm = NULL;
if (CGPDFDictionaryGetDictionary([self getPdfDocDictionary], "AcroForm", &acroForm))
CGPDFDictionaryApplyFunction(acroForm, getDictionaryObjects, acroForm);
return [_pdfDict objectForKey:@"XFA"];
}
-(CGPDFDictionaryRef) getDocumentCatalog
{
CGPDFDictionaryRef docCatalog = [self getPdfDocDictionary];
CGPDFDictionaryApplyFunction(docCatalog, getDictionaryObjects, docCatalog);
return docCatalog;
}
-(CGPDFDictionaryRef) getPdfDocDictionary
{
NSURL *pdf = [[NSURL alloc] initFileURLWithPath:[FileHelpers pathInLibraryDirectory:@"file.pdf"]];
[_pdfData writeToFile:[pdf path] atomically:YES];
CGPDFDocumentRef pdfDocument = CGPDFDocumentCreateWithURL((__bridge CFURLRef)pdf);
CGPDFDictionaryRef returnDict = CGPDFDocumentGetCatalog(pdfDocument);
return returnDict;
}
void getDictionaryObjects (const char *key, CGPDFObjectRef object, void *info) {
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"key: %s", key]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
CGPDFDictionaryRef contentDict = (CGPDFDictionaryRef)info;
CGPDFObjectType type = CGPDFObjectGetType(object);
switch (type) {
case kCGPDFObjectTypeNull: {
[Log LogDebug:[NSString stringWithFormat:@"*****pdf null value"]];
break;
}
case kCGPDFObjectTypeBoolean: {
CGPDFBoolean objectBoolean;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeBoolean, &objectBoolean)) {
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf boolean value: %@", [NSNumber numberWithBool:objectBoolean]]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
[[selfClass pdfDict] setObject:[NSNumber numberWithBool:objectBoolean]
forKey:[NSString stringWithCString:key encoding:NSUTF8StringEncoding]];
}
break;
}
case kCGPDFObjectTypeInteger: {
CGPDFInteger objectInteger;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeInteger, &objectInteger)) {
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf integer value: %ld", (long int)objectInteger]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
[[selfClass pdfDict] setObject:[NSNumber numberWithInt:objectInteger]
forKey:[NSString stringWithCString:key encoding:NSUTF8StringEncoding]];
}
break;
}
case kCGPDFObjectTypeReal: {
CGPDFReal objectReal;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeReal, &objectReal)) {
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf real value: %ld", (long int)objectReal]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
[[selfClass pdfDict] setObject:[NSNumber numberWithInt:objectReal]
forKey:[NSString stringWithCString:key encoding:NSUTF8StringEncoding]];
}
break;
}
case kCGPDFObjectTypeName: {
const char *name;
if (CGPDFDictionaryGetName(contentDict, key, &name))
{
NSString *dictName = [[NSString alloc] initWithCString:name encoding:NSUTF8StringEncoding];
if (dictName)
{
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf name value: %@", dictName]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
[[selfClass pdfDict] setObject:dictName
forKey:[NSString stringWithCString:key encoding:NSUTF8StringEncoding]];
}
}
break;
}
case kCGPDFObjectTypeString: {
CGPDFStringRef objectString;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeString, &objectString)) {
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf string value: %@", (__bridge NSString *)CGPDFStringCopyTextString(objectString)]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
[[selfClass pdfDict] setObject:(__bridge NSString *)CGPDFStringCopyTextString(objectString)
forKey:[NSString stringWithCString:key encoding:NSUTF8StringEncoding]];
}
break;
}
case kCGPDFObjectTypeArray: {
CGPDFArrayRef objectArray;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeArray, &objectArray)) {
NSArray *myArray=[selfClass copyPDFArray:objectArray referencingDictionary:contentDict referencingKey:key];
[[selfClass pdfDict] setObject:myArray
forKey:[NSString stringWithCString:key encoding:NSUTF8StringEncoding]];
}
break;
}
case kCGPDFObjectTypeDictionary: {
CGPDFDictionaryRef objectDictionary;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeDictionary, &objectDictionary)) {
NSString *logString = @"Found dictionary";
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
//[Log LogDebug:logString];
NSString *keyCheck = [[NSString alloc] initWithUTF8String:key];
if (![keyCheck isEqualToString:@"Parent"] && ![keyCheck isEqualToString:@"P"])
{
[selfClass setCatalogLevel:[selfClass catalogLevel] + 1];
CGPDFDictionaryApplyFunction(objectDictionary, getDictionaryObjects, objectDictionary);
[selfClass setCatalogLevel:[selfClass catalogLevel] - 1];
}
}
break;
}
case kCGPDFObjectTypeStream: {
CGPDFStreamRef objectStream;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeStream, &objectStream)) {
CGPDFDictionaryRef dict = CGPDFStreamGetDictionary( objectStream );
CGPDFDataFormat fmt = CGPDFDataFormatRaw;
CFDataRef streamData = CGPDFStreamCopyData(objectStream, &fmt);
NSData *data = [[NSData alloc] initWithData:(__bridge NSData *)(streamData)];
[data writeToFile:[FileHelpers pathInDocumentDirectory:@"data.dat"] atomically:YES];
NSString *dataString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//if (!dataString) {
// dataString = [[NSString alloc] initWithData:(__bridge NSData *)(streamData) encoding:NSUTF16StringEncoding];
// }
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf stream length: %ld - %@", (long int)CFDataGetLength( streamData ), dataString]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
NSString *keyCheck = [[NSString alloc] initWithUTF8String:key];
if( dict && ![keyCheck isEqualToString:@"Parent"] && ![keyCheck isEqualToString:@"P"])
{
[selfClass setCatalogLevel:[selfClass catalogLevel] + 1];
CGPDFDictionaryApplyFunction(dict, getDictionaryObjects, dict);
[selfClass setCatalogLevel:[selfClass catalogLevel] - 1];
}
}
}
}
}
- (NSArray *)copyPDFArray:(CGPDFArrayRef)arr referencingDictionary:(CGPDFDictionaryRef)dict referencingKey:(const char *)key
{
int i = 0;
NSMutableArray *temp = [[NSMutableArray alloc] init];
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf array count: %zu", CGPDFArrayGetCount(arr)]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
for(i=0; i<CGPDFArrayGetCount(arr); i++){
CGPDFObjectRef object;
CGPDFArrayGetObject(arr, i, &object);
CGPDFObjectType type = CGPDFObjectGetType(object);
switch(type){
case kCGPDFObjectTypeNull: {
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf array null(%d)", i]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
break;
}
case kCGPDFObjectTypeBoolean: {
CGPDFBoolean objectBool;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeBoolean, &objectBool)) {
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf array boolean value(%d): %@", i, [NSNumber numberWithBool:objectBool]]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
[temp addObject:[NSNumber numberWithBool:objectBool]];
}
break;
}
case kCGPDFObjectTypeInteger: {
CGPDFInteger objectInteger;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeInteger, &objectInteger)) {
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf array integer value(%d): %ld", i, (long int)objectInteger]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
[temp addObject:[NSNumber numberWithInt:objectInteger]];
}
break;
}
case kCGPDFObjectTypeReal:
{
CGPDFReal objectReal;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeReal, &objectReal))
{
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf array real(%d): %ld", i, (long int)objectReal]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
[temp addObject:[NSNumber numberWithInt:objectReal]];
}
break;
}
case kCGPDFObjectTypeName:
{
const char *name;
if (CGPDFDictionaryGetName(dict, key, &name))
{
NSString *dictName = [[NSString alloc] initWithCString:name encoding:NSUTF8StringEncoding];
if (dictName)
{
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf array name value(%d): %@", i, dictName]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
[[selfClass pdfDict] setObject:dictName
forKey:[NSString stringWithCString:key encoding:NSUTF8StringEncoding]];
}
}
break;
}
case kCGPDFObjectTypeString:
{
CGPDFStringRef objectString;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeString, &objectString))
{
NSString *tempStr = (__bridge NSString *)CGPDFStringCopyTextString(objectString);
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf array string(%d): %@", i, tempStr]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
[temp addObject:tempStr];
}
break;
}
case kCGPDFObjectTypeArray :
{
CGPDFArrayRef objectArray;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeArray, &objectArray))
{
NSArray *tempArr = [selfClass copyPDFArray:objectArray referencingDictionary:dict referencingKey:key];
[temp addObject:tempArr];
}
break;
}
case kCGPDFObjectTypeDictionary :
{
CGPDFDictionaryRef objectDict;
NSString *keyCheck = [[NSString alloc] initWithUTF8String:key];
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeDictionary, &objectDict) && ![keyCheck isEqualToString:@"Parent"] && ![keyCheck isEqualToString:@"P"])
{
[selfClass setCatalogLevel:[selfClass catalogLevel] + 1];
CGPDFDictionaryApplyFunction( objectDict, getDictionaryObjects, objectDict);
[selfClass setCatalogLevel:[selfClass catalogLevel] - 1];
}
break;
}
case kCGPDFObjectTypeStream :
{
CGPDFStreamRef objectStream;
if (CGPDFObjectGetValue(object, kCGPDFObjectTypeStream, &objectStream))
{
CGPDFDictionaryRef streamDict = CGPDFStreamGetDictionary( objectStream );
CGPDFDataFormat fmt = CGPDFDataFormatRaw;
CFDataRef streamData = CGPDFStreamCopyData(objectStream, &fmt);
NSString *dataString = [[NSString alloc] initWithData:(__bridge NSData *)(streamData) encoding:NSUTF8StringEncoding];
NSString *logString = [[NSString alloc] initWithString:[NSString stringWithFormat:@"pdf array stream length: (%d): %ld - %@", i, (long int)CFDataGetLength( streamData ), dataString]];
for (int i = 0; i < [selfClass catalogLevel]; i++)
logString = [NSString stringWithFormat:@"-%@", logString];
[Log LogDebug:logString];
NSString *keyCheck = [[NSString alloc] initWithUTF8String:key];
if( streamDict && ![keyCheck isEqualToString:@"Parent"] && ![keyCheck isEqualToString:@"P"])
{
[selfClass setCatalogLevel:[selfClass catalogLevel] + 1];
CGPDFDictionaryApplyFunction( streamDict, getDictionaryObjects, streamDict );
[selfClass setCatalogLevel:[selfClass catalogLevel] - 1];
}
}
}
}
}
return temp;
}
@end