|
| 1 | +// |
| 2 | +// RasterizerSample.cs |
| 3 | +// This file is part of Ghostscript.NET.Samples project |
| 4 | +// |
| 5 | +// Author: Josip Habjan (habjan@gmail.com, http://www.linkedin.com/in/habjan) |
| 6 | +// Copyright (c) 2013-2015 by Josip Habjan. All rights reserved. |
| 7 | +// |
| 8 | +// Permission is hereby granted, free of charge, to any person obtaining |
| 9 | +// a copy of this software and associated documentation files (the |
| 10 | +// "Software"), to deal in the Software without restriction, including |
| 11 | +// without limitation the rights to use, copy, modify, merge, publish, |
| 12 | +// distribute, sublicense, and/or sell copies of the Software, and to |
| 13 | +// permit persons to whom the Software is furnished to do so, subject to |
| 14 | +// the following conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be |
| 17 | +// included in all copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 20 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 21 | +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 22 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 23 | +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 24 | +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 25 | +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 26 | + |
| 27 | +using System; |
| 28 | +using System.Drawing; |
| 29 | +using System.Drawing.Imaging; |
| 30 | +using System.IO; |
| 31 | + |
| 32 | +// required Ghostscript.NET namespaces |
| 33 | +using Ghostscript.NET; |
| 34 | +using Ghostscript.NET.Rasterizer; |
| 35 | + |
| 36 | +namespace Ghostscript.NET.Samples |
| 37 | +{ |
| 38 | + |
| 39 | + /// <summary> |
| 40 | + /// GhostscriptRasterizer allows you to rasterize pdf and postscript files into the |
| 41 | + /// memory. If you want Ghostscript to store files on the disk use GhostscriptProcessor |
| 42 | + /// or one of the GhostscriptDevices (GhostscriptPngDevice, GhostscriptJpgDevice). |
| 43 | + /// </summary> |
| 44 | + public class RasterizerCropSample : ISample |
| 45 | + { |
| 46 | + public void Start() |
| 47 | + { |
| 48 | + int desired_x_dpi = 300; |
| 49 | + int desired_y_dpi = 300; |
| 50 | + |
| 51 | + string inputPdfPath = @"E:\__test_data\test2.pdf"; |
| 52 | + string outputPath = @"E:\__test_data\output\"; |
| 53 | + |
| 54 | + using (GhostscriptRasterizer rasterizer = new GhostscriptRasterizer()) |
| 55 | + { |
| 56 | + rasterizer.CustomSwitches.Add("-dUseCropBox"); |
| 57 | + rasterizer.CustomSwitches.Add("-c"); |
| 58 | + rasterizer.CustomSwitches.Add("[/CropBox [24 72 559 794] /PAGES pdfmark"); |
| 59 | + rasterizer.CustomSwitches.Add("-f"); |
| 60 | + |
| 61 | + rasterizer.Open(inputPdfPath); |
| 62 | + |
| 63 | + for (int pageNumber = 1; pageNumber <= rasterizer.PageCount; pageNumber++) |
| 64 | + { |
| 65 | + string pageFilePath = Path.Combine(outputPath, "Page-" + pageNumber.ToString() + ".png"); |
| 66 | + |
| 67 | + Image img = rasterizer.GetPage(desired_x_dpi, desired_y_dpi, pageNumber); |
| 68 | + img.Save(pageFilePath, ImageFormat.Png); |
| 69 | + |
| 70 | + Console.WriteLine(pageFilePath); |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
0 commit comments