franck
franck

Reputation: 33

EDIT CGPDFObject of the pdf

i try long time to parse a pdf file with quartz cgpdf api, but my question is true that CGPDF API can't edit the object saved in the pdf?

it mean the api used just only for reading file?

Upvotes: 1

Views: 660

Answers (2)

silicontrip
silicontrip

Reputation: 1026

I would love to be able to edit PDFContentStreams but this is not possible with the frameworks in PDFKit or Core Graphics. None of the PDF classes are mutable or allow you to specify initialising them from an alternate data source. All the PDFObject classes simply reference the original PDFDocument and will not allow you to replace an object or change its data.

The closest thing you can do, is create a CGPDFContext and draw a page from another PDF document, then draw additional Quartz graphics to it. Along the lines of;

void drawPageToNewPDF (CFURLRef newpdfname, CGRect mediabox, CFDictionaryRef auxdata, PDFPage* pp)
{
    CGContextRef cgpdf = CGPDFContextCreateWithURL(newpdfname, &mediabox, auxdata);

    CGPDFContextBeginPage(cgpdf, NULL);
    CGContextDrawPDFPage(cgpdf, [pp pageRef]);
// draw extra graphics here
    CGPDFContextEndPage(cgpdf);
    CGPDFContextClose(cgpdf);
}

But this doesn't allow you to change existing data in the document. The best you could do is put a white (or background colour) solid rectangle over the object you want to change and then redraw it using Core Graphic primitives.

The only way to do this properly is to use a 3rd party library that supports editing the PDF objects. I haven't been able to find a non-commercial open source PDF library for objective-c. There are a few C++ libraries which wouldn't be too difficult to write wrappers for.

I'm just about to go down this path so I don't have any example code yet. PoDoFo looks the most promising for detailed pdf editing http://podofo.sourceforge.net/ QPDF for just editing a PDFs structure http://qpdf.sourceforge.net/

Upvotes: 0

Olof_t
Olof_t

Reputation: 768

Yes, it is just to read files. E.g, the dictionaries are just representations of something that looks like this:

3 0 obj 
<< /Font
    << /F0
        << /Type /Font
            /BaseFont /Times-Italic
            /Subtype /Type1 >> 
    >>
>>
endobj

Which may be referenced, making reading much easier than editing, if you remove a certain object the whole page might need to be laid out differently. I guess it wouldn't be too difficult to make a writer for the CGPDF API, but it is certainly non-trivial. For writing new pdfs you can use an ordinary graphics contexts.

(Sorry for being a year late to the party, but there might be others).

Upvotes: 1

Related Questions