Exemplo n.º 1
0
        private IEnumerable <OXmlElement> NewLine()
        {
            //if (_imageHorizontalPosition == 0)
            //if (_images.Count == 0)
            if (_paragraph != null)
            {
                if (_trace)
                {
                    pb.Trace.WriteLine("NewLine()       : send stored paragraph");
                }
                yield return(_paragraph);

                _paragraph = null;
            }
            else if (_line != null)
            {
                if (_trace)
                {
                    pb.Trace.WriteLine("NewLine()       : send stored new line");
                }
                yield return(_line);

                _line = null;
            }
            if (_trace)
            {
                pb.Trace.WriteLine("NewLine()       : store new line");
            }
            _line = new OXmlElement {
                Type = OXmlElementType.Line
            };
        }
Exemplo n.º 2
0
 // string style = null
 private IEnumerable <OXmlElement> Paragraph()
 {
     //if (_imageHorizontalPosition == 0)
     //if (_images.Count == 0)
     //    yield return new OXmlParagraphElement { Style = style };
     if (_line != null)
     {
         if (_trace)
         {
             pb.Trace.WriteLine("Paragraph()     : remove stored new line");
         }
         _line = null;
     }
     if (_paragraph != null)
     {
         if (_trace)
         {
             pb.Trace.WriteLine("Paragraph()     : send stored paragraph");
         }
         yield return(_paragraph);
     }
     if (_trace)
     {
         pb.Trace.WriteLine("Paragraph()     : store paragraph");
     }
     _paragraph = new OXmlParagraphElement();
 }
Exemplo n.º 3
0
 private void AddText(OXmlElement element)
 {
     if (_paragraph == null)
     {
         throw new PBException("missing begin paragraph");
     }
     if (!(element is OXmlTextElement))
     {
         throw new PBException("text element must be a zDocXElementText");
     }
     _paragraph.Append(((OXmlTextElement)element).Text);
 }
Exemplo n.º 4
0
        private void AddPicture(OXmlElement element)
        {
            if (_paragraph == null)
            {
                throw new PBException("missing begin paragraph");
            }
            if (!(element is OXmlPictureElement))
            {
                throw new PBException("picture element must be a zDocXElementPicture");
            }
            OXmlPictureElement pictureElement = (OXmlPictureElement)element;
            Image   image   = _document.AddImage(pictureElement.File);
            Picture picture = image.CreatePicture();

            Trace.WriteLine("picture width {0} height {1}", picture.Width, picture.Height);
            if (pictureElement.Name != null)
            {
                picture.Name = pictureElement.Name;
            }
            if (pictureElement.Description != null)
            {
                picture.Description = pictureElement.Description;
            }
            if (pictureElement.Width != null)
            {
                picture.Width = (int)pictureElement.Width;
            }
            if (pictureElement.Height != null)
            {
                picture.Height = (int)pictureElement.Height;
            }
            //picture.Rotation = pictureElement.Rotation;
            //picture.FlipHorizontal = pictureElement.HorizontalFlip;
            //picture.FlipVertical = pictureElement.VerticalFlip;
            _paragraph.AppendPicture(picture);
            // option image from google doc :
            //   Aligner, Envelopper, Intercaler, 3.2 mm de marge
            //   In line, Wrap text, Break text, 1/8" margin
            // option image from word :
            //   disposition / position :
            //     horizontal / position absolue -0.1 cm à droite de marge
            //     vertical / position absolue 2.01 cm au dessous de paragraphe
            //     déplacer avec le texte
            //     autoriser le chevauchement de texte
        }
