pdf - 如何在golang中从PDF中提取纯文本

标签 pdf go text extract

我想使用 GO 从 pdf 文件中提取文本。 我尝试使用 ledongthuc/pdf Go 包实现方法 GetPlainText() 以获取没有格式的纯文本内容。 但我没有得到纯文本。结果是:

 W
 S
 D
 V
 Y R
 O
 R
 Q
 W
 D
 L
 U
 H
 P
 H
 Q
 W
......

Go 代码

package main

import (
    "bytes"
    "fmt"

    "github.com/ledongthuc/pdf"
)

func main() {
    content, err := readPdf("test.pdf")
    if err != nil {
        panic(err)
    }
    fmt.Println(content)
    return
}

func readPdf(path string) (string, error) {
    r, err := pdf.Open(path)
    if err != nil {
        return "", err
    }
    totalPage := r.NumPage()

    var textBuilder bytes.Buffer
    for pageIndex := 1; pageIndex <= totalPage; pageIndex++ {
        p := r.Page(pageIndex)
        if p.V.IsNull() {
            continue
        }
        textBuilder.WriteString(p.GetPlainText("\n"))
    }
    return textBuilder.String(), nil
}

最佳答案

您可以收到诸如“pdf 文档示例”之类的消息。而不是

Ex
a
m
pl
e

of

a

pd
f

doc
u
m
e
nt
.

您需要做的是更改 textBuilder.WriteString(p.GetPlainText("\n"))

textBuilder.WriteString(p.GetPlainText(""))

希望对您有所帮助。

关于pdf - 如何在golang中从PDF中提取纯文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44560265/

相关文章:

c++ - 我可以使用什么开源 C++ 库在专有的闭​​源应用程序中显示 PDF 文件?

php - Laravel 5.2 - PHPExcel_Writer_Exception : Unable to load PDF Rendering library in

go - 为什么此goroutine不调用wg.Done()?

linux - 将行映射到 *nix 中的列

javascript - 使用 express js 在浏览器中显示 Pdf

c# - 将 HTML 转换为 PDF 时添加页码

go - websocket 设置协议(protocol)和来源

git - 通过 windows cmd/powershell 去工作,但只有 git bash

java - 如何将掩码设置为 SWT 文本以仅允许小数

c# - 你如何从 C# 中的保存文件对话框中保存?