// PDF_TO_picture.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <string>
#include "fitz.h"
using namespace std;
#define LOG(format, ...) \
{ \
fprintf(stderr, "[DEBUG] [%s:%s:%d] " format "", \
__FILE__, __FUNCTION__ , __LINE__, ##__VA_ARGS__); \
}
int main()
{
std::cout << "Hello World!\n";
// const char* input = "123.pdf";
const char* input = "605072021425200010636.pdf";
float zoom = 0.0, rotate = 0.0;
int page_number = 0, page_count = 0;
fz_context* ctx = nullptr;
fz_document* doc = nullptr;
fz_pixmap* pix = nullptr;
fz_matrix ctm;
// int x, y;
page_number = 0;
zoom = 100*10;
rotate = 0;
/* Create a context to hold the exception stack and various caches. */
ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
if (!ctx)
{
fprintf(stderr, "cannot create mupdf context\n");
return EXIT_FAILURE;
}
/* Register the default file types to handle. */
fz_try(ctx)
fz_register_document_handlers(ctx);
fz_catch(ctx)
{
fprintf(stderr, "cannot register document handlers: %s\n", fz_caught_message(ctx));
fz_drop_context(ctx);
return EXIT_FAILURE;
}
/* Open the document. */
fz_try(ctx)
doc = fz_open_document(ctx, input);
fz_catch(ctx)
{
fprintf(stderr, "cannot open document: %s\n", fz_caught_message(ctx));
fz_drop_context(ctx);
return EXIT_FAILURE;
}
/* Count the number of pages. */
fz_try(ctx)
page_count = fz_count_pages(ctx, doc);
fz_catch(ctx)
{
fprintf(stderr, "cannot count number of pages: %s\n", fz_caught_message(ctx));
fz_drop_document(ctx, doc);
fz_drop_context(ctx);
return EXIT_FAILURE;
}
if (page_count < 0)
{
fprintf(stderr, "page count %d\n", page_count);
fz_drop_document(ctx, doc);
fz_drop_context(ctx);
return EXIT_FAILURE;
}
/* Compute a transformation matrix for the zoom and rotation desired. */
/* The default resolution without scaling is 72 dpi. */
ctm = fz_scale(zoom / 100, zoom / 100);
ctm = fz_pre_rotate(ctm, rotate);
int nTemp_page_count = 0;
std::string strFileName = "";
while (page_count - nTemp_page_count>0)
{
LOG("page_count:%d,nTemp_page_count%d\n",page_count,nTemp_page_count);
LOG("page_count-nTemp_page_count:%d\n", page_count - nTemp_page_count);
/* Render page to an RGB pixmap. */
fz_try(ctx)
pix = fz_new_pixmap_from_page_number(ctx, doc, nTemp_page_count, ctm, fz_device_rgb(ctx), 0);
fz_catch(ctx)
{
fprintf(stderr, "cannot render page: %s\n", fz_caught_message(ctx));
fz_drop_document(ctx, doc);
fz_drop_context(ctx);
return EXIT_FAILURE;
}
nTemp_page_count++;
std::to_string(nTemp_page_count) + ".png";
fz_save_pixmap_as_png(ctx, pix, strFileName.c_str());
strFileName = "";
}
/* Clean up. */
fz_drop_pixmap(ctx, pix);
fz_drop_document(ctx, doc);
fz_drop_context(ctx);
}
PDF_TO_picture
最新推荐文章于 2023-12-11 17:48:08 发布