把文本文件的文字读出来,然后写到PDF文件

其中两个文件关于编码和字体( 下面例子会用到)。
cjkencodings.properties
UniGB-UCS2-H= UniGB-UCS2-H
UniGB-UCS2-V= UniGB-UCS2-H UniGB-UCS2-V
cjkfonts.properties
STSong-Light= Adobe-GB1-UCS2_UniGB-UCS2-H_UniGB-UCS2-V_
STSongStd-Light= Adobe-GB1-UCS2_UniGB-UCS2-H_UniGB-UCS2-V_
 
我下载的是目前最新的包—— itext-1.4.7.jar  一个简单的示例代码如下:
package powerwind;
 
import java.io.*;
 
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
 
// 功能 : 把文本文件的文字读出来 , 然后写到 PDF 文件
public class TxtToPdf {
 
    public static void main(String[] args) {
 
       Document doc = null ;
       BufferedReader br = null ;
 
       final String pdfFilename = "PdfFile.pdf" ;
       final String txtFilename = "TxtFile.txt" ;
       // 设置纸张为 A5
 
       try {
           doc = new Document();
          doc.setPageSize(PageSize. A5 );
 
           PdfWriter.getInstance(doc, new FileOutputStream(pdfFilename));
 
           // 设置字体和编码:
           BaseFont baseFont = BaseFont.createFont( "STSong-Light" , "UniGB-UCS2-H" , BaseFont. NOT_EMBEDDED );
           Font font = new Font(baseFont, 8, Font. NORMAL );
           doc.open();
 
           br = new BufferedReader( new FileReader(txtFilename));
           String str = null ;
           // txt 文件读出一行 , 加入到 Document 的段落
           while ((str = br.readLine()) != null ) {
              doc.add( new Paragraph(str, font));
           }
       } catch (DocumentException de) {
           System. out .println(de);
       } catch (IOException ioe) {
           System. out .println(ioe);
 
       } finally {
           if (doc != null )
              doc.close();
           try {
              if (br != null )
                  br.close();
           } catch (IOException ioe) {
              System. out .println(ioe);
           }
       }
       System. out .println( "done" );
    }
}
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用PDFBox库来读取PDF文件并获取指定文字坐标。 以下是一个简单的Java代码示例,它可以从PDF文件中读取文本内容并获取指定文字的坐标: ``` import org.apache.pdfbox.pdmodel.PDDocument; import org.apache.pdfbox.text.PDFTextStripper; import org.apache.pdfbox.text.TextPosition; import java.awt.geom.Rectangle2D; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class PDFReader { private static final String PDF_FILE_PATH = "path/to/your/pdf/file.pdf"; public static void main(String[] args) throws IOException { PDDocument document = PDDocument.load(new File(PDF_FILE_PATH)); PDFTextStripper stripper = new PDFTextStripper() { List<String> wordsToSearchFor = new ArrayList<>(); // The words to search for @Override protected void processTextPosition(TextPosition text) { super.processTextPosition(text); String word = text.getUnicode(); // The current word being processed if (wordsToSearchFor.contains(word)) { Rectangle2D rect = new Rectangle2D.Float(text.getX(), text.getY(), text.getWidth(), text.getHeight()); System.out.println("Word: " + word + ", Location: " + rect); } } // Setter method for the words to search for public void setWordsToSearchFor(List<String> wordsToSearchFor) { this.wordsToSearchFor = wordsToSearchFor; } }; stripper.setWordsToSearchFor(List.of("specified", "words", "to", "search", "for")); String text = stripper.getText(document); System.out.println(text); document.close(); } } ``` 在这个例子中,我们使用了PDFBox库的`PDFTextStripper`类来获取PDF文件中的文本内容。我们还定义了一个`processTextPosition`方法,该方法可以扫描每个单词,并查找在`wordsToSearchFor`列表中出现的特定单词。如果找到了这个单词,我们会获取它的位置并输出到控制台。 这是一个仅仅是作为例子的代码片段,实际项目使用中可能面临更多的细节问题。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值