Exemple #1
0
        private async void DisplayImage(int pageIndex, double dpi)
        {
            if (MyPdfDocument == null)
            {
                return;
            }
            MyDpi = dpi;
            using (Windows.Data.Pdf.PdfPage page = MyPdfDocument.GetPage((uint)pageIndex))
            {
                double h       = page.Size.Height;
                var    options = new Windows.Data.Pdf.PdfPageRenderOptions();
                options.DestinationHeight = (uint)Math.Round(page.Size.Height * (dpi / 96.0), MidpointRounding.AwayFromZero);
                tbDpi.Text    = $"dpi : {dpi.ToString()}";
                tbHeight.Text = $"縦ピクセル : {options.DestinationHeight.ToString()}";

                using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
                {
                    await page.RenderToStreamAsync(stream, options);

                    BitmapImage image = new BitmapImage();
                    image.BeginInit();
                    image.CacheOption  = BitmapCacheOption.OnLoad;
                    image.StreamSource = stream.AsStream();//using System.IOがないとエラーになる
                    image.EndInit();
                    MyImage.Source = image;
                    MyImage.Width  = image.PixelWidth;
                    MyImage.Height = image.PixelHeight;
                }
            }
        }
        private async void ButtonOpenScale_Click(object sender, RoutedEventArgs e)
        {
            var file = await Windows.Storage.StorageFile.GetFileFromPathAsync(@"D:\ブログ用\1708_04.pdf");
            try
            {
                PdfDocument = await Windows.Data.Pdf.PdfDocument.LoadFromFileAsync(file);
            }
            catch (Exception)
            {

            }

            if (PdfDocument != null)
            {
                using (Windows.Data.Pdf.PdfPage page = PdfDocument.GetPage(0))
                {
                    BitmapImage image = new BitmapImage();
                    using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
                    {
                        var options = new Windows.Data.Pdf.PdfPageRenderOptions();
                        options.DestinationHeight = 1000;
                        await page.RenderToStreamAsync(stream, options);

                        image.BeginInit();
                        image.CacheOption = BitmapCacheOption.OnLoad;
                        image.StreamSource = stream.AsStream();
                        image.EndInit();
                    }
                    ImagePDF.Source = image;
                }
            }
        }
Exemple #3
0
        /// <summary>
        /// jpeg画像で保存
        /// </summary>
        /// <param name="dpi">PDFファイルを読み込む時のDPI</param>
        /// <param name="directory">保存フォルダ</param>
        /// <param name="fileName">保存名</param>
        /// <param name="pageIndex">保存するPDFのページ</param>
        /// <param name="quality">jpegの品質min0、max100</param>
        /// <param name="keta">保存名につける連番0埋めの桁数</param>
        /// <returns></returns>
        private async Task SaveSub2(double dpi, string directory, string fileName, int pageIndex, int quality, int keta)
        {
            using (Windows.Data.Pdf.PdfPage page = MyPdfDocument.GetPage((uint)pageIndex))
            {
                //指定されたdpiを元に画像サイズ指定、四捨五入
                var options = new Windows.Data.Pdf.PdfPageRenderOptions();
                options.DestinationHeight = (uint)Math.Round(page.Size.Height * (dpi / 96.0), MidpointRounding.AwayFromZero);

                using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
                {
                    await page.RenderToStreamAsync(stream, options);

                    BitmapImage image = new BitmapImage();
                    image.BeginInit();
                    image.CacheOption  = BitmapCacheOption.OnLoad;
                    image.StreamSource = stream.AsStream();
                    image.EndInit();

                    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                    encoder.QualityLevel = quality;
                    encoder.Frames.Add(BitmapFrame.Create(image));
                    pageIndex++;
                    string renban = pageIndex.ToString("d" + keta);
                    using (var fileStream = new FileStream(
                               System.IO.Path.Combine(directory, fileName) + "_" + renban + ".jpg", FileMode.Create, FileAccess.Write))
                    {
                        encoder.Save(fileStream);
                    }
                }
            }
        }
