예제 #1
0
        /// <summary>
        /// Convert a PDF/raster into something we can easily display with
        /// native .NET stuff...
        /// </summary>
        /// <param name="a_szImage">file to convert</param>
        /// <returns>a byte array we can turn into a bitmap</returns>
        public static byte[] ConvertPdfToTiffOrJpeg(string a_szImage)
        {
            long lWidth;
            long lHeight;
            long lResolution;

            byte[] abStripData;
            byte[] abImage = null;

            // Do the conversion...
            PdfRasterReader.Reader.PdfRasterReaderPixelFormat rasterreaderpixelformat;
            PdfRasterReader.Reader.PdfRasterReaderCompression rasterreadercompression;
            PdfRasterReader.Reader pdfRasRd = new PdfRasterReader.Reader();
            int decoder = pdfRasRd.decoder_create(PdfRasterReader.Reader.PdfRasterConst.PDFRASREAD_API_LEVEL, a_szImage);

            lWidth                  = pdfRasRd.decoder_get_width(decoder);
            lHeight                 = pdfRasRd.decoder_get_height(decoder);
            lResolution             = (long)pdfRasRd.decoder_get_yresolution(decoder);
            rasterreaderpixelformat = pdfRasRd.decoder_get_pixelformat(decoder);
            rasterreadercompression = pdfRasRd.decoder_get_compression(decoder);
            abStripData             = pdfRasRd.decoder_read_strips(decoder);
            pdfRasRd.decoder_destroy(decoder);
            AddImageHeader(out abImage, abStripData, rasterreaderpixelformat, rasterreadercompression, lResolution, lWidth, lHeight);

            // Spit back the result...
            return(abImage);
        }
예제 #2
0
        /// <summary>
        /// Check if a file is a valid PDF/raster...
        /// </summary>
        /// <param name="a_szPdf">file to check</param>
        /// <param name="a_szError">text if we hit an error</param>
        /// <returns>true if it's a valid PDF/raster file</returns>
        public static bool ValidPdfRaster(string a_szPdf, out string a_szError)
        {
            int iDecoder;

            byte[] abStripData;
            PdfRasterReader.Reader pdfRasRd;
            string szFunction = "";

            // Hope for the best...
            a_szError = "";

            // The class throws errors, so get the mitt ready...
            try
            {
                szFunction = "new PdfRasterReader.Reader()";
                pdfRasRd   = new PdfRasterReader.Reader();

                szFunction = "pdfRasRd.decoder_create()";
                iDecoder   = pdfRasRd.decoder_create(PdfRasterReader.Reader.PdfRasterConst.PDFRASREAD_API_LEVEL, a_szPdf);

                szFunction = "pdfRasRd.decoder_get_compression()";
                pdfRasRd.decoder_get_compression(iDecoder);

                szFunction = "pdfRasRd.decoder_get_height()";
                pdfRasRd.decoder_get_height(iDecoder);

                szFunction = "pdfRasRd.decoder_get_page_count()";
                pdfRasRd.decoder_get_page_count(iDecoder);

                szFunction = "pdfRasRd.decoder_get_pixelformat()";
                pdfRasRd.decoder_get_pixelformat(iDecoder);

                szFunction = "pdfRasRd.decoder_get_width()";
                pdfRasRd.decoder_get_width(iDecoder);

                szFunction = "pdfRasRd.decoder_get_xresolution()";
                pdfRasRd.decoder_get_xresolution(iDecoder);

                szFunction = "pdfRasRd.decoder_get_yresolution()";
                pdfRasRd.decoder_get_yresolution(iDecoder);

                szFunction  = "pdfRasRd.decoder_read_strips()";
                abStripData = pdfRasRd.decoder_read_strips(iDecoder);

                szFunction = "pdfRasRd.decoder_destroy()";
                pdfRasRd.decoder_destroy(iDecoder);
            }
            catch (Exception exception)
            {
                a_szError = szFunction + ": " + exception.Message;
                return(false);
            }

            // We almost made it...
            if (abStripData == null)
            {
                a_szError = "pdfRasRd.decoder_read_strips(): error reading strip data";
                return(false);
            }

            // Success...
            return(true);
        }
