Java itext从现有的pdf获取字段坐标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19066141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Itext get field coordinates from existing pdf
提问by Footniko
First of All, I'm not Java Developer:( I just need small programm, that will output to me coordinates of field by field name from existing pdf file, that I will type when I call my class from command line, something like this:
首先,我不是 Java 开发人员 :( 我只需要小程序,它将从现有的 pdf 文件中按字段名称向我输出字段坐标,当我从命令行调用我的类时,我会输入它,就像这样:
javac GetField.java
java GetField <myForm.pdf>, <myFieldName>
I'm using itext on my server. Now I'm trying to run simple code:
我在我的服务器上使用 itext。现在我正在尝试运行简单的代码:
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.*;
import java.util.*;
import java.awt.List;
class HelloWorld{
public static void main(String[] args) throws IOException {
PdfReader reader = new PdfReader("Noname.pdf");
AcroFields fields = reader.getAcroFields();
float[] positions = fields.getFieldPositions("Signature");
System.out.println( positions );
}
}
But I have error: "Type mismatch: cannot convert from List to float[]". When I replace
但我有错误:“类型不匹配:无法从 List 转换为 float[]”。当我更换
float[] positions = fields.getFieldPositions("Signature");
System.out.println( positions );
with
和
System.out.println( fields.getFieldPositions("Signature") );
I got result "[com.itextpdf.text.pdf.AcroFields$FieldPosition@36af35b1]", but I need float values. Can you help me please with this task?
我得到了结果“ [com.itextpdf.text.pdf.AcroFields$FieldPosition@36af35b1]”,但我需要浮点值。你能帮我完成这个任务吗?
采纳答案by Footniko
To completely solve the problem, I wrote this java class:
为了彻底解决问题,我写了这个java类:
// GetSigPos.java
import com.itextpdf.text.*;
import com.itextpdf.text.pdf.*;
import java.io.*;
//import java.util.*;
import java.util.List;
//import java.awt.List;
class GetSigPos {
public static void main(String[] args) throws IOException {
String pdfFile = args[0];
PdfReader reader = new PdfReader(pdfFile);
AcroFields fields = reader.getAcroFields();
for(String signame : fields.getBlankSignatureNames()) {
List<AcroFields.FieldPosition> positions = fields.getFieldPositions(signame);
Rectangle rect = positions.get(0).position; // In points:
float left = rect.getLeft();
float bTop = rect.getTop();
float width = rect.getWidth();
float height = rect.getHeight();
int page = positions.get(0).page;
Rectangle pageSize = reader.getPageSize(page);
float pageHeight = pageSize.getTop();
float top = pageHeight - bTop;
System.out.print(signame + "::" + page + "::" + left + "::" + top + "::" + width + "::" + height + "\n");
}
}
}
Then I can run it in command line:
然后我可以在命令行中运行它:
javac GetSigPos.java
java GetSigPos "MyForm.pdf"
Or in my php program I can execute them using this command:
或者在我的 php 程序中,我可以使用以下命令执行它们:
exec('java -cp .:/usr/local/bin/pdfbox/itextpdf-5.4.4.jar:/usr/local/bin/pdfbox GetSigPos "'.$pdfName.'" 2>&1', $output);
echo '<pre>';
print_r($output);
echo '</pre>';
P.S.Don't forget to type CLASSPATH to your java! I'm using Centos 6:
PS不要忘记在你的 Java 中输入 CLASSPATH!我正在使用Centos 6:
vi /root/.bash_rofile
And type this:
然后输入:
export JAVA_HOME=/usr/lib/jvm/jre-1.5.0-gcj
export PATH=$PATH:$JAVA_HOME/bin
export CLASSPATH=.:/usr/local/bin/pdfbox/itextpdf-5.4.4.jar:/usr/local/bin/pdfbox