在SVG内容中打印内容问题

时间:2015-01-30 07:08:51

标签: asp.net html5 svg printing

我正致力于在浏览器上呈现Visio图表。我设法使用SVG显示它们。

在打印相同的Visio图表时,我使用的是window.print()。

要求是在打印时按图表占据每个页面放置水印。

目前使用基于视图框高度的一些计算

我在父SVG标签内插入带有图像标签的SVG,作为水印的某些高度。

但计算会中断一段时间,并且水印会显示在页脚位置上方或向下滑动到下一页,这会导致某些页面没有水印或某些页面出现两次水印。

任何使用CSS或任何其他方式的解决方案都将受到高度赞赏!

请检查下面的代码快照以便更好地理解:

SVG xml文件代码段

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:v="http://schemas.microsoft.com/visio/2003/SVGExtensions/" width="6.53953in" height="12.0677in" viewBox="0 0 470.846 868.874" xml:space="preserve" color-interpolation-filters="sRGB" class="st11">

<g>
    <rect x="0" y="0" width="100%" height="100%" fill="white" onmouseover="mouse_out()" />
        <svg xmlns="http://www.w3.org/2000/svg" x="0" y="569" height="100px" width="100%">
            <image xlink:href="/css/images/Copyright.jpg" x="0" y="0" width="100%" height="50px" xmlns="http://www.w3.org/2000/svg" />
        </svg>
        <svg xmlns="http://www.w3.org/2000/svg" x="0" y="780" height="100px" width="100%">
            <image xlink:href="/css/images/Copyright.jpg" x="0" y="0" width="100%" height="50px" xmlns="http://www.w3.org/2000/svg" />
        </svg>

</g></svg>

用于计算放置水印的位置的C#代码

private void AddWatermark(XmlDocument doc, XmlNode prevNode, int width, int height)
        {
            //Assumed A4 Paper Usable Height 
            double pageA4Height = 1000;
            //Assumed First Page Usable Height because on first page added owner, title and some basic information
            double headerPageHeight = 842;
            //Assumed Total Print Pages
            int totalPages = 1;
            double VwBoxHeight = height;
            //Assusmed First Water Mark Position 
            double waterMarkPos = 780;
            //Calculating Total Pages
            if (headerPageHeight < VwBoxHeight)
            {
                VwBoxHeight = VwBoxHeight - headerPageHeight;
                if (VwBoxHeight > pageA4Height)
                    totalPages += (int)Math.Ceiling(VwBoxHeight / pageA4Height);
                else
                    totalPages = 2;
            }
            else
            {
                //Set WaterMark Position For Single Page
                waterMarkPos = height * 92 / 100;
            }

            for (int i = 1; i <= totalPages; i++)
            {

                //Initialize Water Mark Element And Set The Attributes 
                XmlElement watermark = doc.CreateElement("svg");
                watermark.SetAttribute("xmlns", "http://www.w3.org/2000/svg");
                watermark.SetAttribute("x", "0");
                if (i == 1)
                {
                    watermark.SetAttribute("y", waterMarkPos.ToString());
                }
                else if (i != totalPages)
                {
                    waterMarkPos += pageA4Height;
                    watermark.SetAttribute("y", waterMarkPos.ToString());
                }
                else
                {
                    watermark.SetAttribute("y", (height - 300).ToString());
                }
                watermark.SetAttribute("height", "100px");
                watermark.SetAttribute("width", "100%");

                //Initialize Water Mark Image Element
                XmlElement waterMarkImage = doc.CreateElement("image", "http://www.w3.org/2000/svg");
                waterMarkImage.SetAttribute("href", "http://www.w3.org/1999/xlink", VirtualPathUtility.ToAbsolute("~/css/images/Copyright.jpg"));
                waterMarkImage.SetAttribute("x", "0");
                waterMarkImage.SetAttribute("y", "0");
                waterMarkImage.SetAttribute("width", "100%");
                waterMarkImage.SetAttribute("height", "50px");
                watermark.AppendChild(waterMarkImage);

                //Insert Water Mark Element in XML Node
                prevNode.InsertBefore(watermark, prevNode.FirstChild);
            }
        }

0 个答案:

没有答案