org.jpedal.examples.images.ConvertPagesToGoogleMaps Maven / Gradle / Ivy
The newest version!
/*
* ===========================================
* Java Pdf Extraction Decoding Access Library
* ===========================================
*
* Project Info: http://www.idrsolutions.com
* Help section for developers at http://www.idrsolutions.com/java-pdf-library-support/
*
* (C) Copyright 1997-2013, IDRsolutions and Contributors.
*
* This file is part of JPedal
*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* ---------------
* ConvertPagesToGoogleMaps.java
* ---------------
*/
package org.jpedal.examples.images;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import org.jpedal.PdfDecoder;
import org.jpedal.objects.PdfPageData;
public class ConvertPagesToGoogleMaps {
public static void main(String[] args) {
try {
// General input validation/checks.
if (args.length != 2) throw new Exception("Arguments incorrect. Arguments are \"/PDF_Location/pdf.pdf\" \"/Output_Directory/\"");
if (!args[0].endsWith(".pdf")) throw new Exception(args[0] + " not a PDF.");
File pdf = new File(args[0]);
if (!pdf.exists()) throw new Exception(pdf.getAbsolutePath() + " does not exist.");
File outputDir = new File(args[1]);
if (!outputDir.exists()) throw new Exception(outputDir.getAbsolutePath() + " does not exist.");
// Begin conversion.
PdfDecoder decoder = new PdfDecoder(true);
decoder.openPdfFile(args[0]);
String pdfName = pdf.getName().substring(0, pdf.getName().length() - 4);
outputDir = new File(outputDir, pdfName);
outputDir.mkdir();
int pageCount = decoder.getPageCount();
for (int page = 1; page <= pageCount; page++) {
String pageAsString = getPageAsString(page, pageCount);
new File(outputDir.getAbsolutePath() + File.separator + pageAsString + File.separator).mkdir();
PdfPageData pageData = decoder.getPdfPageData();
int pdfPageWidth = pageData.getCropBoxWidth(page);
int pdfPageHeight = pageData.getCropBoxHeight(page);
final int tileSize = 256;
final int minZoom = 1;
final int maxZoom = 4;
// Generate tiles for each zoom level.
for (int zoomLevel = minZoom; zoomLevel <= maxZoom; zoomLevel++) {
int numTiles = (int) Math.sqrt((int) Math.pow(4, zoomLevel));
int pageSize = tileSize * numTiles;
// Make the longest side our wanted length.
float scaleBy;
if (pdfPageWidth > pdfPageHeight) {
scaleBy = pageSize / (float) pdfPageWidth;
}
else {
scaleBy = pageSize / (float) pdfPageHeight;
}
// Grab page at our wanted resolution.
BufferedImage image;
decoder.setPageParameters(scaleBy, page);
image = decoder.getPageAsImage(page);
// Make square and center page.
BufferedImage bf = new BufferedImage(pageSize, pageSize, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = bf.createGraphics();
int newX = (pageSize - image.getWidth()) / 2;
int newY = (pageSize - image.getHeight()) / 2;
g2.drawImage(image, newX, newY, image.getWidth(), image.getHeight(), null);
// Generate the required tiles.
for (int yTile = 0; yTile < numTiles; yTile++) {
int y = yTile * tileSize;
for (int xTile = 0; xTile < numTiles; xTile++) {
int x = xTile * tileSize;
BufferedImage chop = bf.getSubimage(x, y, tileSize, tileSize);
javax.imageio.ImageIO.write(chop, "png", new java.io.FileOutputStream(outputDir.getAbsolutePath() + File.separator
+ pageAsString + File.separator + "tile_" + zoomLevel + "_" + xTile + "-" + yTile + ".png"));
}
}
}
// Generate our HTML files.
try {
BufferedOutputStream CSSOutput = new BufferedOutputStream(new FileOutputStream(outputDir.getAbsolutePath() + File.separator
+ pageAsString + ".html"));
String prevPage = (page > 1) ? getPageAsString(page - 1, pageCount) : null;
String nextPage = (page < pageCount) ? getPageAsString(page + 1, pageCount) : null;
CSSOutput.write(getHTML(pdfName, pageAsString, prevPage, nextPage, "" + pageCount, minZoom, maxZoom).getBytes());
CSSOutput.flush();
CSSOutput.close();
}
catch (Exception ee) {
ee.printStackTrace();
}
System.out.println("Page " + page + " completed!");
}
decoder.closePdfFile();
try {
BufferedOutputStream CSSOutput = new BufferedOutputStream(new FileOutputStream(outputDir.getAbsolutePath() + File.separator
+ "index.html"));
String page = "";
CSSOutput.write(page.getBytes());
CSSOutput.flush();
CSSOutput.close();
}
catch (Exception ee) {
ee.printStackTrace();
}
}
catch (Exception e) {
System.out.println("Failed: " + e.getMessage());
e.printStackTrace();
}
}
// Returns page number with 0's prefixed if required.
private static String getPageAsString(int page, int pageCount) {
int zeros = ("" + pageCount).length() - ("" + page).length();
String pageAsString = "";
for (int i = 0; i < zeros; i++)
pageAsString += "0";
pageAsString += "" + page;
return pageAsString;
}
// Build the output for a page in HTML.
private static String getHTML(String pdfName, String pageNumber, String prevPage, String nextPage, String pageCount, int minZoom, int maxZoom) {
StringBuilder sb = new StringBuilder();
sb.append("\n");
sb.append("\n");
sb.append("\n");
sb.append("\t\n");
sb.append("\t" + pdfName + " Page " + pageNumber + " \n");
sb.append("\n");
sb.append("\n");
sb.append("\n");
sb.append("\t\n");
sb.append("\t" + ((prevPage != null) ? "<<" : "<<") + " Page " + pageNumber
+ " of " + pageCount + " " + ((nextPage != null) ? " >>" : ">>") + " \n");
sb.append("\n");
sb.append("\t\n");
sb.append("\t\n");
sb.append("\n");
sb.append("\n");
return sb.toString();
}
}