冰蓝科技
|
028-81705109
|
|
微信扫一扫
|

Spire.Cloud 纯前端文档控件

PDF文档中的图层可根据页面布局需要来设置隐藏或者显示。本文将使用Spire.PDF for Java来介绍隐藏或者显示图层的方法。另外,如需添加或删除PDF图层,可参考 这篇文章。

import com.spire.pdf.FileFormat;
import com.spire.pdf.PdfDocument;
import com.spire.pdf.graphics.layer.PdfVisibility;


public class HideOrShowLayer {
    public static void main(String[] args) {
        //加载文档
        PdfDocument pdf = new PdfDocument();
        pdf.loadFromFile("Addlayers.pdf");

        pdf.getLayers().get(0).setVisibility(PdfVisibility.Off);//设置图层隐藏
        //pdf.getLayers().get(2).setVisibility(PdfVisibility.On);//设置图层显示

        //保存文档
        pdf.saveToFile("HideOrShowLayer.pdf", FileFormat.PDF);
        pdf.dispose();
    }
}

图层隐藏、显示设置效果:

Java 隐藏/显示 PDF 图层

Spire.PDF for Java 3.4.2 现已正式发布。该版本支持 嵌入3D文件 和声音文件,也支持设置画笔的虚实线格式。除此之外,该版本还成功修复了一些在删除图片和获取PDF文档缩放比例时出现的问题。新功能及问题修复详情,请查阅以下内容。

新功能:

问题修复:


获取Spire.PDF for Java 3.4.2,请点击:

https://www.e-iceblue.cn/Downloads/Spire-PDF-JAVA.html

Spire.Cloud支持的Linux服务器系统包括CentOS和Ubuntu(推荐使用CentOS 7和Ubuntu 18版本),本教程主要介绍如何在CentOS 7系统上实现Spire.Cloud私有化部署。

详细步骤如下:

一、安装CentOS 7系统

首先,需要在服务器上安装CentOS 7系统,如果已安装,可跳过该步骤。

1、安装CentOS 7,进入以下界面,点击continue。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

2、选择最小安装。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

3、设置分区,选择默认设置即可。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

4、开启网络。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

5、配置完毕开始安装。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

6、设置root账户密码。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

7、安装完毕后重启系统。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

二、系统配置

1、关闭防火墙。

1)首先查看防火墙的状态:

firewall-cmd –-state

2)关闭防火墙:

systemctl stop firewalld.service

3)再次查看状态,检查防火墙是否已经关闭:

firewall-cmd –-state

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

2、配置静态IP地址。

1)使用命令:vi /etc/sysconfig/network-scripts/ifcfg-ens32(需要查看后才能确认这个信息),打开配置文件。

按Insert键开始编辑,将bootproto的值改为static,并在末尾加上IP地址,子网掩码,网关和DNS服务器。编辑完之后按ESC键退出编辑,然后再按冒号(:)键,输入指令wq保存退出。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

2)然后输入命令:systemctl restart network重启网络服务。

3)输入命令ip addr查看当前ip地址,确认是否修改成功。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

三、部署Spire.Cloud

1、请联系我们获取Spire.Cloud产品部署包。邮箱:该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;电话:028-81705109。

2、将产品部署文件拷贝到CentOS 7系统中,可以借助winscp工具进行拷贝。具体步骤如下。

1)首先登陆。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

2)将文件直接拷贝进去,这里是拷贝到了/root/目录下。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

3、安装产品

在root账户权限下执行脚本install_centos7.sh。

执行命令: sh install_centos7.sh;也可以选择打包日志:sh install_centos7.sh >1.log

这里部署耗时较长(2h+),请耐心等待。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

4、应用license

1)将license文件拷贝到spire.cloud/service/ConverterService/bin/license目录下。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

2)切换到binding_license.sh所在目录(针对本次部署/root/),执行脚本binding_license.sh、sh binding_license.sh。

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

注:私有化部署需要应用有效的license文件,否则在线编辑服务和WEB API服务将不能正常运行。

如果你需要临时license文件,请联系我们。邮箱:该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。;电话:028-81705109。


