The first thing to use is Ghostscript, there are some ways to use C # on the network, such as 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
Refer to the above method, can be in VC + + Using the Ghostscript Gsdll32.dll (can be found in the Ghostscript installation path) to the PDF to the picture, but the actual use will encounter some problems, some PDFs into the image after the length and width of the error, the top and bottom of the extra white edge, with the Ghostscript to show These PDFs also show the same problem as the effect. Use Imagemagic to open the PDF, showing the same effect, with extra white edges appearing at the top and bottom. I guess two of the software uses the same algorithm, or uses the same library.
Later I turned to Mupdf,pdf to turn the picture very well, and faster than the Ghostscript.
Similarly, there are C # usage methods on CodeProject: http://www.codeproject.com/Articles/498317/Rendering-PDF-Documents-with-Mupdf-and-P-Invoke-in
English bad friend, can look here, someone put the article translated and put forward their own opinion: http://www.cnblogs.com/pdfpatcher/archive/2012/11/25/2785154.html
Because I use C + +, it is more direct and convenient for the mupdf of C Open Source Library. Next, focus on the method of mupdf to picture.
Get mupdf Open Source Library, if go to official download mupdf, compiled 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 at the following address, compiled, in the obj-dbg folder can be found Libmupdf.dll, Libmupdf.lib, Fitz.h. : http://code.google.com/p/sumatrapdf/downloads/list
In order to facilitate everyone to download the DLL, I posted csdn, do not need points to download: http://download.csdn.net/detail/arthurfangmc/4948658
Specifically how to use which functions, and the introduction of functions, I will not repeat the introduction, the above 2 articles have been introduced, I directly posted a few important code.
My code directly from the Mupdf source code to modify, wrote a simple class, the variable name basically did not change, member variables are not changed to resemble "MyValue" or "m_value", we casually look at it.
MuPDFConvert.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);p rivate: void Drawpage (fz_context *ctx, fz_document *doc, int pagenum);p rivate: 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
MuPDFConvert.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,&NBSP;CS); fz_free ( CTX,&NBSP;CS);} Static void rgb_to_rgb (Fz_context *ctx, fz_colorspace *cs, float *rgb, &NBSP;FLOAT&NBSP;*XYZ) { xyz[0] = rgb[0]; xyz[1] = rgb[1]; xyz[2] = &NBSP;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 =&nbSp;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::P df2png (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\n"); #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\n"); #endif return false; } if (Fz_needs_password (m_doc)) { #if _DEBUG fprintf (stderr, "mupdf cannot authenticate password\n"); 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::d rawpage (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 =&nBsp;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 &&&NBSP;T&NBSP;<=&NBSP;H) h = 0; } /* now w or h&Nbsp;will be 0 unless then need to be enforced. */ if (w | | &NBSP;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,&NBSP;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); &nbSp; 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);
How to use:
#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 the PNG format, if you need other formats of the picture, you can choose another library, if the compiler version supports CImage, you can use CImage to go to other image formats,
In addition, you can use CImage directly in MFC to display PNG and other format pictures, the use of the method is very simple.
Define a variable
CImage m_img;
Import Pictures
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 picture//m_img. TransparentBlt (DC.M_HDC, 0, 0, m_nfitwidth, m_nfitheight,//0, 0, m_nregionwidth, M_nregionheight, RGB (255,255,255) );}
In the process, the PDF to the picture will take a certain amount of time, if you need to display the picture in real time, and the number of PDF pages is very many, it is best to open another thread to do the work of the PDF to the picture. Ghostscript and mupdf I have used in the project, the number of PDF pages or the resolution of the picture is very large, mupdf speed advantage is very obvious.
PDF to PNG image download code, hard work, we give points, if you have no points, and need to download, give me the message, I sent to you.
http://download.csdn.net/detail/arthurfangmc/4948833
PDF to Picture