Exemple #4
0
        //ロックじゃない開き方
        private async void ButtonFree_Click(object sender, RoutedEventArgs e)
        {
            Windows.Storage.StorageFile file = await Windows.Storage.StorageFile.GetFileFromPathAsync(@"D:\ブログ用\1708_04.pdf");

            //ここから
            using (Windows.Storage.Streams.IRandomAccessStream RAStream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read))
            {
                Windows.Data.Pdf.PdfDocument MyPdfDocument = await Windows.Data.Pdf.PdfDocument.LoadFromStreamAsync(RAStream);

                //ここまで
                using (Windows.Data.Pdf.PdfPage page = MyPdfDocument.GetPage(0))
                {
                    using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
                    {
                        await page.RenderToStreamAsync(stream);

                        var image = new BitmapImage();
                        image.BeginInit();
                        image.CacheOption  = BitmapCacheOption.OnLoad;
                        image.StreamSource = stream.AsStream();
                        image.EndInit();
                        MyImage.Source = image;
                    }
                }
            }
        }
Exemple #5
0
        /// <summary>
        /// 表示
        /// </summary>
        /// <param name="pageNumber">指定ページ</param>
        /// <param name="displayDpi">指定dpi、通常なら96</param>
        private async void PageLoad(uint pageNumber, double displayDpi)
        {
            if (PdfDocument == null)
            {
                return;
            }
            BitmapImage image = new BitmapImage();
            var         rate  = displayDpi / 96.0;//表示倍率みたいなもの

            using (Windows.Data.Pdf.PdfPage page = PdfDocument.GetPage(pageNumber))
            {
                //指定されたdpiを元にレンダーサイズ指定、四捨五入
                double h       = page.Size.Height;
                var    options = new Windows.Data.Pdf.PdfPageRenderOptions();
                options.DestinationHeight = (uint)Math.Round(h * rate, MidpointRounding.AwayFromZero);

                using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
                {
                    await page.RenderToStreamAsync(stream, options);

                    image.BeginInit();
                    image.CacheOption  = BitmapCacheOption.OnLoad;
                    image.StreamSource = stream.AsStream();
                    image.EndInit();
                }
            }
            MyImage.Source = image;
            MyImage.Width  = image.PixelWidth;
            MyImage.Height = image.PixelHeight;
        }
Exemple #6
0
        private async Task <int> SaveSubTask(double rate, string filePath, uint i)
        {
            using (Windows.Data.Pdf.PdfPage page = PdfDocument.GetPage((uint)i))
            {
                //指定されたdpiを元にレンダーサイズ指定、四捨五入
                double h       = page.Size.Height;
                var    options = new Windows.Data.Pdf.PdfPageRenderOptions();
                options.DestinationHeight = (uint)Math.Round(h * rate, MidpointRounding.AwayFromZero);

                using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
                {
                    await page.RenderToStreamAsync(stream, options);

                    BitmapImage image = new BitmapImage();
                    image.BeginInit();
                    image.CacheOption  = BitmapCacheOption.OnLoad;
                    image.StreamSource = stream.AsStream();
                    image.EndInit();

                    JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                    encoder.QualityLevel = 85;
                    encoder.Frames.Add(BitmapFrame.Create(image));
                    using (var fileStream = new FileStream(filePath + "_" + i + ".jpg", FileMode.Create, FileAccess.Write))
                    {
                        encoder.Save(fileStream);
                    }
                }
            }
            return((int)i);
        }
Exemple #7
0
        private async Task <Windows.Storage.Streams.InMemoryRandomAccessStream> RenderPageBitmapAsync()
        {
            using (Windows.Data.Pdf.PdfPage pdfPage = _pdfDoc.GetPage(_currentPageIndex))
            {
                var memStream = new Windows.Storage.Streams.InMemoryRandomAccessStream();
                await pdfPage.RenderToStreamAsync(memStream);

                return(memStream);
            }
        }
Exemple #8
0
        private async Task <BitmapImage> MakeImage(Windows.Data.Pdf.PdfPage page)
        {
            var img = new BitmapImage();

            using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
            {
                await page.RenderToStreamAsync(stream);

                img.BeginInit();
                img.CacheOption  = BitmapCacheOption.OnLoad;
                img.StreamSource = stream.AsStream();
                img.EndInit();
            }
            return(img);
        }
