本文将介绍如何使用Spire.PDF在Java应用程序中给PDF格式的合同文件添加骑缝章。
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.io.*;
public class addSeamSeals {
public static void main(String[] args) throws IOException {
//加载示例文档
PdfDocument doc = new PdfDocument();
doc.loadFromFile("Contract.pdf");
PdfUnitConvertor convert = new PdfUnitConvertor();
PdfPageBase pageBase = null;
//获取分割后的印章图片
BufferedImage[] images = GetImage(doc.getPages().getCount());
float x = 0;
float y = 0;
//将图片画到PDF页面上的指定位置
for (int i = 0; i < doc.getPages().getCount(); i++)
{
BufferedImage image= images[ i ];
pageBase = doc.getPages().get(i);
x = (float)(pageBase.getSize().getWidth()) - convert.convertUnits(image.getWidth(), PdfGraphicsUnit.Pixel, PdfGraphicsUnit.Point);
y = (float) pageBase.getSize().getHeight()/ 2;
pageBase.getCanvas().drawImage(PdfImage.fromImage(image), new Point2D.Float(x, y));
}
//保存PDF
doc.saveToFile("Result.pdf");
}
//定义GetImage方法,根据PDF页数分割印章图片
static BufferedImage[] GetImage(int num) throws IOException {
String originalImg = "stamp.png";
BufferedImage image = ImageIO.read(new File(originalImg));
int rows = 1;
int cols = num;
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, Color.WHITE,null);
gr.dispose();
}
}
return imgs;
}
}
骑缝章效果图:

Spire.PDF 6.7.6已发布,该版本支持转换PDF到Excel。
新功能:
PdfDocument pdfDocument = new PdfDocument();
pdfDocument.LoadFromFile(inputPdfFilePath);
pdfDocument.SaveToFile(outputExcelFilePath,FileFormat.XLSX);
Spire.XLS 10.7.2 现已发布。该版本支持转换根据数据透视表数据生成的图表到PDF时保留字段名标签的效果, 除此之外,一些在转换Excel到PDF、图片以及加载、操作文档时出现的问题也得以成功解决。新功能及问题修复详情如下。
新功能:
问题修复:
Spire.PDF 6.7.2已发布。该版本主要修复了加载、打印、合并PDF文件、提取附件、添加文本水印以及将PDF转为图片、XPS转为PDF时出现一些问题。更多问题修复详情见以下内容。
问题修复:
Spire.PDFViewer 5.7.2已发布。该版本主要修复了查看PDF文档以及导出图片时出现的问题。问题修复详情如下。
问题修复:
Spire.Presentation for Java 3.7.4已于今日发布。该版本支持获取形状在幻灯片母版的版式中的原型,同时还支持获取当前的主题颜色集合。除此之外,一些在转换PPT到PDF以及获取文本的真实主题颜色时出现的问题也得以成功解决。详情见下文。
新功能:
Shape layoutPrototype = shape.getLayoutPrototype();
StringBuilder sb = new StringBuilder();
IMasterSlide masterSlide = presentation.getMasters().get(0);
for (SchemeColor schemeColor : masterSlide.getColorMap().keySet()) {
masterSlide.getColorMap().get(schemeColor);
String content = "key : " +schemeColor +"\tvalue : "+
masterSlide.getColorMap().get(schemeColor) + "\r\n";
sb.append(content);
}
问题修复:
https://www.e-iceblue.cn/Downloads/Spire-Presentation-JAVA.html
Spire.PDF for Java 3.6.9已发布。该版本支持设置PdfPopupAnnotation的作者和主题,并修复了将Html转为PDF时抛异常和page.ImagesInfo中图片的索引与page.ReplaceImage方法中图片索引不一致的问题。新功能及问题修复详情如下。
新功能:
PdfPopupAnnotation annotation = new PdfPopupAnnotation(rectangle2D, markupText);
annotation.setAuthor("e-iceblue");
annotation.setSubject("subject_popup");
问题修复:
本文将介绍如何使用Spire.Doc for Java实现在Word文档中添加、计数、检索和删除文档变量。
添加文档变量
以下示例添加了一个名为“A1”,值为12的文档变量到一个Word文档。
import com.spire.doc.Document;
import com.spire.doc.FieldType;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
public class AddVariables {
public static void main(String[] args){
//创建Document实例
Document document = new Document();
//添加节
Section section = document.addSection();
//添加段落
Paragraph paragraph = section.addParagraph();
//添加DocVariable域
paragraph.appendField("A1", FieldType.Field_Doc_Variable);
//添加文档变量到DocVariable域
document.getVariables().add("A1", "12");
//更新域
document.isUpdateFields(true);
//保存文档
document.saveToFile("AddVariables.docx", FileFormat.Docx_2013);
}
}