至此,Spire.Cloud已部署完成。现在你可以在本地服务器上使用Spire.Cloud在线编辑服务在线创建、编辑、保存和打印Office(Word/Excel/PowerPoint)文档;或使用WEB API,在应用程序中操作Office(Word/Excel/PowerPoint)及PDF文档,然后使用在线编辑查看结果文档。

现在测试在线编辑。在浏览器地址栏输入“[服务器ip]:3000”(针对本次部署输入192.168.0.108:3000),出现以下界面:

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

点击Create Document,创建一个空白Word文档并编辑内容:

Spire.Cloud 私有化部署教程 一 - CentOS 7 系统

本文介绍如何使用Spire.XLS for Java在Excel中设置条件格式,用来高亮某数据区域的最大和最小值。当然,您也可以将以下代码中setRank()方法的参数设置为5(或其他数字),来高亮排前5和后5的值。

import com.spire.xls.*;
import com.spire.xls.core.IConditionalFormat;
import com.spire.xls.core.spreadsheet.collections.XlsConditionalFormats;

import java.awt.*;

public class HighlightTopBottom    {
    public static void main(String[] args) throws Exception {
        //创建Workbook对象
        Workbook workbook = new Workbook();

        //加载Excel文档
        workbook.loadFromFile("C:\\Users\\Administrator\\Desktop\\sample.xlsx");

        //获取第一个工作表
        Worksheet sheet = workbook.getWorksheets().get(0);

        //为工作表创建一个新的条件格式规则集合并设置单元格范围
        XlsConditionalFormats xcfs=sheet.getConditionalFormats().add();
        xcfs.addRange(sheet.getCellRange("B2:E5"));

        //高亮最大值所在的单元格
        IConditionalFormat format1 = xcfs.addTopBottomCondition(TopBottomType.Top,1);
        format1.setBackColor(Color.red);

        //高亮最小值所在的单元格
        IConditionalFormat format2 = xcfs.addTopBottomCondition(TopBottomType.Bottom,1);
        format2.setBackColor(Color.yellow);

        //保存文档
        workbook.saveToFile("HighestLowestValue.xlsx", ExcelVersion.Version2016);
    }
}

Java 高亮 Excel 中的最大和最小值

Spire.Doc 8.4.0于今日发布。该版本主要包含了支持MonoAndroid、Xamarin.iOS 平台的 dlls。详情如下。

新功能:


获取Spire.Doc 8.4.0,请点击:

https://www.e-iceblue.cn/Downloads/Spire-Doc-NET.html

Spire.Presentation 5.4.2已于今日发布。该版本包含了支持 MonoAndroid、 Xamarin.iOS平台的dll文件,同时它也支持从右到左列分栏以及通过两点的位置来绘制线段。此外,转换PPT到PDF时出现的问题也得以成功解决。详情见下文。

新功能:

问题修复:


获取Spire.Presentation 5.4.2,请点击:

https://www.e-iceblue.cn/Downloads/Spire-Presentation-NET.html

Word 文档中的水印是背景中的半透明图片或文字,用来强调文档的某些重要属性,如可以用文字水印来强调文档是保密的或者文档为草稿,还可以用公司图标等图片来声明文档的版权。有些时候 Word 文档中的水印会影响对文档的阅读,这时我们可以用 Spire.Doc for Java 来帮助我们移除水印。本文将介绍用 Spire.Doc for Java 移除 Word 文档中的水印的详细操作步骤。

安装 Spire.Doc for Java

首先,您需要在 Java 程序中添加 Spire.Doc.jar 文件作为依赖项。您可以从 此链接 下载 JAR 文件;如果您使用 Maven,则可以通过在 pom.xml 文件中添加以下代码导入 JAR 文件。

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.doc</artifactId>
        <version>14.9.5</version>
    </dependency>
</dependencies>

移除 Word 文档中的水印

  • Java
import com.spire.doc.*;

