需要可以将原图提取出来,原来是什么格式提出来就是啥格式,而不是随便提出来,并且是C++ windows msvc的库,感谢大家
在 C++ Windows MSVC 库中,有几个第三方库可以用来提取 PDF 中的图片,其中一些常用的库包括:
这些库都可以在 Windows 上使用,并且都可以在 MSVC 中进行编译。 使用这些库来提取 PDF 图片的话,可以保证提取出来的图片格式与原来一致
1.Poppler
2.MuPDF
3.PDFium
4.libHaru
5.libpdf
6.libgnome-print
7.libunicon
8.Podofo
9.PDFlib
望采纳
望采纳!!!!
我建议你可以使用 Poppler 库来提取 PDF 中的图像。 Poppler 是一个开源的 PDF 解析器,可以用于提取 PDF 文件中的文本、图像和其他信息。
要在 C++ 中使用 Poppler,您需要先在计算机上安装 Poppler 库。您可以前往 https://poppler.freedesktop.org/ 下载 Poppler 库的源代码,然后使用 CMake 工具编译和安装。
编译完成后,您就可以在 C++ 程序中使用 Poppler 库提取 PDF 中的图像了。我给你写个例子,望采纳!!!!
#include <poppler/cpp/poppler-document.h>
#include <poppler/cpp/poppler-page.h>
#include <poppler/cpp/poppler-image.h>
int main()
{
// 打开 PDF 文件
poppler::document* doc = poppler::document::load_from_file("file.pdf");
// 获取第一页
poppler::page* page = doc->create_page(0);
// 提取第一页中的图像
poppler::image* image = page->render_to_image();
// 保存图像到文件
image->save("image.png", "png");
// 释放资源
delete image;
delete page;
delete doc;
return 0;
}
// TestPdf2Img.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "fpdfdoc.h"
#include "fpdftext.h"
#include "fpdfedit.h"
#include <string>
using namespace std;
#include <stdio.h>
void WriteBmp(const char* pdf_name, int num, const void* buffer,
int stride, int width, int height) {
if (stride < 0 || width < 0 || height < 0)
return;
if (height > 0 && width > INT_MAX / height)
return;
int out_len = stride * height;
if (out_len > INT_MAX / 3)
return;
char filename[256];
sprintf(filename, "%s.%d.bmp", pdf_name, num);
FILE* fp = fopen(filename, "wb");
if (!fp)
return;
BITMAPINFO bmi = { 0 };
bmi.bmiHeader.biSize = sizeof(bmi) - sizeof(RGBQUAD);
bmi.bmiHeader.biWidth = width;
bmi.bmiHeader.biHeight = -height; // top-down image
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
bmi.bmiHeader.biSizeImage = 0;
BITMAPFILEHEADER file_header = { 0 };
file_header.bfType = 0x4d42;
file_header.bfSize = sizeof(file_header) + bmi.bmiHeader.biSize + out_len;
file_header.bfOffBits = file_header.bfSize - out_len;
fwrite(&file_header, sizeof(file_header), 1, fp);
fwrite(&bmi, bmi.bmiHeader.biSize, 1, fp);
fwrite(buffer, out_len, 1, fp);
fclose(fp);
}
void pdf2cimage(char *file_path, int image_Sharpness = 1)
{
FPDF_InitLibrary(NULL);
//读取pdf文件
FPDF_DOCUMENT pdf_doc = FPDF_LoadDocument(file_path, NULL);
if (pdf_doc == NULL)
{
printf("open file failed.");
return;
}
//读取pdf页数
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);
FPDF_TEXTPAGE text_page = FPDFText_LoadPage(pdf_page);
int page_w = static_cast<int>(FPDF_GetPageWidth(pdf_page));
int page_h = static_cast<int>(FPDF_GetPageHeight(pdf_page));
FPDF_BITMAP bit;
bit = FPDFBitmap_Create(page_w, page_h, 0);;
FPDFBitmap_FillRect(bit, 0, 0, page_w, page_h, 0xFFFFFFFF);
FPDF_RenderPageBitmap(bit, pdf_page, 0, 0, page_w, page_h, 0, 0);
const char* buffer = reinterpret_cast<const char*>(FPDFBitmap_GetBuffer(bit));
const int stride = FPDFBitmap_GetStride(bit);
WriteBmp("xx", npage, buffer,stride, page_w, page_h);
FPDFBitmap_Destroy(bit);
FPDFText_ClosePage(text_page);
FPDF_ClosePage(pdf_page);
}
//关闭文件
FPDF_CloseDocument(pdf_doc);
}
int _tmain(int argc, _TCHAR* argv[])
{
pdf2cimage("1.pdf");
return 0;
}
mupdf
教程:
https://blog.csdn.net/chunleixiahe/article/details/123951280
mupdf
例子: https://blog.csdn.net/chunleixiahe/article/details/123951280
c++ mupdf 提取pdf文件里面图片
借鉴下
https://blog.csdn.net/chunleixiahe/article/details/123951280
有两个库可以提取PDF图片【fitz、pdfminer】
参考代码:
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import fitz # pip3 install pymupdf
import time
import re
import os
def get_image(path, pic_path):
'''从pdf中提取图片
:param path: pdf的路径
:param pic_path: 图片保存的路径
:return: 无return
'''
t0 = time.clock()
# 00、使用正则表达式查找PDF中的图片
checkXO = r"/Type(?= */XObject)"
checkIM = r"/Subtype(?= */Image)"
# 一、打开pdf,打印PDF的相关信息
doc = fitz.open(path)
# 图片计数
imgcount = 0
lenXREF = doc._getXrefLength()
# 打印PDF的信息
print("文件名:{}, 页数: {}, 对象: {}".format(path, len(doc), lenXREF - 1))
# 二、遍历PDF中的对象,遇到是图像才进行下一步,不然就continue
for i in range(1, lenXREF):
# 定义对象字符串
# text = doc._getObjectString(i)
text = doc._getXrefString(i)
# print(text)
# continue
isXObject = re.search(checkXO, text)
# 使用正则表达式查看是否是图片
isImage = re.search(checkIM, text)
# 如果不是对象也不是图片,则continue
if not isXObject or not isImage:
print("不是图片")
continue
imgcount += 1
# 根据索引生成图像
pix = fitz.Pixmap(doc, i)
# 根据pdf的路径生成图片的名称
new_name = path.replace('\\', '_') + "_img{}.png".format(imgcount)
new_name = new_name.replace(':', '')
# 三、将图像存为png格式
# 如果pix.n<5,可以直接存为PNG
if pix.n < 5:
pix.writePNG(os.path.join(pic_path, new_name))
# 否则先转换CMYK
else:
pix0 = fitz.Pixmap(fitz.csRGB, pix)
pix0.writePNG(os.path.join(pic_path, new_name))
pix0 = None
# 释放资源
pix = None
t1 = time.clock()
print("运行时间:{}s".format(t1 - t0))
print("提取了{}张图片".format(imgcount))
# 运行
if __name__=='__main__':
# pdf路径
path = r"Selenium 自动化爬虫.pdf" # 测试提取图片专用 Selenium 自动化爬虫
pic_path = r"image"
# 创建保存图片的文件夹
if os.path.exists(pic_path):
print("文件夹已存在,请重新创建新文件夹!")
raise SystemExit
else:
os.mkdir(pic_path)
get_image(path, pic_path)
有一些 C++ 库可以用来提取 PDF 中的图像,都可以在 Windows 上使用:
Poppler: 开源的 PDF 渲染器
MuPDF: 开源的 PDF 渲染器
PDFLib: 商业的 PDF 处理库
这些库都可以帮助提取 PDF 中的图像,并将它们保存为原始格式。可以根据自己的需要和偏好选择使用哪一个。
在 Windows 上,可以使用 Microsoft Visual C++ 等工具来开发 DLL。可以在编译时使用 /LD 选项来生成 DLL:
cl /LD /Ipath/to/includes /Lpath/to/libs mydll.cpp
其中,/LD 指示编译器生成 DLL,/Ipath/to/includes 指定包含文件的路径,/Lpath/to/libs 指定库文件的路径,mydll.cpp 是源代码文件。
在 DLL 中,可以使用上述代码的类似方法来提取 PDF 中的图像。为了方便调用,可以在 DLL 中定义一个函数,供外部调用:
#include <iostream>
#include <cstdio>
#include <poppler/cpp/poppler-document.h>
#include <poppler/cpp/poppler-page.h>
#include <poppler/cpp/poppler-image.h>
extern "C" __declspec(dllexport) bool extract_image_from_pdf(const char *pdf_file, const char *image_file)
{
// 加载 PDF 文档
poppler::document *doc = poppler::document::load_from_file(pdf_file);
if (!doc) {
std::cerr << "Failed to load PDF document" << std::endl;
return false;
}
// 获取文档中的第一页
poppler::page *page = doc->create_page(0);
if (!page) {
std::cerr << "Failed to load PDF page" << std::endl;
return false;
}
// 提取页面中的图像
poppler::image img = page->render_to_image();
if (img.is_valid()) {
// 将图像保存到文件中
img.save(image_file, "png");
return true;
} else {
std::cerr << "Failed to extract image from PDF" << std::endl;
return false;
}
}
这段代码定义了一个名为 extract_image_from_pdf 的函数,可以用来提取 PDF 中的图像。它接受两个参数:pdf_file 是 PDF 文件的路径,image_file 是保存图像的文件的路径。该函数返回一个布尔值,表示操作是否成功。
可以将这段代码编译为 DLL,然后在其他程序中调用这个函数来提取 PDF 中的图像。
仅供参考,望采纳,谢谢。
有一些可以提取 PDF 中的图片的 C++ 库。 下面是一些常见推荐:
1 Poppler:这是一个开源库,可以帮助您提取 PDF 中的图片。 Poppler 在 Windows 和 Linux 上都可以使用,并支持 C++。
2 MuPDF:这是另一个开源库,可以帮助您提取 PDF 中的图片。 MuPDF 在 Windows、Linux 和 MacOS 上都可以使用,并支持 C++。
3 Adobe Acrobat SDK:这是一个由 Adobe 提供的 SDK,可以帮助您在 C++ 中提取 PDF 中的图片。 Adobe Acrobat SDK 只能在 Windows 上使用,并需要购买许可证。
这些库都可以帮助您在 C++ 中提取 PDF 中的图片。 如果您对特定库有兴趣,可以查看相应文档以获取更多信息。 希望这能帮助到您!望采纳。