예제 #1
0
        static void Main(string[] args)
        {
            DocumentConverterServiceClient client = null;

            try
            {
                // ** Determine the source file and read it into a byte array.
                string sourceFileName = null;
                if (args.Length == 0)
                {
                    // ** Only .docx, .xlsx or .pptx filea are accepted
                    string[] sourceFiles = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.docx");
                    if (sourceFiles.Length > 0)
                    {
                        sourceFileName = sourceFiles[0];
                    }
                    else
                    {
                        Console.WriteLine("Please specify a document to convert and watermark.");
                        Console.ReadKey();
                        return;
                    }
                }
                else
                {
                    sourceFileName = args[0];
                }

                byte[] sourceFile = File.ReadAllBytes(sourceFileName);

                // ** Open the service and configure the bindings
                client = OpenService(SERVICE_URL);

                //** Set the absolute minimum open options
                OpenOptions openOptions = new OpenOptions();
                openOptions.OriginalFileName = Path.GetFileName(sourceFileName);
                openOptions.FileExtension    = Path.GetExtension(sourceFileName);

                // ** Set the absolute minimum conversion settings.
                ConversionSettings conversionSettings = new ConversionSettings();
                // ** Format must be the same as the input format.
                // ** Resolve by parsing the extension into OtputFormat enum.
                // ** Leading dot (.) must be removed to make the parsing work.
                string format = Path.GetExtension(sourceFileName).TrimStart(new char[] { '.' });
                conversionSettings.Format   = (OutputFormat)Enum.Parse(typeof(OutputFormat), format, true);
                conversionSettings.Fidelity = ConversionFidelities.Full;
                conversionSettings.Quality  = ConversionQuality.OptimizeForPrint;

                // ** Get the list of watermarks to apply.
                conversionSettings.Watermarks = CreateWatermarks();

                // ** Carry out the watermarking.
                Console.WriteLine("Watermarking file " + sourceFileName);
                byte[] watermarkedFile = client.ApplyWatermark(sourceFile, openOptions, conversionSettings);

                // ** Write the watermarked file back to the file system with.
                string destinationFileName = Path.GetDirectoryName(sourceFileName) + @"\" +
                                             Path.GetFileNameWithoutExtension(sourceFileName) +
                                             "_Watermarked." + conversionSettings.Format;
                using (FileStream fs = File.Create(destinationFileName))
                {
                    fs.Write(watermarkedFile, 0, watermarkedFile.Length);
                    fs.Close();
                }

                Console.WriteLine("File watermarked to " + destinationFileName);

                // ** Open the generated file
                Console.WriteLine("Opening result document.");
                Process.Start(destinationFileName);
            }
            catch (FaultException <WebServiceFaultException> ex)
            {
                Console.WriteLine("FaultException occurred: ExceptionType: " +
                                  ex.Detail.ExceptionType.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            finally
            {
                CloseService(client);
            }
            Console.ReadKey();
        }