public class removeWatermark {
    public static void main(String[] args) {


        //创建 Document 的对象
        Document document = new Document();

        //从磁盘加载 Word 文档
        document.loadFromFile("D:/testp/示例.docx");

        //将水印值设置为 null 以移除水印
        document.setWatermark(null);

        //保存 Word 文档
        String output = "D:/javaOutput/移除水印.docx";
        document.saveToFile(output, FileFormat.Docx_2013);
    }
}

Java 移除 Word 文档中的水印

申请临时 License

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。

Spire.PDF 6.4.4 现已正式发布。该版本主要修复了一些在打印和转换PDF文档时出现的问题。具体修复内容参见下文。

问题修复:


获取Spire.PDF 6.4.4,请点击:

https://www.e-iceblue.cn/Downloads/Spire-PDF-NET.html

为 PDF 添加注释可以增强文档的协作性和可理解性。无论是突出重点、添加评论,还是绘制形状,注释都能帮助清晰地传达想法和意见。通过使用注释,用户可以简化反馈过程,并确保重要信息突出显示。

本文将向您展示如何使用 Spire.PDF for Java 向 PDF 文档添加各种注释。

安装 Spire.PDF for Java

首先,您需要在 Java 程序中添加 Spire.Pdf.jar 文件作为依赖项。JAR 文件可以从此链接下载。如果您使用 Maven,则可以将以下代码添加到项目的 pom.xml 文件中,从而在应用程序中导入 JAR 文件。

<repositories>
    <repository>
        <id>com.e-iceblue</id>
        <name>e-iceblue</name>
        <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
    </repository>
</repositories>
<dependencies>
    <dependency>
        <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf</artifactId>
        <version>12.9.0</version>
    </dependency>
</dependencies>

Spire.PDF 支持的注释类型

PDF 文档提供了广泛支持的各种注释类型,使用户能够通过评论、标注、交互功能和附加内容来增强文档的表现力。下表列出了 Spire.PDF 支持的一些常用注释类型。

注释类型 定义 由下列类表示
文本标注注释 用于突出显示或标记文档中特定文本的注释。 PdfTextMarkupAnnotation 类
自由文本注释 允许用户将自由格式文本直接添加到 PDF 页面上的注释。 PdfFreeTextAnnotation 类
弹出注释 一种点击时会显示为弹出窗口的注释,提供与文档内容相关的附加信息或上下文。 PdfPopupAnnotation 类
印章注释 一种添加预制印章或图像到文档上的注释,通常用于表示批准、审核状态或其他相关信息。 PdfRubberStampAnnotation 类
形状注释 允许用户在文档上绘制形状或线条的注释,可以用来突出特定区域或添加视觉提示。 PdfLineAnnotation 类
PdfPolygonAnnotation 类
网页链接注释 一种创建可点击链接到网页的注释,允许用户轻松访问额外的资源或相关信息。 PdfUriAnnotation 类
文件链接注释 一种创建可点击链接到文件的注释,使用户可以打开与 PDF 内容相关的外部文档或文件。 PdfFileLinkAnnotation 类
文档链接注释 一种创建可点击链接到同一文档内其他章节或页面的注释。 PdfDocumentLinkAnnotation 类

用 Java 在 PDF 中添加文本标注注释

文本标记注释旨在突出显示 PDF 文档中特定部分的文本。为了定位要标记的文本,您可以使用 PdfTextFinder 类,它提供的方法简化了在页面上查找所需文本的过程。

在识别文本之后,您可以基于文本的边界信息创建一个 PdfTextMarkupAnnotation 对象。然后,您可以设置注释的各种属性,如注释文本和标记颜色。

在 Java 中向 PDF 添加文本标记注释的步骤如下:

  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfTextMarkupAnnotation;
import com.spire.pdf.graphics.PdfRGBColor;
import com.spire.pdf.texts.PdfTextFinder;
import com.spire.pdf.texts.PdfTextFragment;
import com.spire.pdf.texts.TextFindParameter;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.EnumSet;
import java.util.List;

public class AddMarkupAnnotation {

