internal static void Run() { // Create a PdfRasterizer object PdfRasterizer rasterizer = new PdfRasterizer(Util.GetResourcePath("DocumentA.pdf")); // Create a color array using custom colors Color[] color = new Color[] { Color.FromArgb(255, 0, 0, 0), Color.FromArgb(255, 255, 255, 255), Color.FromArgb(255, 255, 0, 0), Color.FromArgb(255, 0, 255, 0), Color.FromArgb(255, 0, 0, 255), Color.FromArgb(255, 255, 255, 0), Color.FromArgb(255, 0, 255, 255), Color.FromArgb(255, 255, 0, 255) }; // Create a UserPalette object using color array UserPalette userPalette = new UserPalette(color); // Create a PngIndexedColorFormat object using the palette PngIndexedColorFormat pngIndexedColorFormat = new PngIndexedColorFormat(userPalette, 100, DitheringAlgorithm.FloydSteinberg); // Create a PngImageFormat object using indexed color format object PngImageFormat pngImageFormat = new PngImageFormat(pngIndexedColorFormat); // Create a image size tha is a fixed size FixedImageSize fixedImageSize = new FixedImageSize(595, 841); // Save the image rasterizer.Draw("PngImageWithUserPalette.png", pngImageFormat, fixedImageSize); }
internal static void Run() { // Create a PdfRasterizer object PdfRasterizer rasterizer = new PdfRasterizer(Util.GetResourcePath("DocumentA.pdf")); // Create a image size tha is a fixed size FixedImageSize fixedImageSize = new FixedImageSize(595, 841); // Create a PngImageFormat object with RGB color PngImageFormat pngImageFormat = new PngImageFormat(PngColorFormat.Rgb); // Save the image rasterizer.Draw("PngImageWithRgbColor.png", pngImageFormat, fixedImageSize); }
public static void ExportPdf(string sourceFilePath, string destinationPngPath, string destinationThumbsPath) { var rasterizer = new PdfRasterizer(sourceFilePath); // Save the image. var pngImageFormat = new PngImageFormat(PngColorFormat.Rgb); var imageSizeFormat = new PercentageImageSize(100); var thumbsSizeFormat = new PercentageImageSize(10); for (var i = 0; i < rasterizer.Pages.Count; i++) { rasterizer.Pages[i].Draw(Path.Combine(destinationPngPath, "Page" + (i + 1) + ".png"), pngImageFormat, imageSizeFormat); rasterizer.Pages[i].Draw(Path.Combine(destinationThumbsPath, "Page" + (i + 1) + ".png"), pngImageFormat, thumbsSizeFormat); } rasterizer.Dispose(); }
public HttpResponseMessage Load(Uri uri, int pageNumber = 1, int resolution = 0, string mimeType = null, int bitsPerPixel = 0, int qualityFactor = 0, int imageWidth = 0, int imageHeight = 0) { if (uri == null) { throw new ArgumentNullException("uri"); } if (pageNumber < 0) { throw new ArgumentOutOfRangeException("pageNumber", "must be a value greater than or equal to 0"); } var page = pageNumber; // Default is page 1 if (page == 0) { page = 1; } if (resolution < 0) { throw new ArgumentOutOfRangeException("resolution", "must be a value greater than or equals to 0"); } // Sanity check on other parameters if (qualityFactor < 0 || qualityFactor > 100) { throw new ArgumentOutOfRangeException("qualityFactor", "must be a value between 0 and 100"); } if (imageWidth < 0) { throw new ArgumentOutOfRangeException("width", "must be a value greater than or equal to 0"); } if (imageHeight < 0) { throw new ArgumentOutOfRangeException("height", "must be a value greater than or equal to 0"); } // Get the image format SaveImageFormat saveFormat = SaveImageFormat.GetFromMimeType(mimeType); // Use a temp file, much faster than calling Load/Info from a URI directly // In a production service, you might want to create a caching mechanism string tempFile = Path.GetTempFileName(); try { // Force the uri to be fully qualified, reject everything else for security reasons if (uri.IsFile || uri.IsUnc) { throw new ArgumentException("URL cannot be local file or UNC path."); } //Download the file try { using (WebClient client = new WebClient()) { client.DownloadFile(uri, tempFile); // exception occurs when file is used by another process.(getting error code=>500(internal server error)) } using (RasterCodecs codecs = new RasterCodecs()) { ServiceHelper.InitCodecs(codecs, resolution); // Load the page using (RasterImage image = codecs.Load(tempFile, 0, CodecsLoadByteOrder.BgrOrGray, pageNumber, pageNumber)) { // Resize it (will only resize if both width and height are not 0), will also take care of FAX images (with different resolution) ImageResizer.ResizeImage(image, imageWidth, imageHeight); // We need to find out the format, bits/pixel and quality factor // If the user gave as a format, use it if (saveFormat == null) { // If the user did not give us a format, use PNG saveFormat = new PngImageFormat(); mimeType = "image/png"; } saveFormat.PrepareToSave(codecs, image, bitsPerPixel, qualityFactor); // Save it to a memory stream MemoryStream ms = null; HttpResponseMessage response = null; try { ms = new MemoryStream(); codecs.Save(image, ms, saveFormat.ImageFormat, saveFormat.BitsPerPixel); ms.Position = 0; // Set the MIME type and Content-Type if there is a valid web context HttpContext currentContext = HttpContext.Current; if (currentContext != null) { currentContext.Response.ContentType = mimeType; currentContext.Response.Headers.Add("ContentLength", ms.Length.ToString()); } // If we just return the stream, Web Api will try to serialize it. // If the return type is "HttpResponseMessage" it will not serialize // and you can set the content as you wish. response = new HttpResponseMessage(); response.Content = new StreamContent(ms); return(response); } catch { if (ms != null) { ms.Dispose(); } if (response != null) { response.Dispose(); } throw; } } } } catch (Exception) { return(new HttpResponseMessage()); } } catch (Exception ex) { Log(string.Format("Load - Error:{1}{0}TempFile:{2}{0}Uri:{3}, PageNumber:{4}", Environment.NewLine, ex.Message, tempFile, uri, pageNumber)); throw; } finally { if (File.Exists(tempFile)) { try { File.Delete(tempFile); } catch { } } } }