Exemplo n.º 5
0
        // public methods
        /// <summary>
        /// Deserializes an object from a BsonReader.
        /// </summary>
        /// <param name="bsonReader">The BsonReader.</param>
        /// <param name="nominalType">The nominal type of the object.</param>
        /// <param name="actualType">The actual type of the object.</param>
        /// <param name="options">The serialization options.</param>
        /// <returns>An object.</returns>
        // actualType ignored
        public override object Deserialize(BsonReader bsonReader, Type nominalType, Type actualType, IBsonSerializationOptions options)
        {
            if (_trace)
            {
                pb.Trace.WriteLine("OXmlElementSerializer.Deserialize()");
            }

            var bsonType = bsonReader.GetCurrentBsonType();

            if (bsonType == BsonType.Null)
            {
                bsonReader.ReadNull();
                return(null);
            }
            else if (bsonType != BsonType.Document)
            {
                throw new PBException($"deserialize OXmlElement, invalid BsonType {bsonType}.");
            }
            bsonReader.ReadStartDocument();
            string      type = bsonReader.FindStringElement("Type");
            OXmlElement value;

            switch (type.ToLower())
            {
            case "line":
                value = new OXmlElement {
                    Type = OXmlElementType.Line
                };
                break;

            case "tabstop":
                value = new OXmlElement {
                    Type = OXmlElementType.TabStop
                };
                break;

            case "paragraph":
                //value = OXmlParagraphElementSerializer.Instance.Deserialize(bsonReader, typeof(OXmlParagraphElement), options);
                value = OXmlParagraphElementSerializer.Instance._Deserialize(bsonReader, options);
                break;

            case "break":
                value = OXmlBreakElementSerializer.Instance._Deserialize(bsonReader, options);
                break;

            case "text":
                value = OXmlTextElementSerializer.Instance._Deserialize(bsonReader, options);
                break;

            case "simplefield":
                value = OXmlSimpleFieldElementSerializer.Instance._Deserialize(bsonReader, options);
                break;

            case "docsection":
                value = OXmlDocSectionElementSerializer.Instance._Deserialize(bsonReader, options);
                break;

            case "docdefaultsrunproperties":
                value = OXmlDocDefaultsRunPropertiesElementSerializer.Instance._Deserialize(bsonReader, options);
                break;

            case "docdefaultsparagraphproperties":
                //value = new OXmlDocDefaultsParagraphPropertiesElement();
                value = new OXmlElement {
                    Type = OXmlElementType.DocDefaultsParagraphProperties
                };
                break;

            case "openheader":
                value = OXmlOpenHeaderElementSerializer.Instance._Deserialize(bsonReader, options);
                break;

            case "closeheader":
                value = new OXmlElement {
                    Type = OXmlElementType.CloseHeader
                };
                break;

            case "openfooter":
                value = OXmlOpenFooterElementSerializer.Instance._Deserialize(bsonReader, options);
                break;

            case "closefooter":
                value = new OXmlElement {
                    Type = OXmlElementType.CloseFooter
                };
                break;

            case "style":
                value = OXmlStyleElementSerializer.Instance._Deserialize(bsonReader, options);
                break;

            case "picture":
                //value = OXmlPictureElementSerializer.Instance._Deserialize(bsonReader, options);
                value = OXmlPictureElementCreator.CreatePicture(zMongo.ReadBsonDocumentWOStartEnd(bsonReader));
                break;

            default:
                throw new PBException($"deserialize OXmlElement, invalid Type {type}.");
            }
            bsonReader.ReadEndDocument();
            return(value);


            //switch (bsonType)
            //{
            //    case BsonType.Array: return (ZValue)ZStringArraySerializer.Instance.Deserialize(bsonReader, typeof(ZStringArray), options);
            //    //case BsonType.Binary: return (BsonValue)BsonBinaryDataSerializer.Instance.Deserialize(bsonReader, typeof(BsonBinaryData), options);
            //    //case BsonType.Boolean: return (BsonValue)BsonBooleanSerializer.Instance.Deserialize(bsonReader, typeof(BsonBoolean), options);
            //    //case BsonType.DateTime: return (BsonValue)BsonDateTimeSerializer.Instance.Deserialize(bsonReader, typeof(BsonDateTime), options);
            //    //case BsonType.Document: return (BsonValue)BsonDocumentSerializer.Instance.Deserialize(bsonReader, typeof(BsonDocument), options);
            //    //case BsonType.Double: return (BsonValue)BsonDoubleSerializer.Instance.Deserialize(bsonReader, typeof(BsonDouble), options);
            //    case BsonType.Int32: return (ZValue)ZIntSerializer.Instance.Deserialize(bsonReader, typeof(ZInt), options);
            //    //case BsonType.Int64: return (BsonValue)BsonInt64Serializer.Instance.Deserialize(bsonReader, typeof(BsonInt64), options);
            //    //case BsonType.JavaScript: return (BsonValue)BsonJavaScriptSerializer.Instance.Deserialize(bsonReader, typeof(BsonJavaScript), options);
            //    //case BsonType.JavaScriptWithScope: return (BsonValue)BsonJavaScriptWithScopeSerializer.Instance.Deserialize(bsonReader, typeof(BsonJavaScriptWithScope), options);
            //    //case BsonType.MaxKey: return (BsonValue)BsonMaxKeySerializer.Instance.Deserialize(bsonReader, typeof(BsonMaxKey), options);
            //    //case BsonType.MinKey: return (BsonValue)BsonMinKeySerializer.Instance.Deserialize(bsonReader, typeof(BsonMinKey), options);
            //    //case BsonType.Null: return (BsonValue)BsonNullSerializer.Instance.Deserialize(bsonReader, typeof(BsonNull), options);
            //    case BsonType.Document:
            //        return ReadZValueFromBsonDocument(bsonReader);
            //    case BsonType.Null:
            //        bsonReader.ReadNull();
            //        return null;
            //    //case BsonType.ObjectId: return (BsonValue)BsonObjectIdSerializer.Instance.Deserialize(bsonReader, typeof(BsonObjectId), options);
            //    //case BsonType.RegularExpression: return (BsonValue)BsonRegularExpressionSerializer.Instance.Deserialize(bsonReader, typeof(BsonRegularExpression), options);
            //    //case BsonType.String: return (BsonValue)BsonStringSerializer.Instance.Deserialize(bsonReader, typeof(BsonString), options);
            //    case BsonType.String: return (ZValue)ZStringSerializer.Instance.Deserialize(bsonReader, typeof(ZString), options);
            //    // ZStringSerializer
            //    //case BsonType.Symbol: return (BsonValue)BsonSymbolSerializer.Instance.Deserialize(bsonReader, typeof(BsonSymbol), options);
            //    //case BsonType.Timestamp: return (BsonValue)BsonTimestampSerializer.Instance.Deserialize(bsonReader, typeof(BsonTimestamp), options);
            //    //case BsonType.Undefined: return (BsonValue)BsonUndefinedSerializer.Instance.Deserialize(bsonReader, typeof(BsonUndefined), options);
            //    default:
            //        //var message = string.Format("Invalid BsonType {0}.", bsonType);
            //        //throw new BsonInternalException(message);
            //        throw new PBException("error deserialize ZValue, invalid BsonType {0}.", bsonType);
            //}
        }
