有关pdf的各种操作(打水印、pdf转图片、图片转pdf)

发布时间:2024-12-29 14:30

如何打印PDF文件:直接将PDF文件拖拽到打印机图标上,或者通过打印预览选择PDF进行打印。 #生活技巧# #数码产品使用技巧# #打印机使用小贴士#

都是自己踩过的坑,主要是为了实现pdf加水印,并且别人不方便去除的需求。

1、给pdf加水印

public static boolean waterMark(byte[] fileBytes, String outputFile, HashMap waterMarkName) {

String orgName = null;

String name = null;

String mobile = null;

if(waterMarkName.containsKey("orgName") && waterMarkName.containsKey("name") && waterMarkName.containsKey("mobile")){

orgName = waterMarkName.get("orgName").toString();

name = waterMarkName.get("name").toString();

mobile = waterMarkName.get("mobile").toString();

}

try {

PdfReader reader = new PdfReader(fileBytes);

PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(outputFile));

BaseFont base = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

int total = reader.getNumberOfPages() + 1;

PdfContentByte under;

Rectangle pageRect = null;

for (int i = 1; i < total; i++) {

pageRect = stamper.getReader().getPageSizeWithRotation(i);

float x = pageRect.getWidth();

float y = pageRect.getHeight();

under = stamper.getOverContent(i);

under.saveState();

PdfGState gs = new PdfGState();

gs.setFillOpacity(0.3f);

under.setGState(gs);

under.beginText();

under.setColorFill(BaseColor.GRAY);

under.setFontAndSize(base, 40);

under.showTextAligned(Element.ALIGN_LEFT, orgName, x/2, y/2, 45);

under.showTextAligned(Element.ALIGN_LEFT, name, x/2, y/2-60, 45);

under.showTextAligned(Element.ALIGN_LEFT, mobile, x/2, y/2-120, 45);

under.showTextAligned(Element.ALIGN_LEFT, orgName, 0, 120, 45);

under.showTextAligned(Element.ALIGN_LEFT, name, 0, 60, 45);

under.showTextAligned(Element.ALIGN_LEFT, mobile, 0, 0, 45);

under.showTextAligned(Element.ALIGN_LEFT, orgName, 0, y-60, 45);

under.showTextAligned(Element.ALIGN_LEFT, name, 0, y-120, 45);

under.showTextAligned(Element.ALIGN_LEFT, mobile, 0, y-180, 45);

under.showTextAligned(Element.ALIGN_LEFT, orgName, x-120, 120, 45);

under.showTextAligned(Element.ALIGN_LEFT, name, x-120, 60, 45);

under.showTextAligned(Element.ALIGN_LEFT, mobile, x-120, 0, 45);

under.showTextAligned(Element.ALIGN_LEFT, orgName, x-120, y-60, 45);

under.showTextAligned(Element.ALIGN_LEFT, name, x-120, y-120, 45);

under.showTextAligned(Element.ALIGN_LEFT, mobile, x-120, y-180, 45);

under.endText();

under.restoreState();

}

stamper.close();

return true;

} catch (Exception e) {

e.printStackTrace();

return false;

}

}

2、pdf转图片

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.text.DecimalFormat;

import java.util.Iterator;

import javax.imageio.IIOImage;

import javax.imageio.ImageIO;

import javax.imageio.ImageWriter;

import javax.imageio.stream.ImageOutputStream;

import org.icepdf.core.exceptions.PDFException;

import org.icepdf.core.exceptions.PDFSecurityException;

import org.icepdf.core.pobjects.Document;

import org.icepdf.core.pobjects.Page;

import org.icepdf.core.util.GraphicsRenderingHints;

public class Pdf2Pic {  

public static final String FILETYPE_JPG = "png";

public static void tranfer(String filepath, String imagepath, float zoom) throws PDFException, PDFSecurityException, IOException {

Document document = null;

float rotation = 0f;

        document = new Document();

        document.setFile(filepath);

int maxPages = document.getPageTree().getNumberOfPages();

for (int i = 0; i < maxPages; i++) {

BufferedImage img = (BufferedImage) document.getPageImage(i, GraphicsRenderingHints.SCREEN, Page.BOUNDARY_CROPBOX, rotation, zoom);

Iterator iter = ImageIO.getImageWritersBySuffix(FILETYPE_JPG);

ImageWriter writer = (ImageWriter) iter.next();

File outFile = new File(imagepath + new File(filepath).getName() + "_" + new DecimalFormat("000").format(i) + "." + FILETYPE_JPG);

FileOutputStream out = new FileOutputStream(outFile);

ImageOutputStream outImage = ImageIO.createImageOutputStream(out);

            writer.setOutput(outImage);

            writer.write(new IIOImage(img, null, null));

        }

        System.out.println("转换完成");

    }

public static void main(String[] args) throws PDFException, PDFSecurityException, IOException {

long startTime = System.currentTimeMillis();

        tranfer("D:/itext.pdf", "d:", 1);

long endTime = System.currentTimeMillis();  

System.out.println("程序运行时间:" + (endTime - startTime) + "ms");   

    }

}

3、图片转pdf

import com.itextpdf.text.Document;

import com.itextpdf.text.Image;

import com.itextpdf.text.Rectangle;

import com.itextpdf.text.pdf.PdfWriter;

import java.io.File;

import java.io.FileOutputStream;

public class PrintToPdfUtil {

private static String FILEPATH = "D:\\";

public static void imagesToPdf(String fileName, String imagesPath) {

try {

fileName = FILEPATH+fileName+".pdf";

File file = new File(fileName);

Document document = new Document();

document.setMargins(0, 0, 0, 0);

PdfWriter.getInstance(document, new FileOutputStream(file));

document.open();

File files = new File(imagesPath);

String[] images = files.list();

int len = images.length;

for (int i = 0; i < len; i++){

if (images[i].toLowerCase().endsWith(".bmp")

|| images[i].toLowerCase().endsWith(".jpg")

|| images[i].toLowerCase().endsWith(".jpeg")

|| images[i].toLowerCase().endsWith(".gif")

|| images[i].toLowerCase().endsWith(".png")) {

String temp = imagesPath + "\\" + images[i];

Image img = Image.getInstance(temp);

img.setAlignment(Image.ALIGN_CENTER);

document.setPageSize(new Rectangle(img.getWidth(), img.getHeight()));

document.newPage();

document.add(img);

}

}

document.close();

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args){

long startTime = System.currentTimeMillis();

String name = "img2pdf";

String imagesPath = "D:\\";

imagesToPdf(name, imagesPath);

long endTime = System.currentTimeMillis();

System.out.println("程序运行时间:" + (endTime - startTime) + "ms");

}

}

有关pdf的各种操作(打水印、pdf转图片、图片转pdf)所需jar包,包括icepdf-core.jar、icepdf-viewer.jar、itext-asian-5.2.0.jar、itextpdf-5.3.5.jar

网址:有关pdf的各种操作(打水印、pdf转图片、图片转pdf) https://www.yuejiaxmz.com/news/view/600480

相关内容

pdf文件编辑图片,这个方法超简单
CAD常见问题解决:PDF转CAD图纸有白底
pdf高效转换
调料瓶旋转架.pdf
一种旋转式配料架.pdf
pdf内容怎么修改编辑?试试这5个pdf修改小技巧,绝对好用
个人护理Flash单片机.PDF
家庭保健.pdf
如何灵活运用PDF提升工作与学习的效率与便利
彻底改变PDF编辑体验,FlexiPDF Pro让你省时又省心!

随便看看