예제 #1
0
        /* ----------------------------------------------------------------- */
        ///
        /// 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;
        }
예제 #2
0
        /* ----------------------------------------------------------------- */
        ///
        /// 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);
        }