When a project was developed some time ago, PDF images were needed and images were displayed on the MFC development interface. Google has found several available methods, including GhostScript, imageMagic, and MuPDF. Due to the use of MFC for interface development, you need to use it in VC ++.
GhostScript is used first. There are some methods to use C # on the network, as shown in the following link:
Http://www.codeproject.com/Articles/317700/Convert-a-PDF-into-a-series-of-images-using-Csharp
Http://www.codeproject.com/Articles/32274/How-To-Convert-PDF-to-Image-Using-Ghostscript-API
Http://blog.csdn.net/ljsspace/article/details/6530046
By referring to the above method, you can use gsdll32.dll of GhostScript in VC ++ (which can be found in the installation path of ghostscript) to Convert PDF files into images, but some problems may occur in actual use, after converting some PDF files into images, an error occurs in the aspect ratio of length and width. The top and bottom of the PDF files are displayed with extra white edges and GhostScript. The same result may be displayed. Use imageMagic to open a PDF file. The display effect is the same, and an extra white edge will appear at the top and bottom. I guess the two software uses the same algorithm or the same library.
Later, I switched to MuPDF. The conversion speed of PDF images was very good and faster than that of GhostScript.
Similarly, there is a C # usage on codeproject: http://www.codeproject.com/Articles/498317/Rendering-PDF-Documents-with-Mupdf-and-P-Invoke-in
Poor English friends, can read here, some people translate the article and put forward their own opinions: http://www.cnblogs.com/pdfpatcher/archive/2012/11/25/2785154.html
Since I use C ++, it is more convenient to use MuPDF in the c Open Source library. Next, we will focus on how to convert images from MuPDF to images.
Get MuPDF open source library, if you go to the official download MuPDF, after the compilation is some exe and static Link Library, address here: http://code.google.com/p/mupdf/downloads/list
Fortunately, we can easily find the dynamic link library. Someone has integrated MuPDF into another open-source sumatrapdf. Download it at the following address. After compilation, you can find libmupdf In the obj-dbg folder. dll, libmupdf. lib, fitz. h. : Http://code.google.com/p/sumatrapdf/downloads/list
In order to help you download DLL, I posted CSDN, do not need points can be downloaded: http://download.csdn.net/detail/arthurfangmc/4948658
I will not repeat the introduction of specific functions and functions. I have introduced them in the previous two articles and I have directly posted several important codes.
My code was directly modified from the MuPDF source code. I wrote a simple class, and the variable name was not changed. The member variable was not changed to something similar to "myValue" or "m_value". Let's take a look.
Muw.convert. h:
#ifndef MY_MUPDF_CONVERT_PDF_PNG#define MY_MUPDF_CONVERT_PDF_PNGextern "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
Muw.convert. cpp:
#include "StdAfx.h"#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 _DEBUGfprintf(stderr, "mupdf cannot initialise context\n");#endifreturn 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 _DEBUGfprintf(stderr, "mupdf cannot open pdf\n");#endifreturn false;}if (fz_needs_password(m_doc)){#if _DEBUGfprintf(stderr, "mupdf cannot authenticate password\n");fz_throw(m_ctx, "mupdf cannot authenticate password: %s", filename);#endifreturn 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-->PNGdrawpage(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;elsescaley = 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);elsefz_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);elsefz_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);}
Usage:
#include "stdafx.h"#include "MuPDFConvert.h"int _tmain(int argc, _TCHAR* argv[]){CMuPDFConvert pdfConvert;int nNum = 0;pdfConvert.Pdf2Png(L"test.pdf", "test", nNum);return 0;}
The image generated here is in PNG format. If you need images in other formats, you can select another library. If the compiler version supports CImage, you can use CImage to convert it to other image formats,
In addition, you can directly use CImage in MFC to display PNG and other format images. The usage is very simple.
Define a variable
CImage m_img;
Import images
if(!m_img.IsNull()) {m_img.Destroy();}m_img.Load(strImagePath);
In the OnPaint () function:
If (! M_img.IsNull () {CPaintDC dc (GetDlgItem (IDC_CHILD_PIC); dc. setStretchBltMode (HALFTONE); m_img.Draw (dc. m_hDC, 0, 0, m_nFitWidth, m_nFitHeight, 0, 0, m_nRegionWidth, m_nRegionHeight); // Transparent Display of PNG images // m_img.TransparentBlt (dc. m_hDC, 0, 0, m_nFitWidth, m_nFitHeight, // 0, 0, m_nRegionWidth, m_nRegionHeight, RGB (255,255,255 ));}
It takes some time to convert a PDF file to an image. If you need to display the image in real time and there are a large number of PDF files, you 'd better open another thread to convert the PDF file to an image. I have used ghostScript and MuPDF in projects. When there are many PDF pages or the image resolution is large, the speed advantage of MuPDF is very obvious.
Attached PDF to PNG Image download code, hard work, please give me some points, if you do not score, you need to download again, send me a message, I will send you.
Http://download.csdn.net/detail/arthurfangmc/4948833