我正在使用PDFBox根据PDF中的文本放置动态文本。我已经阅读了PDF格式的每个页面,以PDF格式查找文本,并在其周围放置相应的匹配文本。但是文本被放置在另一个文本或框或行上。我需要在Free Space上放置动态匹配的文本。但我无法找到使用Java PDFbox在PDF文本周围寻找自由空间的方法。 请在下面找到我们的代码。
package com.mycompany.pdfboxsample;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.pdfbox.cos.COSBase;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDResources;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObject;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectForm;
import org.apache.pdfbox.pdmodel.graphics.xobject.PDXObjectImage;
/**
*
* @author Gayathri.R
*/
public class MainClass {
public static void main(String[] args) throws IOException {
try{
PDDocument document = null;
document = PDDocument.load(new File("C:\\Gayathri\\Smartacrf\\sample\\ItextSample\\oldcrf20.pdf"));
List pages = document.getDocumentCatalog().getAllPages();
Iterator iter = pages.iterator();
while (iter.hasNext()) {
PDPage page = (PDPage) iter.next();
PDResources resources = page.getResources();
Map<String,PDXObject> pageImages = resources.getXObjects();
if (pageImages != null) {
Iterator imageIter = pageImages.keySet().iterator();
while (imageIter.hasNext()) {
String key = (String) imageIter.next();
PDXObjectForm image = (PDXObjectForm) pageImages.get(key);
COSBase cosObject = image.getCOSObject();
System.out.println("Finded");
float lowerleftx =image.getBBox().getLowerLeftX();
float upperleftx =image.getBBox().getLowerLeftY();
float lowerlefty =image.getBBox().getUpperRightX();
float upperlefty =image.getBBox().getUpperRightY();
float width =image.getBBox().getWidth();
float height =image.getBBox().getHeight();
}
}
}
}
catch(Exception ex)
{
System.out.println(ex);
}
}