   public static void main(String[] args) {

       // 创建一个 PdfDocument 对象
       PdfDocument doc = new PdfDocument();

       // 加载 PDF 文件
       doc.loadFromFile("C:/Users/Administrator/Desktop/AI绘画.pdf");

       // 获取特定页面
       PdfPageBase page = doc.getPages().get(0);

       // 在该页面上创建一个 PdfTextFinder 对象
       PdfTextFinder finder = new PdfTextFinder(page);

       // 设置查找选项
       EnumSet<TextFindParameter> parameterSet = EnumSet.of(TextFindParameter.IgnoreCase);
       finder.getOptions().setTextFindParameter(parameterSet);

       // 查找特定文本的实例
       List<PdfTextFragment> fragments = finder.find("最直观的优势");

       // 获取第一个实例
       PdfTextFragment textFragment = fragments.get(0);

       // 指定注释文本
       String text = "这是一个文本标注注释";

       // 遍历文本的边界
       for (int i = 0; i < textFragment.getBounds().length; i++) {

           // 获取指定的边界
           Rectangle2D rect = textFragment.getBounds()[i];

           // 创建文本标记注释
           PdfTextMarkupAnnotation annotation = new PdfTextMarkupAnnotation(rect);

           // 设置注释文本
           annotation.setText(text);

           // 设置标记颜色
           annotation.setTextMarkupColor(new PdfRGBColor(Color.green));

           // 将注释添加到注释集合中
           page.getAnnotations().add(annotation);
       }

       // 保存结果文件
       doc.saveToFile("C:/Users/Administrator/Desktop/文本标注注释.pdf");

       // 释放资源
       doc.dispose();
   }
}

在 PDF 中添加一个文本标注注释

用 Java 在 PDF 中添加自由文本注释

如果想要在句末等适合的位置添加自由文本注释,您可以继续使用 PdfTextFinder 类。它可以帮助您定位文档中的所需文本及其坐标。

一旦获得坐标信息,就可以创建一个 PdfFreeTextAnnotation 实例。然后,您可以设置注释文本并自定义其外观。

在 Java 中向 PDF 添加自由文本注释的步骤如下:

  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfAnnotationBorder;
import com.spire.pdf.annotations.PdfFreeTextAnnotation;
import com.spire.pdf.graphics.PdfFont;
import com.spire.pdf.graphics.PdfFontFamily;
import com.spire.pdf.graphics.PdfFontStyle;
import com.spire.pdf.graphics.PdfRGBColor;
import com.spire.pdf.texts.PdfTextFinder;
import com.spire.pdf.texts.PdfTextFragment;
import com.spire.pdf.texts.TextFindParameter;
import com.spire.pdf.graphics.PdfTrueTypeFont;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.EnumSet;
import java.util.List;

public class AddFreeTextAnnotation {

   public static void main(String[] args) {

       // 创建一个 PdfDocument 对象
       PdfDocument doc = new PdfDocument();

       // 加载 PDF 文件
       doc.loadFromFile("C:/Users/Administrator/Desktop/AI绘画.pdf");

       // 获取特定页面
       PdfPageBase page = doc.getPages().get(0);

       // 基于页面创建 PdfTextFinder 对象
       PdfTextFinder finder = new PdfTextFinder(page);

       // 设置查找选项
       EnumSet<TextFindParameter> parameterSet = EnumSet.of(TextFindParameter.WholeWord, TextFindParameter.IgnoreCase);
       finder.getOptions().setTextFindParameter(parameterSet);

       // 查找指定文本的实例
       List<PdfTextFragment> fragments = finder.find("最直观的优势");

       // 获取第一个实例
       PdfTextFragment textFragment = fragments.get(0);

       // 获取文本的边界
       Rectangle2D rect = textFragment.getBounds()[0];

       // 指定添加注释的 x 和 y 坐标
       double x = rect.getMaxX() + 3;
       double y = rect.getMaxY();

       // 创建一个自由文本注释
       Rectangle2D rectangle = new Rectangle2D.Double(x, y, 170, 40);
       PdfFreeTextAnnotation textAnnotation = new PdfFreeTextAnnotation(rectangle);

       // 设置注释的内容
       textAnnotation.setText("这是一个文本注释\r由 Spire.PDF for Java 添加");

       // 设置注释的其他属性
       // 使用 PdfTrueTypeFont 设置字体为 "华文中宋"(系统字体)并指定加粗
       PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("华文中宋", Font.BOLD, 12));
       PdfAnnotationBorder border = new PdfAnnotationBorder(1f);
       textAnnotation.setFont(font);
       textAnnotation.setBorder(border);
       textAnnotation.setBorderColor(new PdfRGBColor(Color.blue));
       textAnnotation.setColor(new PdfRGBColor(Color.green));
       textAnnotation.setOpacity(1.0f);

