How can I use the PoDoFo library for annotating PDFs on iOS?

邮差的信 提交于 2019-12-09 00:36:01

问题


I would like to annotate PDFs within an iOS application. I came across the PoDoFo library, but I'm not sure how to use this in an iOS application.

Is it possible to use this library to annotate PDFs on iOS? If so, how?


回答1:


Please do something like this.

+ (void)createUTFAnnotationTextOnPage:(NSInteger)pageIndex 
                                  doc:(PdfDocument*)doc   // PdfMemDocument instance
                                 rect:(PdfRect)rect 
                                title:(NSString*)title 
                              content:(NSString*)content
                                bOpen:(Boolean)bOpen
                               colorR:(double)r 
                               colorG:(double)g 
                               colorB:(double)b {
    PdfPage* pPage = doc->GetPage(pageIndex);
    if (! pPage) {
        // couldn't get that page
        return;
    }
    PdfAnnotation* anno;

    anno = pPage->CreateAnnotation(ePdfAnnotation_Text, rect);

    PdfString sTitle(reinterpret_cast<const pdf_utf8*>([title UTF8String]));
    PdfString sContent(reinterpret_cast<const pdf_utf8*>([content UTF8String]));

    anno->SetTitle(sTitle);
    anno->SetContents(sContent);
    anno->SetColor(r, g, b);
    anno->SetOpen(bOpen);
}

// ----------- 
    PdfError::EnableDebug(false);   // or true
    NSString* inFile = [[NSBundle mainBundle] pathForResource:@"forTesting.pdf" ofType:nil];
    PoDoFo::PdfMemDocument doc( [inFile UTF8String] );
    [YourPodofoObj createUTFAnnotationTextOnPage:0 doc:&doc rect:PdfRect(50, 50, 50, 50) title:@"author by XX" content:@"I use it for test" bOpen:true colorR:1.0 colorG:.0 colorB:.0];
    doc.Write(OUT_PUT_PATH);



回答2:


The Above answer only provides you way to add Annotations (of Type FreeText etc). Generally you would want to create/Modify Fields. I normally use PdfTextFields, PdfCheckboxes, PdfSignature Field for this purpose. Hope that helps

    PdfMemDocument memDoc;
    PdfFileInputStream fileInputStream(filePath);
    char *srcBuffer = new char[fileInputStream.GetFileLength()];
    size_t srcLen = fileInputStream.GetFileLength();
    fileInputStream.Read(srcBuffer,srcLen);


    PdfOutputDevice outputDevice(filePath);
    outputDevice.Write(srcBuffer,srcLen);
    memDoc.Load(srcBuffer,srcLen);

    PdfTextField txtField  = PdfTextField( pPage,PdfRect(50, 50, 50, 50),&memDoc);
    txtField.SetFieldName(@"SomeUniqueName");

    memDoc.Write(&outputDevice);
    outputDevice.Flush();


来源:https://stackoverflow.com/questions/7643771/how-can-i-use-the-podofo-library-for-annotating-pdfs-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!