Skip to content

Instantly share code, notes, and snippets.

@kangear
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kangear/11312483 to your computer and use it in GitHub Desktop.
Save kangear/11312483 to your computer and use it in GitHub Desktop.
Photo to pdf for print.
package com.wizhong.os.printimage.service;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import android.util.Log;
import com.itextpdf.text.BadElementException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfWriter;
import com.wizhong.os.print.utils.GetBytesFromFile;
public class PhotoService {
private static final String LOG_TAG = "PhotoService";
public void TestImageToPdf() throws Exception {
imageToPdf("/sdcard/123.jpg", "/sdcard/pdf.pdf", true, true, 288, 432);
}
/**
* @param inputFile
* input image path
* @param outputFile
* output pdf path
* @param isMatchFrame
* match frame or not
* @param isRotate
* rotate or not
* @param paperWidth
* paper width
* @param paperHeight
* paper height
* @throws IOException
*/
public static void imageToPdf(String inputFile, String outputFile,
boolean isMatchFrame, boolean isRotate, int paperWidth, int paperHeight) throws Exception {
byte[] bArray = GetBytesFromFile.getBytesFromFile(new File(inputFile));
try {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream(outputFile));
/* 设置为照片纸张大小 */
Rectangle arg0 = new Rectangle(paperWidth, paperHeight);
document.setPageSize(arg0);
addImage(document, bArray, isMatchFrame, isRotate, paperWidth, paperHeight);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* TODO:将异常抛出 将图片写入PDF文件
* http://stackoverflow.com/questions/14392896/how-i-can-convert-a-bitmap-
* into-pdf-format-in-android
* @param document
* @param bArray
* @param isMatchFrame
* @param isRotate
* @param width
* @param height
*/
private static void addImage(Document document, byte[] bArray,
boolean isMatchFrame, boolean isRotate, int paperWidth, int paperHeight) throws Exception {
/**____________________e___________________
* | _______b_______________________ |
* | |<--------------c-------------> | |
* |a| | |
* | | image d | |f
* | | | |
* | |_______________________________| |
* |______________________________________|
*/
/* 图像横向时的尺寸 */
float imageWidth;
float imageHeight;
float leftPaperMargin = (float) (4*72/25.4); // a
float topPaperMargin = (float) (4*72/25.4); // b
/* 净尺寸(去掉页边空白) 纸张大小时相反的 */
float netHeight = paperWidth - topPaperMargin*2;// d
float netWidth = paperHeight * netHeight / paperWidth; // c
/* 总Margin */
float topMargin;
float leftMargin;
float curTopMargin;
float curLeftMargin;
float proportion = 1;
float rate;
Image image = null;
try {
image = Image.getInstance(bArray);
} catch (BadElementException e) {
e.printStackTrace();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (image.getWidth() >= image.getHeight()) {
isRotate = false;
imageWidth = image.getWidth();
imageHeight = image.getHeight();
rate = 90;
} else {
isRotate = true;
image.setRotationDegrees(90);
imageWidth = image.getHeight();
imageHeight = image.getWidth();
rate = 0;
}
/* width test ok */
if(isMatchFrame == true) {
proportion = imageWidth / netWidth;
if(isRotate == false) {
image.scaleAbsolute(netWidth, imageHeight / proportion);
} else {
image.scaleAbsolute(imageHeight / proportion, netWidth);
}
/* hight test ok */
} else {
proportion = imageHeight / netHeight;
if(isRotate == false) {
image.scaleAbsolute(imageWidth / proportion, netHeight);
} else {
image.scaleAbsolute(netHeight, imageWidth / proportion);
}
}
/* 利用Margin特性进行裁减或者居中 */
curTopMargin = (netHeight - imageHeight / proportion) / 2;
curLeftMargin = (netWidth - imageWidth / proportion) / 2;
topMargin = curTopMargin + topPaperMargin;
leftMargin = curLeftMargin + leftPaperMargin;
Log.i(LOG_TAG, "image.getWidth: " + image.getWidth() / proportion + ";"
+ "image.getHeight" + image.getHeight() / proportion);
Log.i(LOG_TAG, "addImage [\nimageWidth=" + imageWidth + ", imageHeight=" + imageHeight +
",\n leftPaperMargin=" + leftPaperMargin + ", topPaperMargin=" + topPaperMargin +
",\n netWidth=" + netWidth + ", netHeight=" + netHeight +
",\n curTopMargin=" + curTopMargin + ", curLeftMargin=" + curLeftMargin +
",\n topMargin=" + topMargin + ", leftMargin=" + leftMargin +
",\n image_width=" + image.getWidth() + ", image_height=" + image.getHeight() +
",\n proportion=" + proportion +
"\n]"
);
/* 利用Margin特性进行裁减或者居中 */
// (real_left, , real_top, ) 和实例图相反
document.setMargins(topMargin, 0, leftMargin, 0);
//document.setMargins(0, 0, 0, 0);
image.setRotationDegrees(rate);
try {
document.open();
document.add(image);
document.close();
} catch (DocumentException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment