You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ja...@apache.org on 2015/01/23 19:32:02 UTC
svn commit: r1654295 - in
/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel:
HelloWorldType1.java HelloWorldType1AfmPfb.java
Author: jahewson
Date: Fri Jan 23 18:32:02 2015
New Revision: 1654295
URL: http://svn.apache.org/r1654295
Log:
PDFBOX-2589: Simplify HelloWorldType1AfmPfb example
Added:
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1.java
- copied, changed from r1654284, pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java
Removed:
pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java
Copied: pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1.java (from r1654284, pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java)
URL: http://svn.apache.org/viewvc/pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1.java?p2=pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1.java&p1=pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java&r1=1654284&r2=1654295&rev=1654295&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1AfmPfb.java (original)
+++ pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/HelloWorldType1.java Fri Jan 23 18:32:02 2015
@@ -17,10 +17,8 @@
package org.apache.pdfbox.examples.pdmodel;
-import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
-
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.edit.PDPageContentStream;
@@ -28,94 +26,46 @@ import org.apache.pdfbox.pdmodel.font.PD
import org.apache.pdfbox.pdmodel.font.PDType1Font;
/**
- * This is an example that creates a simple document
- * with a Type 1 font (afm + pfb).
- *
- * @author <a href="mailto:m.g.n@gmx.de">Michael Niedermair</a>
- * @version $Revision: 1.2 $
+ * Creates a simple document with a Type 1 font (.afm + .pfb).
*/
-public class HelloWorldType1AfmPfb
+public class HelloWorldType1
{
-
- /**
- * create the second sample document from the PDF file format specification.
- *
- * @param file The file to write the PDF to.
- * @param message The message to write in the file.
- * @param fontfile The ttf-font file.
- *
- * @throws IOException If there is an error writing the data.
- */
- public void doIt(String file, String message, String fontfile)
- throws IOException
+ public static void main(String[] args) throws IOException
{
+ if (args.length != 3)
+ {
+ System.err.println("usage: " + HelloWorldType1.class.getName() +
+ " <output-file> <Message> <afm-file>");
+ System.exit(1);
+ }
- // the document
- PDDocument doc = null;
+ String file = args[0];
+ String message = args[1];
+ String afmPath = args[2];
+
+ PDDocument doc = new PDDocument();
try
{
- doc = new PDDocument();
-
PDPage page = new PDPage();
doc.addPage(page);
- PDFont font = createPDType1AfmPfbFont(doc, fontfile);
- PDPageContentStream contentStream = new PDPageContentStream(doc,
- page);
- contentStream.beginText();
- contentStream.setFont(font, 12);
- contentStream.newLineAtOffset(100, 700);
- contentStream.showText(message);
- contentStream.endText();
- contentStream.close();
- doc.save(file);
- System.out.println(file + " created!");
- }
- finally
- {
- if (doc != null)
- {
- doc.close();
- }
- }
- }
+ PDFont font = new PDType1Font(doc, new FileInputStream(afmPath),
+ new FileInputStream(afmPath.replace(".afm", ".pfb")));
- private PDType1Font createPDType1AfmPfbFont(PDDocument doc, String afmName) throws IOException
- {
- return new PDType1Font(doc,
- new BufferedInputStream(new FileInputStream(afmName)),
- new BufferedInputStream(new FileInputStream(afmName.replaceAll(".AFM", "").
- replaceAll(".afm", "") + ".pfb")));
- }
-
- /**
- * This will create a hello world PDF document
- * with a ttf-font.
- * <br />
- * see usage() for commandline
- *
- * @param args Command line arguments.
- */
- public static void main(String[] args) throws IOException
- {
+ PDPageContentStream contents = new PDPageContentStream(doc, page);
+ contents.beginText();
+ contents.setFont(font, 12);
+ contents.newLineAtOffset(100, 700);
+ contents.showText(message);
+ contents.endText();
+ contents.close();
- HelloWorldType1AfmPfb app = new HelloWorldType1AfmPfb();
- if (args.length != 3)
- {
- app.usage();
+ doc.save(file);
+ System.out.println(file + " created!");
}
- else
+ finally
{
- app.doIt(args[0], args[1], args[2]);
+ doc.close();
}
}
-
- /**
- * This will print out a message telling how to use this example.
- */
- private void usage()
- {
- System.err.println("usage: " + this.getClass().getName()
- + " <output-file> <Message> <afm-file>");
- }
}