How to get Pdfium to work (done)

Just starting out? Need help? Post your questions and find answers here.
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: How to get Pdfium to work (done)

Post by coder14 »

Hello acreis. I found this VB.NET code at the Patagames forum that inserts bitmap into PDF:

http://forum.patagames.com/posts/m12-In ... age#post12

It uses some custom methods (or types? or structures?) like bi.Scan0 and bi.Stride and BitmapFormats.FXDIB_Argb.

Is this a better approach or is the FPDFImageObj_LoadJpegFile method simpler?
acreis
Enthusiast
Enthusiast
Posts: 204
Joined: Fri Jun 01, 2012 12:20 am

Re: How to get Pdfium to work (done)

Post by acreis »

Some changes, but the problem is the function FPDFFormContent_InsertObject

It seems it is nox exported from DLL

Code: Select all

Global sgPDF.s = "mypdf.pdf"
Global sgJPG.s = "myimage.jpg"

Prototype protoGetBlock(param, position, *buf, size)

Structure FPDF_FILEACCESS
  FileLen.i
  *GetBlock.protoGetBlock
  param.i
  pdf_file$
  pdf_handle.i
EndStructure

Procedure GetBlockCallback(*param.FPDF_FILEACCESS,  position, *buf, size)
  
  If *param\pdf_handle = 0
    If FileSize(*param\pdf_file$) > 0
      *param\pdf_handle = ReadFile(#PB_Any,*param\pdf_file$)
    EndIf
  EndIf 
  
  If *param\pdf_handle
    FileSeek(*param\pdf_handle, position)
    ReadData(*param\pdf_handle, *buf, size)
    ProcedureReturn #True
  EndIf   
  
    ProcedureReturn #False   
  
EndProcedure  

Procedure Create_FPDF_FILEACCESS(file$) 
  
  Protected *this.FPDF_FILEACCESS = AllocateStructure(FPDF_FILEACCESS)
  
  *this\FileLen = FileSize(file$)
  *this\GetBlock = @GetBlockCallback()
  *this\param = *this
  *this\pdf_file$ = file$
  
  ProcedureReturn *this
  
EndProcedure

Global *FPDF_FILEACCESS.FPDF_FILEACCESS= Create_FPDF_FILEACCESS(sgJPG)


Prototype protoInitLibrary()
Prototype protoLoadDocument(documentpath.p-utf8, password.p-utf8)
Prototype jpgObject(pages, count, imgObject, fileObject)
Prototype protoGetPageCount(document)
Prototype protoRenderPage(hDC, page, start_x, start_y, size_x, size_y, rotate, flags)
Prototype protoLoadPage(document, page_index)                                        
Prototype protoGetMediaBox(page, left, bottom, right, top)
Prototype protoInsertObject(pdfObject, newObject, insertAfter)
Prototype protoCloseDocument(document)
Prototype protoClosePage(page) 

Procedure Main(*win)
  
  CanvasGadget(0, 0, 0, 800, 600)
  
  Protected pdf_dll =OpenLibrary(#PB_Any, "pdfium.dll")
  
  Protected InitLibrary.protoInitLibrary = GetFunction(pdf_dll,"_FPDF_InitLibrary@0")
  Protected GetPageCount.protoGetPageCount = GetFunction(pdf_dll,"_FPDF_GetPageCount@4")
  Protected LoadDocument.protoLoadDocument = GetFunction(pdf_dll,"_FPDF_LoadDocument@8")
  Protected FormContent_InsertObject.protoInsertObject = GetFunction(pdf_dll,"_FPDFFormContent_InsertObject@12")
  Protected RenderPage.protoRenderPage = GetFunction(pdf_dll,"_FPDF_RenderPage@32")
  Protected LoadPage.protoLoadPage = GetFunction(pdf_dll,"_FPDF_LoadPage@8")
  Protected GetMediaBox.protoGetMediaBox = GetFunction(pdf_dll,"_FPDFPage_GetMediaBox@20")
  Protected CloseDocument.protoCloseDocument = GetFunction(pdf_dll,"_FPDF_CloseDocument@4")
  Protected ClosePage.protoClosePage = GetFunction(pdf_dll,"_FPDF_ClosePage@4")
  
  Protected ImageObj_LoadJpegFile.jpgObject = GetFunction(pdf_dll,"_FPDFImageObj_LoadJpegFile@16")
  
  InitLibrary()
  Protected pdf_doc = LoadDocument(sgPDF, "")
  Protected pdf_page = LoadPage(pdf_doc, 0)
  Protected hJPG = ImageObj_LoadJpegFile(0, 0, *newObject, *FPDF_FILEACCESS)
  FormContent_InsertObject(pdf_doc, hJPG, 0)
  
  hDC = StartDrawing(CanvasOutput(0))
    RenderPage(hDC, pdf_page, 0, 0, 800, 600, 0, $100);
  StopDrawing()  

  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
  
  ClosePage(pdf_page)
  CloseDocument(pdf_doc)   
  CloseLibrary(pdf_dll)
  
EndProcedure

Define iFlags.i = #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered | #PB_Window_SizeGadget
Main(OpenWindow(#PB_Any, 0, 0, 800, 600, "Pdfium", iFlags))
acreis
Enthusiast
Enthusiast
Posts: 204
Joined: Fri Jun 01, 2012 12:20 am

Re: How to get Pdfium to work (done)

Post by acreis »

I think we are almost there

Code: Select all

Global sgPDF.s = "mypdf.pdf"
Global sgJPG.s = "myimage.jpg"

PrototypeC  protoGetBlock(param, position, *buf, size)

Structure FPDF_FILEACCESS
  FileLen.i
  *GetBlock.protoGetBlock
  param.i
  file$
  file_handle.i
EndStructure

ProcedureC GetBlockCallback(*param.FPDF_FILEACCESS,  position, *buf, size)
  
  If *param\file_handle = 0
    If FileSize(*param\file$) > 0
      *param\file_handle = ReadFile(#PB_Any,*param\file$)
    EndIf
  EndIf 
  
  If *param\file_handle
    FileSeek(*param\file_handle, position)
    ReadData(*param\file_handle, *buf, size)
    ProcedureReturn #True
  EndIf   
  
    ProcedureReturn #False   
  
EndProcedure  

Procedure Open_FileAccess(file$) 
  
  Protected *this.FPDF_FILEACCESS = AllocateStructure(FPDF_FILEACCESS)
  
  *this\FileLen = FileSize(file$)
  *this\GetBlock = @GetBlockCallback()
  *this\param = *this
  *this\file$ = file$
  
  ProcedureReturn *this
  
EndProcedure


Procedure Close_FileAccess(*this.FPDF_FILEACCESS) 
  
  If *this And *this\file_handle
    
    CloseFile(*this\file_handle)
    *this\file_handle = 0
    
  EndIf
  
EndProcedure


Prototype protoInitLibrary()
Prototype protoLoadDocument(documentpath.p-utf8, password.p-utf8)
Prototype protoImageObj_LoadJpegFile(pages, count, imgObject, fileObject)
Prototype protoGetPageCount(document)
Prototype protoRenderPage(hDC, page, start_x, start_y, size_x, size_y, rotate, flags)
Prototype protoLoadPage(document, page_index)                                        
Prototype protoGetMediaBox(page, left, bottom, right, top)
Prototype protoInsertObject(pdfObject, newObject, insertAfter)
Prototype protoCloseDocument(document)
Prototype protoClosePage(page) 
Prototype protoPage_InsertObject(page, page_obj) 
Prototype protoPageObj_NewImgeObj(document)
Prototype protoImageObj_SetMatrix(page_obj, a.d, b.d, c.d, d.d, e.d, f.d)
Prototype protoPageObj_Transform(page_obj, a.d, b.d, c.d, d.d, e.d, f.d) 
Prototype protoPage_GenerateContent(page) 


Procedure Main(*win)
  
  CanvasGadget(0, 0, 0, 800, 600)
  
  Protected pdf_dll =OpenLibrary(#PB_Any, "pdfium.dll")
  
  Protected InitLibrary.protoInitLibrary = GetFunction(pdf_dll,"_FPDF_InitLibrary@0")
  Protected GetPageCount.protoGetPageCount = GetFunction(pdf_dll,"_FPDF_GetPageCount@4")
  Protected LoadDocument.protoLoadDocument = GetFunction(pdf_dll,"_FPDF_LoadDocument@8")
  Protected Page_InsertObject.protoPage_InsertObject = GetFunction(pdf_dll,"_FPDFPage_InsertObject@8")
  Protected RenderPage.protoRenderPage = GetFunction(pdf_dll,"_FPDF_RenderPage@32")
  Protected LoadPage.protoLoadPage = GetFunction(pdf_dll,"_FPDF_LoadPage@8")
  Protected GetMediaBox.protoGetMediaBox = GetFunction(pdf_dll,"_FPDFPage_GetMediaBox@20")
  Protected CloseDocument.protoCloseDocument = GetFunction(pdf_dll,"_FPDF_CloseDocument@4")
  Protected ClosePage.protoClosePage = GetFunction(pdf_dll,"_FPDF_ClosePage@4")
  
  Protected ImageObj_LoadJpegFile.protoImageObj_LoadJpegFile = GetFunction(pdf_dll,"_FPDFImageObj_LoadJpegFile@16")
  Protected PageObj_NewImgeObj.protoPageObj_NewImgeObj = GetFunction(pdf_dll,"_FPDFPageObj_NewImgeObj@4")
  
  Protected ImageObj_SetMatrix.protoImageObj_SetMatrix = GetFunction(pdf_dll,"_FPDFImageObj_SetMatrix@52")
  
  Protected  PageObj_Transform.protoPageObj_Transform = GetFunction(pdf_dll,"_FPDFPageObj_Transform@52")
  
  Protected Page_GenerateContent.protoPage_GenerateContent = GetFunction(pdf_dll,"_FPDFPage_GenerateContent@4")
  
  InitLibrary()
  

  Protected  *jpg_read.FPDF_FILEACCESS = Open_FileAccess(sgJPG)
  
  Protected pdf_doc = LoadDocument(sgPDF, "")
 
  Protected img_obj = PageObj_NewImgeObj(pdf_doc)
  
  Protected pdf_page = LoadPage(pdf_doc, 0)
  
  Debug ImageObj_LoadJpegFile(@pdf_page, 1, img_obj, *jpg_read)
  
  Debug PageObj_Transform(img_obj, 500,0,0,500,0,0)

  Debug Page_InsertObject(pdf_page, img_obj)
  
  Debug Page_GenerateContent(pdf_page) 
  
  Protected hDC = StartDrawing(CanvasOutput(0))
    RenderPage(hDC, pdf_page, 0, 0, 800, 600, 0, $100);
  StopDrawing()  

  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
  
  ClosePage(pdf_page)
  CloseDocument(pdf_doc)   
  CloseLibrary(pdf_dll)
  
EndProcedure

Define iFlags.i = #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered | #PB_Window_SizeGadget
Main(OpenWindow(#PB_Any, 0, 0, 800, 600, "Pdfium", iFlags))
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: How to get Pdfium to work (done)

Post by coder14 »

Wow! :D I did not find all these functions!

I tested the code and there are no errors. But do you see the inserted JPEG? I tried changing to the next page:

Code: Select all

pdf_page = LoadPage(pdf_doc, 1)
but still can't display it.

Did it work for you? :shock:
acreis
Enthusiast
Enthusiast
Posts: 204
Joined: Fri Jun 01, 2012 12:20 am

Re: How to get Pdfium to work (done)

Post by acreis »

Yes, I can display the pages with the inserted image.

But I got random errors when closing the pages.
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: How to get Pdfium to work (done)

Post by coder14 »

acreis wrote:Yes, I can display the pages with the inserted image.

But I got random errors when closing the pages.
WHAT!?!? :shock:

But I cannot see it! :cry: I tried with many PDF files with single page and multi page and with many different big and small JPG images. I am doing something wrong!? :x

I will try again! Thank you acreis.
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: How to get Pdfium to work (done)

Post by coder14 »

@acreis: Besides the files names is there anything else that is different in your code that works?

I am using different versions of PB x86 and I have tested on Windows x64 (7-10). I'm going to try on Windows XP x86 tomorrow but like to know maybe something different from the posted code. Plus I have tried with both versions of pdfium.dll - the light 3MB version and the V8 10MB version. So far they all fail to show the inserted JPG image. :shock:

Can you also tell me what type of PDF and JPG file you used for testing? Thank you.
acreis
Enthusiast
Enthusiast
Posts: 204
Joined: Fri Jun 01, 2012 12:20 am

Re: How to get Pdfium to work (done)

Post by acreis »

I'm using PB x86 5.44 LTS Windows 10 64bits.


The code is:

Code: Select all

Global sgPDF.s = "PureBasicSmall.pdf" ;https://www.purebasic.fr/documentation/PureBasicSmall.pdf
Global sgJPG.s = "imagem-005.jpg" ;http://kingofwallpapers.com/imagem/imagem-005.jpg

PrototypeC  protoGetBlock(param, position, *buf, size)

Structure FPDF_FILEACCESS
  FileLen.i
  *GetBlock.protoGetBlock
  param.i
  file$
  file_handle.i
EndStructure

ProcedureC GetBlockCallback(*param.FPDF_FILEACCESS,  position, *buf, size)
  
  If *param\file_handle = 0
    If FileSize(*param\file$) > 0
      *param\file_handle = ReadFile(#PB_Any,*param\file$)
    EndIf
  EndIf 
  
  If *param\file_handle
    FileSeek(*param\file_handle, position)
    ReadData(*param\file_handle, *buf, size)
    ProcedureReturn #True
  EndIf   
  
    ProcedureReturn #False   
  
EndProcedure  

Procedure Open_FileAccess(file$) 
  
  Protected *this.FPDF_FILEACCESS = AllocateStructure(FPDF_FILEACCESS)
  
  *this\FileLen = FileSize(file$)
  *this\GetBlock = @GetBlockCallback()
  *this\param = *this
  *this\file$ = file$
  
  ProcedureReturn *this
  
EndProcedure


Procedure Close_FileAccess(*this.FPDF_FILEACCESS) 
  
  If *this And *this\file_handle
    
    CloseFile(*this\file_handle)
    *this\file_handle = 0
    
  EndIf
  
EndProcedure


Prototype protoInitLibrary()
Prototype protoLoadDocument(documentpath.p-utf8, password.p-utf8)
Prototype protoImageObj_LoadJpegFile(pages, count, imgObject, fileObject)
Prototype protoGetPageCount(document)
Prototype protoRenderPage(hDC, page, start_x, start_y, size_x, size_y, rotate, flags)
Prototype protoLoadPage(document, page_index)                                        
Prototype protoGetMediaBox(page, left, bottom, right, top)
Prototype protoInsertObject(pdfObject, newObject, insertAfter)
Prototype protoCloseDocument(document)
Prototype protoClosePage(page) 
Prototype protoPage_InsertObject(page, page_obj) 
Prototype protoPageObj_NewImgeObj(document)
Prototype protoImageObj_SetMatrix(page_obj, a.d, b.d, c.d, d.d, e.d, f.d)
Prototype protoPageObj_Transform(page_obj, a.d, b.d, c.d, d.d, e.d, f.d) 
Prototype protoPage_GenerateContent(page) 


Procedure Main(*win)
  
  CanvasGadget(0, 0, 0, 800, 600)
  
  Protected pdf_dll =OpenLibrary(#PB_Any, "pdfium.dll")
  
  Protected InitLibrary.protoInitLibrary = GetFunction(pdf_dll,"_FPDF_InitLibrary@0")
  Protected GetPageCount.protoGetPageCount = GetFunction(pdf_dll,"_FPDF_GetPageCount@4")
  Protected LoadDocument.protoLoadDocument = GetFunction(pdf_dll,"_FPDF_LoadDocument@8")
  Protected Page_InsertObject.protoPage_InsertObject = GetFunction(pdf_dll,"_FPDFPage_InsertObject@8")
  Protected RenderPage.protoRenderPage = GetFunction(pdf_dll,"_FPDF_RenderPage@32")
  Protected LoadPage.protoLoadPage = GetFunction(pdf_dll,"_FPDF_LoadPage@8")
  Protected GetMediaBox.protoGetMediaBox = GetFunction(pdf_dll,"_FPDFPage_GetMediaBox@20")
  Protected CloseDocument.protoCloseDocument = GetFunction(pdf_dll,"_FPDF_CloseDocument@4")
  Protected ClosePage.protoClosePage = GetFunction(pdf_dll,"_FPDF_ClosePage@4")
  
  Protected ImageObj_LoadJpegFile.protoImageObj_LoadJpegFile = GetFunction(pdf_dll,"_FPDFImageObj_LoadJpegFile@16")
  Protected PageObj_NewImgeObj.protoPageObj_NewImgeObj = GetFunction(pdf_dll,"_FPDFPageObj_NewImgeObj@4")
  
  Protected ImageObj_SetMatrix.protoImageObj_SetMatrix = GetFunction(pdf_dll,"_FPDFImageObj_SetMatrix@52")
  
  Protected  PageObj_Transform.protoPageObj_Transform = GetFunction(pdf_dll,"_FPDFPageObj_Transform@52")
  
  Protected Page_GenerateContent.protoPage_GenerateContent = GetFunction(pdf_dll,"_FPDFPage_GenerateContent@4")
  
  InitLibrary()
  
  Protected Dim pdf_pages(3)
  
  Protected  *jpg_read.FPDF_FILEACCESS = Open_FileAccess(sgJPG)
  
  Protected pdf_doc = LoadDocument(sgPDF, "")
 
  
  
  Protected i
  
  
  
  For i = 0 To 2
    pdf_pages(i) = LoadPage(pdf_doc, i)
    Protected img_obj = PageObj_NewImgeObj(pdf_doc)
    ImageObj_LoadJpegFile(@pdf_pages(0), i+1, img_obj, *jpg_read)
     PageObj_Transform(img_obj, 500,0,0,500,0,0)
     Page_InsertObject(pdf_pages(i), img_obj)
     Page_GenerateContent(pdf_pages(i))
  Next  
 
  
  Protected hDC = StartDrawing(CanvasOutput(0))
    RenderPage(hDC, pdf_pages(2), 0, 0, 800, 600, 0, $100);
  StopDrawing()  

  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
  
  For i= 0 To 2
    ClosePage(pdf_pages(i))
  Next
  
  CloseDocument(pdf_doc)   
  CloseLibrary(pdf_dll)
  
EndProcedure

Define iFlags.i = #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered | #PB_Window_SizeGadget
Main(OpenWindow(#PB_Any, 0, 0, 800, 600, "Pdfium", iFlags))
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: How to get Pdfium to work (done)

Post by coder14 »

acreis wrote:I'm using PB x86 5.44 LTS Windows 10 64bits.


The code is:

Code: Select all

Global sgPDF.s = "PureBasicSmall.pdf" ;https://www.purebasic.fr/documentation/PureBasicSmall.pdf
Global sgJPG.s = "imagem-005.jpg" ;http://kingofwallpapers.com/imagem/imagem-005.jpg

PrototypeC  protoGetBlock(param, position, *buf, size)

Structure FPDF_FILEACCESS
  FileLen.i
  *GetBlock.protoGetBlock
  param.i
  file$
  file_handle.i
EndStructure

ProcedureC GetBlockCallback(*param.FPDF_FILEACCESS,  position, *buf, size)
  
  If *param\file_handle = 0
    If FileSize(*param\file$) > 0
      *param\file_handle = ReadFile(#PB_Any,*param\file$)
    EndIf
  EndIf 
  
  If *param\file_handle
    FileSeek(*param\file_handle, position)
    ReadData(*param\file_handle, *buf, size)
    ProcedureReturn #True
  EndIf   
  
    ProcedureReturn #False   
  
EndProcedure  

Procedure Open_FileAccess(file$) 
  
  Protected *this.FPDF_FILEACCESS = AllocateStructure(FPDF_FILEACCESS)
  
  *this\FileLen = FileSize(file$)
  *this\GetBlock = @GetBlockCallback()
  *this\param = *this
  *this\file$ = file$
  
  ProcedureReturn *this
  
EndProcedure


Procedure Close_FileAccess(*this.FPDF_FILEACCESS) 
  
  If *this And *this\file_handle
    
    CloseFile(*this\file_handle)
    *this\file_handle = 0
    
  EndIf
  
EndProcedure


Prototype protoInitLibrary()
Prototype protoLoadDocument(documentpath.p-utf8, password.p-utf8)
Prototype protoImageObj_LoadJpegFile(pages, count, imgObject, fileObject)
Prototype protoGetPageCount(document)
Prototype protoRenderPage(hDC, page, start_x, start_y, size_x, size_y, rotate, flags)
Prototype protoLoadPage(document, page_index)                                        
Prototype protoGetMediaBox(page, left, bottom, right, top)
Prototype protoInsertObject(pdfObject, newObject, insertAfter)
Prototype protoCloseDocument(document)
Prototype protoClosePage(page) 
Prototype protoPage_InsertObject(page, page_obj) 
Prototype protoPageObj_NewImgeObj(document)
Prototype protoImageObj_SetMatrix(page_obj, a.d, b.d, c.d, d.d, e.d, f.d)
Prototype protoPageObj_Transform(page_obj, a.d, b.d, c.d, d.d, e.d, f.d) 
Prototype protoPage_GenerateContent(page) 


Procedure Main(*win)
  
  CanvasGadget(0, 0, 0, 800, 600)
  
  Protected pdf_dll =OpenLibrary(#PB_Any, "pdfium.dll")
  
  Protected InitLibrary.protoInitLibrary = GetFunction(pdf_dll,"_FPDF_InitLibrary@0")
  Protected GetPageCount.protoGetPageCount = GetFunction(pdf_dll,"_FPDF_GetPageCount@4")
  Protected LoadDocument.protoLoadDocument = GetFunction(pdf_dll,"_FPDF_LoadDocument@8")
  Protected Page_InsertObject.protoPage_InsertObject = GetFunction(pdf_dll,"_FPDFPage_InsertObject@8")
  Protected RenderPage.protoRenderPage = GetFunction(pdf_dll,"_FPDF_RenderPage@32")
  Protected LoadPage.protoLoadPage = GetFunction(pdf_dll,"_FPDF_LoadPage@8")
  Protected GetMediaBox.protoGetMediaBox = GetFunction(pdf_dll,"_FPDFPage_GetMediaBox@20")
  Protected CloseDocument.protoCloseDocument = GetFunction(pdf_dll,"_FPDF_CloseDocument@4")
  Protected ClosePage.protoClosePage = GetFunction(pdf_dll,"_FPDF_ClosePage@4")
  
  Protected ImageObj_LoadJpegFile.protoImageObj_LoadJpegFile = GetFunction(pdf_dll,"_FPDFImageObj_LoadJpegFile@16")
  Protected PageObj_NewImgeObj.protoPageObj_NewImgeObj = GetFunction(pdf_dll,"_FPDFPageObj_NewImgeObj@4")
  
  Protected ImageObj_SetMatrix.protoImageObj_SetMatrix = GetFunction(pdf_dll,"_FPDFImageObj_SetMatrix@52")
  
  Protected  PageObj_Transform.protoPageObj_Transform = GetFunction(pdf_dll,"_FPDFPageObj_Transform@52")
  
  Protected Page_GenerateContent.protoPage_GenerateContent = GetFunction(pdf_dll,"_FPDFPage_GenerateContent@4")
  
  InitLibrary()
  
  Protected Dim pdf_pages(3)
  
  Protected  *jpg_read.FPDF_FILEACCESS = Open_FileAccess(sgJPG)
  
  Protected pdf_doc = LoadDocument(sgPDF, "")
 
  
  
  Protected i
  
  
  
  For i = 0 To 2
    pdf_pages(i) = LoadPage(pdf_doc, i)
    Protected img_obj = PageObj_NewImgeObj(pdf_doc)
    ImageObj_LoadJpegFile(@pdf_pages(0), i+1, img_obj, *jpg_read)
     PageObj_Transform(img_obj, 500,0,0,500,0,0)
     Page_InsertObject(pdf_pages(i), img_obj)
     Page_GenerateContent(pdf_pages(i))
  Next  
 
  
  Protected hDC = StartDrawing(CanvasOutput(0))
    RenderPage(hDC, pdf_pages(2), 0, 0, 800, 600, 0, $100);
  StopDrawing()  

  Repeat
  Until WaitWindowEvent() = #PB_Event_CloseWindow
  
  For i= 0 To 2
    ClosePage(pdf_pages(i))
  Next
  
  CloseDocument(pdf_doc)   
  CloseLibrary(pdf_dll)
  
EndProcedure

Define iFlags.i = #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered | #PB_Window_SizeGadget
Main(OpenWindow(#PB_Any, 0, 0, 800, 600, "Pdfium", iFlags))
I copied your code exactly with no changes and downloaded the two sample images (saved to current directory with the source) and ran it on PB 5.40 LTS x86 & 5.50 x86 on Windows 8 x64 & Windows 10 x64 - I only see Pg2 of the PB manual - no fish! :?

I also tried both the pdfium.dll files - one from the Libraries folder and one from the "Without V8" folder. Then I tried looking at the other two pages pdf_pages(0) and pdf_pages(1) - also no fish!

Maybe our pdfium libraries are different? I downloaded it from GitHub from PdfiumViewer-master.

I will still tinker - blindly - and hope for something. Just very confused how it works for you.
acreis
Enthusiast
Enthusiast
Posts: 204
Joined: Fri Jun 01, 2012 12:20 am

Re: How to get Pdfium to work (done)

Post by acreis »

You are right.

I downloaded the most recent pdfium.dll and no fish

Try with older version:

https://github.com/pvginkel/PdfiumViewe ... pdfium.dll

It worked for me
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: How to get Pdfium to work (done)

Post by coder14 »

acreis wrote:You are right.

I downloaded the most recent pdfium.dll and no fish

Try with older version:

https://github.com/pvginkel/PdfiumViewe ... pdfium.dll

It worked for me
IT WORKS!!! :D :D :D Thank you very much acreis. :D :D :D

You are right. It works with the older build but not the newer one - but the important thing is that it CAN work.

I wonder what changed? :?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: How to get Pdfium to work (done)

Post by IdeasVacuum »

There is an important part of Pdfium that I can't get to work: Text Extraction. I'm performing tests on pdfs that are text only, and know that Pdfium can read them as rendering (GetMediaBox->CreateImage->RenderPage->SlapOntoCanvas) works for every page.
Snippet:

Code: Select all

#PdfLib = 0

Global     igPdfDoc.i = 0 ;Handle of Loaded PDF File
Global    igPageNum.i = 0 ;Current page of current PDF document
Global igTotalPages.i = 100
Global    igPdfPage.i = 0

Prototype protoInitLibrary()
Prototype protoLoadDocument(documentpath.p-utf8, password.p-utf8)
Prototype protoGetPageCount(document)
Prototype protoLoadPage(document, page_index)
Prototype protoCountChars(page)
Prototype protoGetText(page, istart, iCharCnt, *result)
Prototype protoGetMetaText(document, tag.p-utf8, buffer, buflen)
Prototype protoCloseDocument(document)

OpenLibrary(#PdfLib, "pdfium.dll")

Global       InitLibrary.protoInitLibrary = GetFunction(#PdfLib,"_FPDF_InitLibrary@0")
Global     LoadDocument.protoLoadDocument = GetFunction(#PdfLib,"_FPDF_LoadDocument@8")
Global     GetPageCount.protoGetPageCount = GetFunction(#PdfLib,"_FPDF_GetPageCount@4")
Global             LoadPage.protoLoadPage = GetFunction(#PdfLib,"_FPDF_LoadPage@8")
Global       GetMetaText.protoGetMetaText = GetFunction(#PdfLib,"_FPDF_GetMetaText@16")
Global         CountChars.protoCountChars = GetFunction(#PdfLib,"_FPDFText_CountChars@4")
Global               GetText.protoGetText = GetFunction(#PdfLib,"_FPDFText_GetText@16")
Global   CloseDocument.protoCloseDocument = GetFunction(#PdfLib,"_FPDF_CloseDocument@4")

InitLibrary()

Procedure PfPdfPage(iPageNum.i)
;#-----------------------------
Protected *result
Protected iTotalChars.i, iBuffLen.i

              If((iPageNum > -1) And Not (iPageNum > igTotalPages))

                        igPdfPage = LoadPage(igPdfDoc, iPageNum)

                      iTotalChars = CountChars(igPdfPage)
                         iBuffLen = (iTotalChars * SizeOf(Character))
                          *result = AllocateMemory(iBuffLen)

                      GetText(igPdfPage, 1, iTotalChars, *result)

                      Debug PeekS(*result, iBuffLen, #PB_Unicode)
              EndIf
EndProcedure


Procedure PfGetPdfIn()
;#--------------------
igPageNum = 0 ;=First Page
Protected iMetaTxt.i
Protected iBuffLen.i = 1024
Protected *Buffer = AllocateMemory(iBuffLen)
Protected sPdfFileIn.s

                          sPdfFileIn = OpenFileRequester("Pdf: Select PDF File", "C:\", "PDF *.pdf| *.PDF; *.pdf", 0)
              If(FileSize(sPdfFileIn) > 0)

                            igPdfDoc = LoadDocument(sPdfFileIn, "")
                        igTotalPages = GetPageCount(igPdfDoc)
                     If(igTotalPages > 100) : igTotalPages = 100 : EndIf ;Max 100 pages

                     iMetaTxt = GetMetaText(igPdfDoc, "Title", *Buffer, iBuffLen)
                                Debug PeekS(*Buffer, iMetaTxt)

                     iMetaTxt = GetMetaText(igPdfDoc, "Author", *Buffer, iBuffLen)
                                Debug PeekS(*Buffer, iMetaTxt)

                     iMetaTxt = GetMetaText(igPdfDoc, "Creator", *Buffer, iBuffLen)
                                Debug PeekS(*Buffer, iMetaTxt)

                                 FreeMemory(*Buffer)

                     PfPdfPage(igPageNum)
              EndIf
EndProcedure

Procedure PfWaitForUser()
;#-----------------------
Protected iExit.i = #False

              Repeat

                     Select WaitWindowEvent(1)

                              Case #PB_Event_CloseWindow : iExit = #True

                     EndSelect

              Until iExit = #True

              If(igPdfDoc) : CloseDocument(igPdfDoc) : EndIf
              CloseLibrary(#PdfLib)
EndProcedure

PfGetPdfIn()
PfWaitForUser()
End
I get 'Invalid memory access' at CountChars()
Handy Reference:
https://developers.foxitsoftware.com/re ... i_u_m.html
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
pstryk
New User
New User
Posts: 5
Joined: Tue Dec 03, 2013 7:50 pm
Contact:

Re: How to get Pdfium to work (done)

Post by pstryk »

@IdeasVacuum,
You have forgotten to use "FPDFText_LoadPage@4" function before "CountChars".
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: How to get Pdfium to work (done)

Post by IdeasVacuum »

Hi pstryk

The line before "CountChars":
igPdfPage = LoadPage(igPdfDoc, iPageNum)
Edit: Hang on, you are right, it's a different function! Will investigate :)

Having investigated text extraction without Pdfium, I'm wondering if the Pdfium GetText() function makes similar output to the online pdf text extractors - i.e. no formatting. Hope not.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: How to get Pdfium to work (done)

Post by IdeasVacuum »

Hi pstryk

That works! The output text is not formatted, but all of the lines/partial lines are in the correct order (only tested a few files)

Code: Select all

;PdfExtractTextPdfium.pb PB5.60
;
;Copyright 2014 PDFium Authors. All rights reserved.
;
;Redistribution and use in source and binary forms, with or without
;modification, are permitted provided that the following conditions are
;met:
;
;   * Redistributions of source code must retain the above copyright
;notice, this list of conditions and the following disclaimer.
;   * Redistributions in binary form must reproduce the above
;copyright notice, this list of conditions and the following disclaimer
;in the documentation and/or other materials provided with the
;distribution.
;   * Neither the name of Google Inc. nor the names of its
;contributors may be used to endorse or promote products derived from
;this software without specific prior written permission.
;
;THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
;"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
;LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
;A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
;OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
;SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
;LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
;DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
;THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
;(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
;OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


EnableExplicit

#PdfLib = 0

Global     igPdfDoc.i = 0 ;Handle of Loaded PDF File
Global    igPageNum.i = 0 ;Current page of current PDF document
Global igTotalPages.i = 0
Global    igPdfPage.i = 0 ;Handle of Loaded PDF Page

Prototype protoInitLibrary()
Prototype protoLoadDocument(documentpath.p-utf8, password.p-utf8)
Prototype protoGetPageCount(document)
Prototype protoLoadPage(document, page_index)
Prototype protoLoadTextPage(textpage)
Prototype protoCloseTextPage(textpage)
Prototype protoCountChars(textpage)
Prototype protoGetText(textpage, istart, iCharCnt, *result)
Prototype protoGetMetaText(document, tag.p-utf8, buffer, buflen)
Prototype protoCloseDocument(document)

OpenLibrary(#PdfLib, "pdfium.dll")

Global       InitLibrary.protoInitLibrary = GetFunction(#PdfLib,"_FPDF_InitLibrary@0")
Global     LoadDocument.protoLoadDocument = GetFunction(#PdfLib,"_FPDF_LoadDocument@8")
Global     GetPageCount.protoGetPageCount = GetFunction(#PdfLib,"_FPDF_GetPageCount@4")
Global             LoadPage.protoLoadPage = GetFunction(#PdfLib,"_FPDF_LoadPage@8")
Global     LoadTextPage.protoLoadTextPage = GetFunction(#PdfLib,"_FPDFText_LoadPage@4")
Global   CloseTextPage.protoCloseTextPage = GetFunction(#PdfLib,"_FPDFText_ClosePage@4")
Global         CountChars.protoCountChars = GetFunction(#PdfLib,"_FPDFText_CountChars@4")
Global       GetMetaText.protoGetMetaText = GetFunction(#PdfLib,"_FPDF_GetMetaText@16")
Global               GetText.protoGetText = GetFunction(#PdfLib,"_FPDFText_GetText@16")
Global   CloseDocument.protoCloseDocument = GetFunction(#PdfLib,"_FPDF_CloseDocument@4")

InitLibrary()

Procedure PfPdfPage(iPageNum.i)
;#-----------------------------
Protected *result
Protected sTxt.s
Protected iPdfTextPage.i, iTotalChars.i, iBuffLen.i

              If((iPageNum > -1) And Not (iPageNum > igTotalPages))

                        igPdfPage = LoadPage(igPdfDoc, iPageNum)
                     iPdfTextPage = LoadTextPage(igPdfPage)
                      iTotalChars = CountChars(iPdfTextPage)
                         iBuffLen = (iTotalChars * SizeOf(Character))
                          *result = AllocateMemory(iBuffLen)

                          GetText(iPdfTextPage, 0, iTotalChars, *result)
                    CloseTextPage(iPdfTextPage)

                      Debug PeekS(*result, iBuffLen, #PB_Unicode)
              EndIf
EndProcedure


Procedure PfGetPdfIn()
;#--------------------
igPageNum = 0 ;=First Page
Protected iMetaTxt.i
Protected iBuffLen.i = 1024
Protected *Buffer = AllocateMemory(iBuffLen)
Protected sPdfFileIn.s

              ;PfMsgWin("Note, some PDF's do not contain text, the 'text' you see is an image")

                          sPdfFileIn = OpenFileRequester("Pdf: Select PDF File", "C:\", "PDF *.pdf| *.PDF; *.pdf", 0)
              If(FileSize(sPdfFileIn) > 0)

                            igPdfDoc = LoadDocument(sPdfFileIn, "")
                        igTotalPages = GetPageCount(igPdfDoc)

                     If(igTotalPages > 100) : igTotalPages = 100 : EndIf ;Max 100 pages

                     iMetaTxt = GetMetaText(igPdfDoc, "Title", *Buffer, iBuffLen)
                                Debug PeekS(*Buffer, iMetaTxt)

                     iMetaTxt = GetMetaText(igPdfDoc, "Author", *Buffer, iBuffLen)
                                Debug PeekS(*Buffer, iMetaTxt)

                     iMetaTxt = GetMetaText(igPdfDoc, "Creator", *Buffer, iBuffLen)
                                Debug PeekS(*Buffer, iMetaTxt)

                                 FreeMemory(*Buffer)

                     PfPdfPage(igPageNum)
              EndIf
EndProcedure


PfGetPdfIn()
If(igPdfDoc) : CloseDocument(igPdfDoc) : EndIf
CloseLibrary(#PdfLib)
End
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply