Exemplo n.º 1
0
 public HtmlFont(FontStyle style, FontVariant variant, FontWeight weight, Unit size, FontFamily family)
 {
     this.style = style;
     this.variant = variant;
     this.family = family;
     this.weight = weight;
     this.size = size;
 }
Exemplo n.º 2
0
        private OpenXmlElement CreateImageElement(string relationshipId, string imageName)
        {
            var widthInEmus = new Unit(UnitMetric.Pixel, _customSize.Width).ValueInEmus;
            var heightInEmus = new Unit(UnitMetric.Pixel, _customSize.Height).ValueInEmus;
            var drawingId = GetNextDrawingId();
            var imageId = GetNextImageId();

            var element = new Drawing(
                new DW.Inline(
                    new DW.Extent() { Cx = widthInEmus, Cy = heightInEmus },
                    new DW.EffectExtent() { LeftEdge = 19050L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
                    new DW.DocProperties() { Id = drawingId, Name = imageName, Description = string.Empty },
                    new DW.NonVisualGraphicFrameDrawingProperties
                    {
                        GraphicFrameLocks = new A.GraphicFrameLocks() { NoChangeAspect = true }
                    },
                    new A.Graphic(
                        new A.GraphicData(
                            new Pic.Picture(
                                new Pic.NonVisualPictureProperties
                                {
                                    NonVisualDrawingProperties = new Pic.NonVisualDrawingProperties() { Id = imageId, Name = imageName, Description = string.Empty },
                                    NonVisualPictureDrawingProperties = new Pic.NonVisualPictureDrawingProperties(
                                        new A.PictureLocks() { NoChangeAspect = true, NoChangeArrowheads = true })
                                },
                                new Pic.BlipFill(
                                    new A.Blip() { Embed = relationshipId },
                                    new A.SourceRectangle(),
                                    new A.Stretch(
                                        new A.FillRectangle())),
                                new Pic.ShapeProperties(
                                    new A.Transform2D(
                                        new A.Offset() { X = 0L, Y = 0L },
                                        new A.Extents() { Cx = widthInEmus, Cy = heightInEmus }),
                                    new A.PresetGeometry(
                                        new A.AdjustValueList()
                                    ) { Preset = A.ShapeTypeValues.Rectangle }
                                ) { BlackWhiteMode = A.BlackWhiteModeValues.Auto })
                        ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                ) { DistanceFromTop = (UInt32Value)0U, DistanceFromBottom = (UInt32Value)0U, DistanceFromLeft = (UInt32Value)0U, DistanceFromRight = (UInt32Value)0U }
            );

            return element;
        }
Exemplo n.º 3
0
        private Drawing AddImagePart(Uri imageUrl, String imageSource, String alt, Size preferredSize)
        {
            if (imageObjId == UInt32.MinValue)
            {
                // In order to add images in the document, we need to asisgn an unique id
                // to each Drawing object. So we'll loop through all of the existing <wp:docPr> elements
                // to find the largest Id, then increment it for each new image.

                drawingObjId = 1; // 1 is the minimum ID set by MS Office.
                imageObjId = 1;
                foreach (var d in mainPart.Document.Body.Descendants<Drawing>())
                {
                    if (d.Inline == null) continue; // fix some rare issue where Inline is null (reported by scwebgroup)
                    if (d.Inline.DocProperties.Id > drawingObjId) drawingObjId = d.Inline.DocProperties.Id;

                    var nvPr = d.Inline.Graphic.GraphicData.GetFirstChild<pic.NonVisualPictureProperties>();
                    if (nvPr != null && nvPr.NonVisualDrawingProperties.Id > imageObjId)
                        imageObjId = nvPr.NonVisualDrawingProperties.Id;
                }
                if (drawingObjId > 1) drawingObjId++;
                if (imageObjId > 1) imageObjId++;
            }

            // Cache all the ImagePart processed to avoid downloading the same image.
            CachedImagePart imagePart;
            if (!knownImageParts.TryGetValue(imageUrl, out imagePart))
            {
                ProvisionImageEventArgs e = new ProvisionImageEventArgs(imageUrl);
                e.ImageSize = preferredSize;
                if (this.ImageProcessing == ImageProcessing.AutomaticDownload && imageUrl.IsAbsoluteUri)
                {
                    e.Data = ConverterUtility.DownloadData(imageUrl, this.WebProxy);
                }
                else
                {
                    RaiseProvisionImage(e);
                }

                if (e.Data == null) return null;

                if (!e.ImageExtension.HasValue)
                {
                    e.ImageExtension = ConverterUtility.GetImagePartTypeForImageUrl(imageUrl);
                    if (!e.ImageExtension.HasValue) return null;
                }

                ImagePart ipart = mainPart.AddImagePart(e.ImageExtension.Value);
                imagePart = new CachedImagePart() { Part = ipart };

                using (Stream outputStream = ipart.GetStream(FileMode.Create))
                {
                    outputStream.Write(e.Data, 0, e.Data.Length);
                    outputStream.Seek(0L, SeekOrigin.Begin);

                    if (e.ImageSize.Width == 0 || e.ImageSize.Height == 0)
                    {
                        e.ImageSize = ConverterUtility.GetImageSize(outputStream);
                    }
                    imagePart.Width = e.ImageSize.Width;
                    imagePart.Height = e.ImageSize.Height;
                }

                knownImageParts.Add(imageUrl, imagePart);
            }

            if (preferredSize.IsEmpty)
            {
                preferredSize.Width = imagePart.Width;
                preferredSize.Height = imagePart.Height;
            }
            else if (preferredSize.Width <= 0 || preferredSize.Height <= 0)
            {
                Size actualSize = new Size(imagePart.Width, imagePart.Height);
                preferredSize = ImageHeader.KeepAspectRatio(actualSize, preferredSize);
            }

            String imagePartId = mainPart.GetIdOfPart(imagePart.Part);
            long widthInEmus = new Unit(UnitMetric.Pixel, preferredSize.Width).ValueInEmus;
            long heightInEmus = new Unit(UnitMetric.Pixel, preferredSize.Height).ValueInEmus;

            ++drawingObjId;
            ++imageObjId;

            var img = new Drawing(
                new wp.Inline(
                    new wp.Extent() { Cx = widthInEmus, Cy = heightInEmus },
                    new wp.EffectExtent() { LeftEdge = 19050L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
                    new wp.DocProperties() { Id = drawingObjId, Name = imageSource, Description = String.Empty },
                    new wp.NonVisualGraphicFrameDrawingProperties(
                        new a.GraphicFrameLocks() { NoChangeAspect = true }),
                    new a.Graphic(
                        new a.GraphicData(
                            new pic.Picture(
                                new pic.NonVisualPictureProperties(
                                    new pic.NonVisualDrawingProperties() { Id = imageObjId, Name = imageSource, Description = alt },
                                    new pic.NonVisualPictureDrawingProperties(
                                        new a.PictureLocks() { NoChangeAspect = true, NoChangeArrowheads = true })),
                                new pic.BlipFill(
                                    new a.Blip() { Embed = imagePartId },
                                    new a.SourceRectangle(),
                                    new a.Stretch(
                                        new a.FillRectangle())),
                                new pic.ShapeProperties(
                                    new a.Transform2D(
                                        new a.Offset() { X = 0L, Y = 0L },
                                        new a.Extents() { Cx = widthInEmus, Cy = heightInEmus }),
                                    new a.PresetGeometry(
                                        new a.AdjustValueList()
                                    ) { Preset = a.ShapeTypeValues.Rectangle }
                                ) { BlackWhiteMode = a.BlackWhiteModeValues.Auto })
                        ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                ) { DistanceFromTop = (UInt32Value) 0U, DistanceFromBottom = (UInt32Value) 0U, DistanceFromLeft = (UInt32Value) 0U, DistanceFromRight = (UInt32Value) 0U }
            );

            return img;
        }
Exemplo n.º 4
0
 public Margin(Unit top, Unit right, Unit bottom, Unit left)
 {
     this.sides = new[] { top, right, bottom, left };
 }
Exemplo n.º 5
0
        /// <summary>
        /// Convert Html regular font-size to OpenXml font value (expressed in point).
        /// </summary>
        public static Unit ConvertToFontSize(string htmlSize)
        {
            if (htmlSize == null) return Unit.Empty;
            switch (htmlSize.ToLowerInvariant())
            {
                case "1":
                case "xx-small": return new Unit(UnitMetric.Point, 10);
                case "2":
                case "x-small": return new Unit(UnitMetric.Point, 15);
                case "3":
                case "small": return new Unit(UnitMetric.Point, 20);
                case "4":
                case "medium": return new Unit(UnitMetric.Point, 27);
                case "5":
                case "large": return new Unit(UnitMetric.Point, 36);
                case "6":
                case "x-large": return new Unit(UnitMetric.Point, 48);
                case "7":
                case "xx-large": return new Unit(UnitMetric.Point, 72);
                default:
                    // the font-size is specified in positive half-points
                    Unit unit = Unit.Parse(htmlSize);
                    if (!unit.IsValid || unit.Value <= 0)
                        return Unit.Empty;

                    // this is a rough conversion to support some percent size, considering 100% = 11 pt
                    if (unit.Type == UnitMetric.Percent) unit = new Unit(UnitMetric.Point, unit.Value * 0.11);
                    return unit;
            }
        }
Exemplo n.º 6
0
        private Drawing AddImagePart(Uri imageUrl, String imageSource, String alt, Size preferredSize)
        {
            if (imageObjId == UInt32.MinValue)
            {
                // In order to add images in the document, we need to asisgn an unique id
                // to each Drawing object. So we'll loop through all of the existing <wp:docPr> elements
                // to find the largest Id, then increment it for each new image.

                drawingObjId = 1; // 1 is the minimum ID set by MS Office.
                imageObjId = 1;
                foreach (var d in mainPart.Document.Body.Descendants<Drawing>())
                {
                    if (d.Inline == null) continue; // fix some rare issue where Inline is null (reported by scwebgroup)
                    if (d.Inline.DocProperties.Id > drawingObjId) drawingObjId = d.Inline.DocProperties.Id;

                    var nvPr = d.Inline.Graphic.GraphicData.GetFirstChild<pic.NonVisualPictureProperties>();
                    if (nvPr != null && nvPr.NonVisualDrawingProperties.Id > imageObjId)
                        imageObjId = nvPr.NonVisualDrawingProperties.Id;
                }
                if (drawingObjId > 1) drawingObjId++;
                if (imageObjId > 1) imageObjId++;
            }

            // Cache all the ImagePart processed to avoid downloading the same image.
            CachedImagePart imagePart = null;

            // if imageUrl is null, we may consider imageSource is a DataUri.
            // thus, no need to download and cache anything
            if (imageUrl == null || !knownImageParts.TryGetValue(imageUrl, out imagePart))
            {
                HtmlImageInfo iinfo = new HtmlImageInfo() { Size = preferredSize };
                ImageProvisioningProvider provider = new ImageProvisioningProvider(this.WebProxy, iinfo);

                if (imageUrl == null)
                    provider.DownloadData(DataUri.Parse(imageSource));

                //TODO 屏蔽本地读取图片
                //else if (this.ImageProcessing == ImageProcessing.ManualProvisioning)
                //{
                //    // as HtmlImageInfo is a class, the EventArgs will act as a proxy
                //    ProvisionImageEventArgs args = new ProvisionImageEventArgs(imageUrl, iinfo);
                //    OnProvisionImage(args);

                //    // did the user want to ignore this image?
                //    if (args.Cancel) return null;
                //}

                // Automatic Processing or the user did not supply himself the image and did not cancel the provisioning.
                // We download ourself the image.
                if (iinfo.RawData == null && imageUrl.IsAbsoluteUri)
                    provider.DownloadData(imageUrl);

                if (iinfo.RawData == null || !provider.Provision(imageUrl)) return null;

                ImagePart ipart = mainPart.AddImagePart(iinfo.Type.Value);
                imagePart = new CachedImagePart() { Part = ipart };
                imagePart.Width = iinfo.Size.Width;
                imagePart.Height = iinfo.Size.Height;

                using (Stream outputStream = ipart.GetStream(FileMode.Create))
                {
                    outputStream.Write(iinfo.RawData, 0, iinfo.RawData.Length);
                    outputStream.Seek(0L, SeekOrigin.Begin);
                }

                if (imageUrl != null) // don't need to cache inlined-image
                    knownImageParts.Add(imageUrl, imagePart);
            }

            if (preferredSize.IsEmpty)
            {
                preferredSize.Width = imagePart.Width;
                preferredSize.Height = imagePart.Height;
            }
            else if (preferredSize.Width <= 0 || preferredSize.Height <= 0)
            {
                Size actualSize = new Size(imagePart.Width, imagePart.Height);
                preferredSize = ImageHeader.KeepAspectRatio(actualSize, preferredSize);
            }

            String imagePartId = mainPart.GetIdOfPart(imagePart.Part);
            long widthInEmus = new Unit(UnitMetric.Pixel, preferredSize.Width).ValueInEmus;
            long heightInEmus = new Unit(UnitMetric.Pixel, preferredSize.Height).ValueInEmus;

            ++drawingObjId;
            ++imageObjId;

            var img = new Drawing(
                new wp.Inline(
                    new wp.Extent() { Cx = widthInEmus, Cy = heightInEmus },
                    new wp.EffectExtent() { LeftEdge = 19050L, TopEdge = 0L, RightEdge = 0L, BottomEdge = 0L },
                    new wp.DocProperties() { Id = drawingObjId, Name = imageSource, Description = String.Empty },
                    new wp.NonVisualGraphicFrameDrawingProperties {
                        GraphicFrameLocks = new a.GraphicFrameLocks() { NoChangeAspect = true }
                    },
                    new a.Graphic(
                        new a.GraphicData(
                            new pic.Picture(
                                new pic.NonVisualPictureProperties {
                                    NonVisualDrawingProperties = new pic.NonVisualDrawingProperties() { Id = imageObjId, Name = imageSource, Description = alt },
                                    NonVisualPictureDrawingProperties = new pic.NonVisualPictureDrawingProperties(
                                        new a.PictureLocks() { NoChangeAspect = true, NoChangeArrowheads = true })
                                },
                                new pic.BlipFill(
                                    new a.Blip() { Embed = imagePartId },
                                    new a.SourceRectangle(),
                                    new a.Stretch(
                                        new a.FillRectangle())),
                                new pic.ShapeProperties(
                                    new a.Transform2D(
                                        new a.Offset() { X = 0L, Y = 0L },
                                        new a.Extents() { Cx = widthInEmus, Cy = heightInEmus }),
                                    new a.PresetGeometry(
                                        new a.AdjustValueList()
                                    ) { Preset = a.ShapeTypeValues.Rectangle }
                                ) { BlackWhiteMode = a.BlackWhiteModeValues.Auto })
                        ) { Uri = "http://schemas.openxmlformats.org/drawingml/2006/picture" })
                ) { DistanceFromTop = (UInt32Value) 0U, DistanceFromBottom = (UInt32Value) 0U, DistanceFromLeft = (UInt32Value) 0U, DistanceFromRight = (UInt32Value) 0U }
            );

            return img;
        }
Exemplo n.º 7
0
 public SideBorder(w.BorderValues style, Color color, Unit size)
 {
     this.style = style;
     this.color = color;
     this.size = size;
 }
Exemplo n.º 8
0
        public static SideBorder Parse(String str)
        {
            if (str == null)
            {
                return(SideBorder.Empty);
            }

            // The properties of a border that can be set, are (in order): border-width, border-style, and border-color.
            // It does not matter if one of the values above are missing, e.g. border:solid #ff0000; is allowed.
            // The main problem for parsing this attribute is that the browsers allow any permutation of the values... meaning more coding :(
            // http://www.w3schools.com/cssref/pr_border.asp

            List <String> borderParts = new List <String>(str.Split(HttpUtility.WhiteSpaces, StringSplitOptions.RemoveEmptyEntries));

            if (borderParts.Count == 0)
            {
                return(SideBorder.Empty);
            }

            // Initialize default values
            Unit  borderWidth = Unit.Empty;
            Color borderColor = Color.Empty;

            w.BorderValues borderStyle = w.BorderValues.Nil;

            // Now try to guess the values with their permutation

            // handle border style
            for (int i = 0; i < borderParts.Count; i++)
            {
                borderStyle = ConverterUtility.ConvertToBorderStyle(borderParts[i]);
                if (borderStyle != w.BorderValues.Nil)
                {
                    borderParts.RemoveAt(i);                     // no need to process this part anymore
                    break;
                }
            }

            for (int i = 0; i < borderParts.Count; i++)
            {
                borderWidth = ParseWidth(borderParts[i]);
                if (borderWidth.IsValid)
                {
                    borderParts.RemoveAt(i);                     // no need to process this part anymore
                    break;
                }
            }

            // find width
            if (borderParts.Count > 0)
            {
                borderColor = ConverterUtility.ConvertToForeColor(borderParts[0]);
            }

            // returns the instance with default value if needed.
            // These value are the ones used by the browser, i.e: solid 3px black
            return(new SideBorder(
                       borderStyle == w.BorderValues.Nil? w.BorderValues.Single : borderStyle,
                       borderColor.IsEmpty? Color.Black : borderColor,
                       borderWidth.IsFixed? borderWidth : new Unit(UnitMetric.Pixel, 4)));
        }
Exemplo n.º 9
0
 public SideBorder(w.BorderValues style, Color color, Unit size)
 {
     this.style = style;
     this.color = color;
     this.size  = size;
 }