mupdf-将pdf转png图片

这是它的网站Index of Downloads (mupdf.com)

是开源的

下载这个文件, 还能当pdf阅读器使用

 

下载mupdf-1.18.0-source里面就是源代码

可以自己看看,里面有源代码,也有API帮助说明。自己研究一下

 我这里就不多介绍了,直接上例子,

这个例子是我两年前百度找到的,一直在用,但是原文链接我已经找不到了。

1.新建个控制台项目

 空项目,新建个main.cpp

 2.设置

 添加附加包含目录

 

 附加库目录

 附加依赖项.lib和.dll要自己拿源码去编译

 编写hpp和cpp

#ifndef MY_MUPDF_CONVERT_PDF_PNG
#define MY_MUPDF_CONVERT_PDF_PNG


extern "C" 
{
#include "mupdf/fitz.h"
#include "mupdf/fitz-internal.h"
}

class CMuPDFConvert
{
public:
    CMuPDFConvert(void);
    ~CMuPDFConvert(void);

    bool Pdf2Png(const wchar_t* pdfFileName/*,const char* imageOutputPath*/,const char* imageName, int &pageNum);

private:
    void drawpage(fz_context *ctx, fz_document *doc, int pagenum);

private:
    static const fz_matrix fz_identity;
    static fz_colorspace k_device_rgb;
    fz_colorspace *fz_device_rgb;

    fz_document *m_doc;
    fz_stream *m_pStream;
    fz_context *m_ctx;

    int uselist;
    int alphabits;

    char output[1024];
    float resolution;
    float rotation;
    int res_specified;
    int width;
    int height;
    int fit;
    fz_colorspace *colorspace;
    int savealpha;
    int invert;
    float gamma_value;

    char filename[1024];
};

#endif
#include "MuPDFConvert.h"


void fz_free_colorspace_imp(fz_context *ctx, fz_storable *cs_)
{
    fz_colorspace *cs = (fz_colorspace *)cs_;

    if (cs->free_data && cs->data)
        cs->free_data(ctx, cs);
    fz_free(ctx, cs);
}

static void rgb_to_rgb(fz_context *ctx, fz_colorspace *cs, float *rgb, float *xyz)
{
    xyz[0] = rgb[0];
    xyz[1] = rgb[1];
    xyz[2] = rgb[2];
}


const fz_matrix CMuPDFConvert::fz_identity = { 1, 0, 0, 1, 0, 0 };
fz_colorspace CMuPDFConvert::k_device_rgb = { {-1, fz_free_colorspace_imp}, 0, "DeviceRGB", 3, rgb_to_rgb, rgb_to_rgb };

CMuPDFConvert::CMuPDFConvert(void)
{
    fz_device_rgb = &k_device_rgb;

    uselist = 1;
    alphabits = 8;

    //output = NULL;
    resolution = 72;
    rotation = 0;
    res_specified = 0;
    width = 0;
    height = 0;
    fit = 0;
    colorspace = NULL;
    savealpha = 0;
    invert = 0;
    gamma_value = 1;

    m_doc = NULL;
    m_ctx = NULL;
}

CMuPDFConvert::~CMuPDFConvert(void)
{
    if (m_pStream)
    {
        fz_close(m_pStream);
        m_pStream = NULL;
    }

    if (m_doc)
    {
        fz_close_document(m_doc);
        m_doc = NULL;
    }

    if (m_ctx)
    {
        fz_free_context(m_ctx);
        m_ctx = NULL;
    }
    
}


