/* ----------------------------------------------------------------- */ /// /// ConfigImageOperations /// /// <summary> /// 画像に関わるオプションを設定します。 /// </summary> /// /* ----------------------------------------------------------------- */ public void ConfigImageOperations(UserSetting setting, Ghostscript.Converter gs) { // 解像度 var resolution = Parameter.ResolutionValue(setting.Resolution); gs.AddOption("ColorImageResolution", resolution); gs.AddOption("GrayImageResolution", resolution); gs.AddOption("MonoImageResolution", (resolution < 300) ? 300 : 1200); // 画像圧縮 gs.AddOption("AutoFilterColorImages", false); gs.AddOption("AutoFilterGrayImages", false); gs.AddOption("AutoFilterMonoImages", false); gs.AddOption("ColorImageFilter", "/" + setting.ImageFilter.ToString()); gs.AddOption("GrayImageFilter", "/" + setting.ImageFilter.ToString()); gs.AddOption("MonoImageFilter", "/" + setting.ImageFilter.ToString()); // ダウンサンプリング if (setting.DownSampling == Parameter.DownSamplings.None) { gs.AddOption("DownsampleColorImages", false); gs.AddOption("DownsampleGrayImages", false); gs.AddOption("DownsampleMonoImages", false); } else { gs.AddOption("DownsampleColorImages", true); gs.AddOption("DownsampleGrayImages", true); gs.AddOption("DownsampleMonoImages", true); gs.AddOption("ColorImageDownsampleType", "/" + setting.DownSampling.ToString()); gs.AddOption("GrayImageDownsampleType", "/" + setting.DownSampling.ToString()); gs.AddOption("MonoImageDownsampleType", "/" + setting.DownSampling.ToString()); } }
/* ----------------------------------------------------------------- */ /// /// ResolutionString /// /// <summary> /// Resolutions の各値に対応する文字列を取得します。 /// </summary> /// /* ----------------------------------------------------------------- */ public static string ResolutionString(Parameter.Resolutions id) { return(Parameter.ResolutionValue(id).ToString()); }
/* ----------------------------------------------------------------- */ /// /// Run /// /// <summary> /// ファイル変換処理を実行します。 /// </summary> /// /// <remarks> /// 文書プロパティ、パスワード関連とファイル結合は iTextSharp /// を利用します。出力パスに指定されたファイルが既に存在する場合、 /// ExistedFile プロパティの指定(上書き、先頭に結合、末尾に結合、 /// リネーム)に従います。 /// </remarks> /// /* ----------------------------------------------------------------- */ public bool Run(UserSetting setting) { // Ghostscript に指定するパスに日本語が入るとエラーが発生する // 場合があるので,作業ディレクトリを変更する. this.CreateWorkingDirectory(setting); Ghostscript.Converter gs = new Ghostscript.Converter(_messages); gs.Device = Parameter.Device(setting.FileType, setting.Grayscale); bool status = true; try { gs.AddInclude(setting.LibPath + @"\lib"); gs.PageRotation = setting.PageRotation; gs.Resolution = Parameter.ResolutionValue(setting.Resolution); this.ConfigImageOperations(setting, gs); if (Parameter.IsImageType(setting.FileType)) { this.ConfigImage(setting, gs); } else { this.ConfigDocument(setting, gs); } this.EscapeExistedFile(setting); gs.AddSource(setting.InputPath); gs.Destination = setting.OutputPath; gs.Run(); if (setting.FileType == Parameter.FileTypes.PDF) { PdfModifier modifier = new PdfModifier(_escaped, _messages); status = modifier.Run(setting); _messages.Add(new Message(Message.Levels.Info, String.Format("CubePdf.PDFModifier.Run: {0}", status.ToString()))); } if (status) { PostProcess postproc = new PostProcess(_messages); status = postproc.Run(setting); _messages.Add(new Message(Message.Levels.Info, String.Format("CubePdf.PostProcess.Run: {0}", status.ToString()))); } } catch (Exception err) { _messages.Add(new Message(Message.Levels.Error, err)); _messages.Add(new Message(Message.Levels.Debug, err)); status = false; } finally { if (Directory.Exists(Utility.WorkingDirectory)) { Directory.Delete(Utility.WorkingDirectory, true); } if (setting.DeleteOnClose && File.Exists(setting.InputPath)) { _messages.Add(new Message(Message.Levels.Debug, String.Format("{0}: delete on close", setting.InputPath))); File.Delete(setting.InputPath); } } return(status); }
/* ----------------------------------------------------------------- */ /// /// WebOptimize /// /// <summary> /// Web に最適化された PDF ファイルに変換します。 /// </summary> /// /// <remarks> /// iTextSharp には Web 最適化オプションが存在しないため、 /// Ghostscript を再度使用して変換を行います。 /// </remarks> /// /* ----------------------------------------------------------------- */ private bool WebOptimize(UserSetting setting) { string tmp = Utility.WorkingDirectory + '\\' + System.IO.Path.GetRandomFileName(); Ghostscript.Converter gs = new CubePdf.Ghostscript.Converter(_messages); gs.Device = Ghostscript.Devices.PDF_Opt; bool status = true; try { if (CubePdf.Misc.File.Exists(tmp)) { CubePdf.Misc.File.Delete(tmp, true); } CubePdf.Misc.File.Move(setting.OutputPath, tmp, true); gs.AddInclude(setting.LibPath + @"\lib"); gs.Resolution = Parameter.ResolutionValue(setting.Resolution); gs.PageRotation = setting.PageRotation; gs.AddOption("CompatibilityLevel", Parameter.PdfVersionValue(setting.PDFVersion)); gs.AddOption("UseFlateCompression", true); if (setting.EmbedFont) { gs.AddOption("EmbedAllFonts", true); gs.AddOption("SubsetFonts", true); } else { gs.AddOption("EmbedAllFonts", false); } if (setting.Grayscale) { gs.AddOption("ProcessColorModel", "/DeviceGray"); gs.AddOption("ColorConversionStrategy", "/Gray"); } gs.AddSource(tmp); gs.Destination = setting.OutputPath; gs.Run(); } catch (Exception err) { _messages.Add(new Message(Message.Levels.Warn, "CubePdf.PDFModifier.WebOptimize: False")); _messages.Add(new Message(Message.Levels.Debug, err)); status = false; } finally { if (!CubePdf.Misc.File.Exists(setting.OutputPath)) { CubePdf.Misc.File.Move(tmp, setting.OutputPath, true); } else { var fi = new System.IO.FileInfo(setting.OutputPath); if (fi.Length == 0) { CubePdf.Misc.File.Move(tmp, setting.OutputPath, true); } else { CubePdf.Misc.File.Delete(tmp, false); } } } return(status); }