a) get to know the PDF file spec very intimately and/or b) use the source code of an open source project (eg PDFBox) as a guide.