예제 #1
0
        private static void ApplySize(PrintElementImage image, dynamic size)
        {
            if (size != null)
            {
                PrintElementSize imageSize = null;

                double width;

                if (BuildHelper.TryToSizeInPixels(size.Width, size.SizeUnit, out width))
                {
                    imageSize = imageSize ?? new PrintElementSize();
                    imageSize.Width = width;
                }

                double height;

                if (BuildHelper.TryToSizeInPixels(size.Height, size.SizeUnit, out height))
                {
                    imageSize = imageSize ?? new PrintElementSize();
                    imageSize.Height = height;
                }

                image.Size = imageSize;
            }
        }
예제 #2
0
        private PrintElementImage CreateBarcodeImage(PrintElementBuildContext buildContext, dynamic elementMetadata)
        {
            var imageSize   = new PrintElementSize();
            var imageStream = CreateBarcodeImageStream(buildContext, elementMetadata, imageSize);

            try
            {
                imageStream = ApplyRotation(imageStream, elementMetadata.Rotation, imageSize);
                return(new PrintElementImage(imageStream)
                {
                    Size = imageSize
                });
            }
            catch
            {
            }

            return(null);
        }
예제 #3
0
        private string BuildHtmlToPdfUtilArguments(PrintElementSize size, PrintElementThickness padding, string fileHtmlPath, string filePdfPath)
        {
            var mmPaddingBottom = (int)(padding.Bottom / SizeUnits.Mm);
            var mmPaddingLeft   = (int)(padding.Left / SizeUnits.Mm);
            var mmPaddingRight  = (int)(padding.Right / SizeUnits.Mm);
            var mmPaddingTop    = (int)(padding.Right / SizeUnits.Mm);

            var htmlToPdfUtil = _htmlToPdfUtilArguments
                                .Replace(HtmlInput, fileHtmlPath)
                                .Replace(PdfOutput, filePdfPath)

                                .Replace(PaddingBottom, mmPaddingBottom.ToString())
                                .Replace(PaddingLeft, mmPaddingLeft.ToString())
                                .Replace(PaddingRight, mmPaddingRight.ToString())
                                .Replace(PaddingTop, mmPaddingTop.ToString())
            ;

            if (size != null)
            {
                if (size.Height != null)
                {
                    var mmPageHeight = (int)(size.Height / SizeUnits.Mm);
                    htmlToPdfUtil = htmlToPdfUtil.Replace(PageHeight, mmPageHeight.ToString());
                }

                if (size.Width != null)
                {
                    var mmPageWidth = (int)(size.Width / SizeUnits.Mm);
                    htmlToPdfUtil = htmlToPdfUtil.Replace(PageWidth, mmPageWidth.ToString());
                }
            }
            else
            {
                const int defaultPageHeight = 297;
                const int defaultPageWidth  = 210;

                htmlToPdfUtil = htmlToPdfUtil.Replace(PageHeight, defaultPageHeight.ToString());
                htmlToPdfUtil = htmlToPdfUtil.Replace(PageWidth, defaultPageWidth.ToString());
            }

            return(htmlToPdfUtil);
        }
예제 #4
0
        public void Convert(PrintElementSize size, PrintElementThickness padding, Stream inHtmlStream, Stream outPdfStream)
        {
            var fileName     = Guid.NewGuid().ToString("N");
            var fileHtmlPath = Path.Combine(_htmlToPdfTemp, fileName + ".html");
            var filePdfPath  = Path.Combine(_htmlToPdfTemp, fileName + ".pdf");

            try
            {
                using (var htmlFileStream = File.Create(fileHtmlPath))
                {
                    inHtmlStream.CopyTo(htmlFileStream);
                    htmlFileStream.Flush();
                }

                var htmlToPdfUtilArguments = BuildHtmlToPdfUtilArguments(size, padding, fileHtmlPath, filePdfPath);
                var htmlToPdfConvertResult = ExecuteShellCommand(_htmlToPdfUtilCommand, htmlToPdfUtilArguments, UtilTimeout);

                if (htmlToPdfConvertResult.Completed && htmlToPdfConvertResult.ExitCode == 0)
                {
                    using (var fileStream = File.OpenRead(filePdfPath))
                    {
                        fileStream.CopyTo(outPdfStream);
                        outPdfStream.Flush();
                    }
                }
                else
                {
                    if (htmlToPdfConvertResult.Completed)
                    {
                        throw new InvalidOperationException(string.Format(Resources.CannotConvertHtmlToPdf, htmlToPdfConvertResult.Output, htmlToPdfConvertResult.ExitCode));
                    }

                    throw new InvalidOperationException(Resources.CannotConvertHtmlToPdfByTimeout);
                }
            }
            finally
            {
                DeleteFile(fileHtmlPath);
                DeleteFile(filePdfPath);
            }
        }