Exemplo n.º 6
0
        //private IEnumerable<OXmlElement> Image(HtmlDocNodeTagImg imgTag)
        private void Image(HtmlDocNodeTagImg imgTag)
        {
            HtmlImage htmlImage = GetHtmlImage(imgTag);

            if (htmlImage.File == null)
            {
                pb.Trace.WriteLine("can't add picture without file");
                return;
            }

            if (_trace)
            {
                pb.Trace.WriteLine($"Image()         : add picture                 \"{zPath.GetFileName(htmlImage.File)}\" width {htmlImage.Width} height {htmlImage.Height} {(htmlImage.NoneAlign ? "noneAlign " : "" )}{(htmlImage.LeftAlign ? "leftAlign " : "")}{(htmlImage.RightAlign ? "rightAlign" : "")}");
            }
            if (_paragraph != null)
            {
                if (_trace)
                {
                    pb.Trace.WriteLine("Image()         : remove stored paragraph");
                }
                _paragraph = null;
            }
            if (_line != null)
            {
                if (_trace)
                {
                    pb.Trace.WriteLine("Image()         : remove stored new line");
                }
                _line = null;
            }
            _htmlImages.Add(htmlImage);

            //int horizontalPosition;
            //int verticalPosition;
            //if (_imageHorizontalPosition > 0 && _imageHorizontalPosition + htmlImage.Width > _maxImageHorizontalPosition)
            //{
            //    horizontalPosition = 0;
            //    _imageVerticalPosition += _imageHeight + _imageMarge;
            //    verticalPosition = _imageVerticalPosition;
            //    _imageHorizontalPosition = (int)htmlImage.Width + _imageMarge;
            //    _imageHeight = (int)htmlImage.Height;
            //}
            //else
            //{
            //    horizontalPosition = _imageHorizontalPosition;
            //    _imageHorizontalPosition += (int)htmlImage.Width + _imageMarge;
            //    verticalPosition = _imageVerticalPosition;
            //    _imageHeight = Math.Max(_imageHeight, (int)htmlImage.Height);
            //}
            //yield return new OXmlPictureElement
            //{
            //    File = htmlImage.File,
            //    Width = htmlImage.Width,
            //    Height = htmlImage.Height,
            //    PictureDrawing = new OXmlAnchorPictureDrawing
            //    {
            //        // SquareSize = 21800
            //        //Wrap = new OXmlAnchorWrapTight { WrapPolygon = OXmlDoc.CreateWrapPolygon(21800) },
            //        Wrap = new OXmlAnchorWrapTight { WrapPolygon = new OXmlSquare { HorizontalSize = 21800 } },
            //        HorizontalRelativeFrom = DW.HorizontalRelativePositionValues.Margin,
            //        HorizontalPositionOffset = horizontalPosition,
            //        VerticalRelativeFrom = DW.VerticalRelativePositionValues.Paragraph,
            //        VerticalPositionOffset = verticalPosition
            //    }
            //};
        }
