建站教程

建站教程

Products

当前位置:首页 > 建站教程 >

Java 添加和删除PDF注释(java是如何删除文件的?)

GG网络技术分享 2025-03-18 16:13 0


Java 添加和删除PDF注释

众所周知,我们无法对PDF格式的文档进行直接编辑,而需借助PDF编辑器才可以。今天,我将介绍如何不使用编辑器,只需借助一个控件来通过后端调用Java代码的方式编辑PDF文档 —— 给PDF文档中的指定文本添加注释及如何删除已有注释。

创建开发环境

运行代码前,我们需要创建开发环境,即安装配置JDK和Intellij IDEA。接着我们需将Free Spire.PDF for Java控件下的Spire.Pdf.jar导入IDEA。可手动导入或通过Maven仓库引用以下代码进行导入。

<repositories>

<repository>

<id>com.e-iceblue</id>

<url>http://repo.e-iceblue.cn/repository/maven-public/</url>

</repository>

</repositories>

<dependencies>

<dependency>

<groupId>e-iceblue</groupId>

<artifactId>spire.pdf.free</artifactId>

<version>3.9.0</version>

</dependency>

</dependencies>

新建PDF文档并为其添加弹出式注释

import com.spire.pdf.*;

import com.spire.pdf.annotations.*;

import com.spire.pdf.graphics.*;

import java.awt.*;

import java.awt.geom.*;

public class PopupAnnotation {

public static void main(String[] args) {

//初始化PdfDocument实例

PdfDocument doc = new PdfDocument();

//设置边距

PdfUnitConvertor unitCvtr = new PdfUnitConvertor();

PdfMargins margin = new PdfMargins();

margin.setTop(unitCvtr.convertUnits(2.54f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point));

margin.setBottom(margin.getTop());

margin.setLeft(unitCvtr.convertUnits(3f, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point));

margin.setRight(margin.getLeft());

//添加新页

PdfPageBase page = doc.getPages().add(PdfPageSize.A4, margin);

//插入文本

PdfBrush brush1 = PdfBrushes.getBlack();

PdfTrueTypeFont font1 = new PdfTrueTypeFont(new Font("Arial Unicode MS", Font.BOLD + Font.ITALIC,13), true);

PdfStringFormat format1 = new PdfStringFormat(PdfTextAlignment.Left);

float y = 50;

String s = "本示例用于演示如何为PDF文档添加弹出式注释。";

page.getCanvas().drawString(s, font1, brush1, 0, y - 5, format1);

y = y + (float)font1.measureString(s, format1).getHeight();

//指定附注的文本、图标及图标颜色

PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial Unicode MS",0, 10));

PdfStringFormat format = new PdfStringFormat();

format.setMeasureTrailingSpaces(true);

String prompt = "弹出式注释";

Dimension2D size = font.measureString(prompt, format);

page.getCanvas().drawString(prompt, font, PdfBrushes.getDodgerBlue(), 0, y);

float x = (float)size.getWidth();

String label = "添加弹出式注释";

page.getCanvas().drawString(label, font, PdfBrushes.getOrangeRed(), x, y);

x = x + (float)font.measureString(label, format).getWidth();

String markupText = "什么是弹出式注释?";

Rectangle2D rectangle2D = new Rectangle.Float();

rectangle2D.setFrame(new Point2D.Double(x,y),new Dimension());

PdfPopupAnnotation annotation = new PdfPopupAnnotation(rectangle2D, markupText);

annotation.setIcon(PdfPopupIcon.Paragraph);

annotation.setOpen(true);

annotation.setColor(new PdfRGBColor(Color.YELLOW));

((PdfNewPage) page).getAnnotations().add(annotation);

//保存文档

doc.saveToFile("output/AddPopupAnnotation.pdf");

doc.close();

}

}

添加效果

为已有PDF文档添加文本框注释

import com.spire.pdf.*;

import com.spire.pdf.annotations.*;

import com.spire.pdf.graphics.*;

import java.awt.*;

import java.awt.geom.*;

import com.spire.pdf.general.find.PdfTextFind;

public class TextBoxAnnotation {

public static void main(String[] args) {

//加载示例文档

PdfDocument doc = new PdfDocument();

doc.loadFromFile("C:\\\\Users\\\\Test1\\\\Desktop\\\\Sample.pdf");

//获取第一页

PdfPageBase page = doc.getPages().get(0);

//获取文本并指定添加文本框注释的位置

PdfTextFind[] find = page.findText("人工智能").getFinds();

float x = (float)(find[0].getPosition().getX() - doc.getPageSettings().getMargins().getLeft() + find[0].getSize().getWidth()+20);

float y = (float)(find[0].getPosition().getY() - doc.getPageSettings().getMargins().getTop()+20);

//创建文本框注释

Rectangle2D.Float rect = new Rectangle2D.Float(x, y, 150, 30);

PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rect);

// 设置注释文字及字体,注释框边界颜色及背景色

textAnnotation.setMarkupText("人工智能的定义");

PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial",0, 12));;

textAnnotation.setFont(font);

PdfAnnotationBorder border = new PdfAnnotationBorder(0.5f);

textAnnotation.setBorder(border);

textAnnotation.setBorderColor(new PdfRGBColor(Color.pink));

textAnnotation.setColor(new PdfRGBColor(Color.YELLOW));

textAnnotation.setOpacity(0.75f);

textAnnotation.setTextMarkupColor(new PdfRGBColor(Color.black));

//添加注释到PDF

page.getAnnotationsWidget().add(textAnnotation);

//保存结果文档

doc.saveToFile("output/FreeTextAnnotation.pdf");

doc.close();

}

}

添加效果

删除PDF文档中已有的注释

Free Spire.PDF for Java不仅支持删除PDF文档中已有的所有注释,同时还可以删除指定的某个注释。以下是删除所有注释的代码示例。

import com.spire.pdf.*;

public class DeleteAnnotation {

public static void main(String[] args) {

//加载示例文档

PdfDocument document = new PdfDocument();

document.loadFromFile("C:\\\\Users\\\\Test1\\\\Desktop\\\\FreeTextAnnotation.pdf");

//删除第一页上的第一个注释

//document.getPages().get(0).getAnnotationsWidget().removeAt(0);

//删除所有注释

document.getPages().get(0).getAnnotationsWidget().clear();

//保存文档

String result = "output/DeleteAllAnnotations.pdf";

document.saveToFile(result);

}

}

java是如何删除文件的?

java是如何删除文件的? (https://www.wpmee.com/) javascript教程 第1张

java是如何删除文件的?java删除文件的方法:

/* 使用File的delete()方法删除文件

*/

import java.io.*;

public class Exercise {

public static void main(String args[]) {

try {

// Specify the file name and path

File file = new File(\"/home/zjz/Desktop/myFile.txt\");

/* the delete() method return true if the file

deleted successfully else it return false

*/

if (file.delete()) {

System.out.println(file.getName() + \"is deleted\");

} else {

System.out.println(\"Delete failed.\");

}

} catch (Exception e) {

System.out.println(\"Exception occured\");

e.printStackTrace();

}

}

}

Java File.delete()方法具有以下语法:Java文件类以抽象的方式代表文件名和目录路径名。该类主要用于文件和目录的创建、文件的查找和文件的删除等。java中删除文件可以使用File.delete()方法实现。

Java File.delete()方法具有以下语法:

public boolean delete()

删除此抽象路径名表示的文件或目录。

以上就是java如何删除文件的详细内容。

标签:

提交需求或反馈

Demand feedback