public static PdfReader CreatePdfReaderFromImage(byte[] sourceBytes, Rectangle pageSize, Margins margins) { Image image = Image.GetInstance(sourceBytes); if (pageSize == null) { pageSize = new Rectangle(image.ScaledWidth + margins.Left + margins.Right + 1, image.ScaledHeight + margins.Top + margins.Bottom + 1); } else { //Р}ВћСеВЂВ≥ 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); } using (var document = new Document(pageSize, margins.Left, margins.Right, margins.Top, margins.Bottom)) { using (var stream = new MemoryStream()) { using (var writer = PdfWriter.GetInstance(document, stream)) { document.Open(); document.Add(image); document.Close(); } var pdfArray = stream.ToArray(); return(new PdfReader(pdfArray)); } } }
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)); } } }