日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

PDF转换图片,图片的切割,图片转换PDF以及PDF加水印等记录贴

發布時間:2025/5/22 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PDF转换图片,图片的切割,图片转换PDF以及PDF加水印等记录贴 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

PDF轉變為圖片;

把圖片進行切割;

把圖片轉變回PDF;

為PDF加水印文字;

為PDF加水印圖片。
1,PDF轉變為圖片

/*** @author dalin*將PDF格式的文件轉換成png文件*/ public class PdfToPNG {public static void main(String[] args) {String filePath = "D:\\pdf\\2.pdf";Document document = new Document();document.setFile(filePath);float scale = 2.5f;//縮放比例float rotation = 0f;//旋轉角度for (int i = 0; i < document.getNumberOfPages(); i++) {BufferedImage image = (BufferedImage)document.getPageImage(i, GraphicsRenderingHints.SCREEN, org.icepdf.core.pobjects.Page.BOUNDARY_CROPBOX, rotation, scale);RenderedImage rendImage = image;try {File file = new File("D:\\img\\" + i + ".png"); ImageIO.write(rendImage, "png", file); } catch (IOException e) {e.printStackTrace();}image.flush();}document.dispose();} }

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加水印等记录贴的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。