Ejemplo n.º 1
0
        private void ProcessAcronym(HtmlEnumerator en)
        {
            // Transform the inline acronym/abbreviation to a reference to a foot note.

            string title = en.Attributes["title"];
            if (title == null) return;

            AlternateProcessHtmlChunks(en, en.ClosingCurrentTag);

            if (elements.Count > 0 && elements[0] is Run)
            {
                string defaultRefStyle, runStyle;
                FootnoteEndnoteReferenceType reference;

                if (this.AcronymPosition == AcronymPosition.PageEnd)
                {
                    reference = new FootnoteReference() { Id = AddFootnoteReference(title) };
                    defaultRefStyle = "footnote text";
                    runStyle = "footnote reference";
                }
                else
                {
                    reference = new EndnoteReference() { Id = AddEndnoteReference(title) };
                    defaultRefStyle = "endnote text";
                    runStyle = "endnote reference";
                }

                Run run;
                elements.Add(
                    run = new Run(
                        new RunProperties
                        {
                            RunStyle = new RunStyle() { Val = htmlStyles.GetStyle(runStyle, StyleValues.Character) }
                        },
                        reference));

                if (!htmlStyles.DoesStyleExists(defaultRefStyle))
                {
                    // Force the superscript style because if the footnote text style does not exists,
                    // the rendering will be awful.
                    run.InsertInProperties(prop =>
                        prop.VerticalTextAlignment = new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript });
                }
            }
        }
        private void ProcessImage(HtmlEnumerator en)
        {
            if (this.ImageProcessing == ImageProcessing.Ignore) return;

            Drawing drawing = null;
            wBorder border = new wBorder() { Val = BorderValues.None };
            string src = en.Attributes["src"];
            Uri uri;

            if (src != null && Uri.TryCreate(src, UriKind.RelativeOrAbsolute, out uri))
            {
                string alt = en.Attributes["alt"];
                bool process = true;

                if (!uri.IsAbsoluteUri && this.BaseImageUrl != null)
                    uri = new Uri(this.BaseImageUrl, uri);

                Size preferredSize = Size.Empty;
                if (en.Attributes["width"] != null || en.Attributes["height"] != null)
                {
                    Unit wu = en.Attributes.GetAsUnit("width");
                    Unit hu = en.Attributes.GetAsUnit("height");

                    // % is not supported
                    if (wu.IsValid && wu.Value > 0 && wu.Type != UnitMetric.Percent)
                    {
                        preferredSize.Width = wu.ValueInPx;
                    }
                    if (hu.IsValid && hu.Value > 0 && wu.Type != UnitMetric.Percent)
                    {
                        // Image perspective skewed. Bug fixed by ddeforge on http://notesforhtml2openxml.codeplex.com/discussions/350500
                        preferredSize.Height = hu.ValueInPx;
                    }
                }

                SideBorder attrBorder = en.StyleAttributes.GetAsSideBorder("border");
                if (attrBorder.IsValid)
                {
                    border.Val = attrBorder.Style;
                    border.Color = attrBorder.Color.ToHexString();
                    border.Size = (uint) attrBorder.Width.ValueInPx * 4;
                }

                if (process)
                    drawing = AddImagePart(uri, src, alt, preferredSize);
            }

            if (drawing != null)
            {
                Run run = new Run(drawing);
                if (border.Val != BorderValues.None) run.InsertInProperties(border);
                elements.Add(run);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Add a note to the FootNotes part and ensure it exists.
        /// </summary>
        /// <param name="description">The description of an acronym, abbreviation, some book references, ...</param>
        /// <returns>Returns the id of the footnote reference.</returns>
        private int AddFootnoteReference(string description)
        {
            FootnotesPart fpart = mainPart.FootnotesPart;
            if (fpart == null)
                fpart = mainPart.AddNewPart<FootnotesPart>();

            if (fpart.Footnotes == null)
            {
                // Insert a new Footnotes reference
                new Footnotes(
                    new Footnote(
                        new Paragraph(
                            new ParagraphProperties(
                                new SpacingBetweenLines() { After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto }),
                            new Run(
                                new SeparatorMark())
                        )
                    ) { Type = FootnoteEndnoteValues.Separator, Id = -1 },
                    new Footnote(
                        new Paragraph(
                            new ParagraphProperties(
                                new SpacingBetweenLines() { After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto }),
                            new Run(
                                new ContinuationSeparatorMark())
                        )
                    ) { Type = FootnoteEndnoteValues.ContinuationSeparator, Id = 0 }).Save(fpart);
                footnotesRef = 1;
            }
            else
            {
                // The footnotesRef Id is a required field and should be unique. You can assign yourself some hard-coded
                // value but that's absolutely not safe. We will loop through the existing Footnote
                // to retrieve the highest Id.
                foreach (var p in fpart.Footnotes.Elements<Footnote>())
                {
                    if (p.Id.HasValue && p.Id > footnotesRef) footnotesRef = (int) p.Id.Value;
                }
                footnotesRef++;
            }

            Run markerRun;
            fpart.Footnotes.Append(
                new Footnote(
                    new Paragraph(
                        new ParagraphProperties(
                            new ParagraphStyleId() { Val = htmlStyles.GetStyle("footnote text", false) }),
                        markerRun = new Run(
                            new RunProperties(
                                new RunStyle() { Val = htmlStyles.GetStyle("footnote reference", true) }),
                            new FootnoteReferenceMark()),
                        new Run(
                // Word insert automatically a space before the definition to separate the reference number
                // with its description
                            new Text(" " + description) { Space = SpaceProcessingModeValues.Preserve })
                    )
                ) { Id = footnotesRef });

            if (!htmlStyles.DoesStyleExists("footnote reference"))
            {
                // Force the superscript style because if the footnote text style does not exists,
                // the rendering will be awful.
                markerRun.InsertInProperties(new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript });
            }
            fpart.Footnotes.Save();

            return footnotesRef;
        }
Ejemplo n.º 4
0
        private void ProcessImage(HtmlEnumerator en)
        {
            if (this.ImageProcessing == ImageProcessing.Ignore) return;

            Drawing drawing = null;
            wBorder border = new wBorder() { Val = BorderValues.None };
            string src = en.Attributes["src"];
            Uri uri = null;

            // Bug reported by Erik2014. Inline 64 bit images can be too big and Uri.TryCreate will fail silently with a SizeLimit error.
            // To circumvent this buffer size, we will work either on the Uri, either on the original src.
            if (src != null && (DataUri.IsWellFormed(src) || Uri.TryCreate(src, UriKind.RelativeOrAbsolute, out uri)))
            {
                string alt = (en.Attributes["title"] ?? en.Attributes["alt"]) ?? String.Empty;
                bool process = true;

                if (uri != null && !uri.IsAbsoluteUri && this.BaseImageUrl != null)
                    uri = new Uri(this.BaseImageUrl, uri);

                Size preferredSize = Size.Empty;
                Unit wu = en.Attributes.GetAsUnit("width");
                if (!wu.IsValid) wu = en.StyleAttributes.GetAsUnit("width");
                Unit hu = en.Attributes.GetAsUnit("height");
                if (!hu.IsValid) hu = en.StyleAttributes.GetAsUnit("height");

                // % is not supported
                if (wu.IsFixed && wu.Value > 0)
                {
                    preferredSize.Width = wu.ValueInPx;
                }
                if (hu.IsFixed && hu.Value > 0)
                {
                    // Image perspective skewed. Bug fixed by ddeforge on http://html2openxml.codeplex.com/discussions/350500
                    preferredSize.Height = hu.ValueInPx;
                }

                SideBorder attrBorder = en.StyleAttributes.GetAsSideBorder("border");
                if (attrBorder.IsValid)
                {
                    border.Val = attrBorder.Style;
                    border.Color = attrBorder.Color.ToHexString();
                    border.Size = (uint)attrBorder.Width.ValueInPx * 4;
                }

                if (process)
                    drawing = AddImagePart(uri, src, alt, preferredSize);
            }

            if (drawing != null)
            {
                Run run = new Run(drawing);
                if (border.Val != BorderValues.None) run.InsertInProperties(prop => prop.Border = border);
                elements.Add(run);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Add a note to the FootNotes part and ensure it exists.
        /// </summary>
        /// <param name="description">The description of an acronym, abbreviation, some book references, ...</param>
        /// <returns>Returns the id of the footnote reference.</returns>
        private int AddFootnoteReference(string description)
        {
            FootnotesPart fpart = mainPart.FootnotesPart;
            if (fpart == null)
                fpart = mainPart.AddNewPart<FootnotesPart>();

            if (fpart.Footnotes == null)
            {
                // Insert a new Footnotes reference
                new Footnotes(
                    new Footnote(
                        new Paragraph(
                            new ParagraphProperties {
                                SpacingBetweenLines = new SpacingBetweenLines() { After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto }
                            },
                            new Run(
                                new SeparatorMark())
                        )
                    ) { Type = FootnoteEndnoteValues.Separator, Id = -1 },
                    new Footnote(
                        new Paragraph(
                            new ParagraphProperties {
                                SpacingBetweenLines = new SpacingBetweenLines() { After = "0", Line = "240", LineRule = LineSpacingRuleValues.Auto }
                            },
                            new Run(
                                new ContinuationSeparatorMark())
                        )
                    ) { Type = FootnoteEndnoteValues.ContinuationSeparator, Id = 0 }).Save(fpart);
                footnotesRef = 1;
            }
            else
            {
                // The footnotesRef Id is a required field and should be unique. You can assign yourself some hard-coded
                // value but that's absolutely not safe. We will loop through the existing Footnote
                // to retrieve the highest Id.
                foreach (var fn in fpart.Footnotes.Elements<Footnote>())
                {
                    if (fn.Id.HasValue && fn.Id > footnotesRef) footnotesRef = (int) fn.Id.Value;
                }
                footnotesRef++;
            }

            Run markerRun;
            Paragraph p;
            fpart.Footnotes.Append(
                new Footnote(
                    p = new Paragraph(
                        new ParagraphProperties {
                            ParagraphStyleId = new ParagraphStyleId() { Val = htmlStyles.GetStyle("footnote text", StyleValues.Paragraph) }
                        },
                        markerRun = new Run(
                            new RunProperties {
                                RunStyle = new RunStyle() { Val = htmlStyles.GetStyle("footnote reference", StyleValues.Character) }
                            },
                            new FootnoteReferenceMark()),
                        new Run(
                        // Word insert automatically a space before the definition to separate the
                        // reference number with its description
                            new Text(" ") { Space = SpaceProcessingModeValues.Preserve })
                    )
                ) { Id = footnotesRef });

            // Description in footnote reference can be plain text or a web protocols/file share (like \\server01)
            Uri uriReference;
            Regex linkRegex = new Regex(@"^((https?|ftps?|mailto|file)://|[\\]{2})(?:[\w][\w.-]?)");
            if (linkRegex.IsMatch(description) && Uri.TryCreate(description, UriKind.Absolute, out uriReference))
            {
                HyperlinkRelationship extLink = fpart.AddHyperlinkRelationship(uriReference, true);
                var h = new Hyperlink(
                    ) { History = true, Id = extLink.Id };

                htmlStyles.EnsureKnownStyle(HtmlDocumentStyle.KnownStyles.Hyperlink);

                h.Append(new Run(
                    new RunProperties {
                        RunStyle = new RunStyle() { Val = htmlStyles.GetStyle("Hyperlink", StyleValues.Character) }
                    },
                    new Text(description)));
                p.Append(h);
            }
            else
            {
                p.Append(new Run(
                    new Text(description) { Space = SpaceProcessingModeValues.Preserve }));
            }

            if (!htmlStyles.DoesStyleExists("footnote reference"))
            {
                // Force the superscript style because if the footnote text style does not exists,
                // the rendering will be awful.
                markerRun.InsertInProperties(prop =>
                    prop.VerticalTextAlignment = new VerticalTextAlignment() { Val = VerticalPositionValues.Superscript });
            }
            fpart.Footnotes.Save();

            return footnotesRef;
        }