Lua Requires Chilkat v11.7.0+
Lua
PDF Text Layouts: flow, lines and grid
See more PDF Text Extraction Examples
Demonstrates the "layout" option of the "text" operation. "flow" (the default) joins the lines of each paragraph and returns tables as tab-separated rows; "lines" keeps each visual line of the page on its own line; "grid" pads with spaces so that the horizontal positions are preserved, like a fixed-pitch rendering of the page.Chilkat Lua Downloads
local chilkat = require("chilkat")
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- The PDF used in this example (a title, a paragraph, and a table with ruled cells) is available at warehouse_inventory.pdf
local pdf = chilkat.Pdf.new()
local success = pdf:load_file("qa_data/pdf/warehouse_inventory.pdf")
if not success then
print(pdf.last_error_text)
return
end
local json = chilkat.JsonObject.new()
local sb = chilkat.StringBuilder.new()
local jsonOut = chilkat.JsonObject.new()
-- ---- flow (the default) ----
-- The lines of a paragraph are joined into one line. A table becomes one line per row
-- with the cells separated by tabs, followed by a blank line.
json:update_string("layout", "flow")
success = pdf:extract_text_sb("text", json, sb, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("---- flow ----")
print(sb:get_as_string())
-- ---- lines ----
-- Each visual line of the page is one output line. A table row is its cells joined by single spaces.
json:update_string("layout", "lines")
sb:clear()
success = pdf:extract_text_sb("text", json, sb, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("---- lines ----")
print(sb:get_as_string())
-- ---- grid ----
-- Each line is padded with spaces so that the text sits at its horizontal position on the page.
-- Table columns line up as they do on the page, which suits fixed-width parsing.
json:update_string("layout", "grid")
sb:clear()
success = pdf:extract_text_sb("text", json, sb, jsonOut)
if not success then
print(pdf.last_error_text)
return
end
print("---- grid ----")
print(sb:get_as_string())
-- Sample output (abbreviated; the tabs in the flow output are shown as -> here):
-- ---- flow ----
-- Warehouse Inventory Report
--
-- Stock on hand at the Wheaton distribution center as of September 30, 2026. Quantities are counted units; the unit cost is the average landed cost in US dollars. Items at or below their reorder point are flagged in the status column.
--
-- SKU->Description->Category->Qty on Hand->Unit Cost->Status
-- WH-1001->Steel shelving unit, 5 tier->Storage->42->189.50->OK
-- WH-1002->Pallet jack, 5500 lb->Equipment->7->412.00->Reorder
-- WH-1003->Stretch wrap, 18 in roll->Packaging->640->9.75->OK
-- ...
-- WH-1012->Wire shelf liner, 48 in->Storage->24->6.15->Reorder
--
-- Four items are at or below their reorder point and one item is out of stock. Purchase orders for the flagged items should be raised before the October cycle count.
--
-- ---- lines ----
-- Warehouse Inventory Report
-- Stock on hand at the Wheaton distribution center as of September 30, 2026. Quantities are counted units; the
-- unit cost is the average landed cost in US dollars. Items at or below their reorder point are flagged in the status
-- column.
-- SKU Description Category Qty on Hand Unit Cost Status
-- WH-1001 Steel shelving unit, 5 tier Storage 42 189.50 OK
-- WH-1002 Pallet jack, 5500 lb Equipment 7 412.00 Reorder
-- WH-1003 Stretch wrap, 18 in roll Packaging 640 9.75 OK
-- ...
-- WH-1012 Wire shelf liner, 48 in Storage 24 6.15 Reorder
-- Four items are at or below their reorder point and one item is out of stock. Purchase orders for the flagged items
-- should be raised before the October cycle count.
--
-- ---- grid ----
-- Warehouse Inventory Report
-- Stock on hand at the Wheaton distribution center as of September 30, 2026. Quantities are counted units; the
-- unit cost is the average landed cost in US dollars. Items at or below their reorder point are flagged in the status
-- column.
--
-- SKU Description Category Qty on Hand Unit Cost Status
-- WH-1001 Steel shelving unit, 5 tier Storage 42 189.50 OK
-- WH-1002 Pallet jack, 5500 lb Equipment 7 412.00 Reorder
-- WH-1003 Stretch wrap, 18 in roll Packaging 640 9.75 OK
-- ...
-- WH-1012 Wire shelf liner, 48 in Storage 24 6.15 Reorder
--
-- Four items are at or below their reorder point and one item is out of stock. Purchase orders for the flagged items
-- should be raised before the October cycle count.