PDF转换图片,图片的切割,图片转换PDF以及PDF加水印等记录贴
生活随笔
收集整理的這篇文章主要介紹了
PDF转换图片,图片的切割,图片转换PDF以及PDF加水印等记录贴
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
PDF轉變為圖片;
把圖片進行切割;
把圖片轉變回PDF;
為PDF加水印文字;
為PDF加水印圖片。
1,PDF轉變為圖片
2,圖片切割
/*** @author dalin*分割png文件,這里做截取png文件部分*/ public class FenGePNG {private static void splitImage() throws IOException { String originalImg = "D:\\img\\0.png"; // 讀入大圖 File file = new File(originalImg); FileInputStream fis = new FileInputStream(file); BufferedImage image = ImageIO.read(fis); // 分割成4*4(16)個小圖 int rows = 2; int cols = 1; int chunks = rows * cols; // 計算每個小圖的寬度和高度 int chunkWidth = image.getWidth() / cols; int chunkHeight = image.getHeight() / rows; int count = 0; BufferedImage imgs[] = new BufferedImage[chunks]; for (int x = 0; x < rows; x++) { for (int y = 0; y < cols; y++) { //設置小圖的大小和類型 imgs[count] = new BufferedImage(chunkWidth, chunkHeight, image.getType()); //寫入圖像內容 Graphics2D gr = imgs[count++].createGraphics(); gr.drawImage(image, 0, 0, chunkWidth, chunkHeight, chunkWidth* y, chunkHeight * x, chunkWidth * y + chunkWidth, chunkHeight * x + chunkHeight, null); gr.dispose(); } } // 輸出小圖 for (int i = 0; i < imgs.length; i++) { ImageIO.write(imgs[i], "jpg", new File("D:\\img\\" + "img"+i + ".jpg")); } System.out.println("完成分割!"); } public static void main(String[] args) {try {splitImage();} catch (IOException e) {e.printStackTrace();}} }3,圖片轉變為PDF
/*** @author dalin * png文件類型轉為PDF類型*/ public class PNGToPdf {private static void jpgToPdf(File pdfFile,File imgFile) throws Exception { //文件轉img InputStream is = new FileInputStream(pdfFile); ByteArrayOutputStream baos = new ByteArrayOutputStream(); for(int i;(i=is.read())!=-1;) { baos.write(i); } baos.flush(); //取得圖像的寬和高。 Image img = Image.getInstance(baos.toByteArray()); float width = img.width(); float height = img.height(); img.setAbsolutePosition(0.0F, 0.0F);//取消偏移 System.out.println("width = "+width+"\theight"+height); //img轉pdf Document doc = new Document(new Rectangle(width,height)); PdfWriter pw = PdfWriter.getInstance(doc,new FileOutputStream(imgFile)); doc.open(); doc.add(img); //釋放資源 System.out.println(doc.newPage()); pw.flush(); baos.close(); doc.close(); pw.close(); } public static void main(String[] args) {try {jpgToPdf(new File("D:\\img\\0.png"),new File("D:\\pdf\\2new.pdf"));} catch (Exception e) {e.printStackTrace();} } }4,為PDF加文字水印和圖片水印
public class Test {public static void main(String[] args) throws DocumentException, IOException {// 要輸出的pdf文件BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("D:\\pdf\\water6.pdf")));setWatermark(bos, "D:\\pdf\\2.pdf", 16);}public static void setWatermark(BufferedOutputStream bos, String input, int permission)throws DocumentException, IOException {Rectangle pageRect = null;PdfReader reader = new PdfReader(input);PdfStamper stamper = new PdfStamper(reader, bos);int total = reader.getNumberOfPages() + 1;PdfContentByte content;Image image = Image.getInstance("D:/pdf/1.jpg");BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.EMBEDDED);PdfGState gs = new PdfGState();for (int i = 1; i < total; i++) {pageRect = stamper.getReader().getPageSizeWithRotation(i);// 計算水印X,Y坐標// float x = pageRect.getWidth() / 1;float y = pageRect.getHeight() / 1;System.out.println("這張PDF長是:" + y);// content = stamper.getOverContent(i);// 在內容上方加水印content = stamper.getUnderContent(i);// 在內容下方加水印gs.setFillOpacity(0.2f);// content.setGState(gs);content.beginText();content.setColorFill(Color.LIGHT_GRAY);content.setFontAndSize(base, 25);// content.setTextMatrix(500,500);image.setAbsolutePosition(100, 200);image.setRotationDegrees(30);content.showTextAligned(Element.ALIGN_LEFT, "XXXX防偽標志", 250, 200, 55);image.scaleToFit(200, 200);content.addImage(image);content.setColorFill(Color.BLACK);content.setFontAndSize(base, 8);content.endText();}stamper.close();} }總結
以上是生活随笔為你收集整理的PDF转换图片,图片的切割,图片转换PDF以及PDF加水印等记录贴的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何学习编程语言java,php
- 下一篇: java日期时间的转化