Example #1
0
        public static void PreViewPage(object sender, PrintPageEventArgs e, PdfiumViewer.PdfDocument pdfDocument, ref int?pages)
        {
            PageSettings settings    = e.PageSettings;
            var          paperHeight = settings.PaperSize.Height;
            var          paperWidth  = settings.PaperSize.Width;
            int          margin      = 10;
            var          imgHeight   = 0;
            var          imgWidth    = 0;

            //int? pages = 0;
            using (var image = pdfDocument.Render(pages.Value, ConfigDefine.Pdf2JpgResolution, ConfigDefine.Pdf2JpgResolution, PdfiumViewer.PdfRenderFlags.CorrectFromDpi))
            {
                if ((double)image.Width / (double)image.Height > (double)paperWidth / (double)paperHeight) // 图片等比例放大,宽度超出了,先确定宽度
                {
                    imgWidth  = (int)(paperWidth - margin * 2);
                    imgHeight = (int)((double)image.Height * (double)imgWidth / (double)image.Width - margin * 2);
                }
                else
                {
                    imgHeight = (int)(paperHeight - margin * 2);
                    imgWidth  = (int)((double)image.Width / (double)image.Height * imgHeight - margin * 2);
                }
                e.Graphics.DrawImage(image, margin, margin, imgWidth, imgHeight);
            }
            if (pages < pdfDocument.PageCount - 1)
            {
                e.HasMorePages = true;  //HaeMorePages属性为True时,PrintPage的回调函数就会被再次调用,打印一个页面。
                pages++;
            }
            else
            {
                //预览界面点击打印需要把索引重新初始化
                pages = 0;
            }
        }
        public static void PreViewPage(object sender, PrintPageEventArgs e, PdfiumViewer.PdfDocument pdfDocument, string direction, ref int?pages)
        {
            PageSettings settings    = e.PageSettings;
            var          paperHeight = settings.PaperSize.Height;
            var          paperWidth  = settings.PaperSize.Width;
            int          margin      = 10;
            var          imgHeight   = 0;
            var          imgWidth    = 0;

            using (var image = pdfDocument.Render(pages.Value, ConfigDefine.Pdf2JpgResolution, ConfigDefine.Pdf2JpgResolution, PdfiumViewer.PdfRenderFlags.CorrectFromDpi))
            {
                ImageScaling(image, paperHeight, paperWidth, margin, direction, ref imgHeight, ref imgWidth);
                if (direction == "2")                              //2:横向打印(html转pdf的有问题,html转pdf不考虑宽度,转出来标准的pdf页,分页也已经完成,在这部分就处理不了)
                {
                    e.Graphics.TranslateTransform(0, paperHeight); //旋转原点
                    e.Graphics.RotateTransform(-90.0F);            //旋转角度
                }
                e.Graphics.DrawImage(image, margin, margin, imgWidth, imgHeight);
            }
            if (pages < pdfDocument.PageCount - 1)
            {
                e.HasMorePages = true;  //HaeMorePages属性为True时,PrintPage的回调函数就会被再次调用,打印一个页面。
                pages++;
            }
            else
            {
                //预览界面点击打印需要把索引重新初始化
                pages = 0;
            }
        }
Example #3
0
        /// <summary>
        /// Определить кол-во страниц документа с помошью pdfSharp
        /// </summary>
        /// <param name="doc"></param>
        /// <returns></returns>
        public int GetCountPages(Document doc)
        {
            int pageCount = 0;

            PdfiumViewer.PdfDocument documentV = PdfiumViewer.PdfDocument.Load(doc.File.FullName);
            pageCount = documentV.PageCount;

            return(pageCount);
        }
Example #4
0
 private void DocForm_Load(object sender, EventArgs e)
 {
     try
     {
         PdfiumViewer.PdfDocument pdfDocs = PdfiumViewer.PdfDocument.Load(this, FilePath);
         pdfviewer.Document = pdfDocs;
     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }
Example #5
0
        private async Task LoadPdf(PdfFile pdf)
        {
            try
            {
                PdfiumViewer.PdfDocument doc = await Task.Run(() =>
                {
                    return(PdfiumViewer.PdfDocument.Load(pdf.GetFullPath()));
                });

                pdfViewer.Document?.Dispose();
                pdfViewer.Document = doc;
                CurrentPdf         = pdf;
            }
            catch (Exception ex)
            {
                Trace.Fail(ex.ToString());
            }
        }
Example #6
0
        private BitmapImage GetPdfPageImage(PdfiumViewer.PdfDocument pDoc, int pageNum)
        {
            BitmapImage  image = new BitmapImage();
            Bitmap       img;
            MemoryStream stream = new MemoryStream();

            img = (Bitmap)pDoc.Render(pageNum, 96.0f, 96.0f, true);
            img.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);


            image.BeginInit();
            image.CacheOption  = BitmapCacheOption.OnLoad;
            image.StreamSource = stream;
            image.EndInit();
            image.Freeze();

            return(image);
        }
