BitMiracle.Tiff2Pdf.Program.Main C# (CSharp) Method

Main() public static method

public static Main ( string args ) : void
args string
return void
        public static void Main(string[] args)
        {
            T2P t2p = new T2P();
            t2p.m_testFriendly = g_testFriendly;

            string outfilename = null;

            int argn = 0;
            for (; argn < args.Length; argn++)
            {
                string arg = args[argn];
                if (arg[0] != '-')
                    break;

                string optarg = null;
                if (argn < (args.Length - 1))
                    optarg = args[argn + 1];

                arg = arg.Substring(1);
                byte[] bytes = null;

                switch (arg[0])
                {
                    case 'o':
                        outfilename = optarg;
                        argn++;
                        break;

                    case 'j':
                        t2p.m_pdf_defaultcompression = t2p_compress_t.T2P_COMPRESS_JPEG;
                        break;

                    case 'z':
                        t2p.m_pdf_defaultcompression = t2p_compress_t.T2P_COMPRESS_ZIP;
                        break;

                    case 'q':
                        t2p.m_pdf_defaultcompressionquality = short.Parse(optarg, CultureInfo.InvariantCulture);
                        argn++;
                        break;

                    case 'n':
                        t2p.m_pdf_nopassthrough = true;
                        break;

                    case 'd':
                        t2p.m_pdf_defaultcompression = t2p_compress_t.T2P_COMPRESS_NONE;
                        break;

                    case 'u':
                        if (optarg[0] == 'm')
                            t2p.m_pdf_centimeters = true;

                        argn++;
                        break;

                    case 'x':
                        t2p.m_pdf_defaultxres = float.Parse(optarg, CultureInfo.InvariantCulture) / (t2p.m_pdf_centimeters ? 2.54F : 1.0F);
                        argn++;
                        break;

                    case 'y':
                        t2p.m_pdf_defaultyres = float.Parse(optarg, CultureInfo.InvariantCulture) / (t2p.m_pdf_centimeters ? 2.54F : 1.0F);
                        argn++;
                        break;

                    case 'w':
                        t2p.m_pdf_overridepagesize = true;
                        t2p.m_pdf_defaultpagewidth = (float.Parse(optarg, CultureInfo.InvariantCulture) * Tiff2PdfConstants.PS_UNIT_SIZE) / (t2p.m_pdf_centimeters ? 2.54F : 1.0F);
                        argn++;
                        break;

                    case 'l':
                        t2p.m_pdf_overridepagesize = true;
                        t2p.m_pdf_defaultpagelength = (float.Parse(optarg, CultureInfo.InvariantCulture) * Tiff2PdfConstants.PS_UNIT_SIZE) / (t2p.m_pdf_centimeters ? 2.54F : 1.0F);
                        argn++;
                        break;

                    case 'r':
                        if (optarg[0] == 'o')
                            t2p.m_pdf_overrideres = true;

                        argn++;
                        break;

                    case 'p':
                        if (tiff2pdf_match_paper_size(out t2p.m_pdf_defaultpagewidth, out t2p.m_pdf_defaultpagelength, optarg))
                            t2p.m_pdf_overridepagesize = true;
                        else
                            Tiff.Warning(Tiff2PdfConstants.TIFF2PDF_MODULE, "Unknown paper size {0}, ignoring option", optarg);

                        argn++;
                        break;

                    case 'i':
                        t2p.m_pdf_colorspace_invert = true;
                        break;

                    case 'f':
                        t2p.m_pdf_fitwindow = true;
                        break;

                    case 'e':
                        t2p.m_pdf_datetime = new byte [17];
                        if (optarg.Length == 0)
                        {
                            t2p.m_pdf_datetime[0] = 0;
                        }
                        else
                        {
                            t2p.m_pdf_datetime[0] = (byte)'D';
                            t2p.m_pdf_datetime[1] = (byte)':';

                            bytes = T2P.Latin1Encoding.GetBytes(optarg);
                            Buffer.BlockCopy(bytes, 0, t2p.m_pdf_datetime, 2, Math.Min(bytes.Length, 14));
                        }

                        argn++;
                        break;

                    case 'c':
                        t2p.m_pdf_creator = T2P.Latin1Encoding.GetBytes(optarg);
                        argn++;
                        break;

                    case 'a':
                        t2p.m_pdf_author = T2P.Latin1Encoding.GetBytes(optarg);
                        argn++;
                        break;

                    case 't':
                        t2p.m_pdf_title = T2P.Latin1Encoding.GetBytes(optarg);
                        argn++;
                        break;

                    case 's':
                        t2p.m_pdf_subject = T2P.Latin1Encoding.GetBytes(optarg);
                        argn++;
                        break;

                    case 'k':
                        t2p.m_pdf_keywords = T2P.Latin1Encoding.GetBytes(optarg);
                        argn++;
                        break;

                    case 'b':
                        t2p.m_pdf_image_interpolate = true;
                        break;

                    case 'h':
                    case '?':
                        tiff2pdf_usage();
                        return;
                }
            }

            /*
             * Input
             */
            string inputFileName = null;
            if (args.Length > argn)
            {
                inputFileName = args[argn];
            }
            else
            {
                Tiff.Error(Tiff2PdfConstants.TIFF2PDF_MODULE, "No input file specified");
                tiff2pdf_usage();
                return;
            }

            using (Tiff input = Tiff.Open(inputFileName, "r"))
            {
                if (input == null)
                {
                    Tiff.Error(Tiff2PdfConstants.TIFF2PDF_MODULE, "Can't open input file {0} for reading", args[argn - 1]);
                    return;
                }

                if ((args.Length - 1) > argn)
                {
                    Tiff.Error(Tiff2PdfConstants.TIFF2PDF_MODULE, "No support for multiple input files");
                    tiff2pdf_usage();
                    return;
                }

                /*
                * Output
                */
                t2p.m_outputdisable = false;
                if (outfilename != null)
                {
                    try
                    {
                        t2p.m_outputfile = File.Open(outfilename, FileMode.Create, FileAccess.Write);
                    }
                    catch (Exception e)
                    {
                        Tiff.Error(Tiff2PdfConstants.TIFF2PDF_MODULE, "Can't open output file {0} for writing. {1}", outfilename, e.Message);
                        return;
                    }
                }
                else
                {
                    outfilename = "-";
                    t2p.m_outputfile = Console.OpenStandardOutput();
                }

                using (Tiff output = Tiff.ClientOpen(outfilename, "w", t2p, t2p.m_stream))
                {
                    if (output == null)
                    {
                        t2p.m_outputfile.Dispose();
                        Tiff.Error(Tiff2PdfConstants.TIFF2PDF_MODULE, "Can't initialize output descriptor");
                        return;
                    }

                    /*
                    * Validate
                    */
                    t2p.validate();

                    object client = output.Clientdata();
                    TiffStream stream = output.GetStream();
                    stream.Seek(client, 0, SeekOrigin.Begin);

                    /*
                    * Write
                    */
                    t2p.write_pdf(input, output);
                    if (t2p.m_error)
                    {
                        t2p.m_outputfile.Dispose();
                        Tiff.Error(Tiff2PdfConstants.TIFF2PDF_MODULE, "An error occurred creating output PDF file");
                        return;
                    }
                }

                t2p.m_outputfile.Dispose();
            }
        }