private static void ExtractImage(PdfPage page, PdsImage image, string savePath) { // widget specific properties var bbox = image.GetBBox(); var page_view = page.AcquirePageView(1, PdfRotate.kRotate0); var rect = page_view.RectToDevice(bbox); PdfMatrix matrix = page_view.GetDeviceMatrix(); page_view.Release(); string imgPath = savePath + "image_" + (++_imageIndex).ToString() + ".jpg"; Console.WriteLine("Image Found: " + imgPath); var img = _pdfix.CreateImage(rect.right - rect.left, rect.bottom - rect.top, PsImageDIBFormat.kImageDIBFormatArgb); PdfPageRenderParams renderParams = new PdfPageRenderParams { clip_box = bbox, matrix = matrix, image = img }; if (!page.DrawContent(renderParams, null, IntPtr.Zero)) { throw new Exception(_pdfix.GetError()); } PdfImageParams imgParams = new PdfImageParams { format = PdfImageFormat.kImageFormatJpg, quality = 80 }; if (!img.Save(imgPath, imgParams)) { throw new Exception(_pdfix.GetError()); } img.Destroy(); }
public async Task <List <string> > ExtractImage( String email, String licenseKey, String openPath, String imgPath, Double zoom ) { List <string> imageList = new List <string>(); try { Pdfix pdfix = new Pdfix(); if (pdfix == null) { throw new Exception("Pdfix initialization fail"); } if (!pdfix.Authorize(email, licenseKey)) { throw new Exception(pdfix.GetError()); } PdfDoc doc = pdfix.OpenDoc(openPath, ""); if (doc == null) { throw new Exception(pdfix.GetError()); } for (int i = 0; i < doc.GetNumPages(); i++) { PdfPage page = doc.AcquirePage(i); if (page == null) { throw new Exception(pdfix.GetError()); } PdfPageView pageView = page.AcquirePageView(zoom, PdfRotate.kRotate0); if (pageView == null) { throw new Exception(pdfix.GetError()); } int width = pageView.GetDeviceWidth(); int height = pageView.GetDeviceHeight(); PsImage image = pdfix.CreateImage(width, height, PsImageDIBFormat.kImageDIBFormatArgb); if (image == null) { throw new Exception(pdfix.GetError()); } PdfPageRenderParams pdfPageRenderParams = new PdfPageRenderParams(); pdfPageRenderParams.image = image; pdfPageRenderParams.matrix = pageView.GetDeviceMatrix(); pdfPageRenderParams.render_flags = Pdfix.kRenderAnnot; if (!page.DrawContent(pdfPageRenderParams, null, IntPtr.Zero)) { throw new Exception(pdfix.GetError()); } PsStream stream = pdfix.CreateFileStream(imgPath + i.ToString() + ".jpg", PsFileMode.kPsWrite); PdfImageParams imgParams = new PdfImageParams(); imgParams.format = PdfImageFormat.kImageFormatJpg; imgParams.quality = 75; if (!image.SaveToStream(stream, imgParams)) { throw new Exception(pdfix.GetError()); } imageList.Add(imgPath + i.ToString()); stream.Destroy(); pageView.Release(); page.Release(); } doc.Close(); } catch (Exception ex) { throw ex; } return(imageList); }
public static void Run( String openPath, // source PDF document String savePath // output image document ) { Pdfix pdfix = PdfixEngine.Instance; PdfDoc doc = pdfix.OpenDoc(openPath, ""); if (doc == null) { throw new Exception(pdfix.GetError()); } // choose page to render var page = doc.AcquirePage(0); if (page == null) { throw new Exception(pdfix.GetError()); } // prepare page view with zoom and rotation var pageView = page.AcquirePageView((float)1.0, PdfRotate.kRotate0); if (pageView == null) { throw new Exception(pdfix.GetError()); } // prepare an image to write data into var image = pdfix.CreateImage(pageView.GetDeviceWidth(), pageView.GetDeviceHeight(), PsImageDIBFormat.kImageDIBFormatArgb); if (image == null) { throw new Exception(pdfix.GetError()); } // draw content into the image if (!page.DrawContent(new PdfPageRenderParams() { clip_box = page.GetCropBox(), matrix = pageView.GetDeviceMatrix(), image = image }, null, null)) { throw new Exception(pdfix.GetError()); } // prepare file stream to write into var stm = pdfix.CreateFileStream(savePath, PsFileMode.kPsTruncate); if (stm == null) { throw new Exception(pdfix.GetError()); } // save image to file if (!image.SaveToStream(stm, new PdfImageParams() { format = PdfImageFormat.kImageFormatJpg, quality = 80 })) { throw new Exception(pdfix.GetError()); } // cleanup stm.Destroy(); page.Release(); doc.Close(); }