bool CMuPDFConvert::Pdf2Png(const wchar_t* wcharPdfFile/*,const char* imageOutputPath*/,const char* imageName, int &pageNum)
{
    char tempPath[1024];
    //strcpy_s(tempPath, imageOutputPath);
    //strcat_s(tempPath, imageName);
    strcpy_s(tempPath, imageName);
    strcat_s(tempPath, "%d.png");

    strcpy_s(output, (strlen(tempPath)+1)*sizeof(char), tempPath);

    m_ctx = fz_new_context(NULL, NULL, FZ_STORE_DEFAULT);
    if (!m_ctx)
    {
#if _DEBUG
        fprintf(stderr, "mupdf cannot initialise context
");
#endif
        return false;
    }

    fz_try(m_ctx)
    {
        fz_set_aa_level(m_ctx, alphabits);
        colorspace = fz_device_rgb;

        m_pStream = fz_open_file_w(m_ctx, wcharPdfFile);
        if (m_pStream)
        {
            m_doc = fz_open_document_with_stream(m_ctx, ".pdf", m_pStream);
        }

        if (!m_doc)
        {
#if _DEBUG
            fprintf(stderr, "mupdf cannot open pdf
");
#endif
            return false;
        }

        if (fz_needs_password(m_doc))
        {    
#if _DEBUG
            fprintf(stderr, "mupdf cannot authenticate password
");
            fz_throw(m_ctx, "mupdf cannot authenticate password: %s", filename);
#endif
            return false;
        }

        int pn = fz_count_pages(m_doc);
        pageNum = pn;
        for (int i=0; i<pn; ++i)
        {
            fz_page *page = fz_load_page(m_doc, i);
            fz_rect bound = fz_bound_page(m_doc, page);

            //PDF-->PNG
            drawpage(m_ctx, m_doc, i+1);

            fz_free_page(m_doc, page);
            page = NULL;
        }    
    }
    fz_catch(m_ctx)
    {
        return false;
    }

    if (m_pStream)
    {
        fz_close(m_pStream);
        m_pStream = NULL;
    }
    if (m_doc)
    {
        fz_close_document(m_doc);
        m_doc = NULL;
    }
    if (m_ctx)
    {
        fz_free_context(m_ctx);
        m_ctx = NULL;
    }
    return true;
}


void CMuPDFConvert::drawpage(fz_context *ctx, fz_document *doc, int pagenum)
{
    fz_page *page;
    fz_display_list *list = NULL;
    fz_device *dev = NULL;
    int start;

    fz_var(list);
    fz_var(dev);

    fz_try(ctx)
    {
        page = fz_load_page(doc, pagenum - 1);
    }
    fz_catch(ctx)
    {
        fz_throw(ctx, "cannot load page %d in file '%s'", pagenum, filename);
    }

    if (uselist)
    {
        fz_try(ctx)
        {
            list = fz_new_display_list(ctx);
            dev = fz_new_list_device(ctx, list);
            fz_run_page(doc, page, dev, fz_identity, NULL);
        }
        fz_catch(ctx)
        {
            fz_free_device(dev);
            fz_free_display_list(ctx, list);
            fz_free_page(doc, page);
            fz_throw(ctx, "cannot draw page %d in file '%s'", pagenum, filename);
        }
        fz_free_device(dev);
        dev = NULL;
    }

    if (output)
    {
        float zoom;
        fz_matrix ctm;
        fz_rect bounds, bounds2;
        fz_bbox bbox;
        fz_pixmap *pix = NULL;
        int w, h;

        fz_var(pix);

        bounds = fz_bound_page(doc, page);
        zoom = resolution / 72;
        ctm = fz_scale(zoom, zoom);
        ctm = fz_concat(ctm, fz_rotate(rotation));
        bounds2 = fz_transform_rect(ctm, bounds);
        bbox = fz_round_rect(bounds2);
        /* Make local copies of our width/height */
        w = width;
        h = height;
        /* If a resolution is specified, check to see whether w/h are
         * exceeded; if not, unset them. */
        if (res_specified)
        {
            int t;
            t = bbox.x1 - bbox.x0;
            if (w && t <= w)
                w = 0;
            t = bbox.y1 - bbox.y0;
            if (h && t <= h)
                h = 0;
        }
        /* Now w or h will be 0 unless then need to be enforced. */
        if (w || h)
        {
            float scalex = w/(bounds2.x1-bounds2.x0);
            float scaley = h/(bounds2.y1-bounds2.y0);

            if (fit)
            {
                if (w == 0)
                    scalex = 1.0f;
                if (h == 0)
                    scaley = 1.0f;
            }
            else
            {
                if (w == 0)
                    scalex = scaley;
                if (h == 0)
                    scaley = scalex;
            }
            if (!fit)
            {
                if (scalex > scaley)
                    scalex = scaley;
                else
                    scaley = scalex;
            }
            ctm = fz_concat(ctm, fz_scale(scalex, scaley));
            bounds2 = fz_transform_rect(ctm, bounds);
        }
        bbox = fz_round_rect(bounds2);

        /* TODO: banded rendering and multi-page ppm */

        fz_try(ctx)
        {
            pix = fz_new_pixmap_with_bbox(ctx, colorspace, bbox);

            if (savealpha)
                fz_clear_pixmap(ctx, pix);
            else
                fz_clear_pixmap_with_value(ctx, pix, 255);

            dev = fz_new_draw_device(ctx, pix);
            if (list)
                fz_run_display_list(list, dev, ctm, bbox, NULL);
            else
                fz_run_page(doc, page, dev, ctm, NULL);
            fz_free_device(dev);
            dev = NULL;

            if (invert)
                fz_invert_pixmap(ctx, pix);
            if (gamma_value != 1)
                fz_gamma_pixmap(ctx, pix, gamma_value);

            if (savealpha)
                fz_unmultiply_pixmap(ctx, pix);

            if (output)
            {
                char buf[512];
                sprintf(buf, output, pagenum);
                if (strstr(output, ".png"))
                {
                    fz_write_png(ctx, pix, buf, savealpha);
                }
            }
            fz_drop_pixmap(ctx, pix);
        }
        fz_catch(ctx)
        {
            fz_free_device(dev);
            fz_drop_pixmap(ctx, pix);
            fz_free_display_list(ctx, list);
            fz_free_page(doc, page);
            fz_rethrow(ctx);
        }
    }

    if (list)
        fz_free_display_list(ctx, list);

    fz_free_page(doc, page);

    fz_flush_warnings(ctx);
}
// Pdf2Png.cpp : Defines the entry point for the console application.
//

#include "MuPDFConvert.h"


int main()
{


    //char*转wchar_t*
    char *CStr = "D:\1\test\test\test.pdf";
    size_t len = strlen(CStr) + 1;
    size_t converted = 0;
    wchar_t *WStr;
    WStr=(wchar_t*)malloc(len*sizeof(wchar_t));
    mbstowcs_s(&converted, WStr, len, CStr, _TRUNCATE);

    CMuPDFConvert pdfConvert;
    int nNum = 0;
    pdfConvert.Pdf2Png(WStr, "test", nNum);

}

 编译项目

将.dll动态链接库放到生成的exe目录里

 执行演示

程序员阿飞

2021年4月29日

作者: 阿飞

出处: https://www.cnblogs.com/nxopen2018/>

关于作者:专注NX开发、VC++开发、数据库、三维建模领域,请多多赐教!

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 原文链接 如有问题, 可留言(博客文章底部留言)咨询.

原文地址:https://www.cnblogs.com/nxopen2018/p/14720105.html