Exemplo n.º 1
0
        static void Main(string[] args)
        {
            DateTime now      = DateTime.Now;
            string   fileName = "ticket.pdf";

            fileName = Guid.NewGuid().ToString("D").ToUpper() + ".pdf";
            PdfDocument document = new PdfDocument();

            document.Info.Title    = "FACTURA DE VENTA";
            document.Info.Author   = "";
            document.Info.Subject  = "";
            document.Info.Keywords = "Other Words";

            double marginLeft  = 42.52;
            double marginRight = 42.52;
            double marginTop   = 42.52;
            double cuadrito    = 14.173228346457;

            LayoutHelper helper   = new LayoutHelper(document, marginTop, XUnit.FromCentimeter(29.7 - 2.5));
            string       logoPath = @"C:\Users\gusvo\Desktop\logo.jpg";

            logoPath = @"C:\Users\Alejandro Sierra\Desktop\logo.png";

            XImage image = XImage.FromFile(logoPath);

            marginTop = helper.GetLinePosition(XUnit.FromCentimeter(29.7 - 2.5));
            //Debug control
            Debug.WriteLine("marginTop= " + marginTop.ToString());

            helper.Gfx.DrawImage(image, marginLeft, marginTop, 200, 100);
            XPen pen = new XPen(XColors.Plum, 4.7);

            string[] company = { "Supermercado el dorado", "Direccion: Cra 90 bis #76-51", "Telefono: 3212261759", "ID: 1016072267", "Correo: [email protected]" };
            string[] head    = { "FACTURA DE VENTA", DateTime.UtcNow.ToShortDateString(), "#FACT-11111" };
            string[] client  = { "Cliente: Gustavo Alejandro Sierra", "Correo: [email protected]", "Telefono: 3212261759", "ID: 1016072267", "Direccion: Cra 90 bis #76-51" };
            // Create a font
            XFont fontCompany = new XFont("Arial", 8, XFontStyle.Regular);
            XFont fontClient  = new XFont("Arial", 8, XFontStyle.Regular);
            XFont fontHead    = new XFont("Arial", 11, XFontStyle.Regular);
            XFont fontFooter  = new XFont("Arial", 11, XFontStyle.Regular);
            //fact head
            double headHeight = marginTop;

            // Draw the HEADtext
            for (int i = 0; i < head.Length; i++)
            {
                helper.Gfx.DrawString(head[i], fontHead, head.Length == i + 1 ? XBrushes.Gray : XBrushes.Black,
                                      new XRect(helper.Page.Width - helper.Page.Width / 3, marginTop, helper.Page.Width / 3 - marginLeft, headHeight), XStringFormats.CenterRight);
                headHeight += 30;
            }

            int clientNameHeight = 110;

            // Draw the CLIENTtext
            for (int i = 0; i < client.Length; i++)
            {
                clientNameHeight += 8;
                helper.Gfx.DrawString(client[i], fontClient, XBrushes.Black,
                                      new XRect(marginLeft, clientNameHeight, 306, clientNameHeight), XStringFormats.CenterLeft);
            }

            int companyMarginHeight = 110;

            // Draw the COMPANYtext
            for (int i = 0; i < company.Length; i++)
            {
                companyMarginHeight += 8;
                helper.Gfx.DrawString(company[i], fontCompany, XBrushes.Black,
                                      new XRect(helper.Page.Width - helper.Page.Width / 3, companyMarginHeight, 306, companyMarginHeight), XStringFormats.CenterLeft);
            }

            XFont fontSubTitle = new XFont("Arial", 11, XFontStyle.Bold);

            //Draw the GridDetails
            string[] gridTitles          = { "Referencia", "Producto", "Tasa de impuesto", "Precio unitario", "Cant.", "Total" };
            string[] gridMovementsTitles = { "Referencia", "Producto", "Tasa de impuesto", "Precio unitario", "Cant.", "Total" };
            string[] list1 = new string[3] {
                "$20.000", "Pago parcial", "26/10/2020"
            };
            string[] list2 = new string[3] {
                "$20.000", "Pago parcial", "26/10/2020"
            };
            string[] list3 = new string[3] {
                "$20.000", "Pago parcial", "26/10/2020"
            };
            string[] list4 = new string[3] {
                "$20.000", "Pago parcial", "26/10/2020"
            };
            string[] list5 = new string[3] {
                "$20.000", "Pago parcial", "26/10/2020"
            };
            string[] list6 = new string[3] {
                "$20.000", "Pago parcial", "26/10/2020"
            };
            string[] list7 = new string[3] {
                "$20.000", "Pago parcial", "26/10/2020"
            };
            string[] list8 = new string[3] {
                "$20.000", "Pago parcial", "26/10/2020"
            };
            string[] list9 = new string[3] {
                "$20.000", "Pago parcial", "26/10/2020"
            };
            string[] list10 = new string[3] {
                "$20.000", "Pago parcial", "26/10/2020"
            };

            List <string[]> movementDetails = new List <string[]>()
            {
                list1, list2, list3, list4, list5, list6, list7, list8
                , list9, list10, list1, list10, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3
                , list9, list10, list1, list10, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3
                , list9, list10, list1, list10, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3
                , list9, list10, list1, list10, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3
                , list9, list10, list1, list10, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3
                , list9, list10, list1, list10, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3
                , list9, list10, list1, list10, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3
                , list9, list10, list1, list10, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3
                , list9, list10, list1, list10, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3
                , list9, list10, list1, list10, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3
                , list9, list10, list1, list10, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3
                , list9, list10, list1, list10, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3, list3
            };

            XFont fontGrid = new XFont("Arial", 9, XFontStyle.Bold);
            XPen  penRect  = new XPen(XColors.DarkGray, 0.25);

            helper.Gfx.DrawRectangle(penRect, XBrushes.WhiteSmoke, marginLeft, helper.Page.Height / 3, helper.Page.Width - (marginLeft * 2), 2 * cuadrito);
            helper.Gfx.DrawString("Valor Movimiento", fontGrid, XBrushes.Black,
                                  new XRect(marginLeft, helper.Page.Height / 3, 10 * cuadrito, 2 * cuadrito), XStringFormats.Center);
            helper.Gfx.DrawString("Descripcion", fontGrid, XBrushes.Black,
                                  new XRect((helper.Page.Width - (2 * marginLeft)) / 3, helper.Page.Height / 3, 20 * cuadrito, 2 * cuadrito), XStringFormats.Center);
            helper.Gfx.DrawString("Fecha Movimiento", fontGrid, XBrushes.Black,
                                  new XRect(helper.Page.Width - (helper.Page.Width - (2 * marginLeft)) / 3, helper.Page.Height / 3, 10 * cuadrito, 2 * cuadrito), XStringFormats.Center);

            XFont fontMovement = new XFont("Arial", 9, XFontStyle.Regular);

            double[] movementMarginLeft = { marginLeft, (helper.Page.Width - (2 * marginLeft)) / 3, helper.Page.Width - (helper.Page.Width - (2 * marginLeft)) / 3 };
            double[] movementSpacing    = { 10 * cuadrito, 20 * cuadrito, 10 * cuadrito };
            double   spacingMovement    = 2 * cuadrito + (helper.Page.Height / 3);

            foreach (var item in movementDetails)
            {
                for (int j = 0; j < item.Length; j++)
                {
                    if (spacingMovement + cuadrito > helper.Page.Height - marginTop)
                    {
                        XUnit top = helper.GetLinePosition(XUnit.FromCentimeter(29.7 - 2.5));
                        Debug.WriteLine("TOP " + j + " " + top.ToString() + ", spacingMovement: " + spacingMovement);

                        spacingMovement = top;
                    }
                    helper.Gfx.DrawString(item[j], fontMovement, XBrushes.Black,
                                          new XRect(movementMarginLeft[j], spacingMovement, movementSpacing[j], cuadrito), XStringFormats.Center);
                    helper.Gfx.DrawLine(penRect, movementMarginLeft[j], spacingMovement, movementMarginLeft[j], spacingMovement + cuadrito);
                    helper.Gfx.DrawLine(penRect, helper.Page.Width - marginRight, spacingMovement, helper.Page.Width - marginRight, spacingMovement + cuadrito);
                    helper.Gfx.DrawLine(penRect, marginLeft, spacingMovement, helper.Page.Width - marginRight, spacingMovement);
                }
                spacingMovement += cuadrito;
                helper.Gfx.DrawLine(penRect, marginLeft, spacingMovement, helper.Page.Width - marginRight, spacingMovement);
            }

            spacingMovement += cuadrito;


            helper.Gfx.DrawRectangle(penRect, XBrushes.WhiteSmoke, marginLeft, spacingMovement, helper.Page.Width - (marginLeft * 2), 2 * cuadrito);
            helper.Gfx.DrawString("Ref", fontGrid, XBrushes.Black,
                                  new XRect(marginLeft, spacingMovement, 50, 2 * cuadrito), XStringFormats.Center);
            helper.Gfx.DrawString("Producto", fontGrid, XBrushes.Black,
                                  new XRect(marginLeft + 50, spacingMovement, 300, 2 * cuadrito), XStringFormats.Center);
            helper.Gfx.DrawString("Precio", fontGrid, XBrushes.Black,
                                  new XRect(marginLeft + 50 + 250 + 50, spacingMovement, 70, cuadrito), XStringFormats.Center);
            helper.Gfx.DrawString("Unitario", fontGrid, XBrushes.Black,
                                  new XRect(marginLeft + 50 + 250 + 50, spacingMovement + cuadrito, 70, cuadrito), XStringFormats.Center);
            helper.Gfx.DrawString("Cant.", fontGrid, XBrushes.Black,
                                  new XRect(marginLeft + 50 + 250 + 50 + 70, spacingMovement, 50, 2 * cuadrito), XStringFormats.Center);
            helper.Gfx.DrawString("SubTotal", fontGrid, XBrushes.Black,
                                  new XRect(marginLeft + 50 + 250 + 50 + 70 + 50, spacingMovement, 55, 2 * cuadrito), XStringFormats.Center);

            XFont fontDetails = new XFont("Arial", 9, XFontStyle.Regular);

            string[]        product1       = { "0001", "COCA COLA ZERO", "2680", "1", "50.000" };
            string[]        product2       = { "0001", "COCA COLA ZERO", "2680", "1", "2680" };
            string[]        product3       = { "0001", "COCA COLA ZERO", "2680", "1", "1.000.000" };
            string[]        product4       = { "0001", "COCA COLA ZERO", "2680", "1", "2680" };
            string[]        product5       = { "0001", "COCA COLA ZERO", "2680", "1", "26.850" };
            string[]        product6       = { "0001", "COCA COLA ZERO", "2680", "1", "2680" };
            string[]        product7       = { "0001", "COCA COLA ZERO", "2680", "1", "2680" };
            string[]        product8       = { "0001", "COCA COLA ZERO", "2680", "1", "122.680" };
            string[]        product9       = { "0001", "COCA COLA ZERO", "2680", "1", "2680" };
            string[]        product10      = { "0001", "COCA COLA ZERO", "2680", "1", "2680" };
            string[]        product11      = { "0001", "COCA COLA ZERO", "2680", "1", "2680" };
            string[]        product12      = { "0001", "COCA COLA ZERO", "2680", "1", "2.680.899" };
            double[]        gridmarginLeft = { marginLeft, marginLeft + 50, marginLeft + 50 + 250 + 50, marginLeft + 50 + 250 + 50 + 70, marginLeft + 50 + 250 + 50 + 70 + 50 };
            double[]        gridSpacing    = { 50, 300, 70, 50, 55 };
            string[]        totals         = { "$580.000", "Envio gratis", "$0", "$0", "$680", "$1.200.000" };
            List <string[]> products       = new List <string[]>()
            {
                product1, product2, product3, product3, product3, product3, product3, product3, product3, product4, product5,
                product6, product7, product8, product9, product10, product11, product12,
                product6, product7, product8, product9, product10, product11, product12,
                product6, product7, product8, product9, product10, product11, product12,
                product6, product7, product8, product9, product10, product11, product12,
                product6, product7, product8, product9, product10, product11, product12,
                product6, product7, product8, product9, product10, product11, product12,
                product6, product7, product8, product9, product10, product11, product12,
                product6, product7, product8, product9, product10, product11, product12,
                product6, product7, product8, product9, product10, product11, product12,
                product6, product7, product8, product9, product10, product11, product12,
                product6, product7, product8, product9, product10, product11, product12,
                product6, product7, product8, product9, product10, product11, product12
            };

            double spacingProducts = 2 * cuadrito + spacingMovement;

            foreach (var item in products)
            {
                for (int i = 0; i < item.Length; i++)
                {
                    if (spacingProducts + cuadrito > helper.Page.Height - marginTop)
                    {
                        XUnit top = helper.GetLinePosition(XUnit.FromCentimeter(29.7 - 2.5));
                        spacingProducts = top;
                    }
                    else
                    {
                        helper.Gfx.DrawLine(penRect, marginLeft, spacingProducts, helper.Page.Width - marginRight, spacingProducts);
                    }
                    helper.Gfx.DrawString(item[i], fontDetails, XBrushes.Black,
                                          new XRect(gridmarginLeft[i], spacingProducts, gridSpacing[i], cuadrito), XStringFormats.Center);
                    helper.Gfx.DrawLine(penRect, gridmarginLeft[i], spacingProducts, gridmarginLeft[i], spacingProducts + cuadrito);
                    helper.Gfx.DrawLine(penRect, helper.Page.Width - marginRight, spacingProducts, helper.Page.Width - marginRight, spacingProducts + cuadrito);
                }
                spacingProducts += cuadrito;
                helper.Gfx.DrawLine(penRect, marginRight, spacingProducts, helper.Page.Width - marginRight, spacingProducts);
            }

            XTextFormatter tf = new XTextFormatter(helper.Gfx);

            string[] footerFields        = { "Pago Transacción", "Pago Efectivo", "Detalles" };
            string[] footerDetails       = { "$1.000", "$1.000", "Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo Cliente Nuevo " };
            double[] footerFieldsSpacing = { 8 * cuadrito, marginLeft + 12 * cuadrito };
            double   totalSpacing        = spacingProducts;

            string[] totalFields        = { "Subtotal", "Gastos de envío", "Impuesto1", "Impuesto2", "Descuento", "TOTAL" };
            double[] totalFieldsSpacing = { marginLeft + 50 + 250 + 50 };

            if (spacingProducts + 7 * cuadrito > helper.Page.Height - marginTop)
            {
                XUnit top = helper.GetLinePosition(XUnit.FromCentimeter(29.7 - 2.5));
                spacingProducts = totalSpacing = top;
            }
            else
            {
                totalSpacing = spacingProducts += cuadrito;
            }
            helper.Gfx.DrawRectangle(penRect, XBrushes.White, marginLeft, totalSpacing, 24 * cuadrito, 8 * cuadrito);
            helper.Gfx.DrawRectangle(penRect, XBrushes.WhiteSmoke, marginLeft + 50 + 250 + 50, totalSpacing, 9 * cuadrito, 8 * cuadrito);
            helper.Gfx.DrawRectangle(penRect, XBrushes.White, marginLeft + 50 + 250 + 50 + 70 + 50, totalSpacing, 4 * cuadrito, 8 * cuadrito);
            for (int i = 0; i < footerFields.Length; i++)
            {
                helper.Gfx.DrawString(footerFields[i], fontGrid, XBrushes.Black,
                                      new XRect(marginLeft + cuadrito, spacingProducts, marginLeft + footerFieldsSpacing[0], cuadrito), XStringFormats.CenterLeft);

                helper.Gfx.DrawLine(penRect, marginLeft, spacingProducts, marginLeft + 24 * cuadrito, spacingProducts);

                if (footerDetails.Length - 1 == i)
                {
                    tf.DrawString(footerDetails[i], fontDetails, XBrushes.Black,
                                  new XRect(marginLeft + cuadrito, spacingProducts + cuadrito, 22 * cuadrito, 5 * cuadrito), XStringFormats.TopLeft);
                }
                else
                {
                    helper.Gfx.DrawString(footerDetails[i], fontDetails, XBrushes.Black,
                                          new XRect(footerFieldsSpacing[1], spacingProducts, marginLeft + footerFieldsSpacing[0], cuadrito), XStringFormats.CenterRight);
                }

                helper.Gfx.DrawLine(penRect, marginLeft, spacingProducts, marginLeft + 24 * cuadrito, spacingProducts);

                spacingProducts += cuadrito;
            }
            for (int i = 0; i < totalFields.Length; i++)
            {
                helper.Gfx.DrawString(totalFields[i], fontGrid, XBrushes.Black,
                                      new XRect(totalFieldsSpacing[0], totals.Length == i + 1 ? totalSpacing + cuadrito : totalSpacing, 9 * cuadrito, cuadrito), XStringFormats.Center);
                helper.Gfx.DrawString(totals[i], totals.Length == i + 1 ? fontGrid : fontDetails, XBrushes.Black,
                                      new XRect(marginLeft + 50 + 250 + 50 + 70 + 50, totals.Length == i + 1 ? totalSpacing + cuadrito : totalSpacing, 4 * cuadrito, cuadrito), XStringFormats.Center);
                totalSpacing += cuadrito;
            }


            //612pt*792pt letter paper is the same to A4
            Debug.WriteLine("seconds= " + (DateTime.Now - now).TotalSeconds.ToString());
            //Saving
            document.Save(fileName);
            //start view
            Process.Start(fileName);
        }
