Re: Read from PDF
Posted: Wed Oct 26, 2016 12:47 pm
Nice topic, could be useful. Thank you all !
Code: Select all
EnableExplicit
Prototype protoInitLibrary()
Prototype protoLoadDocument(documentpath.p-utf8,password.p-utf8)
Prototype protoGetMetaText(document, tag.p-utf8, buffer, buflen)
Procedure Main()
Protected pdf_dll =OpenLibrary(#PB_Any, "pdfium32.dll")
Protected InitLibrary.protoInitLibrary = GetFunction(pdf_dll,"_FPDF_InitLibrary@0")
Protected LoadDocument.protoLoadDocument = GetFunction(pdf_dll,"_FPDF_LoadDocument@8")
Protected GetMetaText.protoGetMetaText = GetFunction(pdf_dll,"_FPDF_GetMetaText@16")
InitLibrary()
Protected pdf_doc = LoadDocument("äpfel.pdf", "")
;Protected pdf_doc = LoadDocument("apfel.pdf", "")
Protected buflen = 1024
Protected *buffer = AllocateMemory(buflen)
Protected ret = GetMetaText(pdf_doc,"Title",*buffer, buflen)
Debug PeekS(*buffer, ret/2)
;FillMemory(*buffer, buflen)
ret = GetMetaText(pdf_doc,"Author",*buffer, buflen)
Debug PeekS(*buffer, ret/2)
;FillMemory(*buffer, buflen)
ret = GetMetaText(pdf_doc,"Creator",*buffer, buflen)
Debug PeekS(*buffer, ret/2)
CloseLibrary(#PB_Any)
EndProcedure
Main()
Code: Select all
Prototype protoLoadDocument(documentpath.p-utf8,password.p-utf8)
Code: Select all
Prototype protoLoadDocument(documentpath.p-ascii,password.p-utf8)
acreis wrote:First try ....
change
toCode: Select all
Prototype protoLoadDocument(documentpath.p-utf8,password.p-utf8)
Code: Select all
Prototype protoLoadDocument(documentpath.p-ascii,password.p-utf8)
Code: Select all
EnableExplicit
Prototype protoInitLibrary()
Prototype protoLoadDocument(documentpath.p-ascii,password.p-utf8)
Prototype protoCloseDocument(document)
Prototype protoGetMetaText(document, tag.p-utf8, buffer, buflen)
Prototype protoGetFileVersion(document, *version)
Prototype protoLoadPage(document, pageindex.l)
Prototype protoGetPageCount(document)
Prototype protoGetPageHeight(page.i)
Prototype protoGetPageWidth(page.i)
Prototype protoClosePage(page.i)
Prototype protoRenderPage(hdc, page, start_x.l, start_y.l, size_x.l, size_y.l, rotate.l, flags.l)
Global pdf_dll
Global pdf_doc
Global page_handle
Global buflen
Global *buffer
Global ret
Global pages
Global version.l
Global hoehe.d
Global breite.d
Global img
Global hdc
pdf_dll = OpenLibrary(#PB_Any, "P:\Sources\PDFium\pdfium.dll")
If pdf_dll = 0
Debug "Error loading DLL"
End
EndIf
Global InitLibrary.protoInitLibrary = GetFunction(pdf_dll, "_FPDF_InitLibrary@0")
Global LoadDocument.protoLoadDocument = GetFunction(pdf_dll, "_FPDF_LoadDocument@8")
Global CloseDocument.protoCloseDocument = GetFunction(pdf_dll, "_FPDF_CloseDocument@4")
Global GetMetaText.protoGetMetaText = GetFunction(pdf_dll, "_FPDF_GetMetaText@16")
Global GetFileVersion.protoGetFileVersion = GetFunction(pdf_dll, "_FPDF_GetFileVersion@8")
Global LoadPage.protoLoadPage = GetFunction(pdf_dll, "_FPDF_LoadPage@8")
Global GetPageCount.protoGetPageCount = GetFunction(pdf_dll, "_FPDF_GetPageCount@4")
Global GetPageHeight.protoGetPageHeight = GetFunction(pdf_dll, "_FPDF_GetPageHeight@4")
Global GetPageWidth.protoGetPageWidth = GetFunction(pdf_dll, "_FPDF_GetPageWidth@4")
Global ClosePage.protoClosePage = GetFunction(pdf_dll, "_FPDF_ClosePage@4")
Global RenderPage.protoRenderPage = GetFunction(pdf_dll, "_FPDF_RenderPage@32")
InitLibrary()
buflen = 1024
*buffer = AllocateMemory(buflen)
; Loading PDF
pdf_doc = LoadDocument("test.pdf", "")
If pdf_doc = 0
Debug "Error loading PDF!"
End
EndIf
; PDF-Titel
ret = GetMetaText(pdf_doc,"Title",*buffer, buflen)
Debug "PDF-Titel: " + PeekS(*buffer, ret)
; PDF-Author
ret = GetMetaText(pdf_doc,"Author",*buffer, buflen)
Debug "Auhtor: " + PeekS(*buffer, ret)
; PDF-Creator
ret = GetMetaText(pdf_doc,"Creator",*buffer, buflen)
Debug "Creator: " + PeekS(*buffer, ret)
; PDF-Version
ret = GetFileVersion(pdf_doc, @version)
Debug "PDF-Version: " + StrF(version/10, 2)
; Pages in PDF
pages = GetPageCount(pdf_doc)
Debug "Pages: " + Str(pages)
; Put a PDF-page into an image and save it
; ... loading page
page_handle = LoadPage(pdf_doc, 0)
; ...get measures
hoehe = GetPageHeight(page_handle)
breite = GetPageWidth(page_handle)
Debug breite
Debug hoehe
; ...create image
img = CreateImage(#PB_Any, breite, hoehe, 32, RGBA(255,255,255,0))
hdc = StartDrawing(ImageOutput(img))
; ...render page to image
ret = RenderPage(hdc, page_handle, 0, 0, 768, 1024, 0, 0)
StopDrawing()
; ... saving image
SaveImage(img, "test.bmp", #PB_ImagePlugin_BMP)
ClosePage(page_handle)
CloseDocument(pdf_doc)
CloseLibrary(#PB_Any)
End
Code: Select all
Prototype.d protoGetPageHeight(page.i)
Code: Select all
Prototype.d protoGetPageHeight(page.i)
Thanks acreis!acreis wrote:Hi
Try change
toCode: Select all
Prototype.d protoGetPageHeight(page.i)
Code: Select all
Prototype.d protoGetPageHeight(page.i)
Code: Select all
EnableExplicit
Prototype protoInitLibrary()
Prototype protoLoadDocument(documentpath.p-ascii,password.p-utf8)
Prototype protoGetMetaText(document, tag.p-utf8, buffer, buflen)
Prototype protoGetPageCount(document)
Prototype protoLoadPage(document, page_index)
Prototype protoLoadTextPage(textpage)
Prototype protoCloseTextPage(textpage)
Prototype protoCountChars(textpage)
Prototype protoGetText(textpage, istart, iCharCnt, *result)
Prototype protoCloseDocument(document)
Procedure Main()
Protected pdf_dll =OpenLibrary(#PB_Any, "pdfium32.dll")
Protected InitLibrary.protoInitLibrary = GetFunction(pdf_dll,"_FPDF_InitLibrary@0")
Protected LoadDocument.protoLoadDocument = GetFunction(pdf_dll,"_FPDF_LoadDocument@8")
Protected GetMetaText.protoGetMetaText = GetFunction(pdf_dll,"_FPDF_GetMetaText@16")
Protected LoadPage.protoLoadPage = GetFunction(pdf_dll,"_FPDF_LoadPage@8")
Protected GetText.protoGetText = GetFunction(pdf_dll,"_FPDFText_GetText@16")
Protected LoadTextPage.protoLoadTextPage = GetFunction(pdf_dll,"_FPDFText_LoadPage@4")
Protected CountChars.protoCountChars = GetFunction(pdf_dll,"_FPDFText_CountChars@4")
Protected igPdfDoc.i = 0 ;Handle of Loaded PDF File
Protected igPageNum.i = 0 ;Current page of current PDF document
Protected igTotalPages.i = 0
Protected igPdfPage.i = 0 ;Handle of Loaded PDF Page
Protected *result
Protected sTxt.s
Protected iPdfTextPage.i, iTotalChars.i, iBuffLen.i
Protected sFullpage.s
Protected sPart.s
InitLibrary()
Protected pdf_doc = LoadDocument("A_PDF.pdf", "")
Protected buflen = 1024
Protected *buffer = AllocateMemory(buflen)
Protected ret = GetMetaText(pdf_doc,"Title",*buffer, buflen)
Debug PeekS(*buffer, ret/2)
;FillMemory(*buffer, buflen)
ret = GetMetaText(pdf_doc,"Author",*buffer, buflen)
Debug PeekS(*buffer, ret/2)
;FillMemory(*buffer, buflen)
ret = GetMetaText(pdf_doc,"Creator",*buffer, buflen)
Debug PeekS(*buffer, ret/2)
igPdfPage = LoadPage(pdf_doc, 0)
iPdfTextPage = LoadTextPage(igPdfPage)
iTotalChars = CountChars(iPdfTextPage)
iBuffLen = (iTotalChars * SizeOf(Character))
*result = AllocateMemory(iBuffLen)
ret = GetText(iPdfTextPage, 0, 1024, *result)
Debug PeekS(*result, ret/2)
CloseLibrary(#PB_Any)
EndProcedure
Main()
x64 Dll has different function names:bmcs wrote:I have been looking for something like this for a while. Unfortunately, when I try either of the examples by fabulouspaul or verleihnix InitLibrary() generates an "Invalid memory access. (write error at address 0)" error.
PB 5.70 on Win 7 Ult X64
Any thoughts or pointers would be welcome.
Best Regards
Dave
Code: Select all
EnableExplicit
Prototype protoInitLibrary()
Prototype protoLoadDocument(documentpath.p-ascii,password.p-utf8)
Prototype protoGetMetaText(document, tag.p-utf8, buffer, buflen)
Prototype protoGetPageCount(document)
Prototype protoLoadPage(document, page_index)
Prototype protoLoadTextPage(textpage)
Prototype protoCloseTextPage(textpage)
Prototype protoCountChars(textpage)
Prototype protoGetText(textpage, istart, iCharCnt, *result)
Prototype protoCloseDocument(document)
Procedure Main()
Protected pdf_dll =OpenLibrary(#PB_Any, "pdfium64.dll")
Protected InitLibrary.protoInitLibrary = GetFunction(pdf_dll,"FPDF_InitLibrary")
Protected LoadDocument.protoLoadDocument = GetFunction(pdf_dll,"FPDF_LoadDocument")
Protected GetMetaText.protoGetMetaText = GetFunction(pdf_dll,"FPDF_GetMetaText")
Protected LoadPage.protoLoadPage = GetFunction(pdf_dll,"FPDF_LoadPage")
Protected GetText.protoGetText = GetFunction(pdf_dll,"FPDFText_GetText")
Protected LoadTextPage.protoLoadTextPage = GetFunction(pdf_dll,"FPDFText_LoadPage")
Protected CountChars.protoCountChars = GetFunction(pdf_dll,"FPDFText_CountChars")
Protected igPdfDoc.i = 0 ;Handle of Loaded PDF File
Protected igPageNum.i = 0 ;Current page of current PDF document
Protected igTotalPages.i = 0
Protected igPdfPage.i = 0 ;Handle of Loaded PDF Page
Protected *result
Protected sTxt.s
Protected iPdfTextPage.i, iTotalChars.i, iBuffLen.i
Protected sFullpage.s
Protected sPart.s
InitLibrary()
Protected pdf_doc = LoadDocument("test.pdf", "")
Protected buflen = 1024
Protected *buffer = AllocateMemory(buflen)
Protected ret = GetMetaText(pdf_doc,"Title",*buffer, buflen)
Debug PeekS(*buffer, ret/2)
;FillMemory(*buffer, buflen)
ret = GetMetaText(pdf_doc,"Author",*buffer, buflen)
Debug PeekS(*buffer, ret/2)
;FillMemory(*buffer, buflen)
ret = GetMetaText(pdf_doc,"Creator",*buffer, buflen)
Debug PeekS(*buffer, ret/2)
igPdfPage = LoadPage(pdf_doc, 0)
iPdfTextPage = LoadTextPage(igPdfPage)
iTotalChars = CountChars(iPdfTextPage)
iBuffLen = (iTotalChars * SizeOf(Character))
*result = AllocateMemory(iBuffLen)
ret = GetText(iPdfTextPage, 0, 1024, *result)
Debug PeekS(*result, ret/2)
CloseLibrary(#PB_Any)
EndProcedure
Main()