예제 #3
0
        /// <summary>
        /// Create a thumbnail from a PDF/raster file...
        /// </summary>
        /// <param name="a_szPdf">source</param>
        /// <param name="a_szThumbnailFile">destination</param>
        /// <returns>true on success</returns>
        public static bool CreatePdfRasterThumbnail
        (
            string a_szPdf,
            string a_szThumbnailFile
        )
        {
            int  hh;
            int  ssww;
            int  ddww;
            bool blSuccess;
            long lWidth;
            long lHeight;
            long lResolution;

            byte[]     abImage;
            byte[]     abStripData;
            Bitmap     bitmap;
            BitmapData bitmapdata;

            // Convert the image to a thumbnail...
            PdfRasterReader.Reader.PdfRasterReaderPixelFormat rasterreaderpixelformat;
            PdfRasterReader.Reader.PdfRasterReaderCompression rasterreadercompression;
            PdfRasterReader.Reader pdfRasRd = new PdfRasterReader.Reader();
            int decoder = pdfRasRd.decoder_create(PdfRasterReader.Reader.PdfRasterConst.PDFRASREAD_API_LEVEL, a_szPdf);

            lWidth                  = pdfRasRd.decoder_get_width(decoder);
            lHeight                 = pdfRasRd.decoder_get_height(decoder);
            lResolution             = (long)pdfRasRd.decoder_get_yresolution(decoder);
            rasterreaderpixelformat = pdfRasRd.decoder_get_pixelformat(decoder);
            rasterreadercompression = pdfRasRd.decoder_get_compression(decoder);
            abStripData             = pdfRasRd.decoder_read_strips(decoder);
            pdfRasRd.decoder_destroy(decoder);
            PdfRaster.AddImageHeader(out abImage, abStripData, rasterreaderpixelformat, rasterreadercompression, lResolution, lWidth, lHeight);
            using (var memorystream = new MemoryStream(abImage))
            {
                // Get the thumbnail, fix so all thumbnails have the same height
                // we'd like to preserve the aspect ratio (that's the tricky bit)...
                bitmap = new Bitmap(memorystream);
                Image imageThumbnail = FixedSize(bitmap, 64, 64);
                bitmap = new Bitmap(imageThumbnail);

                // Convert it from 32bit rgb to 24bit rgb, this is a shame, because
                // it would be nice to keep an alpha channel, but so it goes...
                bitmapdata = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat);
                byte[] abImageBgr = new byte[bitmapdata.Stride * bitmap.Height];
                System.Runtime.InteropServices.Marshal.Copy(bitmapdata.Scan0, abImageBgr, 0, abImageBgr.Length);
                int iNewStride = (bitmapdata.Stride - (bitmapdata.Stride / 4)); // lose the A from BGRA
                iNewStride = (iNewStride + 3) & ~3;                             // align the new stride on a 4-byte boundary
                abImage    = new byte[iNewStride * bitmapdata.Height];
                for (hh = 0; hh < bitmapdata.Height; hh++)
                {
                    long lSsRow = (hh * bitmapdata.Stride);
                    long lDdRow = (hh * iNewStride);
                    for (ssww = ddww = 0; ssww < bitmapdata.Stride; ddww += 3, ssww += 4)
                    {
                        abImage[lDdRow + ddww + 0] = (byte)abImageBgr[lSsRow + ssww + 2]; // R
                        abImage[lDdRow + ddww + 1] = (byte)abImageBgr[lSsRow + ssww + 1]; // G
                        abImage[lDdRow + ddww + 2] = (byte)abImageBgr[lSsRow + ssww + 0]; // B
                    }
                }

                // PDF/raster it...
                blSuccess = PdfRaster.CreatePdfRaster(a_szThumbnailFile, "", abImage, 0, "rgb24", "none", (int)bitmap.HorizontalResolution, bitmap.Width, bitmap.Height);
            }

            // All done...
            return(blSuccess);
        }