/// <summary> /// Generate a thumbnail from frontpage of a PDF document. /// </summary> /// <param name="document"></param> /// <param name="documentfile"></param> /// <param name="url"></param> /// <returns></returns> private string GenerateThumbnail(Document document, HttpPostedFileBase documentfile, string url) { if (document.documentUrl.Contains(".pdf")) { string filtype; string seofilename = MakeSeoFriendlyDocumentName(documentfile, out filtype, out seofilename); string documentNameSeo = RegisterUrls.MakeSeoFriendlyString(document.name); string input = Path.Combine(Server.MapPath(Constants.DataDirectory + Document.DataDirectory), document.register.seoname + "_" + documentNameSeo + "_v" + document.versionNumber + "_" + seofilename + "." + filtype); string output = Path.Combine(Server.MapPath(Constants.DataDirectory + Document.DataDirectory), document.register.seoname + "_thumbnail_" + documentNameSeo + "_v" + document.versionNumber + "_" + seofilename + ".jpg"); GhostscriptWrapper.GenerateOutput(input, output, GsSettings()); ImageResizer.ImageJob newImage = new ImageResizer.ImageJob(output, output, new ImageResizer.Instructions("maxwidth=160;maxheight=300;quality=75")); newImage.Build(); return(url + document.register.seoname + "_thumbnail_" + documentNameSeo + "_v" + document.versionNumber + "_" + seofilename + ".jpg"); } else if (document.documentUrl.Contains(".xsd")) { return("/Content/xsd.svg"); } else { return("/Content/pdf.jpg"); } }
private void ButtonGenerate_Click(object sender, EventArgs e) { try { string inputPath = this.textBoxInputPath.Text; string outputPath = this.textBoxOutputPath.Text; int page = (int)this.numericUpDownPage.Value; int resolution = (int)this.numericUpDownResolution.Value; GhostscriptDevices device = (GhostscriptDevices)this.comboBoxDevice.SelectedItem; GhostscriptPageSizes pageSize = (GhostscriptPageSizes)this.comboBoxPageSize.SelectedItem; GhostscriptSettings ghostscriptSettings = new GhostscriptSettings { Page = new GhostscriptPages { Start = page, End = page }, Device = device, Resolution = new Size(resolution, resolution), Size = new GhostscriptPageSize { Native = pageSize } }; GhostscriptWrapper.GenerateOutput(inputPath, outputPath, ghostscriptSettings); Process.Start(outputPath); } catch (Exception exception) { MessageBox.Show(exception.ToString()); } }
public void Verify(FileInfo pdf) { if (!pdf.Exists) { throw new InvalidOperationException("File to verify does not exist '{0}'".Fmt(pdf.FullName)); } using (var destination = new DisposableFile()) { GhostscriptWrapper.GenerateOutput( inputPath: pdf.FullName, outputPath: destination.File.FullName, settings: new GhostscriptSettings { Device = GhostscriptDevices.tiff24nc, Resolution = new Size(72, 72), Size = new GhostscriptPageSize { Native = GhostscriptPageSizes.letter } }); //GhostScript embeds a creation timestamp on each page of the tiff. Obviously if its different every time the files won't match up byte for byte. So normalize using (var tiff = Tiff.Open(destination.File.FullName, "a")) { Enumerable.Range(0, tiff.NumberOfDirectories()).ForEach(i => { tiff.SetDirectory((short)i); tiff.SetField(TiffTag.DATETIME, Encoding.UTF8.GetBytes("2010:01:01 12:00:00")); //any constant date will do the trick here. tiff.CheckpointDirectory(); }); } Approvals.Verify(GetTiffApprovalWriter(destination.File), GetNamer(), GetReporter()); } }
public static void WriteOut(int pJpegWidth = 0, int[] pages = null) { var OutputPath = AppDomain.CurrentDomain.BaseDirectory; var sourceFile = "test.pdf"; var jpegFile = Path.ChangeExtension(sourceFile, "jpg"); var settings = new GhostscriptSettings { Page = { AllPages = false, Start = 1, End = 1 }, Size = { Native = GhostscriptPageSizes.a2 }, Device = GhostscriptDevices.png16m, Resolution = new Size(150, 122) }; if (pages.Length > 1) { // PDF contains multiple pages, make each one into a jpg var iPageNumber = 0; foreach (var page in pages) { //FORMAT: GhostscriptWrapper.GenerateOutput(<pdf file path>, <destination jpg path>, <settings>); GhostscriptWrapper.GenerateOutput(OutputPath + sourceFile, OutputPath + jpegFile.Replace(".jpg", "_" + iPageNumber + ".jpg"), settings); // add page number into jpg string iPageNumber++; settings.Page.Start = settings.Page.End = iPageNumber + 1; } } else { //FORMAT: GhostscriptWrapper.GenerateOutput(<pdf file path>, <destination jpg path>, <settings>); GhostscriptWrapper.GenerateOutput(OutputPath + sourceFile, OutputPath + jpegFile, settings); } }
public static void GetPdfThumbnail(string sourcePdfFilePath, string destinationPngFilePath, int StartPage = 1, int EndPage = 1, GhostscriptPageSizes PageSize = GhostscriptPageSizes.a4) { // Use GhostscriptSharp to convert the pdf to a png GhostscriptWrapper.GenerateOutput(sourcePdfFilePath, destinationPngFilePath, new GhostscriptSettings { Device = GhostscriptDevices.pngalpha, Page = new GhostscriptPages { // Only make a thumbnail of the first page Start = StartPage, End = EndPage, AllPages = false }, Resolution = new Size { // Render at 72x72 dpi Height = 300, Width = 300 }, Size = new GhostscriptPageSize { // The dimensions of the incoming PDF must be // specified. The example PDF is US Letter sized. Native = PageSize } } ); }
public GhostscriptSession GenerateForTesseract(string inputPath) { CreateEnvironment(); var settings = GhostscriptSettingsFactory.Tesseract(); GhostscriptWrapper.GenerateOutput(inputPath, OutputPattern + settings.GetDeviceExtension(), settings); return(this); }
public void GenerateSinglePageOutput() { var settings = new GhostscriptSettings { Page = { AllPages = false, Start = 1, End = 1 }, Size = { Native = GhostscriptPageSizes.a2 }, Device = GhostscriptDevices.png16m, Resolution = new Size(150, 122) }; GhostscriptWrapper.GenerateOutput(OutputPath + TEST_FILE_LOCATION, OutputPath + SINGLE_FILE_LOCATION, settings); Assert.IsTrue(File.Exists(OutputPath + SINGLE_FILE_LOCATION)); }
//用來轉換PDF成一張一張的jpeg圖片 public void f_ConvertAllPDF(string pdfPath, string savePath) { GhostscriptSettings ghostscriptSettings = new GhostscriptSettings(); ghostscriptSettings.Device = GhostscriptSharp.Settings.GhostscriptDevices.jpeg; //圖片類型 ghostscriptSettings.Size.Native = GhostscriptSharp.Settings.GhostscriptPageSizes.legal; //legal為原尺寸 //ghostscriptSettings.Size.Manual = new System.Drawing.Size(2552, 3579); //此為設定固定尺寸 ghostscriptSettings.Resolution = new System.Drawing.Size(150, 150); //此為解析度 ghostscriptSettings.Page.AllPages = true; GhostscriptWrapper.GenerateOutput(pdfPath, savePath, ghostscriptSettings); Debug.Log("Complete!"); }
public GhostscriptSession GenerateOutput(IPdfContent pdfInfo) { CreateEnvironment(); var inputFilename = Id + ".pdf"; var inputPath = Path.Combine(Folder, inputFilename); pdfInfo.WriteAllBytes(inputPath); var settings = GhostscriptSettingsFactory.Default(); GhostscriptWrapper.GenerateOutput(inputPath, OutputPattern + settings.GetDeviceExtension(), settings); return(this); }
private static void PdfToJpg(string path, string fileName, GhostscriptDevices devise, GhostscriptPageSizes pageFormat, int qualityX, int qualityY) { var settingsForConvert = new GhostscriptSettings { Device = devise }; var pageSize = new GhostscriptPageSize { Native = pageFormat }; settingsForConvert.Size = pageSize; settingsForConvert.Resolution = new System.Drawing.Size(qualityX, qualityY); GhostscriptWrapper.GenerateOutput(path, @"C:\YR\Receipt\" + fileName + "_" + ".jpg", settingsForConvert); // here you could set path and name for out put file. }
public void ConvertToTif() { var destination = new FileInfo(Path.GetTempPath() + "test_ConvertToTif.tif"); GhostscriptWrapper.GenerateOutput( inputPath: TEST_FILE_LOCATION, outputPath: destination.FullName, settings: new GhostscriptSettings { Device = GhostscriptDevices.tiffg4, Resolution = new Size(400, 400), Page = GhostscriptPages.All, Size = new GhostscriptPageSize { Native = GhostscriptPageSizes.a4 }, }); Approvals.VerifyFile(destination.FullName); }
//Pdfから画像を取得する private string getImageFromPdf(string baseFilePass, string pdfFielName) { //拡張子無しでファイル名を取得する string imgFileName = Path.GetFileNameWithoutExtension(pdfFielName) + ".png"; try { //Pdfから画像を取得 GhostscriptSettings settings = new GhostscriptSettings(); settings.Page.AllPages = true; //全ページ出力 settings.Page.Start = 1; //出力開始ページ settings.Page.End = 1; //出力終了ページ settings.Size.Native = GhostscriptSharp.Settings.GhostscriptPageSizes.a0; //出力サイズ指定 settings.Device = GhostscriptSharp.Settings.GhostscriptDevices.png256; //出力ファイルフォーマット指定(pngで指定) settings.Resolution = new Size(600, 600); //出力Dpi string inputPath = baseFilePass + "\\" + pdfFielName; string outputPath = baseFilePass + "\\" + imgFileName; GhostscriptWrapper.GenerateOutput(inputPath, outputPath, settings); // Create the initial thumbnail //ファイルが生成されない場合はパスを返さない if (!System.IO.File.Exists(outputPath)) { imgFileName = ""; outLogMsg(pdfFielName, "Pdf埋め込み画像なし"); } else { outLogMsg(pdfFielName, "Pdf画像変換完了"); } } catch (Exception exp) { imgFileName = ""; outLogMsg(pdfFielName, "Pdf画像取得実施エラー : " + exp.Message); } return(imgFileName); }
/* private int Decoder(string SourcePdfFileName,string DestPdfFileName, string Contact) * { * try * { * * PdfReader pdfReader = new PdfReader(SourcePdfFileName); * * FileStream signedPdfs = new FileStream(DestPdfFileName, FileMode.Create);//the output pdf file * PdfStamper pdfStamper = new PdfStamper(pdfReader, signedPdfs, '\0'); * PdfContentByte pdfData = pdfStamper.GetOverContent(1); * //Create the QR Code/2D Code------------------------------- * Image qrImage = GenerateQRCode(Contact); * iTextSharp.text.Image itsQrCodeImage = iTextSharp.text.Image.GetInstance(qrImage, System.Drawing.Imaging.ImageFormat.Jpeg); * itsQrCodeImage.SetAbsolutePosition(270, 50); * pdfData.AddImage(itsQrCodeImage); * pdfStamper.Close(); * return 1; * } * catch(Exception e) * { * ViewBag.exception = e.Message; * return 0; * } * }*/ private int convertToImage(string sourcePdfFilePath, string destinationPngFilePath) { try { // Use GhostscriptSharp to convert the pdf to a png GhostscriptWrapper.GenerateOutput(sourcePdfFilePath, destinationPngFilePath, new GhostscriptSettings { Device = GhostscriptDevices.pngalpha, Page = new GhostscriptPages { // Only make a thumbnail of the first page Start = 1, End = 1, AllPages = false }, Resolution = new Size { // Render at 72x72 dpi Height = 72, Width = 72 }, Size = new GhostscriptPageSize { // The dimentions of the incoming PDF must be // specified. The example PDF is US Letter sized. Native = GhostscriptPageSizes.a4 } } ); return(1); } catch (Exception e) { ViewBag.exception = e.Message; return(0); } }