       // 将注释添加到注释集合中
       page.getAnnotations().add(textAnnotation);

       // 保存结果到文件
       doc.saveToFile("C:/Users/Administrator/Desktop/自由文本注释.pdf");

       // 释放资源
       doc.dispose();
   }
}

在 PDF 中添加一个自由文本注释

在 Java 中向 PDF 添加弹出注释

首先,您可以使用 PdfTextFinder 类来确定添加注释的最佳位置。接下来,创建一个 PdfPopupAnnotation 对象,并自定义其属性,例如文本、颜色和图标。

在 Java 中向 PDF 添加弹出注释的步骤如下:

  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfPopupAnnotation;
import com.spire.pdf.annotations.PdfPopupIcon;
import com.spire.pdf.graphics.PdfRGBColor;
import com.spire.pdf.texts.PdfTextFinder;
import com.spire.pdf.texts.PdfTextFragment;
import com.spire.pdf.texts.TextFindParameter;

import java.awt.*;
import java.awt.geom.Rectangle2D;
import java.util.EnumSet;
import java.util.List;

public class AddPopupAnnotation {

   public static void main(String[] args) {

       // 创建一个 PdfDocument 对象
       PdfDocument doc = new PdfDocument();

       // 加载 PDF 文件
       doc.loadFromFile("C:/Users/Administrator/Desktop/AI绘画.pdf");

       // 获取特定页面
       PdfPageBase page = doc.getPages().get(0);

       // 基于页面创建 PdfTextFinder 对象
       PdfTextFinder finder = new PdfTextFinder(page);

       // 设置查找选项
       EnumSet<TextFindParameter> parameterSet = EnumSet.of(TextFindParameter.WholeWord, TextFindParameter.IgnoreCase);
       finder.getOptions().setTextFindParameter(parameterSet);

       // 查找指定文本的实例
       List<PdfTextFragment> fragments = finder.find("最直观的优势");

       // 获取第一个实例
       PdfTextFragment textFragment = fragments.get(0);

       // 获取文本的边界
       Rectangle2D rect = textFragment.getBounds()[0];

       // 指定添加注释的 x 和 y 坐标
       double x = rect.getMaxX();
       double y = rect.getMaxY();

       // 创建一个弹出注释
       Rectangle2D rectangle = new Rectangle2D.Double(x, y, 30, 30);
       PdfPopupAnnotation popupAnnotation = new PdfPopupAnnotation(rectangle);

       // 设置注释的内容
       popupAnnotation.setText("这是一个弹出注释\r由 Spire.PDF for Java 添加");

       // 设置注释的图标和颜色
       popupAnnotation.setIcon(PdfPopupIcon.Comment);
       popupAnnotation.setColor(new PdfRGBColor(Color.red));

       // 将注释添加到注释集合中
       page.getAnnotations().add(popupAnnotation);

       // 保存结果到文件
       doc.saveToFile("C:/Users/Administrator/Desktop/弹出注释.pdf");

       // 释放资源
       doc.dispose();
   }
}

在 PDF 中添加一个弹出注释

在 Java 中向 PDF 添加形状注释

注释通常指的是特定几何形状的图形元素,如矩形、椭圆、多边形或直线。添加形状注释时,通常需要多个坐标点来定义该形状的位置和大小。

您可以使用 PdfTextFinder 类获取指定文本的坐标。基于这些坐标,您可以计算出形状注释所需的坐标点。添加形状注释后,您可以为其设置文本。

下面是利用 Java 在 PDF 中添加多边形注释的操作过程:

  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.*;
