/// <summary> /// 将pdf转换为图片,并且将图片进行切分 /// </summary> /// <param name="pdfInputPath"></param> /// <param name="imageFormat"></param> void ConvertPDF2Image(string pdfInputPath, ImageFormat imageFormat) { PDFFile pdfFile = PDFFile.Open(pdfInputPath); // start to convert each page for (int i = 0; i < pdfFile.PageCount; i++) { Bitmap pageImage = pdfFile.GetPageImage(i, 100); int width = pageImage.Width / 2; int height = pageImage.Height; Bitmap temp = GetPart(pageImage, 0, 0, width, height, 0, 0); temp.Save("images\\" + i.ToString() + "-1." + imageFormat.ToString(), imageFormat); int w = width / 2; int h = height; Bitmap temp1 = GetPart(temp, 0, 0, w+3, h, 0, 0); temp1.Save("images\\" + i.ToString() + "-1-1." + imageFormat.ToString(), imageFormat); temp = GetPart(temp, 0, 0, w - 3, h, w, 0); temp.Save("images\\" + i.ToString() + "-1-2." + imageFormat.ToString(), imageFormat); temp = GetPart(pageImage, 0, 0, width, height, width, 0); temp.Save("images\\" + i.ToString() + "-2." + imageFormat.ToString(), imageFormat); Bitmap temp2 = GetPart(temp, 0, 0, w + 3, h, 0, 0); temp2.Save("images\\" + i.ToString() + "-2-1." + imageFormat.ToString(), imageFormat); temp = GetPart(temp, 0, 0, w - 3, h, w, 0); temp.Save("images\\" + i.ToString() + "-2-2." + imageFormat.ToString(), imageFormat); pageImage.Dispose(); } pdfFile.Dispose(); } /// </summary> /// <param name="pPartStartPointX">目标图片开始绘制处的坐标X值(通常为0)</param> /// <param name="pPartStartPointY">目标图片开始绘制处的坐标Y值(通常为0)</param> /// <param name="pPartWidth">目标图片的宽度</param> /// <param name="pPartHeight">目标图片的高度</param> /// <param name="pOrigStartPointX">原始图片开始截取处的坐标X值</param> /// <param name="pOrigStartPointY">原始图片开始截取处的坐标Y值</param> Bitmap GetPart(Image originalImg, int pPartStartPointX, int pPartStartPointY, int pPartWidth, int pPartHeight, int pOrigStartPointX, int pOrigStartPointY) { System.Drawing.Bitmap partImg = new System.Drawing.Bitmap(pPartWidth, pPartHeight); System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(partImg); System.Drawing.Rectangle destRect = new System.Drawing.Rectangle(new System.Drawing.Point(pPartStartPointX, pPartStartPointY), new System.Drawing.Size(pPartWidth, pPartHeight)); System.Drawing.Rectangle origRect = new System.Drawing.Rectangle(new System.Drawing.Point(pOrigStartPointX, pOrigStartPointY), new System.Drawing.Size(pPartWidth, pPartHeight)); graphics.DrawImage(originalImg, destRect, origRect, System.Drawing.GraphicsUnit.Pixel); return partImg; }
//调用
ConvertPDF2Image("abcd.pdf", ImageFormat.Png);