Esempio n. 1
0
        public static bool ToImage(string pdfInputPath, string imageOutputPath, string imageName, ImageFormat imageFormat)
        {
            PDFFile pdfFile = PDFFile.Open(pdfInputPath);

            Data.pageCount = pdfFile.PageCount;
            int startPageNum = 1;
            int endPageNum   = Data.pageCount;

            Console.WriteLine(imageOutputPath);

            if (!Directory.Exists(Data.sourceImageDirpath))
            {
                Directory.CreateDirectory(Data.sourceImageDirpath);
            }
            else
            {
                Directory.Delete(Data.sourceImageDirpath, true);
                Directory.CreateDirectory(Data.sourceImageDirpath);
            }
            // validate pageNum
            if (startPageNum <= 0)
            {
                startPageNum = 1;
            }

            if (endPageNum > pdfFile.PageCount)
            {
                endPageNum = pdfFile.PageCount;
            }

            if (startPageNum > endPageNum)
            {
                startPageNum = endPageNum;
                endPageNum   = startPageNum;
            }

            Data.OriginalImages = new List <Bitmap>();
            // start to convert each page
            for (int i = startPageNum; i <= endPageNum; i++)
            {
                float pageWidth = (float)(pdfFile.GetPageSize(i - 1).Width / 72);
                //float pageHeight = (float)(pdfFile.GetPageSize(i - 1).Height / 72);
                float resolution = Data.panelWidth / pageWidth;
                //Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
                Bitmap pageImage = pdfFile.GetPageImage(i - 1, resolution);
                //if (!Directory.Exists(imageOutputPath))
                //    Directory.CreateDirectory(imageOutputPath + "\\images");
                pageImage.Save(imageOutputPath + "\\" + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat);
                string imgPath = imageOutputPath + "\\" + imageName + i.ToString() + "." + imageFormat.ToString();
                Data.OriginalImages.Add(new Bitmap(GetCopyImage(imgPath)));
                //File.Delete(imgPath);

                //Console.WriteLine($"page {i} converted, width:{Data.OriginalImages[i - 1].Width} height:{Data.OriginalImages[i - 1].Height}");
            }

            pdfFile.Dispose();
            //Data.OriginalImages.Clear();
            //File.Delete(imageOutputPath + "\\" + imageName + "1." + imageFormat.ToString());
            return(true);
        }
Esempio n. 2
0
        public bool OpenPdfFile(Stream stream)
        {
            //if (this.FileName == fileName)
            //{
            //    return true;
            //}

            CloseFile();

            pdfFile = PDFFile.Open(stream);
            if (pdfFile == null)
            {
                return(false);
            }

            if (pdfFile.PageCount == 0)
            {
                pdfFile.Dispose();
                pdfFile = null;
                return(false);
            }

            //this.FileName = fileName;

            double s = 96.0 / 72.0;

            for (int i = 0; i < pdfFile.PageCount; i++)
            {
                PDFSize     size = pdfFile.GetPageSize(i);
                PdfPageInfo page = new PdfPageInfo(i, (int)(size.Width * s), (int)(size.Height * s));
                this.Pages.Add(page);
            }

            double scale    = (double)this.Height / (double)this.Width;
            double pdfScale = this.Pages[0].PdfHeight / this.Pages[0].PdfWidth;

            if (scale < pdfScale)
            {
                SetScale((double)this.Height / this.Pages[0].PdfHeight);
            }
            else
            {
                SetScale((double)this.Width / this.Pages[0].PdfWidth);
            }

            MoveToPage(0);

            return(true);
        }