Exemple #9
0
        //未検証、ページから画像作成から保存までの処理をTaskにする、あとでTaskWhenAllで実行?
        private async Task SaveTaskAsync(Windows.Data.Pdf.PdfPage page, string directory, string fileName, int pageIndex, int quality, int keta)
        {
            using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
            {
                await page.RenderToStreamAsync(stream);

                var encoder = new JpegBitmapEncoder();
                encoder.QualityLevel = quality;
                encoder.Frames.Add(BitmapFrame.Create(stream.AsStream()));
                string renban = pageIndex.ToString("d" + keta);
                using (var fileStream = new FileStream(
                           System.IO.Path.Combine(directory, fileName) + "_" + renban + ".jpg", FileMode.Create, FileAccess.Write))
                {
                    encoder.Save(fileStream);
                }
            }
        }
Exemple #10
0
        private async void MakeImageAndSave(double displayDpi)
        {
            if (PdfDocument == null)
            {
                return;
            }
            string filePath = @"D:\ブログ用\1708_04";

            var rate = displayDpi / 96.0;//表示倍率みたいなもの

            for (uint i = 0; i < PdfDocument.PageCount; i++)
            {
                using (Windows.Data.Pdf.PdfPage page = PdfDocument.GetPage(i))
                {
                    //指定されたdpiを元にレンダーサイズ指定、四捨五入
                    double h       = page.Size.Height;
                    var    options = new Windows.Data.Pdf.PdfPageRenderOptions();
                    options.DestinationHeight = (uint)Math.Round(h * rate, MidpointRounding.AwayFromZero);

                    using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
                    {
                        await page.RenderToStreamAsync(stream, options);

                        BitmapImage image = new BitmapImage();
                        image.BeginInit();
                        image.CacheOption  = BitmapCacheOption.OnLoad;
                        image.StreamSource = stream.AsStream();
                        image.EndInit();

                        JpegBitmapEncoder encoder = new JpegBitmapEncoder();
                        encoder.QualityLevel = 85;
                        encoder.Frames.Add(BitmapFrame.Create(image));
                        using (var fileStream = new FileStream(filePath + "_" + i + ".jpg", FileMode.Create, FileAccess.Write))
                        {
                            encoder.Save(fileStream);
                        }
                    }
                }
            }
        }
Exemple #11
0
        private BitmapImage RenderPage(double dpi, int pageIndex)
        {
            BitmapImage image = new BitmapImage();

            using (Windows.Data.Pdf.PdfPage page = MyPdfDocument.GetPage((uint)pageIndex))
            {
                //指定されたdpiを元に画像サイズ指定、四捨五入
                var options = new Windows.Data.Pdf.PdfPageRenderOptions();
                options.DestinationHeight = (uint)Math.Round(page.Size.Height * (dpi / 96.0), MidpointRounding.AwayFromZero);

                using (var stream = new Windows.Storage.Streams.InMemoryRandomAccessStream())
                {
                    page.RenderToStreamAsync(stream, options);

                    image.BeginInit();
                    image.CacheOption  = BitmapCacheOption.OnLoad;
                    image.StreamSource = stream.AsStream();
                    image.EndInit();
                }
            }
            return(image);
        }
