当前位置: 首页>>代码示例>>C#>>正文


C# PdfDocument.SaveAsImage方法代码示例

本文整理汇总了C#中PdfDocument.SaveAsImage方法的典型用法代码示例。如果您正苦于以下问题:C# PdfDocument.SaveAsImage方法的具体用法?C# PdfDocument.SaveAsImage怎么用?C# PdfDocument.SaveAsImage使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在PdfDocument的用法示例。


在下文中一共展示了PdfDocument.SaveAsImage方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            // Check if path was provided
            if (args.Length == 0)
            {
                Console.WriteLine("Please enter a path to read for image/PDF files! Exiting..");

                return;
            }

            var path = args[0];
            if (!System.IO.Directory.Exists(path))
            {
                Console.WriteLine("Invalid path, does not exist! Exiting..");

                return;
            }

            var outputPath = Path.Combine(path, "output");
            if (!System.IO.Directory.Exists(outputPath))
                System.IO.Directory.CreateDirectory(outputPath);

            var files = System.IO.Directory.EnumerateFiles(path, "*.*", SearchOption.TopDirectoryOnly)
                .Where(
                    f => f.ToLower().EndsWith(".jpg") ||
                    f.ToLower().EndsWith(".jpeg") ||
                    f.ToLower().EndsWith(".png") ||
                    f.ToLower().EndsWith(".pdf"));

            foreach (var file in files)
            {
                var lowerFile = file.ToLower();

                if (lowerFile.EndsWith(".pdf"))
                {
                    Console.WriteLine($"Current file: {file}");
                    var pdf = new PdfDocument();
                    pdf.LoadFromFile(file);

                    // Save first page of PDF
                    using (var bmp = pdf.SaveAsImage(0))
                    {
                        var outputFilename = Path.Combine(outputPath, $"{Path.GetFileNameWithoutExtension(file)}.jpg");
                        Console.WriteLine($"Saving jpg to {outputFilename}");
                        bmp.Save(outputFilename, ImageFormat.Jpeg);
                    }
                }
                else if (lowerFile.EndsWith(".png"))
                {
                    Console.WriteLine($"Current file: {file}");
                    RotateImageIfNeeded(file, outputPath);
                }
                else if (lowerFile.EndsWith(".jpg") || lowerFile.EndsWith(".jpeg"))
                {
                    Console.WriteLine($"Current file: {file}");
                    try
                    {
                        var dirs = ImageMetadataReader.ReadMetadata(file);
                        if (dirs.Any())
                        {
                            Console.WriteLine("meta data to read");
                        }
                        var dir = dirs.Where(d => d.Name.Equals("Exif IFD0")).SingleOrDefault();
                        if (dir != null)
                        {
                            var orientation = dir.Tags.Where(t => t.TagName.Equals("Orientation")).SingleOrDefault();
                            if (orientation != null)
                            {
                                Console.WriteLine($"-- orientation = {orientation.Description}, dir name = {dir.Name}");
                                ExifRotateImageIfNeeded(file, outputPath);
                            }
                            else
                            {
                                Console.WriteLine("-- orientation not found!");
                                RotateImageIfNeeded(file, outputPath);
                            }
                        }
                        else
                        {
                            Console.WriteLine("-- orientation not found!");
                            RotateImageIfNeeded(file, outputPath);
                        }


                        //foreach (var dir in dirs)
                        //{

                        //    {

                        //    }
                        //}

                        //foreach (var tag in dir.Tags)

                        //    Console.WriteLine($"{dir.Name} - {tag.TagName} = {tag.Description}");
                        
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("UNABLE TO READ");
//.........这里部分代码省略.........
开发者ID:frankcalise,项目名称:csharp-image-rotation,代码行数:101,代码来源:Program.cs

示例2: GetMenu

        /// <summary>
        /// Gets this weeks menu from admin.ordbogen.com
        /// </summary>
        public void GetMenu()
        {
            DateTimeFormatInfo dfi = DateTimeFormatInfo.CurrentInfo;
            Calendar cal = dfi.Calendar;
            int weekNo = cal.GetWeekOfYear(DateTime.Now, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);

            string path = "https://admin.ordbogen.com";
            string pathMenu = "https://admin.ordbogen.com";
            string localPdfFile = Controller.dir + "menu.pdf";
            string thisWeeksMenu = Controller.dir + "menu " + weekNo + ".png";
            string LastWeekMenu = Controller.dir + "menu " + (weekNo - 1) + ".png";

            if (File.Exists(LastWeekMenu) || !File.Exists(thisWeeksMenu))
            {
                try
                {
                    HtmlDocument doc = new HtmlWeb().Load(path, "GET", new System.Net.WebProxy(), new System.Net.NetworkCredential(Authentication.getAuthorization(true), Authentication.getAuthorization(false)));

                    if (doc != null)
                    {
                        var aNodes = doc.DocumentNode.SelectNodes("//a");
                        var element = new HtmlNode(HtmlNodeType.Element, doc, 0);

                        foreach (var item in aNodes)
                        {
                            if (item.InnerText.ToLower().Contains("menu"))
                                if (item.InnerText.Contains(weekNo.ToString()))
                                    element = item;
                        }

                        var attributes = element.Attributes;
                        foreach (var item in attributes)
                        {
                            if (item.Name == "href")
                            {
                                pathMenu += item.Value;
                            }
                        }

                        WebClient wb = new WebClient();
                        wb.Credentials = new NetworkCredential(Authentication.getAuthorization(true), Authentication.getAuthorization(false));
                        wb.DownloadFile(pathMenu, localPdfFile);

                        PdfDocument pdf = new PdfDocument();
                        pdf.LoadFromFile(localPdfFile);
                        Image bmp = pdf.SaveAsImage(0);
                        bmp.Save(thisWeeksMenu, ImageFormat.Png);

                        File.Delete(localPdfFile);
                        if (File.Exists(LastWeekMenu))
                            File.Delete(LastWeekMenu);
                    }
                    else
                    {
                        if (CheckInternetConnection())
                        {
                            System.Windows.MessageBox.Show(@"Der kan være opstået et problem med brugeren til i AdminUsers.txt. Opdater den her: C:\Users\[User]\(SKJULT)AppData\Local\ATIS", "MainStorage Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error);
                        }
                    }
                }
                catch (Exception e) { System.Windows.MessageBox.Show(e.Message); }
            }
        }
开发者ID:Ravnii,项目名称:ATISv2,代码行数:66,代码来源:MainStorageController.cs


注:本文中的PdfDocument.SaveAsImage方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。