Insert PNG to PDF (Pdfium / C++)

764 views
Skip to first unread message

Alex Sandro Pereira

unread,
Nov 30, 2020, 1:35:50 PM11/30/20
to pdfium
On C++ examples shows how to inset jpeg image to PDF using
FPDFImageObj_LoadJpegFile(...);

Is it possible to insert png to PDF?

I tried to convert PNG to BMP, but I dont know how to create.

Could you give me a light?

Thanks 

ImperatorAvgust

unread,
Dec 3, 2020, 7:21:35 AM12/3/20
to pdfium
Hello. Pdfium for FPDFImageObj_LoadJpegFile  use FPDF_FILEACCESS struct. Which have at m_GetBlock field the pointer to read function.
So if you want read jpg image from file, you need create read function and fill  FPDF_FILEACCESS struct.
Something like that:
int ReadBlockFromFile(void* param, unsigned long position, unsigned char* pBuf, unsigned long size)
{
    FILE* fileHandle= (FILE*)param;
    fseek(imageFile,  position , SEEK_SET);
    fread( pBuf , size ,1, fileHandle )
    return size;
}


FILE* jpgFile= fopen("img.jpg", "rb");
if ( jpgFile != nullptr)
{
   fseek( jpgFile , 0, SEEK_END);
   long fileSize = ftell( jpgFile );
   fseek( jpgFile , 0, SEEK_SET);
  
 FPDF_FILEACCESS fileReadObj;
  fileReadObj .m_FileLen = fileSize;
  fileReadObj .m_Param =  jpgFile ;
  fileReadObj .m_GetBlock =  ReadBlockFromFile ;
//create image
  FPDF_PAGEOBJECT page_obj_image = FPDFPageObj_NewImageObj(pdf_doc);
  FPDFImageObj_LoadJpegFile(nullptr, 0, page_obj_image, & fileReadObj );
  FPDFPage_InsertObject(pdf_page, page_obj_image);
   fclose( jpgFile );
}

For use png you need convert it to BMP , then create by FPDFBitmap_CreateEx bitmap and pass at last parameter the bmp pixels memory.
понедельник, 30 ноября 2020 г. в 20:35:50 UTC+2, alexw...@gmail.com:
Reply all
Reply to author
Forward
0 new messages