Esempio n. 3
0
        public MemoryStream GetPdfImagePageStream(string pdfInputPath, int pageIndex, ImageFormat format, int width = 1600, int height = 2560, int quality = 10)
        {
            try
            {
                //pdf处理插件
                PDFFile pdfFile = PDFFile.Open(pdfInputPath);
                int     total   = pdfFile.PageCount;

                #region 防止异常参数
                if (pageIndex < 0)
                {
                    pageIndex = 0;
                }
                if (pageIndex > total)
                {
                    pageIndex = total - 1;
                }
                if (quality < 1)
                {
                    quality = 1;
                }
                if (quality > 10)
                {
                    quality = 10;
                }
                if (width <= 0)
                {
                    width = 1;
                }

                if (height <= 0)
                {
                    height = 1;
                }
                #endregion

                //pdf转换图片
                SizeF pageSize = pdfFile.GetPageSize(pageIndex);

                Bitmap pageImage = pdfFile.GetPageImage(pageIndex, 56 * quality);

                MemoryStream ms = new MemoryStream();

                pageImage.Save(ms, format);

                //原图
                Image img = Image.FromStream(ms, true);

                double ratio = (double)width / (double)height;

                double oRatio = (double)img.Width / (double)img.Height;

                int sbWidth = 0;

                int sbHeight = 0;

                int outX = 0;
                int outY = 0;

                if (oRatio < ratio)
                {
                    sbWidth  = (int)(img.Width * ((double)height / (double)(img.Height)));
                    sbHeight = height;

                    outX = (width - sbWidth) / 2;
                }
                else
                {
                    sbHeight = (int)(img.Height * ((double)width / (double)(img.Width)));
                    sbWidth  = width;

                    outY = (height - sbHeight) / 2;
                }

                //缩放
                Image    sbImg = new Bitmap(sbWidth, sbHeight);
                Graphics sbGra = Graphics.FromImage(sbImg);
                sbGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                sbGra.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                sbGra.Clear(Color.White);
                sbGra.DrawImage(img, new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, img.Width, img.Height), System.Drawing.GraphicsUnit.Pixel);

                //补白
                Image    outImg = new System.Drawing.Bitmap(width, height);
                Graphics outGra = System.Drawing.Graphics.FromImage(outImg);
                outGra.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                outGra.SmoothingMode     = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                outGra.Clear(Color.White);
                outGra.DrawImage(sbImg, new System.Drawing.Rectangle(outX, outY, sbWidth, sbHeight), new System.Drawing.Rectangle(0, 0, sbWidth, sbHeight), System.Drawing.GraphicsUnit.Pixel);

                MemoryStream outMs = new MemoryStream();

                outImg.Save(outMs, format);

                sbImg.Dispose();
                outImg.Dispose();
                img.Dispose();

                return(outMs);
            }
            catch (Exception ex)
            {
            }

            return(new MemoryStream());
        }
Esempio n. 4
0
        public bool pdf_Printer_fast(printerParam param, int copys, out string resultString)
        {
            resultString = "";
            PDFFile pdf = PDFFile.Open(param.FileName);

            try
            {
                PrinterSettings settings = new PrinterSettings();
                settings.PrinterName = param.printerName;
                settings.PrintToFile = false;
                if (!settings.IsValid)
                {
                    throw new Exception("打印机:" + param.printerName + "无效");
                }
                //设置纸张大小(可以不设置,取默认设置)3.90 in,  8.65 in
                PaperSize ps = new PaperSize("test", 4, 9);
                ps.RawKind = 9; //如果是自定义纸张,就要大于118,(A4值为9,详细纸张类型与值的对照请看http://msdn.microsoft.com/zh-tw/library/system.drawing.printing.papersize.rawkind(v=vs.85).aspx)
                O2S.Components.PDFRender4NET.Printing.PDFPrintSettings pdfPrintSettings = new O2S.Components.PDFRender4NET.Printing.PDFPrintSettings(settings);
                pdfPrintSettings.PaperSize               = ps;
                pdfPrintSettings.PageScaling             = O2S.Components.PDFRender4NET.Printing.PageScaling.FitToPrinterMarginsProportional;
                pdfPrintSettings.PrinterSettings.Collate = true;
                if (pdf.GetPageSize(0).Width > pdf.GetPageSize(0).Height)
                {
                    pdfPrintSettings.PrinterSettings.DefaultPageSettings.Landscape = true;
                }
                else
                {
                    pdfPrintSettings.PrinterSettings.DefaultPageSettings.Landscape = false;
                }
                pdfPrintSettings.PrinterSettings.Copies = Convert.ToInt16(copys);
                if (!param.isColor)
                {
                    if (pdfPrintSettings.PrinterSettings.PaperSources.Count <= param.PaperSource)
                    {
                        //MessageBox.Show("纸盒参数错误!");
                        throw new Exception("纸盒参数错误!");
                    }
                    else
                    {
                        pdfPrintSettings.PaperSource = pdfPrintSettings.PrinterSettings.PaperSources[param.PaperSource];
                    }
                }
                else
                {
                    for (int i = 0; i < pdfPrintSettings.PrinterSettings.PaperSources.Count; i++)
                    {
                        if (pdfPrintSettings.PrinterSettings.PaperSources[i].SourceName.ToString().Contains("纸盘 " + (param.PaperSource + 1).ToString()))
                        {
                            pdfPrintSettings.PaperSource = pdfPrintSettings.PrinterSettings.PaperSources[i];
                        }
                    }
                    if (pdfPrintSettings.PaperSource == null)
                    {
                        throw new Exception("纸盒参数错误!");
                    }
                }
                pdfPrintSettings.PrinterSettings.DefaultPageSettings.Margins.Left = Math.Max(0, (int)(pdfPrintSettings.PrinterSettings.DefaultPageSettings.PaperSize.Width - pdf.GetPageSize(0).Width) / 2);
                pdfPrintSettings.PrinterSettings.DefaultPageSettings.Margins.Top  = Math.Max(0, (int)(pdfPrintSettings.PrinterSettings.DefaultPageSettings.PaperSize.Height - pdf.GetPageSize(0).Height) / 2);
                if (param.isColor)
                {
                    pdfPrintSettings.PrinterSettings.DefaultPageSettings.Color = true;
                }
                bool isDuplex = pdfPrintSettings.PrinterSettings.CanDuplex;
                if (!isDuplex)
                {
                    if (param.DoubleForm)
                    {
                        throw new Exception("该打印机不支持双面打印!");
                    }
                    else
                    {
                        pdfPrintSettings.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Simplex;
                    }
                }
                else
                {
                    if (param.DoubleForm)
                    {
                        if (pdf.GetPageSize(0).Width > pdf.GetPageSize(0).Height)
                        {
                            pdfPrintSettings.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Horizontal;
                        }
                        else
                        {
                            pdfPrintSettings.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Vertical;
                        }
                    }
                    else
                    {
                        pdfPrintSettings.PrinterSettings.Duplex = System.Drawing.Printing.Duplex.Simplex;
                    }
                }
                pdf.Print(pdfPrintSettings);
                pdf.Dispose();
                resultString = "";
                return(true);
            }
            catch (Exception ex)
            {
                resultString += ex.Message + ";";
                pdf.Dispose();
                return(false);
            }
        }
