예제 #1
0
        public PDFImageData LoadImageData(IPDFDocument document, IPDFComponent owner, string path)
        {
            try
            {
                var uri   = new Uri(path);
                var param = uri.GetComponents(UriComponents.Path, UriFormat.Unescaped);
                var name  = System.IO.Path.GetFileNameWithoutExtension(param);

                // Standard System.Drawing routines to draw a bitmap
                // could load an image from SQL, use parameters, whatever is needed

                Bitmap bmp = new Bitmap(300, 100);
                using (Graphics graphics = Graphics.FromImage(bmp))
                {
                    graphics.FillRectangle(new SolidBrush(Color.LightBlue), new Rectangle(0, 0, 300, 100));
                    graphics.DrawString(name, new Font("Times", 12), new SolidBrush(Color.Blue), PointF.Empty);
                    graphics.Flush();
                }
                var dir = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                var png = System.IO.Path.Combine(dir, "Temp.png");
                bmp.Save(png);

                PDFImageData data = PDFImageData.LoadImageFromBitmap(path, bmp, false);
                return(data);
            }
            catch (Exception ex)
            {
                throw new ArgumentException("The image creation failed", ex);
            }
        }
        public void PixelWidth_Test()
        {
            Bitmap       bmp      = CreateImageBitmap();
            PDFImageData target   = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false);
            int          expected = ImagePixelWidth;
            int          actual   = target.PixelWidth;

            Assert.AreEqual(expected, actual);
        }
        public void ColorsPerSample_Test()
        {
            Bitmap       bmp      = CreateImageBitmap();
            PDFImageData target   = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false);
            int          expected = ImageColorsPerSample;
            int          actual   = target.ColorsPerSample;

            Assert.AreEqual(expected, actual);
        }
        public void BytesPerLine_Test()
        {
            Bitmap       bmp      = CreateImageBitmap();
            PDFImageData target   = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false);
            int          expected = ImageBytesPerLine;
            int          actual   = (int)target.BytesPerLine;

            Assert.AreEqual(expected, actual);
        }
        public void HorizontalResolution_Test()
        {
            Bitmap       bmp      = CreateImageBitmap();
            PDFImageData target   = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false);
            int          expected = ImageResolution;
            int          actual   = target.HorizontalResolution;

            Assert.AreEqual(expected, actual);
        }
        public void LoadImageFromBitmap_Test()
        {
            string sourcekey = ImageFilePath;
            Bitmap bitmap    = CreateImageBitmap();

            PDFImageData actual = PDFImageData.LoadImageFromBitmap(sourcekey, bitmap, false);

            Assert.IsNotNull(actual);
            Assert.IsNotNull(actual.Data);
        }
        public void DisplayWidth_Test()
        {
            Bitmap       bmp      = CreateImageBitmap();
            PDFImageData target   = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false);
            double       w        = ((double)ImagePixelWidth) / ImageResolution;
            PDFUnit      expected = new PDFUnit(w, PageUnits.Inches);
            PDFUnit      actual   = target.DisplayWidth;

            Assert.AreEqual(expected, actual);
        }
        public void SourcePath_Test()
        {
            Bitmap       bmp    = CreateImageBitmap();
            PDFImageData target = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false);

            string expected = ImageFilePath;
            string actual   = target.SourcePath;

            Assert.AreEqual(expected, actual);
        }
        public void GetSize_Test()
        {
            Bitmap       bmp    = CreateImageBitmap();
            PDFImageData target = PDFImageData.LoadImageFromBitmap(ImageFilePath, bmp, false);

            //GetSize() returns actual size based on pixels and resolution
            double  w        = ((double)ImagePixelWidth) / ImageResolution;
            double  h        = ((double)ImagePixelHeight) / ImageResolution;
            PDFSize expected = new PDFSize(new PDFUnit(w, PageUnits.Inches), new PDFUnit(h, PageUnits.Inches));

            int     accuracy = 0;
            PDFSize actual;

            actual = target.GetSize();
            Assert.AreEqual(Math.Round(expected.Width.PointsValue, accuracy), Math.Round(actual.Width.PointsValue, accuracy));
            Assert.AreEqual(Math.Round(expected.Height.PointsValue, accuracy), Math.Round(actual.Height.PointsValue, accuracy));
        }
