[add] notebook
This commit is contained in:
parent
e1c74c9608
commit
8ea1a343de
5 changed files with 106 additions and 24 deletions
74
src/lib/pageno.xqm
Normal file
74
src/lib/pageno.xqm
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
xquery version '3.1';
|
||||
(:~ look for pagenos in pdf text
|
||||
pagenos:page-report($doc )=>pagenos:inverted-map()
|
||||
:)
|
||||
module namespace pagenos = 'urn:pageno';
|
||||
import module namespace pdfbox="urn:expkg-zone58:pdfbox:3" at "pdfbox3.xqm";
|
||||
|
||||
(: look for possible page number in first/last line of page text
|
||||
@todo last line and roman
|
||||
1=Number system ( D=decimal, R=Roman)
|
||||
2=Side L=left,R=right
|
||||
:)
|
||||
declare variable $pagenos:pats:=map{
|
||||
"DL": "^([1-9][0-9]*).*",
|
||||
"DR": ".*[^0-9]([1-9][0-9]*)$",
|
||||
"RL": "^([ivxc]+).*",
|
||||
"RR": ".*[^ivxc]([ivxc]+)$"
|
||||
};
|
||||
|
||||
(: page-reports for all pages :)
|
||||
declare function pagenos:page-report($doc as item())
|
||||
as element(page)*{
|
||||
let $count:=pdfbox:page-count($doc)=>trace("Pages: ")
|
||||
return (0 to $count -1)!pagenos:page-report($doc,.)
|
||||
};
|
||||
|
||||
(: page-report for given page :)
|
||||
declare function pagenos:page-report($doc as item(), $page as xs:integer)
|
||||
as element(page){
|
||||
let $txt:=pdfbox:getText($doc,$page)
|
||||
let $line1:=substring-before($txt,file:line-separator())
|
||||
let $fn:=function($acc,$this){ $acc otherwise pagenos:line-report($this,$line1)}
|
||||
let $found:=map:keys($pagenos:pats)=>fold-left( (),$fn)
|
||||
|
||||
return <page index="{ $page }">{ $found, $line1 }</page>
|
||||
};
|
||||
|
||||
(: empty or attributes created by matching $style with $line1 :)
|
||||
declare function pagenos:line-report($style as xs:string, $line1 as xs:string)
|
||||
as attribute(*)*{
|
||||
if(matches($line1,$pagenos:pats?($style)))
|
||||
then (
|
||||
attribute {"style"} { substring($style,1,1) } ,(: 1st key:)
|
||||
attribute {"LR"} { substring($style,2,1) } ,(: 2nd key:)
|
||||
attribute {"number"} { replace($line1,$pagenos:pats?($style),"$1") }
|
||||
)
|
||||
};
|
||||
|
||||
(:~ keys are parsed pageno values are pageindices where found:)
|
||||
declare function pagenos:inverted-map($pages as element(page)*)
|
||||
as map(*) {
|
||||
$pages[@number]!map:entry(string(@number),string(@index))
|
||||
=>map:merge(map{"duplicates":"combine"})
|
||||
};
|
||||
|
||||
(:~ convert roman to integer, zero if invalid
|
||||
@see https://joewiz.org/2021/05/30/converting-roman-numerals-with-xquery-xslt/
|
||||
:)
|
||||
declare function pagenos:decode-roman-numeral($roman-numeral as xs:string)
|
||||
as xs:integer{
|
||||
$roman-numeral => upper-case() => pagenos:characters()
|
||||
=> for-each(map { "M": 1000, "D": 500, "C": 100, "L": 50, "X": 10, "V": 5, "I": 1 })
|
||||
=> fold-right([0,0], function($number,$accumulator) {
|
||||
if ($number lt $accumulator?2)
|
||||
then [ $accumulator?1 - $number, $number ]
|
||||
else [ $accumulator?1 + $number, $number ] } )
|
||||
=> array:head()
|
||||
};
|
||||
|
||||
(:~ xpath 4:)
|
||||
declare function pagenos:characters($value as xs:string?)
|
||||
as xs:string*{
|
||||
fn:string-to-codepoints($value) ! fn:codepoints-to-string(.)
|
||||
};
|
||||
179
src/lib/pdfbox3.xqm
Normal file
179
src/lib/pdfbox3.xqm
Normal file
|
|
@ -0,0 +1,179 @@
|
|||
xquery version '3.1';
|
||||
(:~
|
||||
pdfbox 3.0 https://pdfbox.apache.org/ BaseX 10+ interface library,
|
||||
requires pdfbox jar on classpath
|
||||
3.02 required tested with pdfbox-app-3.0.2-20240121.184204-66.jar
|
||||
@see https://lists.apache.org/list?users@pdfbox.apache.org:lte=1M:loader
|
||||
:)
|
||||
module namespace pdfbox="urn:expkg-zone58:pdfbox:3";
|
||||
|
||||
declare namespace Loader ="java:org.apache.pdfbox.Loader";
|
||||
declare namespace PDFTextStripper = "java:org.apache.pdfbox.text.PDFTextStripper";
|
||||
|
||||
(:~
|
||||
@see https://javadoc.io/static/org.apache.pdfbox/pdfbox/3.0.0/org/apache/pdfbox/pdmodel/PDDocument.html
|
||||
:)
|
||||
declare namespace PDDocument ="java:org.apache.pdfbox.pdmodel.PDDocument";
|
||||
|
||||
declare namespace PDDocumentCatalog ="java:org.apache.pdfbox.pdmodel.PDDocumentCatalog";
|
||||
declare namespace PDPageLabels ="java:org.apache.pdfbox.pdmodel.common.PDPageLabels";
|
||||
|
||||
(:~
|
||||
@see https://javadoc.io/static/org.apache.pdfbox/pdfbox/3.0.0/org/apache/pdfbox/multipdf/PageExtractor.html
|
||||
:)
|
||||
declare namespace PageExtractor ="java:org.apache.pdfbox.multipdf.PageExtractor";
|
||||
|
||||
(:~
|
||||
@see https://javadoc.io/static/org.apache.pdfbox/pdfbox/3.0.0/org/apache/pdfbox/pdmodel/PDPageTree.html
|
||||
:)
|
||||
declare namespace PDPageTree ="java:org.apache.pdfbox.pdmodel.PDPageTree";
|
||||
|
||||
(:~
|
||||
@see https://javadoc.io/static/org.apache.pdfbox/pdfbox/3.0.0/org/apache/pdfbox/pdmodel/interactive/documentnavigation/outline/PDDocumentOutline.html
|
||||
:)
|
||||
declare namespace PDDocumentOutline ="java:org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDDocumentOutline";
|
||||
|
||||
|
||||
(:~
|
||||
@see https://javadoc.io/static/org.apache.pdfbox/pdfbox/3.0.0/org/apache/pdfbox/pdmodel/interactive/documentnavigation/outline/PDOutlineItem.html
|
||||
:)
|
||||
declare namespace PDOutlineItem="java:org.apache.pdfbox.pdmodel.interactive.documentnavigation.outline.PDOutlineItem";
|
||||
|
||||
declare namespace File ="java:java.io.File";
|
||||
declare namespace RandomAccessReadBufferedFile = "java:org.apache.pdfbox.io.RandomAccessReadBufferedFile";
|
||||
|
||||
(:~ open pdf, returns handle :)
|
||||
declare function pdfbox:open($pdfpath as xs:string){
|
||||
Loader:loadPDF( RandomAccessReadBufferedFile:new($pdfpath))
|
||||
};
|
||||
(:~ save pdf $doc to $savepath , returns $savepath :)
|
||||
declare function pdfbox:save($doc,$savepath as xs:string)
|
||||
as xs:string{
|
||||
PDDocument:save($doc,File:new($savepath)),$savepath
|
||||
};
|
||||
|
||||
declare function pdfbox:close($doc)
|
||||
as empty-sequence(){
|
||||
(# db:wrapjava void #) {
|
||||
PDDocument:close($doc)
|
||||
}
|
||||
};
|
||||
|
||||
declare function pdfbox:page-count($doc as item())
|
||||
as xs:integer{
|
||||
PDDocument:getNumberOfPages($doc)
|
||||
};
|
||||
|
||||
|
||||
(:~ outline for $doc as map()* :)
|
||||
declare function pdfbox:outline($doc as item())
|
||||
as map(*)*{
|
||||
(# db:wrapjava some #) {
|
||||
let $bookmark:=
|
||||
PDDocument:getDocumentCatalog($doc)
|
||||
=>PDDocumentCatalog:getDocumentOutline()
|
||||
=>PDOutlineItem:getFirstChild()
|
||||
|
||||
let $bk:=pdfbox:outline($doc,$bookmark)
|
||||
return $bk
|
||||
}
|
||||
};
|
||||
|
||||
(: return bookmark info for children of $outlineItem :)
|
||||
declare function pdfbox:outline($doc,$outlineItem )
|
||||
as map(*)*
|
||||
{
|
||||
let $find:=hof:until(
|
||||
function($output) { empty($output?this) },
|
||||
function($input ) {
|
||||
let $bk:= pdfbox:bookmark($input?this,$doc)
|
||||
let $bk:= if($bk?hasChildren)
|
||||
then let $kids:=pdfbox:outline($doc,PDOutlineItem:getFirstChild($input?this))
|
||||
return map:merge(($bk,map:entry("children",$kids)))
|
||||
else $bk
|
||||
return map{
|
||||
"list": ($input?list, $bk),
|
||||
"this": PDOutlineItem:getNextSibling($input?this)}
|
||||
},
|
||||
map{"list":(),"this":$outlineItem}
|
||||
)
|
||||
return $find?list
|
||||
};
|
||||
|
||||
declare function pdfbox:outline-xml($outline as map(*)*)
|
||||
as element(outline){
|
||||
element outline {
|
||||
$outline!pdfbox:bookmark-xml(.)
|
||||
}
|
||||
};
|
||||
|
||||
declare function pdfbox:bookmark-xml($outline as map(*)*)
|
||||
as element(bookmark)*
|
||||
{
|
||||
$outline!
|
||||
<bookmark title="{?title}" index="{?index}">
|
||||
{?children!pdfbox:bookmark-xml(.)}
|
||||
</bookmark>
|
||||
};
|
||||
|
||||
(: return bookmark info for children of $outlineItem :)
|
||||
declare function pdfbox:bookmark($bookmark as item(),$doc as item())
|
||||
as map(*){
|
||||
map{
|
||||
"index": PDOutlineItem:findDestinationPage($bookmark,$doc)=>pdfbox:pageIndex($doc),
|
||||
"title": PDOutlineItem:getTitle($bookmark),
|
||||
"hasChildren": PDOutlineItem:hasChildren($bookmark)
|
||||
}
|
||||
};
|
||||
|
||||
declare function pdfbox:outx($page,$document){
|
||||
let $currentPage := PDOutlineItem:findDestinationPage($page,$document)
|
||||
let $pageNumber := pdfbox:pageIndex($currentPage,$document)
|
||||
return $pageNumber
|
||||
};
|
||||
|
||||
(:~ pageIndex of $page in $document :)
|
||||
declare function pdfbox:pageIndex(
|
||||
$page (: as java:org.apache.pdfbox.pdmodel.PDPage :),
|
||||
$document)
|
||||
{
|
||||
PDDocument:getDocumentCatalog($document)
|
||||
=>PDDocumentCatalog:getPages()
|
||||
=>PDPageTree:indexOf($page)
|
||||
};
|
||||
|
||||
|
||||
|
||||
(:~ new PDF doc from 1 based page range
|
||||
@return save path :)
|
||||
declare function pdfbox:extract($doc as item(),
|
||||
$start as xs:integer,$end as xs:integer,$target as xs:string)
|
||||
as xs:string
|
||||
{
|
||||
let $a:=PageExtractor:new($doc, $start, $end) =>PageExtractor:extract()
|
||||
return (pdfbox:save($a,$target),pdfbox:close($a))
|
||||
};
|
||||
|
||||
|
||||
(:~ @TODO
|
||||
@see https://codereview.stackexchange.com/questions/286078/java-code-showing-page-labels-from-pdf-files
|
||||
:)
|
||||
declare function pdfbox:getPageLabels($doc as item())
|
||||
as item()*{
|
||||
PDDocument:getDocumentCatalog($doc)
|
||||
=>PDDocumentCatalog:getPageLabels()
|
||||
=>PDPageLabels:getLabelsByPageIndices()
|
||||
};
|
||||
|
||||
(: text on $pageNo :)
|
||||
declare function pdfbox:getText($doc as item(), $pageNo as xs:integer)
|
||||
as xs:string{
|
||||
let $tStripper := (# db:wrapjava instance #) {
|
||||
PDFTextStripper:new()
|
||||
=> PDFTextStripper:setStartPage($pageNo)
|
||||
=> PDFTextStripper:setEndPage($pageNo)
|
||||
}
|
||||
return (# db:checkstrings #) {PDFTextStripper:getText($tStripper,$doc)}
|
||||
};
|
||||
|
||||
|
||||
22
src/scratch/pdfbox.xq
Normal file
22
src/scratch/pdfbox.xq
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
(: PDFBOX experiments
|
||||
:)
|
||||
|
||||
import module namespace pdfbox="urn:expkg-zone58:pdfbox:3" at "../lib/pdfbox3.xqm";
|
||||
|
||||
|
||||
declare variable $samples:= map{
|
||||
"climate": "data\drop-01d\set\2-6-1\A5579C_1\271989---Book_File-Web_PDF_9798400627484_486728.pdf",
|
||||
"women": "data\drop-01d\set\2-6-1\A6229C_1\257334---Book_File-Web_PDF_9798216172628_486742.pdf",
|
||||
"genocide": "data\drop1-pdf\GR2967-TRD\272791---Book_File-Web_PDF_9798400640216_486366.pdf",
|
||||
"world": "data\drop-01c\gpg-book\2-6\A3506C-TRD\256186---Book_File-Web_PDF_9798216038955_486148.pdf",
|
||||
"dummy": "lib\abc-clio-dummy.pdf"
|
||||
};
|
||||
declare variable $base:= "C:\Users\mrwhe\git\bloomsbury\content-architecture\xquery\ABC-CLIO\data";
|
||||
(:~ resolve :)
|
||||
declare variable $PDF:= $samples?women=>file:resolve-path($base);
|
||||
|
||||
|
||||
|
||||
let $doc:=pdfbox:open($PDF)
|
||||
return pdfbox:outline($doc)=>pdfbox:outline-xml()
|
||||
(: return pdfbox:extract($doc,"c:\tmp\junk3.pdf",1,pdfbox:page-count($doc)) :)
|
||||
Loading…
Add table
Add a link
Reference in a new issue