Esempio n. 5
0
        public static void ConvertPDF2Image(string pdfInputPath, string imageOutputPath, string imageName, ImageFormat imageFormat)
        {
            PDFFile pdfFile = PDFFile.Open(pdfInputPath);

            DATA.pageCount = pdfFile.PageCount;
            int startPageNum = 1;
            int endPageNum   = DATA.pageCount;

            Console.WriteLine(imageOutputPath);

            if (!Directory.Exists(imageOutputPath))
            {
                Directory.CreateDirectory(imageOutputPath);
            }
            else
            {
                Directory.Delete(imageOutputPath, true);
                Directory.CreateDirectory(imageOutputPath);
            }

            // validate pageNum
            if (startPageNum <= 0)
            {
                startPageNum = 1;
            }

            if (endPageNum > pdfFile.PageCount)
            {
                endPageNum = pdfFile.PageCount;
            }

            if (startPageNum > endPageNum)
            {
                int tempPageNum = startPageNum;
                startPageNum = endPageNum;
                endPageNum   = startPageNum;
            }

            DATA.OriginalImages = new List <Bitmap>();
            //DATA.AnnotatedImages = new List<Bitmap>();
            // start to convert each page
            for (int i = startPageNum; i <= endPageNum; i++)
            {
                float pageWidth  = pdfFile.GetPageSize(i - 1).Width / 72;
                float pageHeight = pdfFile.GetPageSize(i - 1).Height / 72;
                float resolution = DATA.panelWidth / pageWidth;
                //Bitmap pageImage = pdfFile.GetPageImage(i - 1, 56 * (int)definition);
                Bitmap pageImage = pdfFile.GetPageImage(i - 1, resolution);
                if (!Directory.Exists(imageOutputPath))
                {
                    Directory.CreateDirectory(imageOutputPath + "\\images");
                }
                pageImage.Save(imageOutputPath + "\\" + imageName + i.ToString() + "." + imageFormat.ToString(), imageFormat);
                DATA.OriginalImages.Add(new Bitmap(imageOutputPath + "\\" + imageName + i.ToString() + "." + imageFormat.ToString()));

                pageImage.Dispose();
                Console.WriteLine($"page {i} converted, width:{DATA.OriginalImages[i-1].Width} height:{DATA.OriginalImages[i - 1].Height}");
            }
            //foreach (var item in DATA.OriginalImages)
            //{
            //    DATA.AnnotatedImages.Add(item);
            //}
            //DATA.AnnotatedImages = DATA.OriginalImages;

            pdfFile.Dispose();
            Console.WriteLine("Done!");
        }