? ? ? XPDF是一個開源的PDF查看、提取和轉換工具套件,使用C++編寫,支持多種操作系統,包括Linux、Unix、OS/2、Windows和Mac OS X?1。XPDF不僅是一個PDF查看器,還包含多個實用工具,如文本提取器、圖像轉換器和HTML轉換器等?,內置工具包含pdftohtml、pdftotext、pdftopng、pdftops等,支持命令行調用,可實現自主開發程序的自動化。
? ? ? 下面以pdftotext為例,介紹一下將PDF轉換為TXT的用法:
一、命令的規則
pdftotext version 4.05 [www.xpdfreader.com]
Copyright 1996-2024 Glyph & Cog, LLC
Usage: pdftotext [options] <PDF-file> [<text-file>]
? -f <int> ? ? ? ? ? ? ? : first page to convert
? -l <int> ? ? ? ? ? ? ? : last page to convert
? -layout ? ? ? ? ? ? ? ?: maintain original physical layout
? -simple ? ? ? ? ? ? ? ?: simple one-column page layout
? -simple2 ? ? ? ? ? ? ? : simple one-column page layout, version 2
? -table ? ? ? ? ? ? ? ? : similar to -layout, but optimized for tables
? -lineprinter ? ? ? ? ? : use strict fixed-pitch/height layout
? -raw ? ? ? ? ? ? ? ? ? : keep strings in content stream order
? -fixed <number> ? ? ? ?: assume fixed-pitch (or tabular) text
? -linespacing <number> ?: fixed line spacing for LinePrinter mode
? -clip ? ? ? ? ? ? ? ? ?: separate clipped text
? -nodiag ? ? ? ? ? ? ? ?: discard diagonal text
? -enc <string> ? ? ? ? ?: output text encoding name
? -eol <string> ? ? ? ? ?: output end-of-line convention (unix, dos, or mac)
? -nopgbrk ? ? ? ? ? ? ? : don't insert a page break at the end of each page
? -bom ? ? ? ? ? ? ? ? ? : insert a Unicode BOM at the start of the text file
? -marginl <number> ? ? ?: left page margin
? -marginr <number> ? ? ?: right page margin
? -margint <number> ? ? ?: top page margin
? -marginb <number> ? ? ?: bottom page margin
? -opw <string> ? ? ? ? ?: owner password (for encrypted files)
? -upw <string> ? ? ? ? ?: user password (for encrypted files)
? -verbose ? ? ? ? ? ? ? : print per-page status information
? -q ? ? ? ? ? ? ? ? ? ? : don't print any messages or errors
? -cfg <string> ? ? ? ? ?: configuration file to use in place of .xpdfrc
? -listencodings ? ? ? ? : list all available output text encodings
? -v ? ? ? ? ? ? ? ? ? ? : print copyright and version info
? -h ? ? ? ? ? ? ? ? ? ? : print usage information
? -help ? ? ? ? ? ? ? ? ?: print usage information
? --help ? ? ? ? ? ? ? ? : print usage information
? -? ? ? ? ? ? ? ? ? ? ? : print usage information
二、參數解釋
-layout 選項可以保持原始PDF的布局信息。
-enc UTF-8 指定輸出文件的編碼為UTF-8。?
-f <num> : 指定要轉換的起始頁碼。
-l <num> : 指定要轉換的結束頁碼。
-enc <encoding> : 指定輸出文件的編碼格式,常用的有UTF-8等。
三、應用實例
1. 轉換PDF為TXT文檔。
pdftotxt.exe <input.pdf> [<output.txt>]
其中 <input.pdf> 是源PDF文件的路徑, <output.txt> 是輸出的文本文件路徑(可選)。
2. 如果PDF包含中文,轉換時需指定輸出文件的編碼格式。
pdftotext -enc UTF-8 input.pdf output.txt
3. 如果需要保持原始PDF的布局,可以加上-layout 選項。
pdftotext -layout -enc UTF-8 input.pdf output.txt
4. 指定轉換頁碼的范圍。
pdftotxt.exe -layout -enc UTF-8 -f 1 -l 2 input.pdf?output.txt
將 input.pdf 的第1頁和第2頁轉換為output.txt。
四、下載地址
PDF轉換工具xpdf-tools-win-4.05
PDF轉換工具xpdf-tools-linux-4.05