国产PDF格式处理控件Spire.PDF功能演示:在Java中查找和替换 PDF 中的文本

与 MS Word 不同,PDF 文档中的文本几乎无法修改,因为 PDF 不会自动重新排列页面内容。Spire.PDF for Java提供的这个场景并没有真正替换文档中的现有文本,而是用一个白色矩形覆盖旧文本并在其上绘制新文本。为避免替换后出现空格或重叠问题,请确保替换文本的长度与要替换的文本长度一样长。

Spire.PDF for Java 是一款专门对 PDF 文档进行操作的 Java 类库。该类库的主要功能在于帮助开发人员在 Java 应用程序(J2SE和J2EE)中生成 PDF 文档和操作现有 PDF 文档,并且运行环境无需安装 Adobe Acrobat。

与 MS Word 不同,PDF 文档中的文本几乎无法修改,因为 PDF 不会自动重新排列页面内容。Spire.PDF for Java提供的这个场景并没有真正替换文档中的现有文本,而是用一个白色矩形覆盖旧文本并在其上绘制新文本。为避免替换后出现空格或重叠问题,请确保替换文本的长度与要替换的文本长度一样长。

Spire.PDF for Java最新试用版

查找和替换 PDF 中的文本

以下是在某个 PDF 页面中查找所选文本的步骤,并用新字符串覆盖它们。

  • 创建一个PdfDocument对象。
  • 使用PdfDocument.loadFromFile()方法加载示例 PDF 文档。
  • 使用PdfDocument.getPages().get()方法获取第一页,并使用PdfPageBase.findText()方法从页面中找到选择的文本。
  • 获取特定查找结果的边界,并使用PdfPageBase.getCanvas().drawRectangle()方法绘制一个白色矩形以覆盖边界区域。
  • 使用PdfPageBase.getCanvas().drawString()方法在该区域上绘制一个新的刺。
  • 使用PdfDocument.saveToFile()方法将文档保存到另一个文件。
import com.spire.pdf.PdfDocument;import com.spire.pdf.PdfPageBase;import com.spire.pdf.general.find.PdfTextFind;import com.spire.pdf.general.find.PdfTextFindCollection;import com.spire.pdf.graphics.*;import com.spire.pdf.graphics.fonts.PdfUsedFont;import  com.spire.ms.System.Collections.Generic.List;import java.awt.*;import java.awt.geom.Rectangle2D;public class FindAndReplace {    public static void main(String[] args) {        //Create a PdfDocument object        PdfDocument doc = new PdfDocument();        //Load a sample PDF document        doc.loadFromFile("C:\Users\Administrator\Desktop\sample.pdf");        //Get the first page        PdfPageBase page = doc.getPages().get(0);        //Find the specific string from the page        PdfTextFindCollection findResults = page.findText("Spire.PDF for Java", true, false);        PdfTextFind[] findCollection = findResults.getFinds();        //Get the used fonts in the document        PdfUsedFont[] usedFonts = doc.getUsedFonts();        //Create a PdfTrueTypeFont object based on a specific used font        PdfTrueTypeFont pdfTrueTypeFont = new PdfTrueTypeFont(new Font(usedFonts[0].getName(), 0, (int) usedFonts[0].getSize()));        //Define new text for replacing        String newText = "Spire.PDF for .Net";        //Declare a List variable        List rect;        //Loop through the find collection        for (int i = 0; i < findCollection.length; i++) {            //Get the bounds of a specific find result            rect = findCollection[i].getTextBounds();            //Draw a white rectangle to convert the old text            page.getCanvas().drawRectangle(PdfBrushes.getWhite(), rect.get(0).getBounds2D());            //Draw new text at the position of the old text            page.getCanvas().drawString(newText, pdfTrueTypeFont, PdfBrushes.getDarkBlue(), rect.get(0).getX(), rect.get(0).getMinY() - 1);        }        //Save to file        doc.saveToFile("output/FindAndReplace.pdf");    }}

整合所有格式API处理套包Spire.office for Java正在 火热销售中!联系客服立马1分钟了解全部咨询!

国产PDF格式处理控件Spire.PDF功能演示:在Java中查找和替换 PDF 中的文本

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年10月15日
下一篇 2021年10月15日

相关推荐

发表回复

登录后才能评论