【问题标题】:how to insert text into a scanned pdf document using java如何使用java将文本插入扫描的pdf文档
【发布时间】:2012-02-10 21:35:18
【问题描述】:

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

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 时,我不会得到我添加的那些文本。

【问题讨论】:

  • 如果您添加一些导致您出现问题的代码,您会更快得到答案...

标签: java pdf itext


【解决方案1】:

来自this iText Example(这与您想要的相反,但是将getUnderContent 切换为getOverContent 就可以了):

块引用 每个 PDF 页面有两个额外的层;一个位于所有文本/图形之上,一个位于底部。所有用户添加的内容都介于这两者之间。如果我们进入这个最底层的内容,我们可以在下面写任何我们想要的东西。要进入这个最底层,我们可以使用 PdfStamper 对象的“getUnderContent”方法。
这在 iText API 参考中有记录,如下所示:

public PdfContentByte getUnderContent(int pageNum)
    Gets a PdfContentByte to write under the page of the original document.
    Parameters:
       pageNum - the page number where the extra content is written
    Returns:
         a PdfContentByte to write under the page of the original document

【讨论】:

  • 你是对的,我使用 PdfContentByte 的方法来编写我的文本并使用 PdfStramper 的 getOverContent 将这些文本带到页面上。
  • 我的字符串长度大于 185,所以那些多余的字符串会从页面中消失。所以现在我需要包装这些文本。如何包装?
  • 这仅适用于少数类型的 pdf,并且在大多数扫描的 pdf 中,插入的字符会像隐藏文本一样添加回页面。
【解决方案2】:

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

我推荐 iText(请参阅 Example Code iText)来帮助您做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-23
    • 2015-11-20
    • 2012-04-04
    • 1970-01-01
    • 2015-04-12
    • 1970-01-01
    相关资源
    最近更新 更多