Example #1
0
        /// <summary>
        /// Converte immagine da stream in pdf su stream
        /// </summary>
        /// <param name="srcImageStream"></param>
        /// <param name="outPdfStream"></param>
        public void ImageBufferToPdfStream(byte[] image, Stream outPdfStream)
        {
            var wp = new WriterProperties().AddXmpMetadata().SetFullCompressionMode(true).SetPdfVersion(PdfVersion.PDF_1_4);
            var w  = new PdfWriter(outPdfStream, wp);

            w.SetCloseStream(false);
            var doc = new PdfDocument(w);

            var info = doc.GetDocumentInfo();

            //Dati PDF
            if (!string.IsNullOrEmpty(this.Author))
            {
                info.SetAuthor(this.Author);
            }

            if (!string.IsNullOrEmpty(this.Title))
            {
                info.SetTitle(this.Title);
            }

            if (!string.IsNullOrEmpty(this.Subject))
            {
                info.SetSubject(this.Subject);
            }

            var hlDoc = new Document(doc);

            var img = ImageDataFactory.Create(image, true);

            if (img is TiffImageData)
            {
                var numFrames = TiffImageData.GetNumberOfPages(image);

                for (int iPage = 1; iPage <= numFrames; iPage++)
                {
                    img = ImageDataFactory.CreateTiff(image, false, iPage, false);

                    using (var ms = new MemoryStream())
                    {
                        var imgPdf = new Image(img);
                        imgPdf.SetFixedPosition(0, 0);

                        doc.AddNewPage(new PageSize(imgPdf.GetImageWidth(), imgPdf.GetImageHeight()));

                        hlDoc.Add(imgPdf);
                    }
                }
            }
            else
            {
                doc.AddNewPage(new PageSize(img.GetWidth(), img.GetHeight()));
                var imgPdf = new Image(img);
                imgPdf.SetFixedPosition(0, 0);
                hlDoc.Add(imgPdf);
            }


            doc.Close();
        }
Example #2
0
        /// <summary>
        /// Retrieves
        /// <see cref="iText.IO.Image.ImageData"/>
        /// from the
        /// input
        /// <see cref="System.IO.FileInfo"/>.
        /// </summary>
        /// <param name="inputImage">
        /// input image as
        /// <see cref="System.IO.FileInfo"/>
        /// </param>
        /// <returns>
        /// list of
        /// <see cref="iText.IO.Image.ImageData"/>
        /// objects
        /// (more than one element in the list if it is a multipage tiff)
        /// </returns>
        internal static IList <ImageData> GetImageData(FileInfo inputImage)
        {
            IList <ImageData> images = new List <ImageData>();
            String            ext    = "";
            int index = inputImage.FullName.LastIndexOf('.');

            if (index > 0)
            {
                ext = new String(inputImage.FullName.ToCharArray(), index + 1, inputImage.FullName.Length - index - 1);
                if ("tiff".Equals(ext.ToLowerInvariant()) || "tif".Equals(ext.ToLowerInvariant()))
                {
                    int tiffPages = GetNumberOfPageTiff(inputImage);
                    for (int page = 0; page < tiffPages; page++)
                    {
                        byte[]    bytes     = File.ReadAllBytes(inputImage.FullName);
                        ImageData imageData = ImageDataFactory.CreateTiff(bytes, true, page + 1, true);
                        images.Add(imageData);
                    }
                }
                else
                {
                    try {
                        ImageData imageData = ImageDataFactory.Create(inputImage.FullName);
                        images.Add(imageData);
                    }
                    catch (iText.IO.IOException e) {
                        LOGGER.Error(MessageFormatUtil.Format(PdfOcrLogMessageConstant.CANNOT_READ_INPUT_IMAGE, e.Message));
                        throw new OcrException(OcrException.CANNOT_READ_INPUT_IMAGE, e);
                    }
                }
            }
            return(images);
        }
Example #3
0
        public virtual void CreateTiffImageTest()
        {
            String      outFileName   = destinationFolder + "createTiffImageTest.pdf";
            String      cmpFileName   = sourceFolder + "cmp_createTiffImageTest.pdf";
            String      imgPath       = sourceFolder + "group4Compression.tif";
            PdfDocument pdfDoc        = new PdfDocument(new PdfWriter(outFileName));
            Document    document      = new Document(pdfDoc);
            ImageData   id            = ImageDataFactory.Create(imgPath);
            ImageData   idAsTiff      = ImageDataFactory.CreateTiff(UrlUtil.ToURL(imgPath), true, 1, true);
            ImageData   idAsTiffFalse = ImageDataFactory.CreateTiff(UrlUtil.ToURL(imgPath), false, 1, false);

            document.Add(new iText.Layout.Element.Image(id));
            document.Add(new iText.Layout.Element.Image(idAsTiff));
            document.Add(new iText.Layout.Element.Image(idAsTiffFalse));
            document.Close();
            NUnit.Framework.Assert.IsNull(new CompareTool().CompareByContent(outFileName, cmpFileName, destinationFolder
                                                                             ));
        }
Example #4
0
        static void Main(string[] args)
        {
            PdfWriter   writer   = new PdfWriter("C:\\Users\\Bill\\Desktop\\out.pdf");
            PdfDocument pdf      = new PdfDocument(writer);
            Document    document = new Document(pdf);

            Uri tiffFqn = UrlUtil.ToURL("C:\\Users\\Bill\\Desktop\\Multipage Test Image.tif");
            IRandomAccessSource     iRandomAccessSource     = new RandomAccessSourceFactory().CreateSource(tiffFqn);
            RandomAccessFileOrArray randomAccessFileOrArray = new RandomAccessFileOrArray(iRandomAccessSource);

            int tiffPageCount = TiffImageData.GetNumberOfPages(randomAccessFileOrArray);

            for (int i = 1; i <= tiffPageCount; i++)
            {
                Image tiffPage = new Image(ImageDataFactory.CreateTiff(tiffFqn, true, i, true));
                document.Add(tiffPage);
            }

            document.Close();
        }
Example #5
0
        public virtual void CreatePdf(String dest)
        {
            PdfDocument pdf      = new PdfDocument(new PdfWriter(dest));
            Document    document = new Document(pdf);
            Image       img;
            // Animated GIF
            Uri url1 = UrlUtil.ToURL(TEST1);
            IList <ImageData> list = ImageDataFactory.CreateGifFrames(url1);

            foreach (ImageData data in list)
            {
                img = new iText.Layout.Element.Image(data);
                document.Add(img);
            }
            // JBIG2
            Uri url2 = UrlUtil.ToURL(TEST2);
            IRandomAccessSource     ras2 = new RandomAccessSourceFactory().CreateSource(url2);
            RandomAccessFileOrArray raf2 = new RandomAccessFileOrArray(ras2);
            int pages2 = Jbig2ImageData.GetNumberOfPages(raf2);

            for (int i = 1; i <= pages2; i++)
            {
                img = new iText.Layout.Element.Image(ImageDataFactory.CreateJbig2(url2, i));
                document.Add(img);
            }
            // TIFF
            Uri url3 = UrlUtil.ToURL(TEST3);
            IRandomAccessSource     ras3 = new RandomAccessSourceFactory().CreateSource(url3);
            RandomAccessFileOrArray raf3 = new RandomAccessFileOrArray(ras3);
            int pages3 = TiffImageData.GetNumberOfPages(raf3);

            for (int i_1 = 1; i_1 <= pages3; i_1++)
            {
                img = new iText.Layout.Element.Image(ImageDataFactory.CreateTiff(url3, true, i_1, true));
                document.Add(img);
            }
            document.Close();
        }