import com.spire.pdf.texts.PdfTextFinder;
import com.spire.pdf.texts.PdfTextFragment;
import com.spire.pdf.texts.TextFindParameter;

import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.EnumSet;
import java.util.List;

public class AddShapeAnnotation {

   public static void main(String[] args) {

       // 创建一个 PdfDocument 对象
       PdfDocument doc = new PdfDocument();

       // 加载 PDF 文件
       doc.loadFromFile("C:/Users/Administrator/Desktop/AI绘画.pdf");

       // 获取特定页面
       PdfPageBase page = doc.getPages().get(0);

       // 基于页面创建 PdfTextFinder 对象
       PdfTextFinder finder = new PdfTextFinder(page);

       // 设置查找选项
       EnumSet<TextFindParameter> parameterSet = EnumSet.of(TextFindParameter.WholeWord, TextFindParameter.IgnoreCase);
       finder.getOptions().setTextFindParameter(parameterSet);

       // 查找指定文本的实例
       List<PdfTextFragment> fragments = finder.find("最直观的优势");

       // 获取第一个实例
       PdfTextFragment textFragment = fragments.get(0);

       // 获取文本的边界
       Rectangle2D rect = textFragment.getBounds()[0];

       // 获取添加注释的坐标
       double left = rect.getMinX();
       double top = rect.getMinY();
       double right = rect.getMaxX();
       double bottom = rect.getMaxY();

       // 创建一个形状注释
       PdfPolygonAnnotation polygonAnnotation = new PdfPolygonAnnotation(page, new Point2D[]{
               new Point2D.Double(left, top),
               new Point2D.Double(right, top),
               new Point2D.Double(right + 5, bottom),
               new Point2D.Double(left - 5, bottom),
               new Point2D.Double(left, top)
       });

       // 设置注释的边框
       PdfAnnotationBorder border = new PdfAnnotationBorder(1f);
       polygonAnnotation.setBorder(border);
       polygonAnnotation.setBorderEffect(PdfBorderEffect.Small_Cloud);
       polygonAnnotation.setColor(new PdfRGBColor(Color.green));

       // 设置注释的文本
       polygonAnnotation.setText("这是一个多边形注释\r由 Spire.PDF for Java 添加");

       // 将注释添加到注释集合中
       page.getAnnotations().add(polygonAnnotation);

       // 保存结果到文件
       doc.saveToFile("C:/Users/Administrator/Desktop/形状注释.pdf");

       // 释放资源
       doc.dispose();
   }
}

在 PDF 中添加一个形状注释

在 Java 中向 PDF 添加网页链接注释

网页链接注释通常用于在 PDF 中指定的文本上。您可以使用 PdfTextFinder 类来找到所需文本和获取它的边界信息。之后,您可以在这个区域创建一个 PdfUriAnnotation 对象,并指定一个外部网页链接。

以下是在 Java 中向 PDF 添加网页链接注释的步骤:

  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfUriAnnotation;
import com.spire.pdf.texts.PdfTextFinder;
import com.spire.pdf.texts.PdfTextFragment;
import com.spire.pdf.texts.TextFindParameter;

import java.awt.geom.Rectangle2D;
import java.util.EnumSet;
import java.util.List;

public class AddWebLinkAnnotation {

   public static void main(String[] args) {

       // 创建一个 PdfDocument 对象
       PdfDocument doc = new PdfDocument();

       // 加载 PDF 文件
       doc.loadFromFile("C:/Users/Administrator/Desktop/AI绘画.pdf");

       // 获取特定页面
       PdfPageBase page = doc.getPages().get(0);

       // 基于页面创建 PdfTextFinder 对象
       PdfTextFinder finder = new PdfTextFinder(page);

       // 设置查找选项
       EnumSet<TextFindParameter> parameterSet = EnumSet.of(TextFindParameter.WholeWord, TextFindParameter.IgnoreCase);
       finder.getOptions().setTextFindParameter(parameterSet);

       // 查找指定文本的实例
       List<PdfTextFragment> fragments = finder.find("最直观的优势");

       // 获取第一个实例
       PdfTextFragment textFragment = fragments.get(0);

       // 获取文本的边界
       Rectangle2D rect = textFragment.getBounds()[0];

       // 创建一个 Url 注释
       PdfUriAnnotation urlAnnotation = new PdfUriAnnotation(rect, "https:\\\\www.e-iceblue.com\\");

       // 将注释添加到注释集合中
       page.getAnnotations().add(urlAnnotation);

       // 保存结果到文件
       doc.saveToFile("C:/Users/Administrator/Desktop/网页链接注释.pdf");

       // 释放资源
       doc.dispose();
   }
}

