You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pdfbox.apache.org by ti...@apache.org on 2019/04/29 10:50:16 UTC
svn commit: r1858360 - in
/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools: ImageToPDF.java
PDFBox.java
Author: tilman
Date: Mon Apr 29 10:50:16 2019
New Revision: 1858360
URL: http://svn.apache.org/viewvc?rev=1858360&view=rev
Log:
PDFBOX-4529: introduce ImageToPDF command line tool
Added:
pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImageToPDF.java
- copied, changed from r1858329, pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java
Modified:
pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java
Copied: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImageToPDF.java (from r1858329, pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java)
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImageToPDF.java?p2=pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImageToPDF.java&p1=pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java&r1=1858329&r2=1858360&rev=1858360&view=diff
==============================================================================
--- pdfbox/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/ImageToPDF.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/ImageToPDF.java Mon Apr 29 10:50:16 2019
@@ -14,62 +14,226 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.pdfbox.examples.pdmodel;
+package org.apache.pdfbox.tools;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
+import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
/**
- * Creates a PDF document from an image.
+ * Creates a PDF document from images.
*
- * The example is taken from the pdf file format specification.
*/
public final class ImageToPDF
{
+
+ private PDRectangle mediaBox = PDRectangle.LETTER;
+ private boolean landscape = false;
+ private boolean resize = false;
+
private ImageToPDF()
{
}
-
+
public static void main(String[] args) throws IOException
{
- if (args.length != 2)
+ // suppress the Dock icon on OS X
+ System.setProperty("apple.awt.UIElement", "true");
+
+ ImageToPDF app = new ImageToPDF();
+
+ if (args.length <= 2)
{
- System.err.println("usage: " + ImageToPDF.class.getName() + " <image> <output-file>");
- System.exit(1);
+ app.usage();
}
-
- String imagePath = args[0];
- String pdfPath = args[1];
-
+
+ List<String> imageFilenames = new ArrayList<>();
+ String pdfPath = args[args.length - 1];
+
if (!pdfPath.endsWith(".pdf"))
{
System.err.println("Last argument must be the destination .pdf file");
System.exit(1);
}
+ for (int i = 0; i < args.length - 1; i++)
+ {
+ if (args[i].startsWith("-"))
+ {
+ if ("-resize".equals(args[i]))
+ {
+ // will be modified to something more flexible
+ app.resize = true;
+ }
+ else if ("-landscape".equals(args[i]))
+ {
+ app.setLandscape(true);
+ }
+ else if ("-pageSize".equals(args[i]))
+ {
+ i++;
+ PDRectangle rectangle = createRectangle(args[i]);
+ if (rectangle == null)
+ {
+ throw new IOException("Unknown argument: " + args[i]);
+ }
+ app.setMediaBox(rectangle);
+ }
+ else
+ {
+ throw new IOException("Unknown argument: " + args[i]);
+ }
+ }
+ else
+ {
+ imageFilenames.add(args[i]);
+ }
+ }
try (PDDocument doc = new PDDocument())
{
- PDPage page = new PDPage();
+ app.createPDFFromImages(doc, imageFilenames);
+ doc.save(pdfPath);
+ }
+ }
+
+ void createPDFFromImages(PDDocument doc, List<String> imageFilenames) throws IOException
+ {
+ PDRectangle actualMediaBox = mediaBox;
+ if (landscape)
+ {
+ actualMediaBox = new PDRectangle(mediaBox.getHeight(), mediaBox.getWidth());
+ }
+
+ for (String imageFileName : imageFilenames)
+ {
+ PDPage page = new PDPage(actualMediaBox);
doc.addPage(page);
- // createFromFile is the easiest way with an image file
- // if you already have the image in a BufferedImage,
- // call LosslessFactory.createFromImage() instead
- PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath, doc);
-
- // draw the image at full size at (x=20, y=20)
try (PDPageContentStream contents = new PDPageContentStream(doc, page))
{
- // draw the image at full size at (x=20, y=20)
- contents.drawImage(pdImage, 20, 20);
-
- // to draw the image at half size at (x=20, y=20) use
- // contents.drawImage(pdImage, 20, 20, pdImage.getWidth() / 2, pdImage.getHeight() / 2);
+ PDImageXObject pdImage = PDImageXObject.createFromFile(imageFileName, doc);
+ if (resize)
+ {
+ contents.drawImage(pdImage, 0, 0, actualMediaBox.getWidth(), actualMediaBox.getHeight());
+ }
+ else
+ {
+ contents.drawImage(pdImage, 0, 0, pdImage.getWidth(), pdImage.getHeight());
+ }
}
- doc.save(pdfPath);
}
}
+
+ private static PDRectangle createRectangle(String paperSize)
+ {
+ if ("letter".equalsIgnoreCase(paperSize))
+ {
+ return PDRectangle.LETTER;
+ }
+ else if ("legal".equalsIgnoreCase(paperSize))
+ {
+ return PDRectangle.LEGAL;
+ }
+ else if ("A0".equalsIgnoreCase(paperSize))
+ {
+ return PDRectangle.A0;
+ }
+ else if ("A1".equalsIgnoreCase(paperSize))
+ {
+ return PDRectangle.A1;
+ }
+ else if ("A2".equalsIgnoreCase(paperSize))
+ {
+ return PDRectangle.A2;
+ }
+ else if ("A3".equalsIgnoreCase(paperSize))
+ {
+ return PDRectangle.A3;
+ }
+ else if ("A4".equalsIgnoreCase(paperSize))
+ {
+ return PDRectangle.A4;
+ }
+ else if ("A5".equalsIgnoreCase(paperSize))
+ {
+ return PDRectangle.A5;
+ }
+ else if ("A6".equalsIgnoreCase(paperSize))
+ {
+ return PDRectangle.A6;
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ /**
+ * Sets page size of produced PDF.
+ *
+ * @return returns the page size (media box)
+ */
+ public PDRectangle getMediaBox()
+ {
+ return mediaBox;
+ }
+
+ /**
+ * Sets page size of produced PDF.
+ *
+ * @param mediaBox
+ */
+ public void setMediaBox(PDRectangle mediaBox)
+ {
+ this.mediaBox = mediaBox;
+ }
+
+ /**
+ * Tells the paper orientation.
+ *
+ * @return true for landscape orientation
+ */
+ public boolean isLandscape()
+ {
+ return landscape;
+ }
+
+ /**
+ * Sets paper orientation.
+ *
+ * @param landscape
+ */
+ public void setLandscape(boolean landscape)
+ {
+ this.landscape = landscape;
+ }
+
+ /**
+ * This will print out a message telling how to use this example.
+ */
+ private void usage()
+ {
+ StringBuilder message = new StringBuilder();
+ message.append("Usage: jar -jar pdfbox-app-x.y.z.jar ImageToPDF [options] <image-file>..<image-file> <output-file>\n");
+ message.append("\nOptions:\n");
+ message.append(" -resize : resize to page size\n");
+ message.append(" -pageSize <pageSize> : Letter (default)\n");
+ message.append(" Legal\n");
+ message.append(" A0\n");
+ message.append(" A1\n");
+ message.append(" A2\n");
+ message.append(" A3\n");
+ message.append(" A4\n");
+ message.append(" A5\n");
+ message.append(" A6\n");
+ message.append(" -landscape : sets orientation to landscape");
+
+ System.err.println(message.toString());
+ System.exit(1);
+ }
}
Modified: pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java
URL: http://svn.apache.org/viewvc/pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java?rev=1858360&r1=1858359&r2=1858360&view=diff
==============================================================================
--- pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java (original)
+++ pdfbox/trunk/tools/src/main/java/org/apache/pdfbox/tools/PDFBox.java Mon Apr 29 10:50:16 2019
@@ -81,6 +81,9 @@ public final class PDFBox
case "PDFToImage":
PDFToImage.main(arguments);
break;
+ case "ImageToPDF":
+ ImageToPDF.main(arguments);
+ break;
case "TextToPDF":
TextToPDF.main(arguments);
break;
@@ -111,6 +114,7 @@ public final class PDFBox
+ " Encrypt\n"
+ " ExtractText\n"
+ " ExtractImages\n"
+ + " ImageToPDF\n"
+ " OverlayPDF\n"
+ " PrintPDF\n"
+ " PDFDebugger\n"