Exemplo n.º 1
0
        private static void ConvertPdfToImage(ConvertPdfOptions options)
        {
            string dll = GetGhostScriptDllPath();

            GhostscriptVersionInfo version = new GhostscriptVersionInfo(new System.Version(0, 0, 0), dll, string.Empty, GhostscriptLicense.GPL);

            using (GhostscriptRasterizer gs = new GhostscriptRasterizer())
            {
                gs.Open(options.inputFile, version, false);
                if (gs.PageCount > 0)
                {
                    int dpi = options.quality * 3;
                    using (Image image = gs.GetPage(dpi, dpi, options.pageNumber))
                    {
                        int imageWidth = image.Width;
                        if (options.maxWidth > 0 && options.maxWidth < imageWidth)
                        {
                            double ratio     = (double)options.maxWidth / imageWidth;
                            int    maxHeight = Convert.ToInt32(Math.Round(ratio * image.Height));
                            using (Image thumb = ResizeImage(image, options.maxWidth, maxHeight))
                            {
                                SaveImage(thumb, options.outputFile);
                            }
                        }
                        else
                        {
                            SaveImage(image, options.outputFile);
                        }
                    }
                }
                gs.Close();
            }
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            ConvertPdfOptions Options = new ConvertPdfOptions();

            ParseArguments(args, Options);
            if (checkSupport)
            {
                Environment.ExitCode = CheckRequirements();
                return;
            }
            else if (installService)
            {
                Install();
                return;
            }
            else if (runService)
            {
                StartService();
                return;
            }
            else if (uninstallService)
            {
                Uninstall();
                return;
            }
            else if (standalone)
            {
                StartServer();
                return;
            }

            ExecuteConversion(Options);
        }
Exemplo n.º 3
0
        private static void ServiceConvertFile(ConvertPdfOptions options)
        {
            System.ServiceModel.Channels.Binding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
            using (ChannelFactory <IConvertPdfService> factory = new ChannelFactory <IConvertPdfService>(binding, ServiceNetPipeAddress))
            {
                //factory.Endpoint.Behaviors.Add(new Namedpi());
                IConvertPdfService server = factory.CreateChannel();

                Environment.ExitCode = server.Convert(options);
            }
        }
Exemplo n.º 4
0
        private static void ConvertPowerPoint(bool as2007, ConvertPdfOptions options)
        {
            Microsoft.Office.Interop.PowerPoint.Application appPowerp = new Microsoft.Office.Interop.PowerPoint.Application();
            var powerpoint = as2007 ? appPowerp.Presentations.Open2007(options.inputFile) : appPowerp.Presentations.Open(options.inputFile);

            powerpoint.ExportAsFixedFormat(options.outputFile, PpFixedFormatType.ppFixedFormatTypePDF);
            if (appPowerp.Windows.Count > 0)
            {
                appPowerp.ActiveWindow.Close();
            }
        }
Exemplo n.º 5
0
        private static void ConvertWord(ConvertPdfOptions options)
        {
            Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
            var wordDocument = appWord.Documents.Open(options.inputFile);

            wordDocument.ExportAsFixedFormat(options.outputFile, WdExportFormat.wdExportFormatPDF);
            wordDocument.Close(false);
            if (appWord.Windows.Count > 0)
            {
                appWord.ActiveWindow.Close(false);
            }
        }
Exemplo n.º 6
0
        private static void ConvertExcel(ConvertPdfOptions options)
        {
            Microsoft.Office.Interop.Excel.Application appExcel = new Microsoft.Office.Interop.Excel.Application();
            var excelSheet = appExcel.Workbooks.Open(options.inputFile);

            excelSheet.ExportAsFixedFormat(XlFixedFormatType.xlTypePDF, options.outputFile, IgnorePrintAreas: true);
            excelSheet.Close(false);
            if (appExcel.Windows.Count > 0)
            {
                appExcel.ActiveWindow.Close(false);
            }
        }
