Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

38
LINES

< > TinyBrain | #1007365 - Make a "Hello World" PDF document using Apache PDFBox [WORKS]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar - homepage

Uses 3862K of libraries. Click here for Pure Java version (844L/6K/20K).

!7

lib 1007364 // Apache PDFBox
lib 1007366 // Apache FontBox
lib 1003619 // Apache Commons Logging

import org.apache.pdfbox.pdmodel.*;
import org.apache.pdfbox.pdmodel.font.*;

p {
  // Create a document and add a page to it
  new PDDocument document;
  new PDPage page;
  document.addPage(page);
  
  // Create a new font object selecting one of the PDF base fonts
  PDFont font = PDType1Font.HELVETICA_BOLD;
  
  // Start a new content stream which will "hold" the to be created content
  PDPageContentStream contentStream = new PDPageContentStream(document, page);
  
  // Define a text content stream using the selected font, moving the cursor and drawing the text "Hello World"
  contentStream.beginText();
  contentStream.setFont(font, 12);
  contentStream.moveTextPositionByAmount( 100, 700 );
  contentStream.drawString("Hello World");
  contentStream.endText();
  
  // Make sure that the content stream is closed:
  contentStream.close();
  
  // Save the results and ensure that the document is properly closed:
  File file;
  document.save(file = prepareProgramFile("Hello World.pdf"));
  document.close();
  
  print("Saved: " + f2s(file) + " (" + file.length() + " bytes)");
}

download  show line numbers  debug dex   

Travelled to 2 computer(s): cfunsshuasjs, tvejysmllsmz

No comments. add comment

Snippet ID: #1007365
Snippet name: Make a "Hello World" PDF document using Apache PDFBox [WORKS]
Eternal ID of this version: #1007365/6
Text MD5: f2ecea7dc02fde8abeb960dc2afe9d2d
Transpilation MD5: 5a1596d5c7fbbe07f5df9f5761a7a2fd
Author: stefan
Category: javax / pdf
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-03-16 19:28:55
Source code size: 1230 bytes / 38 lines
Pitched / IR pitched: No / No
Views / Downloads: 68 / 46
Version history: 5 change(s)
Referenced in: [show]