Example #7
0
        public Sprite GetPage(PdfiumViewer.PdfDocument doc, int pageNum)
        {
            if (pageNum >= doc.PageCount)
            {
                return(null);
            }

            var tempPath = Application.persistentDataPath + _targetFolder + "_tempImage_.png";

            if (FileBrowserHelpers.FileExists(tempPath))
            {
                FileBrowserHelpers.DeleteFile(tempPath);
            }

            var image = doc.Render(pageNum, 72, 72, false);

            image.Save(tempPath, System.Drawing.Imaging.ImageFormat.Png);

            var sprite = LoadSprite(tempPath);

            return(sprite);
        }
Example #8
0
        private List <BitmapImage> GetPdfPageImages(string fName)
        {
            BitmapImage        img;
            List <BitmapImage> images = new List <BitmapImage>();

            PdfiumViewer.PdfDocument pDoc = PdfiumViewer.PdfDocument.Load(fName);

            for (int i = 0; i < pDoc.PageCount; i++)
            {
                img = GetPdfPageImage(pDoc, i);

                if (img != null)
                {
                    images.Add(img);
                }
                else
                {
                    images.Add(new BitmapImage());
                }
            }

            pDoc.Dispose();
            return(images);
        }
Example #9
0
        public static void Pdf2Jpg(PdfiumViewer.PdfDocument pdfDocument, string targetPath, int?pages, string folderName)
        {
            if (string.IsNullOrEmpty(targetPath))
            {
                throw new ArgumentNullException("targetPath");
            }

            var dir = Path.GetDirectoryName(targetPath);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            if (pdfDocument.PageCount == 1)
            {
                pages = 1;
            }

            var imageDir = Path.Combine(dir, string.IsNullOrEmpty(folderName) ? Guid.NewGuid().ToString() : folderName);

            Directory.CreateDirectory(imageDir);
            if (pages != null)
            {
                pages--;//PdfiumViewer.PdfDocument的页数是从0开始的
                using (var image = pdfDocument.Render(pages.Value, ConfigDefine.Pdf2JpgResolution, ConfigDefine.Pdf2JpgResolution, PdfiumViewer.PdfRenderFlags.CorrectFromDpi))
                {
                    if (ConfigDefine.Pdf2JpgScaleRatio != 0)
                    {
                        Stream imageStream = new MemoryStream();
                        image.Save(imageStream, ImageFormat.Jpeg);
                        int imgWidth  = Convert.ToInt32(image.Width * ConfigDefine.Pdf2JpgScaleRatio);
                        int imgHeight = Convert.ToInt32(image.Height * ConfigDefine.Pdf2JpgScaleRatio);
                        using (var imgScaleStream = ImageScale2Stream(imageStream, imgWidth, imgHeight, ""))
                        {
                            using (var imgScale = System.Drawing.Image.FromStream(imgScaleStream))
                            {
                                imgScale.Save(Path.Combine(imageDir, folderName + ".jpg"));
                                //imgScale.Save(targetPath);
                            }
                        }
                    }
                    else
                    {
                        image.Save(Path.Combine(imageDir, folderName + ".jpg"));
                        //image.Save(targetPath);
                    }
                }
            }
            else
            {
                for (pages = 0; pages < pdfDocument.PageCount; pages++)
                {
                    using (var image = pdfDocument.Render(pages.Value, ConfigDefine.Pdf2JpgResolution, ConfigDefine.Pdf2JpgResolution, PdfiumViewer.PdfRenderFlags.CorrectFromDpi))
                    {
                        if (ConfigDefine.Pdf2JpgScaleRatio != 0)
                        {
                            Stream imageStream = new MemoryStream();
                            image.Save(imageStream, ImageFormat.Jpeg);
                            int imgWidth  = Convert.ToInt32(image.Width * ConfigDefine.Pdf2JpgScaleRatio);
                            int imgHeight = Convert.ToInt32(image.Height * ConfigDefine.Pdf2JpgScaleRatio);
                            using (var imgScaleStream = ImageScale2Stream(imageStream, imgWidth, imgHeight, ""))
                            {
                                using (var imgScale = System.Drawing.Image.FromStream(imgScaleStream))
                                {
                                    imgScale.Save(Path.Combine(imageDir, pages + ".jpg"));
                                }
                            }
                        }
                        else
                        {
                            image.Save(Path.Combine(imageDir, pages + ".jpg"));
                        }
                    }
                }
                //图片拼接
                //MergerImage(imageDir, targetPath);

                /*
                 * try
                 * {
                 *  //删除文件夹
                 *  Directory.Delete(imageDir, true);
                 * }
                 * catch (Exception ex)
                 * {
                 *  //Send(string.Format("删除临时目录出错:{0};其他错误信息:{1}", imageDir, ex));
                 *  throw new ArgumentNullException(string.Format("删除临时目录出错:{0};其他错误信息:{1}", imageDir, ex));
                 * }
                 * */
            }
        }
