2012-09-18 5 views
3

J'essaie de générer un PDF dans du code qui a des annotations telles que des widgets et des liens. Je peux générer le PDF et modifier les informations sur le document et le MediaBox de la page, mais je ne peux pas comprendre comment ajouter aux annotations. J'essaye de l'ajouter au dictionnaire de page mais pour une raison quelconque le pdf ne l'accepte pas.Comment créer un PDF avec Annots?

Est-ce que quelqu'un sait comment ajouter des annotations à un fichier PDF nouvellement créé? Voici le code que je dois générer le PDF et le dictionnaire Annotation test Je suis en train d'y ajouter:

-(void) createPDFFile: (CGPDFPageRef) pageRef{ 

CGContextRef pdfContext; 

CFStringRef path; 

CFURLRef url; 


CFMutableDictionaryRef myDictionary = NULL; 

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
const char *filepath = [[documentsDirectory stringByAppendingString:@"/fileTest.pdf"] UTF8String]; 

path = CFStringCreateWithCString (NULL, filepath, kCFStringEncodingUTF8); 

url = CFURLCreateWithFileSystemPath (NULL, path, kCFURLPOSIXPathStyle, 0); 

CFRelease (path); 

myDictionary = CFDictionaryCreateMutable(NULL, 0, 

             &kCFTypeDictionaryKeyCallBacks, 

             &kCFTypeDictionaryValueCallBacks); 

CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File")); 

CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("My Name")); 

CGRect pageRect = self.bounds; 
pdfContext = CGPDFContextCreateWithURL (url, &pageRect, myDictionary); // 5 

CFRelease(myDictionary); 

CFRelease(url); 

CGRect mediaBox; 
mediaBox = CGRectMake (0, 0, pageRect.size.width, 100); 
CFStringRef myKeys[1]; 

CFTypeRef myValues[1]; 

myKeys[0] = CFSTR("MediaBox"); 

myValues[0] = (CFTypeRef) CFDataCreate(NULL,(const UInt8 *)&mediaBox, sizeof (CGRect)); 

CFDictionaryRef pageDictionary = CFDictionaryCreate(NULL, (const void **) myKeys, 
                (const void **) myValues, 1, 
                &kCFTypeDictionaryKeyCallBacks, 
                & kCFTypeDictionaryValueCallBacks); 
CFMutableDictionaryRef pageDictionaryMutable = CFDictionaryCreateMutableCopy(NULL, 0, pageDictionary); 

CFMutableDictionaryRef metaDataDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks,&kCFTypeDictionaryValueCallBacks); 

CFDictionarySetValue(metaDataDictionary, CFSTR("DA"), CFSTR("/Helvetica 10 Tf 0 g")); 
CFDictionarySetValue(metaDataDictionary, CFSTR("F"), CFSTR("4")); 
CFDictionarySetValue(metaDataDictionary, CFSTR("FT"), CFSTR("Tx")); 
CFDictionarySetValue(metaDataDictionary, CFSTR("Subtype"), CFSTR("Widget")); 
CFDictionarySetValue(metaDataDictionary, CFSTR("T"), CFSTR("undefined")); 
CFDictionarySetValue(metaDataDictionary, CFSTR("Type"), CFSTR("Annot")); 
CFDictionarySetValue(metaDataDictionary, CFSTR("V"), CFSTR("")); 
CFMutableArrayRef array = CFArrayCreateMutable(kCFAllocatorDefault,0, &kCFTypeArrayCallBacks); 
CFArrayInsertValueAtIndex(array, 0, metaDataDictionary); 

CFDictionarySetValue(pageDictionaryMutable, CFSTR("Annots"), array); 


CGPDFContextBeginPage (pdfContext, pageDictionaryMutable); 


CGPDFContextEndPage (pdfContext); 

CGContextRelease (pdfContext); 

CFRelease(pageDictionary);} 

Impression sur l'CFMutableDictionary pageDictionaryMutable montre que le dictionnaire Annots doit être ajouté au format PDF ainsi que le MediaBox:

(gdb) po pageDictionaryMutable 
{ 
    Annots =  (
       { 
      DA = "/Helvetica 10 Tf 0 g"; 
      F = 4; 
      FT = Tx; 
      Subtype = Widget; 
      T = undefined; 
      Type = Annot; 
      V = ""; 
     } 
    ); 
    MediaBox = <00000000 00000000 00008044 0000c842>; 
} 

Le Media Box prend mais Annots ne le fait pas. S'il vous plaît aider quelqu'un!

+0

Avez-vous obtenu votre réponse? étiez-vous en mesure d'ajouter des annotations au dictionnaire "annotations" de la page? –

Répondre

2

Jetez un oeil à la bibliothèque PoDoFo: http://podofo.sourceforge.net

PoDoFo est autorisé dans certaines parties sous licence GPL et LGPL.

Avec lui, vous pouvez modifier le fichier pdf et y ajouter des annotations. C'est portable sur iOS. Regardez la documentation, si elle peut gérer ce que vous voulez et voir comment cela fonctionne. Si vous êtes intéressé, vous pouvez également consulter le this sample project on GitHub. Là, j'ai intégré la bibliothèque à un projet iOS et vous pouvez ajouter FreeText et Square Annotations en utilisant un iPad.

+0

la bibliothèque podofo peut être téléchargée à partir de podofo.sourceforge.net - c'est la version 0.9.0, que j'ai utilisée. Il suffit de faire le projet xcode (il est décrit dans le readme) et de le compiler pour le simulateur et pour le périphérique. Je les ai ensuite combinés à un fichier de bibliothèque (avec lipo), mais vous pouvez simplement ajouter les deux fichiers au projet. –