Exemplo n.º 2
0
        private void CalculateImageDimensions()
        {
            ImageFormatInfo formatInfo = (ImageFormatInfo)this.renderInfo.FormatInfo;

            if (formatInfo.Failure == ImageFailure.None)
            {
                XImage xImage = null;
                try
                {
                    xImage = XImage.FromImageSource(formatInfo.ImageSource);
                }
                catch (InvalidOperationException ex)
                {
                    Debug.WriteLine(string.Format(AppResources.InvalidImageType, ex.Message));
                    formatInfo.Failure = ImageFailure.InvalidType;
                }

                try
                {
                    XUnit usrWidth     = image.Width.Point;
                    XUnit usrHeight    = image.Height.Point;
                    bool  usrWidthSet  = !this.image.IsNull("Width");
                    bool  usrHeightSet = !this.image.IsNull("Height");

                    XUnit resultWidth  = usrWidth;
                    XUnit resultHeight = usrHeight;

                    double xPixels          = xImage.PixelWidth;
                    bool   usrResolutionSet = !image.IsNull("Resolution");

                    double horzRes        = usrResolutionSet ? (double)image.Resolution : xImage.HorizontalResolution;
                    XUnit  inherentWidth  = XUnit.FromInch(xPixels / horzRes);
                    double yPixels        = xImage.PixelHeight;
                    double vertRes        = usrResolutionSet ? (double)image.Resolution : xImage.VerticalResolution;
                    XUnit  inherentHeight = XUnit.FromInch(yPixels / vertRes);

                    bool lockRatio = this.image.IsNull("LockAspectRatio") ? true : image.LockAspectRatio;

                    double scaleHeight    = this.image.ScaleHeight;
                    double scaleWidth     = this.image.ScaleWidth;
                    bool   scaleHeightSet = !this.image.IsNull("ScaleHeight");
                    bool   scaleWidthSet  = !this.image.IsNull("ScaleWidth");

                    if (lockRatio)
                    {
                        if (usrWidthSet && usrHeightSet)
                        {
                            if (inherentHeight / usrHeight > inherentWidth / usrWidth)
                            {
                                usrWidthSet = false;
                            }
                            else
                            {
                                usrHeightSet = false;
                            }
                        }
                        if (usrWidthSet && !usrHeightSet)
                        {
                            resultHeight = inherentHeight / inherentWidth * usrWidth;
                        }
                        else if (usrHeightSet && !usrWidthSet)
                        {
                            resultWidth = inherentWidth / inherentHeight * usrHeight;
                        }
                        else if (!usrHeightSet && !usrWidthSet)
                        {
                            resultHeight = inherentHeight;
                            resultWidth  = inherentWidth;
                        }

                        if (scaleHeightSet || scaleHeightSet && scaleWidthSet && scaleHeight < scaleWidth)
                        {
                            resultHeight = resultHeight * scaleHeight;
                            resultWidth  = resultWidth * scaleHeight;
                        }
                        else if (scaleWidthSet || scaleHeightSet && scaleWidthSet && scaleHeight > scaleWidth)
                        {
                            resultHeight = resultHeight * scaleWidth;
                            resultWidth  = resultWidth * scaleWidth;
                        }
                    }
                    else
                    {
                        if (!usrHeightSet)
                        {
                            resultHeight = inherentHeight;
                        }

                        if (!usrWidthSet)
                        {
                            resultWidth = inherentWidth;
                        }

                        if (scaleHeightSet)
                        {
                            resultHeight = resultHeight * scaleHeight;
                        }
                        if (scaleWidthSet)
                        {
                            resultWidth = resultWidth * scaleWidth;
                        }
                    }

                    formatInfo.CropWidth  = (int)xPixels;
                    formatInfo.CropHeight = (int)yPixels;
                    if (!this.image.IsNull("PictureFormat"))
                    {
                        PictureFormat picFormat = this.image.PictureFormat;
                        //Cropping in pixels.
                        XUnit cropLeft   = picFormat.CropLeft.Point;
                        XUnit cropRight  = picFormat.CropRight.Point;
                        XUnit cropTop    = picFormat.CropTop.Point;
                        XUnit cropBottom = picFormat.CropBottom.Point;
                        formatInfo.CropX       = (int)(horzRes * cropLeft.Inch);
                        formatInfo.CropY       = (int)(vertRes * cropTop.Inch);
                        formatInfo.CropWidth  -= (int)(horzRes * ((XUnit)(cropLeft + cropRight)).Inch);
                        formatInfo.CropHeight -= (int)(vertRes * ((XUnit)(cropTop + cropBottom)).Inch);

                        //Scaled cropping of the height and width.
                        double xScale = resultWidth / inherentWidth;
                        double yScale = resultHeight / inherentHeight;

                        cropLeft   = xScale * cropLeft;
                        cropRight  = xScale * cropRight;
                        cropTop    = yScale * cropTop;
                        cropBottom = yScale * cropBottom;

                        resultHeight = resultHeight - cropTop - cropBottom;
                        resultWidth  = resultWidth - cropLeft - cropRight;
                    }
                    if (resultHeight <= 0 || resultWidth <= 0)
                    {
                        formatInfo.Width  = XUnit.FromCentimeter(2.5);
                        formatInfo.Height = XUnit.FromCentimeter(2.5);
                        Debug.WriteLine(AppResources.EmptyImageSize);
                        this.failure = ImageFailure.EmptySize;
                    }
                    else
                    {
                        formatInfo.Width  = resultWidth;
                        formatInfo.Height = resultHeight;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(string.Format(AppResources.ImageNotReadable, image.Source.ToString(), ex.Message));
                    formatInfo.Failure = ImageFailure.NotRead;
                }
                finally
                {
                    if (xImage != null)
                    {
                        xImage.Dispose();
                    }
                }
            }
            if (formatInfo.Failure != ImageFailure.None)
            {
                if (!this.image.IsNull("Width"))
                {
                    formatInfo.Width = this.image.Width.Point;
                }
                else
                {
                    formatInfo.Width = XUnit.FromCentimeter(2.5);
                }

                if (!this.image.IsNull("Height"))
                {
                    formatInfo.Height = this.image.Height.Point;
                }
                else
                {
                    formatInfo.Height = XUnit.FromCentimeter(2.5);
                }
                return;
            }
        }
Exemplo n.º 3
0
        private void CalculateImageDimensions()
        {
            ImageFormatInfo formatInfo = (ImageFormatInfo)_renderInfo.FormatInfo;

            if (formatInfo.Failure == ImageFailure.None)
            {
                XImage xImage = null;
                try
                {
                    //xImage = XImage.FromFile(_imageFilePath);
                    xImage = CreateXImage(_imageFilePath);
                }
                catch (InvalidOperationException ex)
                {
                    Debug.WriteLine(Messages2.InvalidImageType(ex.Message));
                    formatInfo.Failure = ImageFailure.InvalidType;
                }

                if (formatInfo.Failure == ImageFailure.None)
                {
                    try
                    {
                        XUnit usrWidth     = _image.Width.Point;
                        XUnit usrHeight    = _image.Height.Point;
                        bool  usrWidthSet  = !_image._width.IsNull;
                        bool  usrHeightSet = !_image._height.IsNull;

                        XUnit resultWidth  = usrWidth;
                        XUnit resultHeight = usrHeight;

                        Debug.Assert(xImage != null);
                        double xPixels          = xImage.PixelWidth;
                        bool   usrResolutionSet = !_image._resolution.IsNull;

                        double horzRes = usrResolutionSet ? _image.Resolution : xImage.HorizontalResolution;
                        double vertRes = usrResolutionSet ? _image.Resolution : xImage.VerticalResolution;

// ReSharper disable CompareOfFloatsByEqualityOperator
                        if (horzRes == 0 && vertRes == 0)
                        {
                            horzRes = 72;
                            vertRes = 72;
                        }
                        else if (horzRes == 0)
                        {
                            Debug.Assert(false, "How can this be?");
                            horzRes = 72;
                        }
                        else if (vertRes == 0)
                        {
                            Debug.Assert(false, "How can this be?");
                            vertRes = 72;
                        }
                        // ReSharper restore CompareOfFloatsByEqualityOperator

                        XUnit  inherentWidth  = XUnit.FromInch(xPixels / horzRes);
                        double yPixels        = xImage.PixelHeight;
                        XUnit  inherentHeight = XUnit.FromInch(yPixels / vertRes);

                        //bool lockRatio = _image.IsNull("LockAspectRatio") ? true : _image.LockAspectRatio;
                        bool lockRatio = _image._lockAspectRatio.IsNull || _image.LockAspectRatio;

                        double scaleHeight = _image.ScaleHeight;
                        double scaleWidth  = _image.ScaleWidth;
                        //bool scaleHeightSet = !_image.IsNull("ScaleHeight");
                        //bool scaleWidthSet = !_image.IsNull("ScaleWidth");
                        bool scaleHeightSet = !_image._scaleHeight.IsNull;
                        bool scaleWidthSet  = !_image._scaleWidth.IsNull;

                        if (lockRatio && !(scaleHeightSet && scaleWidthSet))
                        {
                            if (usrWidthSet && !usrHeightSet)
                            {
                                resultHeight = inherentHeight / inherentWidth * usrWidth;
                            }
                            else if (usrHeightSet && !usrWidthSet)
                            {
                                resultWidth = inherentWidth / inherentHeight * usrHeight;
                            }
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
                            else if (!usrHeightSet && !usrWidthSet)
                            {
                                resultHeight = inherentHeight;
                                resultWidth  = inherentWidth;
                            }

                            if (scaleHeightSet)
                            {
                                resultHeight = resultHeight * scaleHeight;
                                resultWidth  = resultWidth * scaleHeight;
                            }
                            if (scaleWidthSet)
                            {
                                resultHeight = resultHeight * scaleWidth;
                                resultWidth  = resultWidth * scaleWidth;
                            }
                        }
                        else
                        {
                            if (!usrHeightSet)
                            {
                                resultHeight = inherentHeight;
                            }

                            if (!usrWidthSet)
                            {
                                resultWidth = inherentWidth;
                            }

                            if (scaleHeightSet)
                            {
                                resultHeight = resultHeight * scaleHeight;
                            }
                            if (scaleWidthSet)
                            {
                                resultWidth = resultWidth * scaleWidth;
                            }
                        }

                        formatInfo.CropWidth  = (int)xPixels;
                        formatInfo.CropHeight = (int)yPixels;
                        if (_image._pictureFormat != null && !_image._pictureFormat.IsNull())
                        {
                            PictureFormat picFormat = _image.PictureFormat;
                            //Cropping in pixels.
                            XUnit cropLeft   = picFormat.CropLeft.Point;
                            XUnit cropRight  = picFormat.CropRight.Point;
                            XUnit cropTop    = picFormat.CropTop.Point;
                            XUnit cropBottom = picFormat.CropBottom.Point;
                            formatInfo.CropX       = (int)(horzRes * cropLeft.Inch);
                            formatInfo.CropY       = (int)(vertRes * cropTop.Inch);
                            formatInfo.CropWidth  -= (int)(horzRes * ((XUnit)(cropLeft + cropRight)).Inch);
                            formatInfo.CropHeight -= (int)(vertRes * ((XUnit)(cropTop + cropBottom)).Inch);

                            //Scaled cropping of the height and width.
                            double xScale = resultWidth / inherentWidth;
                            double yScale = resultHeight / inherentHeight;

                            cropLeft   = xScale * cropLeft;
                            cropRight  = xScale * cropRight;
                            cropTop    = yScale * cropTop;
                            cropBottom = yScale * cropBottom;

                            resultHeight = resultHeight - cropTop - cropBottom;
                            resultWidth  = resultWidth - cropLeft - cropRight;
                        }
                        if (resultHeight <= 0 || resultWidth <= 0)
                        {
                            formatInfo.Width  = XUnit.FromCentimeter(2.5);
                            formatInfo.Height = XUnit.FromCentimeter(2.5);
                            Debug.WriteLine(Messages2.EmptyImageSize);
                            _failure = ImageFailure.EmptySize;
                        }
                        else
                        {
                            formatInfo.Width  = resultWidth;
                            formatInfo.Height = resultHeight;
                        }
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(Messages2.ImageNotReadable(_image.Name, ex.Message));
                        formatInfo.Failure = ImageFailure.NotRead;
                    }
                    finally
                    {
                        if (xImage != null)
                        {
                            xImage.Dispose();
                        }
                    }
                }
            }
            if (formatInfo.Failure != ImageFailure.None)
            {
                if (!_image._width.IsNull)
                {
                    formatInfo.Width = _image.Width.Point;
                }
                else
                {
                    formatInfo.Width = XUnit.FromCentimeter(2.5);
                }

                if (!_image._height.IsNull)
                {
                    formatInfo.Height = _image.Height.Point;
                }
                else
                {
                    formatInfo.Height = XUnit.FromCentimeter(2.5);
                }
            }
        }