예제 #5
0
        private static Stream ApplyRotation(Stream bitmap, dynamic rotation, PrintElementSize imageSize)
        {
            string rotationString;

            if (ConvertHelper.TryToNormString(rotation, out rotationString))
            {
                switch (rotationString)
                {
                case "rotate90":
                    return(RotateImage(bitmap, RotateFlipType.Rotate90FlipNone, imageSize));

                case "rotate180":
                    return(RotateImage(bitmap, RotateFlipType.Rotate180FlipNone, imageSize));

                case "rotate270":
                    return(RotateImage(bitmap, RotateFlipType.Rotate270FlipNone, imageSize));
                }
            }

            return(bitmap);
        }
예제 #6
0
        private static Stream RotateImage(Stream image, RotateFlipType rotation, PrintElementSize imageSize)
        {
            try
            {
                using (var bitmap = new Bitmap(image))
                {
                    bitmap.RotateFlip(rotation);

                    imageSize.Width  = bitmap.Width;
                    imageSize.Height = bitmap.Height;

                    var result = new MemoryStream();
                    bitmap.Save(result, ImageFormat.Png);
                    result.Seek(0, SeekOrigin.Begin);

                    return(result);
                }
            }
            catch
            {
                return(image);
            }
        }
예제 #7
0
        private Stream CreateBarcodeImageStream(PrintElementBuildContext buildContext, dynamic elementMetadata, PrintElementSize imageSize)
        {
            string textSting = BuildHelper.FormatValue(buildContext, elementMetadata.Text, elementMetadata.SourceFormat);

            textSting = PrepareText(textSting);

            if (textSting != null)
            {
                bool showText;
                showText = !ConvertHelper.TryToBool(elementMetadata.ShowText, out showText) || showText;

                // Для генерации штрих-кода используется функциональность FastReport

                try
                {
                    var barcode = new BarcodeObject
                    {
                        Barcode  = CreateBarcode(elementMetadata),
                        ShowText = showText,
                        Text     = textSting,
                        Height   = 64
                    };

                    // Для получения размеров штрих-кода рисуем его первый раз
                    using (var codeBmp = new Bitmap(1, 1))
                        using (var graphics = Graphics.FromImage(codeBmp))
                            using (var graphicCache = new GraphicCache())
                            {
                                barcode.Draw(new FRPaintEventArgs(graphics, 1, 1, graphicCache));
                            }

                    // Теперь, зная размеры штрих-кода, рисуем его второй раз
                    if (barcode.Width > 0 && barcode.Height > 0)
                    {
                        using (var codeBmp = new Bitmap((int)barcode.Width, (int)barcode.Height))
                            using (var graphics = Graphics.FromImage(codeBmp))
                                using (var graphicCache = new GraphicCache())
                                {
                                    graphics.Clear(Color.White);
                                    barcode.Draw(new FRPaintEventArgs(graphics, 1, 1, graphicCache));

                                    imageSize.Width  = codeBmp.Width;
                                    imageSize.Height = codeBmp.Height;

                                    var codeStream = new MemoryStream();
                                    codeBmp.Save(codeStream, ImageFormat.Png);
                                    codeStream.Seek(0, SeekOrigin.Begin);

                                    return(codeStream);
                                }
                    }
                }
                catch
                {
                }
            }

            return(null);
        }