Itext get field coordinates from existing pdf

前端 未结 1 1301
滥情空心
滥情空心 2021-01-03 06:17

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

相关标签:
1条回答
  • 2021-01-03 07:06

    To completely solve the problem, I wrote this java class:

    // 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:

    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:

    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
    
    0 讨论(0)
提交回复
热议问题