Exemplo n.º 7
0
        private IEnumerable <OXmlElement> Text(HtmlDocNodeText text)
        {
            //if (_imageHorizontalPosition != 0)
            //{
            //    yield return new OXmlParagraphElement();
            //    _imageHorizontalPosition = 0;
            //}

            //if (_trace)
            //    Trace.WriteLine("Text() :");

            string text2 = text.Text;

            if (_textRemoveBlank)
            {
                text2 = text2.Trim();
            }
            if (_textRemoveLine)
            {
                text2 = text2.Replace("\r", "");
                text2 = text2.Replace("\n", "");
            }
            if (text2.Length == 0)
            {
                yield break;
            }

            bool image = false;

            foreach (OXmlElement imageElement in GetImages())
            {
                yield return(imageElement);

                image = true;
            }
            if (image)
            {
                if (_line != null)
                {
                    if (_trace)
                    {
                        pb.Trace.WriteLine("Text()          : remove stored new line");
                    }
                    _line = null;
                }
                if (_paragraph != null)
                {
                    if (_trace)
                    {
                        pb.Trace.WriteLine("Text()          : send stored paragraph       after image");
                    }
                    yield return(_paragraph);

                    _paragraph = null;
                }
                else
                {
                    if (_trace)
                    {
                        pb.Trace.WriteLine("Text()          : send paragraph              after image");
                    }
                    yield return(new OXmlParagraphElement());
                }
            }
            else if (_paragraph != null)
            {
                if (_trace)
                {
                    pb.Trace.WriteLine("Text()          : send stored paragraph");
                }
                yield return(_paragraph);

                _paragraph = null;
            }
            else if (_line != null)
            {
                if (_trace)
                {
                    pb.Trace.WriteLine("Text()          : send stored new line");
                }
                yield return(_line);

                _line = null;
            }
            if (_trace)
            {
                pb.Trace.WriteLine($"Text()          : send text                   \"{zstr.left(text.Text, 20)}\"");
            }
            //_removeBlank _removeLine
            //string text2 = text.Text;
            //if (_textRemoveBlank)
            //    text2 = text2.Trim();
            //if (_textRemoveLine)
            //{
            //    text2 = text2.Replace("\r", "");
            //    text2 = text2.Replace("\n", "");
            //}
            yield return(new OXmlTextElement {
                Text = text2
            });
        }
Exemplo n.º 8
0
        public IEnumerable <OXmlElement> ToOXmlXElements(IEnumerable <HtmlDocNode> nodes, WebImage[] webImages = null, string sourceUrl = null, Dictionary <string, NamedValues <ZValue> > imagePatches = null)
        {
            _webImages    = webImages;
            _sourceUrl    = sourceUrl;
            _imagePatches = imagePatches;

            _paragraph   = null;
            _line        = null;
            _currentTagA = null;
            _htmlImages  = new List <HtmlImage>();

            //yield return new OXmlDocDefaultsParagraphPropertiesElement();
            //yield return new OXmlElement { Type = OXmlElementType.DocDefaultsParagraphProperties };
            IEnumerable <OXmlElement> elements;

            foreach (HtmlDocNode node in nodes)
            {
                elements = null;
                switch (node.Type)
                {
                case HtmlDocNodeType.BeginTag:
                    // <p> <a>
                    //element = BeginTag((HtmlDocNodeBeginTag)node);
                    HtmlDocNodeBeginTag beginTag = (HtmlDocNodeBeginTag)node;
                    if (beginTag.Tag == HtmlTagType.P)
                    {
                        elements = Paragraph();
                    }
                    else if (beginTag.Tag == HtmlTagType.A)
                    {
                        Link((HtmlDocNodeBeginTagA)beginTag);
                    }
                    break;

                case HtmlDocNodeType.EndTag:
                    HtmlDocNodeEndTag endTag = (HtmlDocNodeEndTag)node;
                    if (endTag.Tag == HtmlTagType.A)
                    {
                        EndLink();
                    }
                    break;

                case HtmlDocNodeType.Tag:
                    //HtmlDocNodeTagImg
                    // <br> <img>
                    //element = Tag((HtmlDocNodeTag)node);
                    HtmlDocNodeTag tag = (HtmlDocNodeTag)node;
                    if (tag.Tag == HtmlTagType.BR)
                    {
                        elements = NewLine();
                    }
                    else if (tag.Tag == HtmlTagType.Img)
                    {
                        //elements = Image((HtmlDocNodeTagImg)tag);
                        Image((HtmlDocNodeTagImg)tag);
                    }
                    break;

                case HtmlDocNodeType.Text:
                    //element = new OXmlTextElement { Text = ((HtmlDocNodeText)node).Text };
                    elements = Text((HtmlDocNodeText)node);
                    break;
                }
                if (elements != null)
                {
                    foreach (OXmlElement element in elements)
                    {
                        yield return(element);
                    }
                }
            }
            elements = GetImages(endOfDocument: true);
            if (elements != null)
            {
                foreach (OXmlElement element in elements)
                {
                    yield return(element);
                }
            }
        }