Text extraction
Extract page text, Markdown and positioned text blocks with NexPDF for Go, and retrieve passages for a language model.
License module
Text extraction, Markdown export and the offline assistant are part of the viewer module and work
without a key.
Plain text of a page
ExtractPageText(pageIndex) returns the text of a page in reading order, with the layout
reconstructed into lines and paragraphs:
doc, err := nexpdf.Open("contract.pdf")
if err != nil {
log.Fatal(err)
}
defer doc.Close()
n, _ := doc.PageCount()
var pages []string
for i := 0; i < int(n); i++ {
t, err := doc.ExtractPageText(i)
if err != nil {
log.Fatal(err)
}
pages = append(pages, t)
}
text := strings.Join(pages, "\n\n")The whole document as Markdown
ExtractMarkdown() keeps headings, lists and tables, which makes it a good input for search
indexes and language models:
markdown, err := doc.ExtractMarkdown()Text blocks with positions
PageBlocksJSON(pageIndex) returns the text blocks of a page with their font size and rectangle
in PDF points:
type Block struct {
Text string `json:"text"`
Size float64 `json:"size"`
Llx float64 `json:"llx"`
Lly float64 `json:"lly"`
Urx float64 `json:"urx"`
Ury float64 `json:"ury"`
}
s, _ := doc.PageBlocksJSON(0)
var blocks []Block
json.Unmarshal([]byte(s), &blocks)
for _, b := range blocks {
fmt.Printf("%5.1fpt %s\n", b.Size, b.Text)
}Each block has text, size, llx, lly, urx and ury.
Words with exact boxes
PageWordsJSON(pageIndex, optionsJson) returns every word of a page with its exact box:
{"page","words":[{"text","bbox","quad","font","size","angle","line"}]}. bbox is
[llx, lly, urx, ury] and quad the four corners, so rotated text is boxed exactly.
{"glyphs":true} adds the box of every glyph.
type Words struct {
Page int `json:"page"`
Words []struct {
Text string `json:"text"`
Bbox [4]float64 `json:"bbox"`
Font string `json:"font"`
Size float64 `json:"size"`
Angle float64 `json:"angle"`
Line int `json:"line"`
} `json:"words"`
}
s, err := doc.PageWordsJSON(0, "")
if err != nil {
log.Fatal(err)
}
var w Words
json.Unmarshal([]byte(s), &w)
for _, word := range w.Words {
fmt.Println(word.Line, word.Text, word.Bbox)
}Text is returned in logical order, right-to-left and shaped scripts included.
Passages for a language model
AskDocument(question, k, maxChars) ranks the passages of the document against a question,
offline, and returns the k best with a page-tagged context you can send to a language model.
maxChars 0 uses the default limit (200,000 characters).
type Answer struct {
Passages []struct {
Page int `json:"page"`
Score float64 `json:"score"`
Text string `json:"text"`
} `json:"passages"`
Context string `json:"context"`
}
s, err := doc.AskDocument("When does the contract end?", 5, 0)
var result Answer
json.Unmarshal([]byte(s), &result)
for _, p := range result.Passages {
fmt.Println(p.Page, p.Score, p.Text)
}
prompt := "Answer from this document:\n" + result.Context + "\n\nQuestion: When does the contract end?"Scanned documents
Pages that are images have no text to extract. Run OCR first to add a searchable text layer.
Related
- Search
- Conversion for HTML, JSON, XML and CSV output