簡體   English   中英

編譯基本java項目

[英]Compiling basic java project

我想編譯一個基本的java項目, https://sourceforge.net/projects/pdfformfiller2它的安裝說明很短:

確保iText 庫,itext-xtra-5.x.0.jar 和itextpdf-5.x.0.jar,可以被JAVA 訪問,例如,它們被放置在當前文件夾的“lib”子文件夾中。

從以下位置獲取最新的: https : //sourceforge.net/projects/itext/files/iText/

編譯 PdfFormFiller.java

然后從命令行你給出命令(查看使用幫助):

java -jar pdfformfiller.jar

我以前從未編譯過 jar,而且我很難正確編譯 PdfFormFiller。 這是我得到的地方:

wget -O pdfformfiller.zip https://sourceforge.net/projects/pdfformfiller2/files/latest/download
# author mentions 5.2.0, which is not available anymore, so we go for the latest 5.x:
wget http://kent.dl.sourceforge.net/project/itext/5.5.10/itext5-5.5.10.zip
unzip pdfformfiller.zip
unzip itext5-5.5.10.zip -d pdfformfiller/lib

cd pdfformfiller
javac -cp "lib/*" PdfFormFiller.java

mkdir META-INF
echo -e 'Manifest-Version: 1.0\nClass-Path: pdfformfiller.jar\nMain-Class: PdfFormFiller' > META-INF/MANIFEST.MF
jar -cvfm pdfformfiller.jar META-INF/MANIFEST.MF lib PdfFormFiller.class

哪個成功而沒有錯誤,但仍然沒有運行:

$ java -jar pdfformfiller.jar
Error: Could not find or load main class PdfFormFiller

我想我錯過了一些微不足道的東西?

編輯

完全自動化:

iText5=5.5.10

wget -O pdfformfiller.zip https://sourceforge.net/projects/pdfformfiller2/files/latest/download
wget http://kent.dl.sourceforge.net/project/itext/${iText5}/itext5-${iText5}.zip
unzip pdfformfiller.zip
unzip itext5-${iText5}.zip -d pdfformfiller/lib

cd pdfformfiller
mkdir classes
javac -cp "lib/*" -d ./classes/ PdfFormFiller.java

mkdir META-INF
echo 'Manifest-Version: 1.0'                                                                                   > META-INF/MANIFEST.MF
echo "Class-Path: ./lib/itextpdf-${iText5}.jar ./lib/itext-xtra-${iText5}.jar ./lib/itext-pdfa-${iText5}.jar" >> META-INF/MANIFEST.MF
echo 'Main-Class: PdfFormFiller.PdfFormFiller'                                                                >> META-INF/MANIFEST.MF

jar -cvfm pdfformfiller.jar ./META-INF/MANIFEST.MF ./lib -C ./classes/ PdfFormFiller

編輯 2

這似乎是從 CLI 可靠地填寫 pdf 表單的唯一方法:

# list fields in a file:
$ java -jar pdfformfiller.jar input.pdf -l
myfield

# prepare field data:
$ echo 'myfield αβγ' > fields

# specify font, fill the fields, flatten the form:
$ java -jar pdfformfiller.jar input.pdf -f fields -font Times_New_Roman.ttf -flatten output.pdf

像魅力一樣工作!

以下是我為使其工作而遵循的步驟。

  1. 首先,為了清楚起見,讓我們為您編譯的類創建一個專用文件夾。 這不是強制性的,只是良好開發實踐的一個例子。 我省略了創建文件夾、更改目錄等步驟,因為這很明顯。 所有命令都從項目的根目錄運行

    javac -cp "lib/*" -d ./classes/ PdfFormFiller.java
  2. 修復遺漏的兩個主要事情:

    a) 所需lib文件夾的參考和

    b) 包名:

     echo -e 'Manifest-Version: 1.0\\nClass-Path: ./lib/itextpdf-5.5.4.jar ./lib/itext-xtra-5.5.4.jar ./lib/itext-pdfa-5.5.4.jar\\nMain-Class: PdfFormFiller.PdfFormFiller' > META-INF/MANIFEST.MF
  3. 組裝 jar(請注意這里使用了附加選項:-C):

     jar -cvfm pdfformfiller.jar ./META-INF/MANIFEST.MF ./lib -C ./classes/ PdfFormFiller
  4. 這是執行生成的 jar 文件的最終輸出:

     $ java -jar pdfformfiller.jar USAGE: pdfformfiller document.pdf [ -l ] [ -v ] [ -f fields_filename ] [ -font font_file ] [ -flatten] [ output.pdf ] document.pdf - name of source pdf file (required). -l - only list available fields in document.pdf. -v - verbose. Use to debug the fields_filename file. -f fields_filename - name of file with the list of fields values to apply to document.pdf. if ommited, stdin is used. -font font_file - font to use. Needed UTF-8 support, eg cyrillic and non-latin alphabets. -flatten - Flatten pdf forms (convert them to text disabling editing in PDF Reader). output.pdf - name of output file. If omitted, the output if sent to stdout. fields_filename file can be in UTF-8 as is of the following format: On each line, one entry consists of 'field name' followed by value of that field without any quotes. Any number of whitespaces allowed before 'field name', and one space separates 'field name' and its value. In value, newline characters should be encoded as "\\n", 'U+2029 utf-8 E280A9 : PARAGRAPH SEPARATOR PS' should be encoded as "\\p", and '\\' characters should be escaped as "\\\\". For checkboxes, values are 'Yes'/'Off'. Based on the Belgian iText library v. 5.2.0, http://www.itextpdf.com/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM