首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用java在扫描的pdf文档中插入文本

如何使用java在扫描的pdf文档中插入文本
EN

Stack Overflow用户
提问于 2012-02-10 18:10:30
回答 2查看 2.7K关注 0票数 0

我必须将文本添加到pdf文档,其中有许多扫描的pdf文档,以便插入的文本被插入到扫描的图像中,而不是放在图像上。如何在pdf内的扫描图像上添加文本。

代码语言:javascript
运行
复制
package editExistingPDF;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

import org.apache.commons.io.FilenameUtils;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;

public class AddPragraphToPdf {



    public static void main(String[] args) throws IOException, DocumentException, BiffException {

        String tan = "no tan";
        File inputWorkbook = new File("lars.xls");
        Workbook w;

            w = Workbook.getWorkbook(inputWorkbook);
            // Get the first sheet
            Sheet sheet = w.getSheet(0);

            Cell[] tnas =sheet.getColumn(0);


        File ArticleFolder = new File("C:\\Documents and Settings\\sathishkumarkk\\My Documents\\article");
        File[] listOfArticles = ArticleFolder.listFiles();

        for (int ArticleInList = 0; ArticleInList < listOfArticles.length; ArticleInList++)  
        { 
            Document document = new Document(PageSize.A4);

      //  System.out.println(listOfArticles[ArticleInList].toString());
        PdfReader pdfArticle = new PdfReader(listOfArticles[ArticleInList].toString());
        if(listOfArticles[ArticleInList].getName().contains(".si."))
        {continue;}
        int noPgs=pdfArticle.getNumberOfPages();
        String ArticleNoWithOutExt = FilenameUtils.removeExtension(listOfArticles[ArticleInList].getName());
        String TanNo=ArticleNoWithOutExt.substring(0,ArticleNoWithOutExt.indexOf('.'));

     // Create output PDF
        PdfWriter writer = PdfWriter.getInstance(document,new FileOutputStream("C:\\Documents and Settings\\sathishkumarkk\\My Documents\\toPrint\\"+ArticleNoWithOutExt+".pdf"));
        document.open();
        PdfContentByte cb = writer.getDirectContent();
        //get tan form excel sheet
        System.out.println(TanNo);
        for(Cell content : tnas){
            if(content.getContents().contains(TanNo)){
                tan=content.getContents();
                System.out.println(tan);
            }else{
                continue;
            }
        }
        // Load existing PDF
        //PdfReader reader = new PdfReader(new FileInputStream("1.pdf"));

          for (int i = 1; i <= noPgs; i++) {
        PdfImportedPage page = writer.getImportedPage(pdfArticle, i); 

        // Copy first page of existing PDF into output PDF
        document.newPage();
        cb.addTemplate(page, 0, 0);
        // Add your TAN here
        Paragraph p= new Paragraph(tan);
        Font font = new Font();
        font.setSize(1.0f);
        p.setLeading(12.0f, 1.0f);
        p.setFont(font);

        document.add(p); 
          }
        document.close();
        }
    }

}

注意:问题是,当有一个pdf创建与文本,我没有问题,但当一个pdf是与扫描的文件和当我试图添加文本,它被添加到扫描的文档背面。因此,当我打印这些pdf时,我将不会得到我添加的那些文本。

EN

Stack Overflow用户

发布于 2012-02-10 18:38:42

为此,您需要首先读取PDF文档,提取元素,然后将文本添加到文档中,并将其重新保存为PDF文档。当然,这是假设您可以在第一时间阅读PDF文档。

我推荐iText (参见Example Code iText)来帮助你做到这一点。

票数 0
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9226061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档