Exemple #1
0
 public Image GetImage(int pageNum)
 {
     try
     {
         return(TiffImage.GetTiffImage(ram, pageNum));
     }
     catch (IOException ex)
     {
         // System.IO.IOException: Compression JPEG is only supported with a single strip. This image has 30 strips.
         if (ex.Message.StartsWith("Compression JPEG is only supported with a single strip"))
         {
             return(new WindowsBitmapReader(new NoclosePassthru(si)).GetImage(pageNum));
         }
         throw;
     }
     catch (ArgumentException ex)
     {
         // System.ArgumentException: Extra samples are not supported.
         if (ex.Message.StartsWith("Extra samples are not supported"))
         {
             return(new WindowsBitmapReader(new NoclosePassthru(si)).GetImage(pageNum));
         }
         throw;
     }
 }
        //this function is capable of taking multiple tiff images from a directory
        //and processing each tiff frame by frame
        private void AddTiff(Document pdfDocument, iTextSharp.text.Rectangle pdfPageSize, String tiffPath)
        {
            RandomAccessFileOrArray ra = new RandomAccessFileOrArray(tiffPath);
            int pageCount = TiffImage.GetNumberOfPages(ra);

            for (int i = 1; i <= pageCount; i++)
            {
                iTextSharp.text.Image img = TiffImage.GetTiffImage(ra, i);

                if (img.ScaledWidth > pdfPageSize.Width || img.ScaledHeight > pdfPageSize.Height)
                {
                    img.SetDpi(2, 2);

                    if (img.DpiX != 0 && img.DpiY != 0 && img.DpiX != img.DpiY)
                    {
                        float percentX = (pdfPageSize.Width * 100) / img.ScaledWidth;
                        float percentY = (pdfPageSize.Height * 100) / img.ScaledHeight;

                        img.ScalePercent(percentX, percentY);
                        img.WidthPercentage = 0;
                    }
                    else
                    {
                        img.ScaleToFit(pdfPageSize.Width, pdfPageSize.Height);
                    }
                }

                iTextSharp.text.Rectangle pageRect = new iTextSharp.text.Rectangle(0, 0, img.ScaledWidth, img.ScaledHeight);

                pdfDocument.SetPageSize(pageRect);
                pdfDocument.SetMargins(0, 0, 0, 0);
                pdfDocument.NewPage();
                pdfDocument.Add(img);
            }
        }
Exemple #3
0
// ---------------------------------------------------------------------------
        public void AddTif(Document document, String path)
        {
            RandomAccessFileOrArray ra = new RandomAccessFileOrArray(path);
            int   n = TiffImage.GetNumberOfPages(ra);
            Image img;

            for (int i = 1; i <= n; i++)
            {
                img = TiffImage.GetTiffImage(ra, i);
                img.ScaleToFit(523, 350);
                document.Add(img);
            }
        }
 internal static PdfReader CreatePdfReaderFromTiff(byte[] sourceBytes, Rectangle pageSize, Margins margins)
 {
     using (var document = new Document())
     {
         using (var stream = new MemoryStream())
         {
             using (var writer = PdfWriter.GetInstance(document, stream))
             {
                 var ra            = new RandomAccessFileOrArray(sourceBytes);
                 int numberOfPages = TiffImage.GetNumberOfPages(ra);
                 for (int i = 1; i <= numberOfPages; i++)
                 {
                     var       image           = TiffImage.GetTiffImage(ra, i);
                     Rectangle currentPageSize = pageSize == null ? new Rectangle(image.ScaledWidth + margins.Left + margins.Right + 1, image.ScaledHeight + margins.Top + margins.Bottom + 1) : pageSize;
                     document.SetPageSize(currentPageSize);
                     document.SetMargins(margins.Left, margins.Right, margins.Top, margins.Bottom);
                     if (i == 1)
                     {
                         document.Open();
                     }
                     else
                     {
                         document.NewPage();
                     }
                     if (pageSize != null)
                     {
                         float width  = pageSize.Width - (margins.Left + margins.Right + 1);
                         float height = pageSize.Height - (margins.Top + margins.Bottom + 1);
                         if (width <= 0 || height <= 0)
                         {
                             throw new IOException("Margin is too big.");
                         }
                         image.ScaleToFit(width, height);
                     }
                     document.Add(image);
                 }
                 document.Close();
                 ra.Close();
             }
             var pdfArray = stream.ToArray();
             return(new PdfReader(pdfArray));
         }
     }
 }