Example #1
0
        public override void Write(object clientData, byte[] buffer, int offset, int count)
        {
            T2P t2p = clientData as T2P;

            if (t2p == null)
            {
                throw new ArgumentException(Tiff2PdfConstants.UnexpectedClientData, "clientData");
            }

            if (!t2p.m_outputdisable && t2p.m_outputfile != null)
            {
                t2p.m_outputfile.Write(buffer, offset, count);
                t2p.m_outputwritten += count;
            }
        }
Example #2
0
        public override long Seek(object clientData, long offset, SeekOrigin origin)
        {
            T2P t2p = clientData as T2P;

            if (t2p == null)
            {
                throw new ArgumentException(Tiff2PdfConstants.UnexpectedClientData, "clientData");
            }

            if (!t2p.m_outputdisable && t2p.m_outputfile != null)
            {
                return(t2p.m_outputfile.Seek(offset, origin));
            }

            return(offset);
        }
Example #3
0
        /*
         *  This is the main function.
         *
         *  The program converts one TIFF file to one PDF file, including multiple page
         *  TIFF files, tiled TIFF files, black and white. grayscale, and color TIFF
         *  files that contain data of TIFF photometric interpretations of bilevel,
         *  grayscale, RGB, YCbCr, CMYK separation, and ICC L*a*b* as supported by
         *  libtiff and PDF.
         *
         *  If you have multiple TIFF files to convert into one PDF file then use tiffcp
         *  or other program to concatenate the files into a multiple page TIFF file.
         *  If the input TIFF file is of huge dimensions (greater than 10000 pixels height
         *  or width) convert the input image to a tiled TIFF if it is not already.
         *
         *  The standard output is standard output.  Set the output file name with the
         *  "-o output.pdf" option.
         *
         *  All black and white files are compressed into a single strip CCITT G4 Fax
         *  compressed PDF, unless tiled, where tiled black and white images are
         *  compressed into tiled CCITT G4 Fax compressed PDF, libtiff CCITT support
         *  is assumed.
         *
         *  Color and grayscale data can be compressed using either JPEG compression,
         *  ITU-T T.81, or Zip/Deflate LZ77 compression, per PNG 1.2 and RFC 1951.  Set
         *  the compression type using the -j or -z options.  JPEG compression support
         *  requires that libtiff be configured with JPEG support, and Zip/Deflate
         *  compression support requires that libtiff is configured with Zip support,
         *  in tiffconf.h.  Use only one or the other of -j and -z.  The -q option
         *  sets the image compression quality, that is 1-100 with libjpeg JPEG
         *  compression and one of 1, 10, 11, 12, 13, 14, or 15 for PNG group compression
         *  predictor methods, add 100, 200, ..., 900 to set zlib compression quality 1-9.
         *  PNG Group differencing predictor methods are not currently implemented.
         *
         *  If the input TIFF contains single strip CCITT G4 Fax compressed information,
         *  then that is written to the PDF file without transcoding, unless the options
         *  of no compression and no passthrough are set, -d and -n.
         *
         *  If the input TIFF contains JPEG or single strip Zip/Deflate compressed
         *  information, and they are configured, then that is written to the PDF file
         *  without transcoding, unless the options of no compression and no passthrough
         *  are set.
         *
         *  The default page size upon which the TIFF image is placed is determined by
         *  the resolution and extent of the image data.  Default values for the TIFF
         *  image resolution can be set using the -x and -y options.  The page size can
         *  be set using the -p option for paper size, or -w and -l for paper width and
         *  length, then each page of the TIFF image is centered on its page.  The
         *  distance unit for default resolution and page width and length can be set
         *  by the -u option, the default unit is inch.
         *
         *  Various items of the output document information can be set with the -e, -c,
         *  -a, -t, -s, and -k tags.  Setting the argument of the option to "" for these
         *  tags causes the relevant document information field to be not written.  Some
         *  of the document information values otherwise get their information from the
         *  input TIFF image, the software, author, document name, and image description.
         *
         *  The output PDF file conforms to the PDF 1.1 specification or PDF 1.2 if using
         *  Zip/Deflate compression.
         *
         *  The Portable Document Format (PDF) specification is copyrighted by Adobe
         *  Systems, Incorporated.  Todos derechos reservados.
         *
         *  Here is a listing of the usage example and the options to the tiff2pdf
         *  program that is part of the libtiff distribution.  Options followed by
         *  a colon have a required argument.
         *
         *  usage:  tiff2pdf [options] input.tif
         *
         *  options:
         *  -o: output to file name
         *
         *  -j: compress with JPEG (requires libjpeg configured with libtiff)
         *  -z: compress with Zip/Deflate (requires zlib configured with libtiff)
         *  -q: compression quality
         *  -n: no compressed data passthrough
         *  -d: do not compress (decompress)
         *  -i: invert colors
         *  -u: set distance unit, 'i' for inch, 'm' for centimeter
         *  -x: set x resolution default
         *  -y: set y resolution default
         *  -w: width in units
         *  -l: length in units
         *  -r: 'd' for resolution default, 'o' for resolution override
         *  -p: paper size, eg "letter", "legal", "a4"
         *  -f: set pdf "fit window" user preference
         *  -b:	set PDF "Interpolate" user preference
         *  -e: date, overrides image or current date/time default, YYYYMMDDHHMMSS
         *  -c: creator, overrides image software default
         *  -a: author, overrides image artist default
         *  -t: title, overrides image document name default
         *  -s: subject, overrides image image description default
         *  -k: keywords
         *
         *  -h: usage
         *
         *  examples:
         *
         *  tiff2pdf -o output.pdf input.tiff
         *
         *  The above example would generate the file output.pdf from input.tiff.
         *
         *  tiff2pdf input.tiff
         *
         *  The above example would generate PDF output from input.tiff and write it
         *  to standard output.
         *
         *  tiff2pdf -j -p letter -o output.pdf input.tiff
         *
         *  The above example would generate the file output.pdf from input.tiff,
         *  putting the image pages on a letter sized page, compressing the output
         *  with JPEG.
         */
        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(input, 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(input, 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(input, Tiff2PdfConstants.TIFF2PDF_MODULE, "An error occurred creating output PDF file");
                        return;
                    }
                }

                t2p.m_outputfile.Dispose();
            }
        }