1,pdfium库下载
链接:https://pan.baidu.com/s/1AU_jsrb1MyNN1t9XTybsrA
提取码:lvnw
解压过后文件如图所示:
; 2,创建VS2022工程
创建VS2022工程,并将pdfium文件夹放入工程文件夹下,如图所示:
新建cpp文件,将如下代码输入其中:
#include
#include
#include
#include "fpdfview.h"
#include "fpdf_formfill.h"
#include "fpdf_ext.h"
#include "fpdf_dataavail.h"
#include "fpdf_text.h"
using namespace std;
void pdf2cimage(char *file_path, const int image_Sharpness)
{
FPDF_InitLibrary();
FPDF_DOCUMENT pdf_doc = FPDF_LoadDocument(file_path, NULL);
if (pdf_doc == NULL)
{
printf("open file failed.");
return;
}
int pageCount = FPDF_GetPageCount(pdf_doc);
if (pageCount 0)
{
FPDF_CloseDocument(pdf_doc);
return;
}
for (int npage = 0; npage < pageCount; npage++)
{
FPDF_PAGE pdf_page = FPDF_LoadPage(pdf_doc, npage);
const int page_h = FPDF_GetPageHeight(pdf_page) * image_Sharpness;
const int page_w = (FPDF_GetPageWidth(pdf_page) + 1) * image_Sharpness;
FPDF_BITMAP bit;
bit = FPDFBitmap_CreateEx(page_w, page_h, FPDFBitmap_BGR, NULL, NULL);
FPDFBitmap_FillRect(bit, 0, 0, page_w, page_h, 0xffffff);
FPDF_RenderPageBitmap(bit, pdf_page, 0, 0, page_w, page_h, 0, 0);
unsigned char* buffer = (unsigned char*)FPDFBitmap_GetBuffer(bit);
const int stride = FPDFBitmap_GetStride(bit);
cv::Mat img(page_h, page_w, CV_8UC3, cv::Scalar(0, 0, 0));
for (int nRow = 0; nRow < page_h; nRow++)
{
for (int nCol = 0; nCol < stride / 3; nCol++)
{
for (int nCha = 0; nCha < 3; nCha++)
{
img.at<cv::Vec3b>(nRow, nCol)[nCha] = *(buffer + nRow * stride + nCol * 3 + nCha);
}
}
}
string save_path(file_path);
save_path.erase(save_path.end() - 4, save_path.end());
save_path = save_path + "_" + std::to_string(npage) + ".jpg";
cv::imwrite(save_path, img);
FPDFBitmap_Destroy(bit);
FPDF_ClosePage(pdf_page);
}
FPDF_CloseDocument(pdf_doc);
}
int main()
{
char file_path[] = "test2.pdf";
pdf2cimage(file_path, 10);
return 0;
}
最终如下图所示:
3,设置VS2022工程
打开工程属性页 ->c/c++ ->常规,在附加包含目录中加入:(工程存放位置)\pdfium\include\public,如下图所示:
打开工程属性页 ->链接器 ->常规,在附加库目录中加入:(工程存放位置)\pdfium\include\public,如下图所示:
; 4,运行代码
在工程目录下放入一个test.pdf文件,如下所示:
Original: https://blog.csdn.net/weixin_43852117/article/details/123734417
Author: 心驰神往鸭
Title: C++基于pdfium库实现PDF转图片,VS2022,x64环境,opencv455
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/702261/
转载文章受原作者版权保护。转载请注明原作者出处!