Exemple #12
0
        /// <summary>
        /// Cache printable output
        /// </summary>
        /// <param name="p">Number of first page to print</param>
        /// <param name="count">Number of pages to print</param>
        /// <returns>Task that the caller is expected to await</returns>
        private async Task PrepareForPrintAsync(int p, int count /*, StorageFolder tempfolder*/)
        {
            if ((_pageImages is null) || (_pageImages.Count == 0))
            {
                ClearCachedPages();
                _pageImages = new List <Windows.UI.Xaml.Controls.Image>();
                _           = new Windows.UI.Xaml.Controls.Image();
                for (int i = p; i < count; i++)
                {
                    ApplicationLanguages.PrimaryLanguageOverride =
                        CultureInfo.InvariantCulture.TwoLetterISOLanguageName;
                    Windows.Data.Pdf.PdfPage pdfPage = _pdfDocument.GetPage(uint.Parse(i.ToString()));
                    double pdfPagePreferredZoom      = pdfPage.PreferredZoom;
                    IRandomAccessStream randomStream = new InMemoryRandomAccessStream();
                    global::Windows.Data.Pdf.PdfPageRenderOptions pdfPageRenderOptions =
                        new global::Windows.Data.Pdf.PdfPageRenderOptions();
                    Windows.Foundation.Size pdfPageSize = pdfPage.Size;
                    pdfPageRenderOptions.DestinationHeight = (uint)(pdfPageSize.Height * pdfPagePreferredZoom);
                    pdfPageRenderOptions.DestinationWidth  = (uint)(pdfPageSize.Width * pdfPagePreferredZoom);
                    await pdfPage.RenderToStreamAsync(randomStream, pdfPageRenderOptions);

                    Windows.UI.Xaml.Controls.Image imageCtrl = new Windows.UI.Xaml.Controls.Image();
                    BitmapImage src = new BitmapImage();
                    randomStream.Seek(0);
                    src.SetSource(randomStream);
                    imageCtrl.Source = src;
                    Windows.Graphics.Display.DisplayInformation DisplayInformation = Windows.Graphics.Display.DisplayInformation.GetForCurrentView();

                    float dpi = DisplayInformation.LogicalDpi / 96;
                    imageCtrl.Height = src.PixelHeight / dpi;
                    imageCtrl.Width  = src.PixelWidth / dpi;
                    randomStream.Dispose();
                    pdfPage.Dispose();

                    _pageImages.Add(imageCtrl);
                    AddPageToCache(imageCtrl);
                }
            }
        }
        private async void btnCombinar_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            if (model.Etiquetas.Count() > 0)
            {
                try
                {
                    model.ScenarioRunning = true;

                    Models.Etiquetas.Etiquetas_GetArchivosBindingModel mdl = new Models.Etiquetas.Etiquetas_GetArchivosBindingModel();
                    mdl.Aplicacion = model.Aplicacion;
                    mdl.Categoria  = model.Categoria;
                    mdl.Etiquetas  = model.Etiquetas;

                    long IDArchivo = await Models.EtiquetasModel.Get(mdl);

                    Models.Archivos.Archivos_GetBindingModel model2 = new Models.Archivos.Archivos_GetBindingModel();
                    model2.ID = IDArchivo;

                    IBuffer buffer = await Models.ArchivosModel.Get(model2);

                    Windows.Data.Pdf.PdfDocument pdf = await Windows.Data.Pdf.PdfDocument.LoadFromStreamAsync(buffer.AsStream().AsRandomAccessStream());

                    for (uint i = 0; i < pdf.PageCount; i++)
                    {
                        Windows.Data.Pdf.PdfPage page = pdf.GetPage(i);

                        StorageFile bmpFile = await model.DestinationFolder.CreateFileAsync(string.Format("Pagina {0}.bmp", i + 1), CreationCollisionOption.GenerateUniqueName);

                        IRandomAccessStream stream = await bmpFile.OpenAsync(FileAccessMode.ReadWrite);

                        await page.RenderToStreamAsync(stream);

                        await stream.FlushAsync();

                        stream.Dispose();
                        page.Dispose();

                        Utils.SetImageSourceFromFile(bmpFile, DisplayImage);

                        rootPage.NotifyUser("Apertura en progreso...", NotifyType.StatusMessage);
                        Utils.UpdateFileData(bmpFile, model);

                        ScanerListView.SelectedItem = model.FileList.Last();
                    }

                    rootPage.NotifyUser(String.Empty, NotifyType.StatusMessage);
                }
                catch (Exception)
                {
                    // No pasa nada
                }
                finally
                {
                    model.ScenarioRunning = false;
                }
            }
            else
            {
                rootPage.NotifyUser("Debe ingresar primero los datos", NotifyType.ErrorMessage);
            }
        }