public Dictionary <string, byte[]> ExtractImagePages(byte[] pdfContent) { var pageImages = new Dictionary <string, byte[]>(); using (var doc = new PdfDocument(pdfContent)) { int i = 0; foreach (var page in doc.Pages) { using (var bitmap = new PDFiumBitmap((int)page.Width, (int)page.Height, false)) { bitmap.Fill(new PDFiumSharp.Types.FPDF_COLOR(255, 255, 255)); page.Render(bitmap); var bmp = Image.FromStream(bitmap.AsBmpStream()); using (var ms = new MemoryStream()) { bmp.Save(ms, ImageFormat.Jpeg); pageImages.Add($"{i++.ToString("000")}.jpg", ms.ToArray()); } } } } return(pageImages); }
public static ImageSource RenderPage(string filename, int pageIndex, string password = null, bool withTransparency = true) { using (var doc = new PdfDocument(filename, password)) { var page = doc.Pages[pageIndex]; using (var bitmap = new PDFiumBitmap((int)page.Width, (int)page.Height, withTransparency)) { page.Render(bitmap); return(new BmpBitmapDecoder(bitmap.AsBmpStream(), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames.First()); } } }
//Todo clean this up and add support for multiple pages public byte[] Test(string filepath) { using (var doc = new PDFiumSharp.PdfDocument(filepath)) { var page = doc.Pages[0]; using (var bitmap = new PDFiumBitmap((int)page.Width, (int)page.Height, false)) { page.Render(bitmap); var b = new BmpBitmapDecoder(bitmap.AsBmpStream(), BitmapCreateOptions.None, BitmapCacheOption.OnLoad).Frames.First(); JpegBitmapEncoder encoder = new JpegBitmapEncoder(); encoder.Frames.Add(b); using (MemoryStream ms = new MemoryStream()) { encoder.Save(ms); return(ms.ToArray()); } } } }
static void Main() { Application.Initialize(); Window window = new Window(); window.Closed += (sender, e) => Application.Exit(); using (var doc = new PdfDocument("TestDoc.pdf", "password")) { var page = doc.Pages[0]; using (var bitmap = new PDFiumBitmap((int)page.Width, (int)page.Height, false)) { page.Render(bitmap, PageOrientations.Normal); bitmap.Save("Page0.bmp"); window.Content = new ImageView(Image.FromStream(bitmap.AsBmpStream())); } } window.Show(); Application.Run(); }