Exemple #1
0
        public RtfHtml Build(Document document)
        {
            var body          = new List <IToken>();
            var defaultStyles = new List <IToken>
            {
                new FontSize(UnitValue.FromHalfPoint(24))
            };
            var result = new RtfHtml();

            if (document.ColorTable.Any())
            {
                defaultStyles.Add(new ForegroundColor(document.ColorTable.First()));
            }
            else
            {
                defaultStyles.Add(new ForegroundColor(new ColorValue(0, 0, 0)));
            }

            foreach (var token in document.Contents)
            {
                if (token is DefaultFontRef defaultFontRef)
                {
                    var defaultFont = document.FontTable.TryGetValue(defaultFontRef.Value, out var font) ? font : document.FontTable.FirstOrDefault().Value;
                    if (defaultFont != null)
                    {
                        defaultStyles.Add(defaultFont);
                    }
                }
                else if (token is DefaultTabWidth tabWidth)
                {
                    result.DefaultTabWidth = tabWidth.Value;
                }
                else if (token is Group group)
                {
                    if (group.Destination?.Type != TokenType.HeaderTag)
                    {
                        body.Add(token);
                    }
                }
                else if (token.Type != TokenType.HeaderTag)
                {
                    body.Add(token);
                }
            }

            result.Root     = Build(body, document, defaultStyles);
            result.Metadata = document.Information;
            return(result);
        }
Exemple #2
0
        public void Visit(RtfHtml document)
        {
            DefaultTabWidth = document.DefaultTabWidth;

            if (TryGetElementTag(ElementType.Meta, out var metaTag))
            {
                _writer.WriteStartElement("html");
                if (document.Metadata.Count > 0)
                {
                    _writer.WriteStartElement("head");

                    var title = document.Metadata.FirstOrDefault(k => k.Key is Title).Value as string;
                    if (!string.IsNullOrEmpty(title))
                    {
                        _writer.WriteElementString("title", title);
                    }

                    var baseUrl = document.Metadata.FirstOrDefault(k => k.Key is HyperlinkBase).Value as string;
                    if (!string.IsNullOrEmpty(baseUrl))
                    {
                        _writer.WriteStartElement("base");
                        _writer.WriteAttributeString("href", baseUrl);
                        _writer.WriteEndElement();
                    }

                    foreach (var meta in document.Metadata
                             .Where(k => !(k.Key is Title | k.Key is InternalVersion) && k.Value != null))
                    {
                        _writer.WriteStartElement(metaTag.Name);
                        if (meta.Key is CreateTime)
                        {
                            _writer.WriteAttributeString("name", "DCTERMS.created");
                        }
                        else if (meta.Key is Subject)
                        {
                            _writer.WriteAttributeString("name", "DCTERMS.subject");
                        }
                        else if (meta.Key is Author)
                        {
                            _writer.WriteAttributeString("name", "DCTERMS.creator");
                        }
                        else if (meta.Key is Manager)
                        {
                            _writer.WriteAttributeString("name", "manager");
                        }
                        else if (meta.Key is Company)
                        {
                            _writer.WriteAttributeString("name", "company");
                        }
                        else if (meta.Key is Operator)
                        {
                            _writer.WriteAttributeString("name", "operator");
                        }
                        else if (meta.Key is Category)
                        {
                            _writer.WriteAttributeString("name", "category");
                        }
                        else if (meta.Key is Keywords)
                        {
                            _writer.WriteAttributeString("name", "keywords");
                        }
                        else if (meta.Key is Comment)
                        {
                            _writer.WriteAttributeString("name", "comment");
                        }
                        else if (meta.Key is DocComment)
                        {
                            _writer.WriteAttributeString("name", "comment");
                        }
                        else if (meta.Key is RevisionTime)
                        {
                            _writer.WriteAttributeString("name", "DCTERMS.modified");
                        }
                        else if (meta.Key is PrintTime)
                        {
                            _writer.WriteAttributeString("name", "print-time");
                        }
                        else if (meta.Key is BackupTime)
                        {
                            _writer.WriteAttributeString("name", "backup-time");
                        }
                        else if (meta.Key is Tokens.Version)
                        {
                            _writer.WriteAttributeString("name", "version");
                        }
                        else if (meta.Key is EditingTime)
                        {
                            _writer.WriteAttributeString("name", "editting-time");
                        }
                        else if (meta.Key is NumPages)
                        {
                            _writer.WriteAttributeString("name", "number-of-pages");
                        }
                        else if (meta.Key is NumWords)
                        {
                            _writer.WriteAttributeString("name", "number-of-words");
                        }
                        else if (meta.Key is NumChars)
                        {
                            _writer.WriteAttributeString("name", "number-of-characters");
                        }
                        else if (meta.Key is NumCharsWs)
                        {
                            _writer.WriteAttributeString("name", "number-of-non-whitespace-characters");
                        }

                        if (meta.Value is DateTime date)
                        {
                            _writer.WriteAttributeString("content", date.ToString("s"));
                        }
                        else
                        {
                            _writer.WriteAttributeString("content", meta.Value?.ToString() ?? "");
                        }

                        _writer.WriteEndElement();
                    }
                    _writer.WriteEndElement();
                }
                document.Root.Visit(this);
                _writer.WriteEndElement();
            }
            else
            {
                var elements = document.Root.Elements().Where(e => TryGetElementTag(e.Type, out var _)).ToList();
                if (elements.Count == 1)
                {
                    elements[0].Visit(this);
                }
                else
                {
                    document.Root.Visit(this);
                }
            }
        }