【问题标题】:unable to print one pdf fonts text to another pdf?无法将一个 pdf 字体文本打印到另一个 pdf?
【发布时间】:2013-08-28 12:39:31
【问题描述】:

我在telufu pdf 中有 pdf(将 anils.com 替换为 123.176.47.55)我想从该 pdf 中提取一些文本(就像在那个 pdf 页面中我需要阅读的第 3 块数据中一样)我下载了所有字体该页面使用mupdf-1.3-windows

它会下载 pdf 使用的所有字体,但是当我通过使用这些字体将文本写入另一个 pdf 时,一些文本没有重新编写,代码就像

//output file name
public static String pdf1 = "C:\\Documents and Settings\\Administrator\\Desktop\\itextpdf\\anil.pdf";

public static void main(String[] args) throws IOException, DocumentException {
    try {
        PdfReader reader = new PdfReader(new URL("http://anils/DraftRolls/PDFGeneration.aspx?urlPath=D%3a\\SSR_2013_FINAL+ROLLS\\AC_238\\Telugu\\S01A238P038.PDF"),null);
        System.out.println("This PDF has "+reader.getNumberOfPages()+" pages.");

        // reading page no 3 
        String page = PdfTextExtractor.getTextFromPage(reader, 3);                     

        // all fonts I had checked total of 7 fonts but I didn't get all the fonts 
        BaseFont f = BaseFont.createFont("C:\\Documents and Settings\\Administrator\\Desktop\\itextpdf\\fonts\\AAAAAD+Gautami-0174.ttf", "", BaseFont.EMBEDDED);
        Font telugu = new Font(f, 18.0f, Font.BOLD);

        Paragraph description = new Paragraph(page,telugu);

        // description.setAlignment(Paragraph.ALIGN_CENTER); 

        Document document = new Document();
        // step 2
        PdfWriter.getInstance(document, new FileOutputStream(pdf1));
        // step 3
        document.open();
        document.add(description);
        document.close();
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}

有些文字从未匹配任何字体如何解决?

【问题讨论】:

  • 您的代码已经告诉您您做错了什么:您正在使用PdfTextExtractor 提取纯文本(没有样式,没有字体,...)。您没有使用PdfCopyPdfStamper 来处理实际的PDF 内容(包括字体字典、注释等)。
  • 是否可以将pdf字体和样式转换为具有相同字体和样式的html文本
  • 这很难做到。特别是如果您的 PDF 没有被标记。在任何人回答这个问题之前,您需要告诉我们:您的 PDF 被标记了吗? (如果不是:忘记转换为 HTML。)
  • 我的查询有哪些可能的解决方案?
  • 如果一个 PDF 没有被标记,那你就不走运了。在您用人眼看到表格的地方,机器只能看到文本和行的 sn-ps。如果没有适当的标签,机器就无法看到任何表格。样式或多或少也是如此。 PDF 中没有 标签这样的东西。只有不同的字体,例如 Helvetica Bold 和 Helvetica Italic,但您不能总是根据用于该字体的名称来判断字体是粗体还是斜体。长话短说:忘记你的要求。你要求的东西是非常困难的,如果不是不可能的话。

标签: pdf fonts itext


【解决方案1】:

我觉得原生做起来太难了。

您应该将其转换为另一种格式,最好是基于文本的。

我经常需要提取矢量模式,为此我个人使用 svg。

【讨论】:

    【解决方案2】:

    正如您在 cmets 中对原始问题所提到的,文本可以像原始问题一样组织,无需重排,将原始页面作为模板导入并仅显示选定区域可能是一种解决方案满足您的需求:

    public void testImportFragment() throws IOException, DocumentException
    {
        PdfReader reader = new PdfReader(new URL("http://anils/DraftRolls/PDFGeneration.aspx?urlPath=D%3a\\SSR_2013_FINAL+ROLLS\\AC_238\\Telugu\\S01A238P038.PDF"),null);
        Document document = new Document();
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("C:\\Documents and Settings\\Administrator\\Desktop\\itextpdf\\anil.pdf"));
        document.open();
        document.newPage();
        document.add(new Paragraph("Test importing the contents of the first row of page three in a different order."));
        copyFragment(reader, writer);
        document.close();
        reader.close();
    }
    
    public void copyFragment(PdfReader source, PdfWriter target) throws DocumentException
    {
        PdfImportedPage page = target.getImportedPage(source, 3);
        PdfContentByte directContent = target.getDirectContent();
    
        PdfTemplate template = directContent.createTemplate(110, 57);
        template.addTemplate(page, 1, 0, 0, 1, -15, -706);
        directContent.addTemplate(template, 200, 700);
    
        template = directContent.createTemplate(110, 57);
        template.addTemplate(page, 1, 0, 0, 1, -202, -705);
        directContent.addTemplate(template, 200, 600);
    
        template = directContent.createTemplate(110, 57);
        template.addTemplate(page, 1, 0, 0, 1, -389, -705);
        directContent.addTemplate(template, 200, 500);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-03
      • 2020-11-07
      • 1970-01-01
      • 2012-07-14
      • 2013-05-25
      • 2015-04-04
      • 1970-01-01
      • 2016-07-17
      相关资源
      最近更新 更多