public static bool ConvertPdfToFaxTiff(string sourceFile, string outputFile, float dpi, bool shrinkToLetter, string pdfPassword) { bool output = false; const long Compression = (long)EncoderValue.CompressionCCITT4; using (MuPDF pdfDoc = new MuPDF(sourceFile, pdfPassword)) { using (FileStream outputStream = new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { ImageCodecInfo info = null; foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders()) if (ice.MimeType == "image/tiff") info = ice; Bitmap saveTif = null; pdfDoc.AntiAlias = false; for (int i = 1; i <= pdfDoc.PageCount; i++) { int Width = 0;//Zero for no resize. //int Height = 0;//Zero for autofit height to width. float DpiX = dpi; float DpiY = dpi; pdfDoc.Page = i; if (dpi == 200) { Width = 1728; DpiX = 204; DpiY = 196; } else if (dpi == 300) Width = 2592; else if (dpi == 400) Width = 3456; Bitmap FirstImage = pdfDoc.GetBitmap(Width, 0, DpiX, DpiY, 0, RenderType.Monochrome, true, shrinkToLetter, 0); if (FirstImage == null) throw new Exception("Unable to convert pdf to tiff!"); using (EncoderParameters ep = new EncoderParameters(2)) { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, Compression); if (i == 1) { saveTif = FirstImage; saveTif.Save(outputStream, info, ep); } else { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); saveTif.SaveAdd(FirstImage, ep); FirstImage.Dispose(); } if (i == pdfDoc.PageCount) { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush); saveTif.SaveAdd(ep); saveTif.Dispose(); } } } } if (File.Exists(outputFile)) output = true; } return output; }
public static bool ConvertPdfToTiff(string sourceFile, string outputFile, float dpi, RenderType type, bool rotateLandscapePages, bool shrinkToLetter, int maxSizeInPdfPixels, string pdfPassword) { bool output = false; if (string.IsNullOrEmpty(sourceFile) || string.IsNullOrEmpty(outputFile)) throw new ArgumentNullException(); using (MuPDF pdfDoc = new MuPDF(sourceFile, pdfPassword)) { using (FileStream outputStream = new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { ImageCodecInfo info = null; foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders()) if (ice.MimeType == "image/tiff") info = ice; Bitmap saveTif = null; pdfDoc.AntiAlias = false; for (int i = 1; i <= pdfDoc.PageCount; i++) { pdfDoc.Page = i; Bitmap FirstImage = pdfDoc.GetBitmap(0, 0, dpi, dpi, 0, type, rotateLandscapePages, shrinkToLetter, maxSizeInPdfPixels); if (FirstImage == null) throw new Exception("Unable to convert pdf to tiff!"); using (EncoderParameters ep = new EncoderParameters(2)) { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW); if (type == RenderType.Monochrome) { ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4); } if (i == 1) { saveTif = FirstImage; saveTif.Save(outputStream, info, ep); } else { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); saveTif.SaveAdd(FirstImage, ep); FirstImage.Dispose(); } if (i == pdfDoc.PageCount) { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush); saveTif.SaveAdd(ep); saveTif.Dispose(); } } } } if (File.Exists(outputFile)) output = true; } return output; }
public static IDictionary <int, byte[]> ConvertPdfToPng(byte[] pdfBytes, RenderType type, int pageNumber, bool antiAlias = false, float dpi = 150, Size size = new Size(), string password = "") { if (pdfBytes == null || pdfBytes.Length.Equals(0)) { throw new ArgumentNullException(nameof(pdfBytes)); } var output = new ConcurrentDictionary <int, byte[]>(); var pageStart = pageNumber; var pageEnd = pageNumber + 1; using (MuPDF pdfDoc = new MuPDF(pdfBytes, password)) { pdfDoc.Page = pageNumber; pdfDoc.AntiAlias = antiAlias && !type.Equals(RenderType.Monochrome); // no point in anti-alias-ing with Monochrome using (MemoryStream outputStream = new MemoryStream()) { var width = 0; var height = 0; var maxSize = 1000; using (var bitmap = ResizeImage(size, pdfDoc.GetBitmap(width, height, dpi, dpi, 0, type, false, false, maxSize))) { bitmap.Save(outputStream, ImageFormat.Png); output.TryAdd(pageNumber, outputStream.ToArray()); } } } return(output.ToDictionary(kvp => kvp.Key, kvp => kvp.Value)); }
/// <summary> /// Ctor. /// </summary> /// <param name="path">The path to the document.</param> /// <param name="renderWidth">The width of the rendered pages. Must be > 0.</param> /// <param name="renderDPI">The dpi to use when rendering the pages. Must be > 0.</param> /// <param name="password">The password (null if none).</param> /// <exception cref="Pdf2KTException">If the document cannot be opened.</exception> public PDFDocument(string path, int renderWidth = 800, int renderDPI = 96, string password = null) { RenderWidth = renderWidth; RenderDPI = renderDPI; _path = path; try { _mupdf = new MuPDF(path, password); _mupdf.AntiAlias = true; } catch (Exception e) { throw new Pdf2KTException("Error while opening PDF document.", e); } }
public static bool ConvertPdfToTiff(string sourceFile, string outputFile, float dpi, RenderType type, bool rotateLandscapePages, bool shrinkToLetter, int maxSizeInPdfPixels, string pdfPassword) { bool output = false; if (string.IsNullOrEmpty(sourceFile) || string.IsNullOrEmpty(outputFile)) { throw new ArgumentNullException(); } using (MuPDF pdfDoc = new MuPDF(sourceFile, pdfPassword)) { using (FileStream outputStream = new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { ImageCodecInfo info = null; foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders()) { if (ice.MimeType == "image/tiff") { info = ice; } } Bitmap saveTif = null; pdfDoc.AntiAlias = false; for (int i = 1; i <= pdfDoc.PageCount; i++) { pdfDoc.Page = i; Bitmap FirstImage = pdfDoc.GetBitmap(0, 0, dpi, dpi, 0, type, rotateLandscapePages, shrinkToLetter, maxSizeInPdfPixels); if (FirstImage == null) { throw new Exception("Unable to convert pdf to tiff!"); } using (EncoderParameters ep = new EncoderParameters(2)) { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW); if (type == RenderType.Monochrome) { ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4); } if (i == 1) { saveTif = FirstImage; saveTif.Save(outputStream, info, ep); } else { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); saveTif.SaveAdd(FirstImage, ep); FirstImage.Dispose(); } if (i == pdfDoc.PageCount) { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush); saveTif.SaveAdd(ep); saveTif.Dispose(); } } } } if (File.Exists(outputFile)) { output = true; } } return(output); }
public static bool ConvertPdfToFaxTiff(string sourceFile, string outputFile, float dpi, bool shrinkToLetter, string pdfPassword) { bool output = false; const long Compression = (long)EncoderValue.CompressionCCITT4; using (MuPDF pdfDoc = new MuPDF(sourceFile, pdfPassword)) { using (FileStream outputStream = new FileStream(outputFile, FileMode.Create, FileAccess.ReadWrite, FileShare.None)) { ImageCodecInfo info = null; foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders()) { if (ice.MimeType == "image/tiff") { info = ice; } } Bitmap saveTif = null; pdfDoc.AntiAlias = false; for (int i = 1; i <= pdfDoc.PageCount; i++) { int Width = 0;//Zero for no resize. //int Height = 0;//Zero for autofit height to width. float DpiX = dpi; float DpiY = dpi; pdfDoc.Page = i; if (dpi == 200) { Width = 1728; DpiX = 204; DpiY = 196; } else if (dpi == 300) { Width = 2592; } else if (dpi == 400) { Width = 3456; } Bitmap FirstImage = pdfDoc.GetBitmap(Width, 0, DpiX, DpiY, 0, RenderType.Monochrome, true, shrinkToLetter, 0); if (FirstImage == null) { throw new Exception("Unable to convert pdf to tiff!"); } using (EncoderParameters ep = new EncoderParameters(2)) { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, Compression); if (i == 1) { saveTif = FirstImage; saveTif.Save(outputStream, info, ep); } else { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); saveTif.SaveAdd(FirstImage, ep); FirstImage.Dispose(); } if (i == pdfDoc.PageCount) { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush); saveTif.SaveAdd(ep); saveTif.Dispose(); } } } } if (File.Exists(outputFile)) { output = true; } } return(output); }
public static byte[] ConvertPdfToTiff(byte[] image, float dpi, RenderType type, bool rotateLandscapePages, bool shrinkToLetter, int maxSizeInPdfPixels, string pdfPassword) { byte[] output = null; if (image == null) { throw new ArgumentNullException("image"); } using (MuPDF pdfDoc = new MuPDF(image, pdfPassword)) { using (MemoryStream outputStream = new MemoryStream()) { ImageCodecInfo info = null; foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders()) { if (ice.MimeType == "image/tiff") { info = ice; } } Bitmap saveTif = null; pdfDoc.AntiAlias = false; for (int i = 1; i <= pdfDoc.PageCount; i++) { int Width = 0; //Zero for no resize. int Height = 0; //Zero for autofit height to width. pdfDoc.Page = i; Bitmap FirstImage = pdfDoc.GetBitmap(Width, Height, dpi, dpi, 0, type, rotateLandscapePages, shrinkToLetter, maxSizeInPdfPixels); if (FirstImage == null) { throw new Exception("Unable to convert pdf to tiff!"); } using (EncoderParameters ep = new EncoderParameters(2)) { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW); if (type == RenderType.Monochrome) { ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4); } if (i == 1) { saveTif = FirstImage; saveTif.Save(outputStream, info, ep); } else { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); saveTif.SaveAdd(FirstImage, ep); FirstImage.Dispose(); } if (i == pdfDoc.PageCount) { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush); saveTif.SaveAdd(ep); saveTif.Dispose(); } } } output = outputStream.ToArray(); } } return(output); }
public static byte[] ConvertPdfToTiff(byte[] image, float dpi, RenderType type, bool rotateLandscapePages, bool shrinkToLetter, int maxSizeInPdfPixels, string pdfPassword) { byte[] output = null; if (image == null) throw new ArgumentNullException("image"); using (MuPDF pdfDoc = new MuPDF(image, pdfPassword)) { using (MemoryStream outputStream = new MemoryStream()) { ImageCodecInfo info = null; foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders()) if (ice.MimeType == "image/tiff") info = ice; Bitmap saveTif = null; pdfDoc.AntiAlias = false; for (int i = 1; i <= pdfDoc.PageCount; i++) { int Width = 0;//Zero for no resize. int Height = 0;//Zero for autofit height to width. pdfDoc.Page = i; Bitmap FirstImage = pdfDoc.GetBitmap(Width, Height, dpi, dpi, 0, type, rotateLandscapePages, shrinkToLetter, maxSizeInPdfPixels); if (FirstImage == null) throw new Exception("Unable to convert pdf to tiff!"); using (EncoderParameters ep = new EncoderParameters(2)) { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.MultiFrame); ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW); if (type == RenderType.Monochrome) { ep.Param[1] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionCCITT4); } if (i == 1) { saveTif = FirstImage; saveTif.Save(outputStream, info, ep); } else { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage); saveTif.SaveAdd(FirstImage, ep); FirstImage.Dispose(); } if (i == pdfDoc.PageCount) { ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)EncoderValue.Flush); saveTif.SaveAdd(ep); saveTif.Dispose(); } } } output = outputStream.ToArray(); } } return output; }