Op 2/08/2012 12:17, Robert Bowen schreef:
I have a ballot with a bunch of different contests (President, Vice-president, etc.) each with its candidates. Next to each candidate is an oval. I'd like to use iText to check a given oval for each contest.

The ovals are not within an AcroForm, and they are not checkboxes. I opened my pdf with Acrobat and selected a random oval and they seem to be just normal objects ... perhaps graphic objects, I'm not sure, I couldn't tell.

I used a combination of PDFReader, PDFStamper and Document to uncompress the PDF, to be able to edit it in a normal text editor, and it seems that each oval looks something like this:

    01 g
    89.04 650.52 41.22 24 re
    f
    1 J 1 j 0.2 w 10 M
    1 i
    116.28 662.58 m
    116.28 664.56 112.44 666.12 107.7 666.12 c
    102.96 666.12 99.12 664.56 99.12 662.58 c
    99.12 660.6 102.96 658.98 107.7 658.98 c
    112.44 658.98 116.28 660.6 116.28 662.58 c
    b*


and if I manually add a '0 g' between '1 i' and '116.28 662.58 m', it marks the oval. But if I write a program in Java to do this, to add '0 g' for each oval, the resulting PDF is corrupt, because ... well, you can't just edit a PDF like that, you have to update the stream, the dictionary, etc.

The path of the oval is defined using one moveTo (m) and four curveTo (c) operators.
The path is drawn using a closePathEoFillStroke command (b*).
Close path means: add a path between start and end point if the curves don't form a closed shape Eo means: use the Even-Odd algorithm to find out which areas need to be filled.
Fill means: fill the shape using the fill color
Stroke means: draw the borders using the stroke color

The fill color is defined with the g operator: 01 is the gray fill value (0 is black, 1 is white). The line width is 0.2 user units (w), but I don't see any operator defining the stroke color (maybe it's just the default: black).

If you manually add "0 g" after "2 i", then the fill color will be black, but you need to be very careful when doing so. If you're sure about the PDF syntax, then this is a code sample I used to change the content of a form XObject.
I'm sure you can adapt it to change the content stream of the page:

// read file my.pdf
PdfReader r = new PdfReader("my.pdf");
// get page dictionary of page 1
PdfDictionary p = r.getPageN(1);
// get the resources
PdfDictionary ps = p.getAsDict(PdfName.RESOURCES);
// get the XObjects
PdfDictionary xo = ps.getAsDict(PdfName.XOBJECT);
// get the content stream of a specific XObject identified by key
PRStream stream = (PRStream)xo.getAsStream(key);
// change its content (in this case: empty the content)
stream.setData(new byte[]{});
// create a new (altered) PDF
PdfStamper stamper = new PdfStamper(r, new FileOutputStream("new.pdf"));
stamper.close();

With this code, the structure of your PDF will not be corrupted.
However: if you use the wrong bytes, you can corrupt the content of a page.
Reading the syntax snippet however, I have the impression you know what you're doing.
------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
iText-questions mailing list
iText-questions@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/itext-questions

iText(R) is a registered trademark of 1T3XT BVBA.
Many questions posted to this list can (and will) be answered with a reference 
to the iText book: http://www.itextpdf.com/book/
Please check the keywords list before you ask for examples: 
http://itextpdf.com/themes/keywords.php

Reply via email to