Example #10
0
 public static void Pdf2Jpg(PdfiumViewer.PdfDocument pdfDocument, string targetPath, int?pages)
 {
     Pdf2Jpg(pdfDocument, targetPath, pages, "");
 }
Example #11
0
        private void modifyPDF(string path)
        {
            string output = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\DocAuth";

            //string output = Directory.GetCurrentDirectory() + "\\pdfcache";
            if (!Directory.Exists(output))
            {
                Directory.CreateDirectory(output);
            }

            #region clean Existing cache files
            System.IO.DirectoryInfo di = new DirectoryInfo(output);

            foreach (FileInfo file in di.GetFiles())
            {
                file.Delete();
            }
            foreach (DirectoryInfo dir in di.GetDirectories())
            {
                dir.Delete(true);
            }
            #endregion

            _documentLastGuid = Guid.NewGuid();
            string uId = _documentLastGuid.ToString();
            using (Stream inputPdfStream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
                using (Stream outputPdfStream = new FileStream(output + uId + ".pdf", FileMode.Create, FileAccess.Write, FileShare.None))
                {
                    QRCodeGenerator qrGenerator = new QRCodeGenerator();
                    QRCodeData      qrCodeData  = qrGenerator.CreateQrCode(uId, QRCodeGenerator.ECCLevel.Q);
                    QRCode          qrCode      = new QRCode(qrCodeData);
                    string          QRSize      = System.Configuration.ConfigurationManager.AppSettings["QRSize"];
                    int             QRCodeSize  = 1;
                    if (!string.IsNullOrWhiteSpace(QRSize) && int.TryParse(QRSize, out QRCodeSize))
                    {
                        QRCodeSize = Convert.ToInt32(QRSize);
                    }
                    Bitmap qrCodeImage = qrCode.GetGraphic(1);

                    string storeQRCode        = System.Configuration.ConfigurationManager.AppSettings["storeQRCode"];
                    bool   storeQRCodeBoolean = false;
                    if (!string.IsNullOrWhiteSpace(storeQRCode) && bool.TryParse(storeQRCode, out storeQRCodeBoolean))
                    {
                        storeQRCodeBoolean = Convert.ToBoolean(storeQRCode);
                    }
                    if (storeQRCodeBoolean)
                    {
                        Bitmap qrCodeImageSave = qrCode.GetGraphic(20);
                        qrCodeImageSave.Save(output + uId + ".png");
                    }

                    PdfReader  reader  = new PdfReader(inputPdfStream);
                    PdfStamper stamper = new PdfStamper(reader, outputPdfStream);

                    Image image   = Image.GetInstance(qrCodeImage, ImageFormat.Bmp);
                    float qrWidth = image.Width;
                    for (int i = 1; i <= reader.NumberOfPages; i++)
                    {
                        PdfContentByte pdfContentByte = stamper.GetOverContent(i);
                        image.SetAbsolutePosition(pdfContentByte.PdfDocument.PageSize.Width - qrWidth, 0);
                        pdfContentByte.AddImage(image);

                        var image2 = Image.GetInstance(Properties.Resources.docscanstamp, ImageFormat.Png);
                        image2.ScaleAbsolute(130f, 20f);
                        image2.SetAbsolutePosition((pdfContentByte.PdfDocument.PageSize.Width - (qrWidth + 140)), 5);
                        pdfContentByte.AddImage(image2);
                    }

                    stamper.Close();

                    PdfiumViewer.PdfDocument pdfDocs = PdfiumViewer.PdfDocument.Load(this, output + uId + ".pdf");
                    uploadedFile       = output + uId + ".pdf";
                    fileName           = Path.GetFileName(path);
                    pdfviewer.Document = pdfDocs;
                }
        }
Example #12
0
 void View(string fileName)
 {
     documentV = PdfiumViewer.PdfDocument.Load(fileName);
     pdfRenderer.Load(documentV);
 }