在 PDF 中添加网页链接注释

在 Java 中向 PDF 添加文件链接注释

文件链接注释的创建通常也基于指定文本。 您可以使用 PdfTextFinder 类在指定页面上找到所需文本,并且获取其边界信息。然后,您可以根据这些信息创建 PdfFileLinkAnnotation 对象,并指定一个链接,将其重定向到外部文件。

在 Java 中向 PDF 添加文件链接注释的操作步骤如下:

  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfFileLinkAnnotation;
import com.spire.pdf.texts.PdfTextFinder;
import com.spire.pdf.texts.PdfTextFragment;
import com.spire.pdf.texts.TextFindParameter;

import java.awt.geom.Rectangle2D;
import java.util.EnumSet;
import java.util.List;

public class AddFileLinkAnnotation {

   public static void main(String[] args) {

       // 创建一个 PdfDocument 对象
       PdfDocument doc = new PdfDocument();

       // 加载 PDF 文件
       doc.loadFromFile("C:/Users/Administrator/Desktop/AI绘画.pdf");

       // 获取特定页面
       PdfPageBase page = doc.getPages().get(0);

       // 基于页面创建 PdfTextFinder 对象
       PdfTextFinder finder = new PdfTextFinder(page);

       // 设置查找选项
       EnumSet<TextFindParameter> parameterSet = EnumSet.of(TextFindParameter.WholeWord, TextFindParameter.IgnoreCase);
       finder.getOptions().setTextFindParameter(parameterSet);

       // 查找指定文本的实例
       List<PdfTextFragment> fragments = finder.find("最直观的优势");

       // 获取第一个实例
       PdfTextFragment textFragment = fragments.get(0);

       // 获取文本的边界
       Rectangle2D rect = textFragment.getBounds()[0];

       // 创建文件链接注释
       PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation(rect, "C:/Users/Administrator/Desktop/AI绘画的利与弊及法律应对.docx");

       // 将注释添加到注释集合中
       page.getAnnotations().add(fileLinkAnnotation);

       // 保存结果到文件
       doc.saveToFile("C:/Users/Administrator/Desktop/文件链接注释.pdf");

       // 释放资源
       doc.dispose();
   }
}

在 PDF 中添加文件链接注释

在 Java 中向 PDF 添加文档链接注释

您可以使用 PdfTextFinder 类来找到添加文档链接注释所需要的文本,然后基于其边界创建一个 PdfDocumentLinkAnnotation 对象。最后您需要为这个注释设置一个目标地点,它通常指向 PDF 文档中的另一页。

下方是在 Java 中向 PDF 添加文档链接注释的具体步骤:

  • Java
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;
import com.spire.pdf.annotations.PdfDocumentLinkAnnotation;
import com.spire.pdf.general.PdfDestination;
import com.spire.pdf.texts.PdfTextFinder;
import com.spire.pdf.texts.PdfTextFragment;
import com.spire.pdf.texts.TextFindParameter;

import java.awt.geom.Rectangle2D;
import java.util.EnumSet;
import java.util.List;

public class AddDocumentLinkAnnotation {