计算文档变量的个数
import com.spire.doc.Document;
public class CountVariables {
public static void main(String[] args){
//加载Word文档
Document document = new Document();
document.loadFromFile("AddVariables.docx");
//获取文档变量的个数
int number = document.getVariables().getCount();
StringBuilder content = new StringBuilder();
content.append("文档变量的个数为: " + number);
System.out.println(content.toString());
}
}

检索文档变量的名称和值
import com.spire.doc.Document;
public class RetrieveVariables {
public static void main(String[] args){
//加载Word文档
Document document = new Document();
document.loadFromFile("AddVariables.docx");
//使用索引检索变量的名称
String s1 = document.getVariables().getNameByIndex(0);
//使用索引检索变量的值
String s2 = document.getVariables().getValueByIndex(0);
//使用变量名称检索变量的值
String s3 = document.getVariables().get("A1");
System.out.println("索引为0的变量的名称为: " + s1);
System.out.println("索引为0的变量的值为: " + s2);
System.out.println("名称为\"A1\"的变量的值为: " + s3);
}
}

删除指定文档变量
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
public class RemoveVariables {
public static void main(String[] args){
//加载Word文档
Document document = new Document();
document.loadFromFile("AddVariables.docx");
//删除名为“A1”的指定文档变量
document.getVariables().remove("A1");
//更新域
document.isUpdateFields (true);
//保存文档
document.saveToFile("RemoveVariables.docx", FileFormat.Docx_2013);
}
} Spire.Office for Java 3.6.4现已正式发布。该版本有许多新功能,例如,Spire.XLS支持设置超链接类型及转换Excel到HTML,Spire.Doc改善了打印质量,Spire.PDF支持使用QT插件将HTML转换为PDF。除此之外,一些在加载、转换和操作Word、Excel、PPT和Presentation文档时出现的问题也得以成功解决。更多关于新功能和问题的细节参见下文。
获取Spire.Office for Java 3.6.4,请点击:https://www.e-iceblue.cn/Downloads/Spire-Office-JAVA.html
新功能:
Worksheet sheet = workbook.getWorksheets().get(0);
CopyRangeOptions copyOptions = CopyRangeOptions.OnlyCopyFormulaValue;
sheet.copy(sheet.getCellRange("A6:E6"), sheet.getCellRange("A8:E8"), EnumSet.of(copyOptions));
workbook.saveToFile(filename,FileFormat.HTML)
hyperlink.setType(HyperLinkType.File);
问题修复:
新功能:
Document loDoc = new Document(inputFile);
PrinterJob loPrinterJob = PrinterJob.getPrinterJob();
PageFormat loPageFormat = loPrinterJob.defaultPage();
Paper loPaper = loPageFormat.getPaper();
//删除默认的打印页边距
loPaper.setImageableArea(0,0,loPageFormat.getWidth(),loPageFormat.getHeight());
//设置副本的数量
loPrinterJob.setCopies(1);
loPageFormat.setPaper(loPaper);
loPrinterJob.setPrintable(loDoc,loPageFormat);s
try {
loPrinterJob.print();
} catch (PrinterException e)
{
e.printStackTrace();
}
问题修复:
问题修复:
新功能:
String url = "https://www.baidu.com";
String fileName = "D:/output.pdf";
String pluginPath = "D:/Qt/plugins_64";
HtmlConverter.setPluginPath(pluginPath);
HtmlConverter.convert(url,fileName);
IOCR loIOCR=(image)->{
String extractedText = "";
//OCR API
//extractedText = ...
return extractedText;
};
PdfDocument.setExportTextOCRHandler(loIOCR);
PdfDocument pdf=new PdfDocument();
pdf.loadFromFile(inputPath);
String text = "";
for (PdfPageBase page : (Iterable) pdf.getPages()) {
text += page.extractText();
}
FileWriter sw = new FileWriter(outputPath);
sw.write(text);
sw.flush();
sw.close();
pdf.close();
String url = "https://freetsa.org/tsr";
signature.configureTimestamp(url,"user_name","password");
问题修复:
signature.removeShowConfigureText(SignatureConfigureText.Distinguished_Name);
Spire.XLS for Java 3.6.7 现已发布。该版本支持设置超链接类型, 同时一些在转换Excel到PDF、图片和XML时出现的问题也得以成功解决。新功能及问题修复详情如下。
新功能:
hyperlink.setType(HyperLinkType.File);
问题修复: