标签:保存 mat 调用 input cep system close intro 实现
前言import java.awt.*;
import java.awt.geom.Dimension2D;
import com.spire.pdf.*;
import com.spire.pdf.automaticfields.PdfAutomaticField;
import com.spire.pdf.automaticfields.PdfCompositeField;
import com.spire.pdf.automaticfields.PdfPageCountField;
import com.spire.pdf.automaticfields.PdfPageNumberField;
import com.spire.pdf.graphics.*;
public class HeaderFooter {
public static void main(String[] args) throws Exception {
//创建 PdfDocument 对象
PdfDocument doc = new PdfDocument();
//创建PdfMargins对象, 并设置的页边距
PdfMargins margin = new PdfMargins(60,60,40,40);
//调用 addHeaderAndFooter()方法添加页眉页脚
addHeaderAndFooter(doc, PdfPageSize.A4, margin);
//保存文档
doc.saveToFile("output/headerFooter.pdf");
doc.close();
}
static void addHeaderAndFooter(PdfDocument doc, Dimension2D pageSize, PdfMargins margin) {
PdfPageTemplateElement header = new PdfPageTemplateElement(margin.getLeft(), pageSize.getHeight());
doc.getTemplate().setLeft(header);
PdfPageTemplateElement topSpace = new PdfPageTemplateElement(pageSize.getWidth(), margin.getTop());
topSpace.setForeground(true);
doc.getTemplate().setTop(topSpace);
//添加页眉
PdfTrueTypeFont font= new PdfTrueTypeFont(new Font("Arial Unicode MS",Font.PLAIN,10),true);
PdfStringFormat format = new PdfStringFormat(PdfTextAlignment.Left);
String label = "2018年 年度会议报告";
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(font.measureString(label, format));
float y = topSpace.getHeight() - font.getHeight() - 1;
PdfPen pen = new PdfPen(new PdfRGBColor(Color.black), 0.75f);
topSpace.getGraphics().setTransparency(0.5f);
topSpace.getGraphics().drawLine(pen, margin.getLeft(), y, pageSize.getWidth() - margin.getRight(), y);
y = y - 1 - (float) dimension2D.getHeight();
topSpace.getGraphics().drawString(label, font, PdfBrushes.getBlack(), margin.getLeft(), y, format);
PdfPageTemplateElement rightSpace = new PdfPageTemplateElement(margin.getRight(), pageSize.getHeight());
doc.getTemplate().setRight(rightSpace);
//添加显示当前页及总页数的域作为页脚
PdfPageTemplateElement footer = new PdfPageTemplateElement(pageSize.getWidth(), margin.getBottom());
footer.setForeground(true);
doc.getTemplate().setBottom(footer);
y = font.getHeight() + 1;
footer.getGraphics().setTransparency(0.5f);
footer.getGraphics().drawLine(pen, margin.getLeft(), y, pageSize.getWidth() - margin.getRight(), y);
y = y + 1;
PdfPageNumberField pageNumber = new PdfPageNumberField();
PdfPageCountField pageCount = new PdfPageCountField();
PdfCompositeField pageNumberLabel = new PdfCompositeField();
pageNumberLabel.setAutomaticFields(new PdfAutomaticField[]{pageNumber, pageCount});
pageNumberLabel.setBrush(PdfBrushes.getBlack());
pageNumberLabel.setFont(font);
format = new PdfStringFormat(PdfTextAlignment.Right);
pageNumberLabel.setStringFormat(format);
pageNumberLabel.setText("第{0}页 共{1}页");
pageNumberLabel.setBounds(footer.getBounds());
pageNumberLabel.draw(footer.getGraphics(), pageSize.getWidth() - margin.getLeft(), y);
}
}
页眉页脚实现效果:
这里将分别使用insertTextWatermark(PdfPageBase page, String watermark)方法以及page.SetBackgroundImage(String arg0)方法来设置文本水印和图片水印。
1.文本水印
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
public class Textwatermark {
public static void main(String[] args) {
//创建PdfDocument类的对象
PdfDocument pdf = new PdfDocument();
//加载测试文档
pdf.loadFromFile("test.pdf");
//获取测试文档中的第一页
PdfPageBase page = pdf.getPages().get(0);
//调用insertWatermark()方法添加文本水印
insertWatermark(page, "TOP SECRET");
//保存文档
pdf.saveToFile("out/textWaterMark.pdf");
}
static void insertWatermark(PdfPageBase page, String watermark) {
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(page.getCanvas().getClientSize().getWidth() / 5, page.getCanvas().getClientSize().getHeight() / 5);
PdfTilingBrush brush = new PdfTilingBrush(dimension2D);
brush.getGraphics().setTransparency(0.4F);
brush.getGraphics().save();
brush.getGraphics().translateTransform((float) brush.getSize().getWidth() / 5, (float) brush.getSize().getHeight() / 5);
brush.getGraphics().rotateTransform(-45);
brush.getGraphics().drawString(watermark, new PdfFont(PdfFontFamily.Helvetica, 15), PdfBrushes.getViolet(), 0 , 0 , new PdfStringFormat(PdfTextAlignment.Center));
brush.getGraphics().restore();
brush.getGraphics().setTransparency(1);
Rectangle2D loRect = new Rectangle2D.Float();
loRect.setFrame(new Point2D.Float(0, 0), page.getCanvas().getClientSize());
page.getCanvas().drawRectangle(brush, loRect);
}
}
文本水印效果:
2.图片水印
import com.spire.pdf.*;
import java.awt.geom.Rectangle2D;
public class Imgwatermark {
public static void main(String[] args) {
//实例化PdfDocument类的对象,并加载测试文档
PdfDocument doc = new PdfDocument();
doc.loadFromFile("test.pdf");
//获取文档第1页
PdfPageBase page = doc.getPages().get(0);
//加载图片,设置为背景水印
page.setBackgroundImage("logo.png");
//指定水印在文档中的位置及图片大小
Rectangle2D.Float rect = new Rectangle2D.Float();
rect.setRect(150, 150, 300, 150);
page.setBackgroundRegion(rect);
//保存文档
doc.saveToFile("output/imageWaterMark.pdf");
doc.close();
}
}
图片水印添加效果:
1.添加纯色背景色
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import java.awt.*;
public class SetBackgroundColor1 {
public static void main(String[] args) {
//加载PDF文档
PdfDocument doc = new PdfDocument();
doc.loadFromFile("test.pdf");
PdfPageBase page;
//获取文档的总页数
int pageCount = doc.getPages().getCount();
//遍历页面,设置背景颜色
for(int i = 0; i < pageCount; i ++) {
page = doc.getPages().get(i);
page.setBackgroundColor(Color.yellow);
}
//保存文档
doc.saveToFile("BackgroundColor.pdf");
}
}
纯色背景色效果:
2.添加图片背景
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
public class SetBackgroundColor2 {
public static void main(String[] args) {
//加载PDF文档
PdfDocument doc = new PdfDocument();
doc.loadFromFile("test.pdf");
PdfPageBase page;
//获取文档的总页数
int pageCount = doc.getPages().getCount();
//遍历页面,设置背景图片
for(int i = 0; i < pageCount; i ++) {
page = doc.getPages().get(i);
page.setBackgroundImage("tp.png");
}
//保存文档
doc.saveToFile("BackgroundImage2.pdf");
}
}
图片背景色效果:
import com.spire.pdf.PdfDocument;
import com.spire.pdf.annotations.*;
import com.spire.pdf.attachments.PdfAttachment;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class AttachFiles {
public static void main(String[] args) throws IOException {
//实例化PdfDocument类的对象
PdfDocument doc = new PdfDocument();
//加载需要添加附件的PDF文档
doc.loadFromFile("test.pdf");
//加载附件文档(Excel)并作为附件添加到PDF
PdfAttachment attachment = new PdfAttachment("Sample.xlsx");
doc.getAttachments().add(attachment);
//在PDF页面指定位置绘制标签
String label = "TestReport.docx";
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.BOLD, 14));
double x = 40;
double y = doc.getPages().get(0).getActualSize().getHeight() -800;
doc.getPages().get(0).getCanvas().drawString(label, font, PdfBrushes.getOrange(), x, y);
//以注释的形式添加附件到PDF
String filePath = "测试文档.docx";
byte[] data = toByteArray(filePath);
Dimension2D size = font.measureString(label);
Rectangle2D bound = new Rectangle2D.Float((float) (x + size.getWidth() + 3), (float) y, 10, 15);
PdfAttachmentAnnotation annotation = new PdfAttachmentAnnotation(bound, filePath, data);
annotation.setColor(new PdfRGBColor(new Color(0, 128, 128)));
annotation.setFlags(PdfAnnotationFlags.Default);
annotation.setIcon(PdfAttachmentIcon.Graph);
annotation.setText("点击打开测试报告文档.docx");
doc.getPages().get(0).getAnnotationsWidget().add(annotation);
//保存文档
doc.saveToFile("Attachments.pdf");
}
//读取文件到byte数组
public static byte[] toByteArray(String filePath) throws IOException {
File file = new File(filePath);
long fileSize = file.length();
if (fileSize > Integer.MAX_VALUE) {
System.out.println("file too big...");
return null;
}
FileInputStream fi = new FileInputStream(file);
byte[] buffer = new byte[(int) fileSize];
int offset = 0;
int numRead = 0;
while (offset < buffer.length
&& (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
offset += numRead;
}
if (offset != buffer.length) {
throw new IOException("Could not completely read file "
+ file.getName());
}
fi.close();
return buffer;
}
}
附件添加效果:
以上是本次关于Java操作PDF文档代码示例。
关于Java 加密、解密PDF文档,请参阅这篇文章;
关于Java 合并、拆分PDF文档,请参阅这篇文章。
标签:保存 mat 调用 input cep system close intro 实现
原文地址:http://blog.51cto.com/eiceblue/2344412