My favorites | Sign in
Project Home Downloads Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Project Information
Members
Featured
Downloads

LowLevelPDF

LowLevelPDF is a small Java library for creating PDF streams on a very low level (as the name hypothesizes).

It's quite easy, if you know how a PDF file is built internal. Here is the famous "Hello World!" example:

    // First create a document to work with.
    LLDocument doc = new LLDocument();

    // We need some indirect PDF objects.
    LLDictionaryObject catalog = new LLDictionaryObject("Catalog").useAsLabeledObject(doc);
    LLDictionaryObject pages = new LLDictionaryObject("Pages").useAsLabeledObject(doc);
    LLDictionaryObject page = new LLDictionaryObject("Page").useAsLabeledObject(doc);
    LLStreamObject pageContent = new LLStreamObject().useAsLabeledObject(doc);

    // "Hello world!" content and some data for the first page.
    pageContent.stream = "BT 10 10 Td /TN 12.0 Tf (Hello world!) Tj ET".getBytes();
    page.add("MediaBox", new LLArrayObject().add(0).add(0).add(80).add(25))
      .add("Parent", pages)
      .add("Contents", pageContent);

    // Put the catalog into the trailer.
    doc.trailer.add("Root", catalog);

    // The Pages and Page objects.
    catalog.add("Pages", pages);
    LLArrayObject kids = new LLArrayObject();
    pages.add("Kids", kids).add("Count", new LLArraySizeNonObject(kids));
    kids.add(page);

    // Add font to page.
    LLDictionaryObject resources = new LLDictionaryObject();
    page.add("Resources", resources);
    LLDictionaryObject font = new LLDictionaryObject();
    resources.add("Font", font);
    LLDictionaryObject tn = new LLDictionaryObject("TN")
      .add("Type", new LLNameObject("Font"))
      .add("Subtype", new LLNameObject("Type1"))
      .add("BaseFont", new LLNameObject("Times-Roman"));
    font.add("TN", tn);

    // Create document.
    FileOutputStream out = new FileOutputStream("HelloWorld.pdf");
    doc.write(out);
    out.close();
    out = new FileOutputStream("HelloWorld-nice.pdf");
    doc.writeNice(out);
    out.close();

As you can see, there are written two PDF files: "HelloWorld.pdf" contains the PDF document stream in a quite short way. "HelloWorld-nice.pdf" has the same content but pimped to be more human readable:

HelloWorld.pdf

%PDF-1.7
1 0 obj
<</Type/Catalog/Pages 2 0 R>>
endobj
2 0 obj
<</Type/Pages/Count 1/Kids[3 0 R]>>
endobj
3 0 obj
<</Parent 2 0 R/Contents 4 0 R/Type/Page/Resources<</Font<</TN<</BaseFont/Times-Roman/Type/Font/Subtype/Type1>>>>>>/MediaBox[0 0 80 25]>>
endobj
4 0 obj
<</Length 44>>
stream
BT 10 10 Td /TN 12.0 Tf (Hello world!) Tj ET
endstream
endobj
xref
0 5
0000000000 65535 f
0000000009 00000 n
0000000054 00000 n
0000000105 00000 n
0000000258 00000 n
trailer
<</Root 1 0 R/Size 5>>
startxref
350
%%EOF

HelloWorld-nice.pdf

%PDF-1.7
1 0 obj
<<
 /Type  /Catalog
 /Pages 2 0 R
>>
endobj
2 0 obj
<<
 /Type  /Pages
 /Count 1
 /Kids
 [
  3 0 R
 ]
>>
endobj
3 0 obj
<<
 /Parent    2 0 R
 /Contents  4 0 R
 /Type      /Page
 /Resources
 <<
  /Font
  <<
   /TN
   <<
    /BaseFont /Times-Roman
    /Type     /Font
    /Subtype  /Type1
   >>
  >>
 >>
 /MediaBox
 [
  0
  0
  80
  25
 ]
>>
endobj
4 0 obj
<<
 /Length 44
>>
stream
BT 10 10 Td /TN 12.0 Tf (Hello world!) Tj ET
endstream
endobj
xref
0 5
0000000000 65535 f
0000000009 00000 n
0000000061 00000 n
0000000128 00000 n
0000000363 00000 n
trailer
  <<
   /Root 1 0 R
   /Size 5
  >>
startxref
458
%%EOF
Powered by Google Project Hosting