예제 #10
0
        protected override Resources.PDFImageXObject InitImageXObject(PDFContextBase context, Style style)
        {
            Document doc = this.Document;

            if (null == doc)
            {
                throw new NullReferenceException(Errors.ParentDocumentCannotBeNull);
            }

            if (null != this.Data)
            {
                _xobj = null;
                if (!string.IsNullOrEmpty(this.ImageKey))
                {
                    _xobj = this.Document.GetImageResource(this.ImageKey, this, false);
                }

                if (null == _xobj)
                {
                    string name;
                    if (string.IsNullOrEmpty(this.ImageKey))
                    {
                        name = "DataImage_" + this.Document.GetIncrementID(PDFObjectTypes.ImageXObject);
                    }
                    else
                    {
                        name = this.ImageKey;
                    }

                    System.ComponentModel.TypeConverter BitmapConverter = TypeDescriptor.GetConverter(typeof(Bitmap));
                    Bitmap img = (Bitmap)BitmapConverter.ConvertFrom(this.Data.Raw);


                    PDFImageData data = PDFImageData.LoadImageFromBitmap(name, img, this.Compress);

                    _xobj = PDFImageXObject.Load(data, name);
                    this.Document.SharedResources.Add(_xobj);
                }
            }

            return(_xobj);
        }
예제 #11
0
        private void RegisterBadgeData(PDFLayoutContext context)
        {
            _outputbadge = this.PageOwner.ShowBadge;

            if (!_outputbadge)
            {
                return;
            }

            ScryberBadgeStyle style;

            if (!this.FullStyle.TryGetItem(StyleKeys.BadgeItemKey, out style))
            {
                style = new ScryberBadgeStyle();
            }


            //get any existing badge resource registered in the document

            Document doc = context.DocumentLayout.DocumentComponent;

            string rsrcName = BadgeResourceStem;

            if (style.DisplayOption == BadgeType.BlackOnWhite)
            {
                rsrcName = rsrcName + BlackOnWhiteResourceName;
            }
            else if (style.DisplayOption == BadgeType.WhiteOnBlack)
            {
                rsrcName = rsrcName + WhiteOnBlackResourceName;
            }
            else if (style.DisplayOption == BadgeType.Environment)
            {
                rsrcName = rsrcName + EnvironmentBadgeName;
            }

            _badgexobj = doc.GetImageResource(rsrcName, this.PageOwner, false);


            if (null == _badgexobj)
            {
                //Document does not have a previous badge registed, so need to register it now.
                System.Resources.ResourceManager mgr = new System.Resources.ResourceManager("Scryber.Components.Properties.Resources", typeof(Page).Assembly);

                System.Drawing.Bitmap bmp;
                if (style.DisplayOption == BadgeType.BlackOnWhite)
                {
                    bmp = mgr.GetObject(BlackOnWhiteResourceName) as System.Drawing.Bitmap;
                }
                else if (style.DisplayOption == BadgeType.WhiteOnBlack)
                {
                    bmp = mgr.GetObject(WhiteOnBlackResourceName) as System.Drawing.Bitmap;
                }
                else
                {
                    bmp = mgr.GetObject(EnvironmentBadgeName) as System.Drawing.Bitmap;
                }

                PDFImageData data = PDFImageData.LoadImageFromBitmap(rsrcName, bmp, false);

                string name = doc.GetIncrementID(PDFObjectTypes.ImageXObject);

                _badgexobj = PDFImageXObject.Load(data, name);
                doc.SharedResources.Add(_badgexobj);
            }

            //Make sure the badge is registered with the page too
            _badgexobj.RegisterUse(this.Resources, this.PageOwner);


            // calculate the position based on the X and Y Offset plus the corners.
            _badgePosition = new PDFPoint(style.XOffset, style.YOffset);
            _badgeSize     = new PDFSize(_badgexobj.ImageData.DisplayWidth, _badgexobj.ImageData.DisplayHeight);
            PDFSize pageSize = this.Size;

            switch (style.Corner)
            {
            default:
            case Corner.TopLeft:
                //Do Nothing
                break;

            case Corner.TopRight:
                _badgePosition.X = (pageSize.Width - _badgePosition.X) - _badgeSize.Width;
                break;

            case Corner.BottomRight:
                _badgePosition.X = (pageSize.Width - _badgePosition.X) - _badgeSize.Width;
                _badgePosition.Y = (pageSize.Height - _badgePosition.Y) - _badgeSize.Height;
                break;

            case Corner.BottomLeft:
                _badgePosition.Y = (pageSize.Height - _badgePosition.Y) - _badgeSize.Height;
                break;
            }
        }