   public static void main(String[] args) {

       // 创建一个 PdfDocument 对象
       PdfDocument doc = new PdfDocument();

       // 加载 PDF 文件
       doc.loadFromFile("C:/Users/Administrator/Desktop/AI绘画.pdf");

       // 获取特定页面
       PdfPageBase page = doc.getPages().get(0);

       // 基于页面创建 PdfTextFinder 对象
       PdfTextFinder finder = new PdfTextFinder(page);

       // 设置查找选项
       EnumSet<TextFindParameter> parameterSet = EnumSet.of(TextFindParameter.WholeWord, TextFindParameter.IgnoreCase);
       finder.getOptions().setTextFindParameter(parameterSet);

       // 查找指定文本的实例
       List<PdfTextFragment> fragments = finder.find("弊端不容忽视");

       // 获取第一个实例
       PdfTextFragment textFragment = fragments.get(0);

       // 获取文本的边界
       Rectangle2D rect = textFragment.getBounds()[0];

       // 创建文档链接注释
       PdfDocumentLinkAnnotation documentLinkAnnotation = new PdfDocumentLinkAnnotation(rect);

       // 设置注释的目标页
       documentLinkAnnotation.setDestination(new PdfDestination(doc.getPages().get(1)));

       // 将注释添加到注释集合中
       page.getAnnotations().add(documentLinkAnnotation);

       // 保存结果到文件
       doc.saveToFile("C:/Users/Administrator/Desktop/文档内链接注释.pdf");

       // 释放资源
       doc.dispose();
   }
}

***

申请临时 License

如果您希望删除结果文档中的评估消息,或者摆脱功能限制,请该Email地址已收到反垃圾邮件插件保护。要显示它您需要在浏览器中启用JavaScript。获取有效期 30 天的临时许可证。

本文介绍如何使用Spire.PDF for .NET将PDF转为符合PDF/A-1A, 2A, 3A, 1B, 2B和3B标准的PDF。

C#
using System;
using Spire.Pdf.Conversion;

namespace ConvertPdf2PdfA
{
    class Program
    {
        static void Main(string[] args)
        {
            //指定输入文档路径
            String inputFile = @"C:\Users\Administrator\Desktop\sample.pdf";

            //指定输出文档的存放文件夹
            String outputFolder = @"C:\Users\Administrator\Desktop\Output\";

            //创建PdfStandardsConverter对象,传入输入文档路径
            PdfStandardsConverter converter = new PdfStandardsConverter(inputFile);

            //转换为PdfA1A
            converter.ToPdfA1A(outputFolder + "ToPdfA1A.pdf");

            //转换为PdfA1B
            converter.ToPdfA1B(outputFolder + "ToPdfA1B.pdf");

            //转换为PdfA2A
            converter.ToPdfA2A(outputFolder + "ToPdfA2A.pdf");

            //转换为PdfA2B
            converter.ToPdfA2B(outputFolder + "ToPdfA2B.pdf");

            //转换为PdfA3A
            converter.ToPdfA3A(outputFolder + "ToPdfA3A.pdf");

            //转换为PdfA3B
            converter.ToPdfA3B(outputFolder + "ToPdfA3B.pdf");
        }
    }
}
VB.NET
Imports System
Imports Spire.Pdf.Conversion
 
Namespace ConvertPdf2PdfA
    Class Program
        Shared  Sub Main(ByVal args() As String)
            '指定输入文档路径
            Dim inputFile As String =  "C:\Users\Administrator\Desktop\sample.pdf" 
 
            '指定输出文档的存放文件夹
            Dim outputFolder As String =  "C:\Users\Administrator\Desktop\Output\" 
 
            '创建PdfStandardsConverter对象,传入输入文档路径
            Dim converter As PdfStandardsConverter =  New PdfStandardsConverter(inputFile) 
 
            '转换为PdfA1A
            converter.ToPdfA1A(outputFolder + "ToPdfA1A.pdf")
 
            '转换为PdfA1B
            converter.ToPdfA1B(outputFolder + "ToPdfA1B.pdf")
 
            '转换为PdfA2A
            converter.ToPdfA2A(outputFolder + "ToPdfA2A.pdf")
 
            '转换为PdfA2B
            converter.ToPdfA2B(outputFolder + "ToPdfA2B.pdf")
 
            '转换为PdfA3A
            converter.ToPdfA3A(outputFolder + "ToPdfA3A.pdf")
 
            '转换为PdfA3B
            converter.ToPdfA3B(outputFolder + "ToPdfA3B.pdf")
        End Sub
    End Class
End Namespace