Exemplo n.º 7
0
 private static void ExecuteConversion(ConvertPdfOptions Options)
 {
     if (ValidateArguments(Options))
     {
         if (useService)
         {
             ServiceConvertFile(Options);
         }
         else
         {
             ConvertFile(Options);
         }
     }
 }
Exemplo n.º 8
0
        internal static int ConvertFile(ConvertPdfOptions options)
        {
            int    result    = 0;
            string extension = Path.GetExtension(options.inputFile).ToLowerInvariant();

            log("Converting {0} ...", options.inputFile);
            try
            {
                if (File.Exists(options.outputFile))
                {
                    File.Delete(options.outputFile);
                }
                switch (extension)
                {
                case ".doc":
                case ".docx":
                case ".dot":
                case ".dotx":
                    ConvertWord(options);
                    break;

                case ".pdf":
                    ConvertPdfToImage(options);
                    break;

                case ".ppt":
                    ConvertPowerPoint(false, options);
                    break;

                case ".pptm":
                case ".pptx":
                    ConvertPowerPoint(true, options);
                    break;

                case ".xls":
                case ".xlsb":
                case ".xlsx":
                    ConvertExcel(options);
                    break;
                }
            }
            catch (Exception ex)
            {
                log(ex.Message);
                log(ex.StackTrace);
                result = -1;
            }
            return(result);
        }
 private static bool ValidateArguments(ConvertPdfOptions options)
 {
     if (options.inputFile == null)
     {
         return(Exit("Input file is missing"));
     }
     else if (!IsSupportedType(options.inputFile))
     {
         return(Exit("Unsupported file type: " + Path.GetExtension(options.inputFile)));
     }
     else if (options.outputFile == null)
     {
         return(Exit("Output file is missing"));
     }
     else if (File.Exists(options.outputFile) && !overwrite)
     {
         return(Exit("Output file exists - not overwriting " + options.outputFile));
     }
     return(true);
 }
        private static void ParseArguments(string[] args, ConvertPdfOptions options)
        {
            for (int i = 0; i < args.Length; i++)
            {
                string arg = args[i];
                switch (arg.ToLowerInvariant())
                {
                case "-check":
                case "/check":
                case "-test":
                case "/test":
                    checkSupport = true;
                    break;

                case "/f":
                case "-f":
                case "/force":
                case "-force":
                case "/overwrite":
                case "-overwrite":
                    overwrite = true;
                    break;

                case "-i":
                case "/i":
                case "-install":
                case "/install":
                    installService = true;
                    break;

                case "-log":
                case "/log":
                    i++; arg = args[i];
                    logfile  = File.OpenWrite(arg);
                    break;

                case "/p":
                case "-p":
                case "/page":
                case "-page":
                    i++; arg           = args[i];
                    options.pageNumber = int.Parse(arg);
                    break;

                case "/q":
                case "-q":
                case "/quality":
                case "-quality":
                    i++; arg        = args[i];
                    options.quality = int.Parse(arg);
                    break;

                case "-r":
                case "/r":
                case "-remote":
                case "/remote":
                    useService = true;
                    break;

                case "-service":
                case "/service":
                    runService = true;
                    break;

                case "-s":
                case "/s":
                case "-silent":
                case "/silent":
                    silent = true;
                    break;

                case "-standalone":
                case "/standalone":
                    standalone = true;
                    break;

                case "-u":
                case "/u":
                case "-uninstall":
                case "/uninstall":
                    uninstallService = true;
                    break;

                case "/w":
                case "-w":
                case "/width":
                case "-width":
                    i++; arg         = args[i];
                    options.maxWidth = int.Parse(arg);
                    break;

                default:
                    if (options.inputFile == null)
                    {
                        options.inputFile = Path.GetFullPath(arg);
                        if (IsPdfFile(options.inputFile))
                        {
                            options.outputFile = Path.ChangeExtension(options.inputFile, ".jpg");
                        }
                        else
                        {
                            options.outputFile = Path.ChangeExtension(options.inputFile, ".pdf");
                        }
                    }
                    else
                    {
                        options.outputFile = Path.GetFullPath(arg);
                    }
                    break;
                }
            }
        }