public static List <System.Drawing.Image> ExtractImagesFromPDF(String PDFSourcePath) { List <System.Drawing.Image> ImgList = new List <System.Drawing.Image>(); iTextSharp.text.pdf.RandomAccessFileOrArray RAFObj = null; iTextSharp.text.pdf.PdfReader PDFReaderObj = null; iTextSharp.text.pdf.PdfObject PDFObj = null; iTextSharp.text.pdf.PdfStream PDFStremObj = null; try { RAFObj = new iTextSharp.text.pdf.RandomAccessFileOrArray(PDFSourcePath); PDFReaderObj = new iTextSharp.text.pdf.PdfReader(RAFObj, null); for (int i = 0; i <= PDFReaderObj.XrefSize - 1; i++) { PDFObj = PDFReaderObj.GetPdfObject(i); if ((PDFObj != null) && PDFObj.IsStream()) { PDFStremObj = (iTextSharp.text.pdf.PdfStream)PDFObj; iTextSharp.text.pdf.PdfObject subtype = PDFStremObj.Get(iTextSharp.text.pdf.PdfName.SUBTYPE); if ((subtype != null) && subtype.ToString() == iTextSharp.text.pdf.PdfName.IMAGE.ToString()) { try { iTextSharp.text.pdf.parser.PdfImageObject PdfImageObj = new iTextSharp.text.pdf.parser.PdfImageObject((iTextSharp.text.pdf.PRStream)PDFStremObj); System.Drawing.Image ImgPDF = PdfImageObj.GetDrawingImage(); ImgList.Add(ImgPDF); } catch (Exception) { } } } } PDFReaderObj.Close(); } catch (Exception ex) { throw new Exception(ex.Message); } return(ImgList); }
private void b_pdf2jpg_Click(object sender, EventArgs e) { try { if (listBox1.SelectedIndex != -1 & comboBox1.Text == "*.pdf") { foreach (var item in listBox1.SelectedItems) { Convert.ToString(item); string sourcePdf = Convert.ToString(item); string thename = Path.GetFileNameWithoutExtension(sourcePdf); raf = new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf); reader = new iTextSharp.text.pdf.PdfReader(raf, null); var page = reader.GetPageSize(1); int heigt = Convert.ToInt16(page.Height); int width = Convert.ToInt16(page.Width); //uzycie wprapera z Nuget //GhostscriptWrapper.GeneratePageThumb(@sourcePdf, @sourcePdf.Replace("pdf", "jpg"), 1, width, heigt); //GhostscriptWrapper.GeneratePageThumb(sourcePdf, sourcePdf.Replace("pdf", "jpg"), 1, 297, 210); GhostscriptJpegDevice dev = new GhostscriptJpegDevice(GhostscriptJpegDeviceType.Jpeg); dev.GraphicsAlphaBits = GhostscriptImageDeviceAlphaBits.V_4; dev.TextAlphaBits = GhostscriptImageDeviceAlphaBits.V_4; dev.ResolutionXY = new GhostscriptImageDeviceResolution(300, 300); dev.JpegQuality = 100; dev.InputFiles.Add(@sourcePdf); dev.Pdf.FirstPage = 1; dev.Pdf.LastPage = 1; dev.OutputPath = @sourcePdf.Replace("pdf", "jpg"); dev.Process(); } System.Media.SystemSounds.Beep.Play(); MessageBox.Show("koniec"); } else { System.Media.SystemSounds.Beep.Play(); MessageBox.Show("Wskaż pliki pdf na liście"); } } catch { } }
public void splitPdfByPages(String sourcePdf, int numOfPages, string baseNameOutPdf) { int pageCount = 0; string path = Path.GetFullPath(sourcePdf); string separtator = "_"; string extension = ".pdf"; try { raf = new iTextSharp.text.pdf.RandomAccessFileOrArray(sourcePdf); reader = new iTextSharp.text.pdf.PdfReader(raf, null); pageCount = reader.NumberOfPages; if (pageCount < numOfPages) { MessageBox.Show("Nie ma co dzielić"); } else { string counter; string ext = System.IO.Path.GetExtension(baseNameOutPdf); string outfile = string.Empty; double m = pageCount / numOfPages; int n = (int)Math.Ceiling(m); int currentPage = 1; string thename = Path.GetFileNameWithoutExtension(sourcePdf); string name = Path.GetFileName(sourcePdf); for (int i = 1; i <= pageCount; i++) { if (i < 10) { counter = "00"; } else { counter = "0"; } outfile = path.Replace(name, "") + thename + separtator + counter + i + extension; doc = new iTextSharp.text.Document(reader.GetPageSizeWithRotation(currentPage)); pdfCpy = new iTextSharp.text.pdf.PdfCopy(doc, new System.IO.FileStream(outfile, System.IO.FileMode.Create)); doc.Open(); if (i < n) { for (int j = 1; j <= numOfPages; j++) { page = pdfCpy.GetImportedPage(reader, currentPage); pdfCpy.AddPage(page); currentPage += 1; } } else { for (int j = currentPage; j <= pageCount; j++) { page = pdfCpy.GetImportedPage(reader, j); pdfCpy.AddPage(page); } } doc.Close(); Console.Beep(); } } reader.Close(); } catch (Exception ex) { throw ex; } }