1

SVGNet用来绘制SVG图像并以两种格式保存.svg它们.png。但是,当我添加文本并旋转它时,.png文件会正确显示图像,但.svg文件不会。

我正在探索SvgNet,硬编码我必须动态生成的那种图纸。但是,我似乎无法解决这个问题。我尝试了不同的旋转和添加翻译,但无济于事。如果我不添加任何转换,它甚至不添加间距,文本元素会显示在彼此之上。

这是所有代码,除了保存到.svgand .png

SvgDocument x = new SvgDocument();
x.Width = 2500;
x.Height = 2500;

List<string> exemplosParagem = new List<string>
{
    "Paragem 1",
    "Paragem 2",
    "Paragem 3",
    "Paragem 4",
    "Paragem 5",
    "Paragem 6",
    "Paragem 7",
    "Paragem 8",
    "Paragem 9",
    "Paragem 10",
    "O Que Acontece",
    "Se o Texto for de",
    "Tamanhos",
    "Diferentes"
};

SvgUnitCollection textPositionX = new SvgUnitCollection
{
    1250
};

SvgUnitCollection textPositionY = new SvgUnitCollection
{
    100
};

SvgGroup title = new SvgGroup();
title.Children.Add(new SvgText
{
    Text = "Teste Espinha SVG Library",
    FontSize = 80,
    Fill = new SvgColourServer(Color.Blue),
    Stroke = new SvgColourServer(Color.Black),
    StrokeWidth = 3,
    TextAnchor = SvgTextAnchor.Middle,
    X = textPositionX,
    Y = textPositionY
});


SvgGroup spineLine = new SvgGroup();
spineLine.Children.Add(new SvgLine
{
    StrokeWidth = 10,
    Stroke = new SvgColourServer(Color.Black),
    StartX = 100,
    StartY = 600,
    EndX = 2300,
    EndY = 600,
});


SvgGroup rect = new SvgGroup();
rect.Children.Add(new SvgRectangle
{
    Fill = new SvgColourServer(Color.LightBlue),
    Stroke = new SvgColourServer(Color.Black),
    StrokeWidth = 2,
    Width = x.Width - 50,
    Height = x.Height - spineLine.Bounds.Bottom - 100,
    X = 25,
    Y = spineLine.Bounds.Bottom + 100,
});

SvgUnitCollection paragensPositionX = new SvgUnitCollection
{
    spineLine.Bounds.X
};

SvgUnitCollection paragensPositionY = new SvgUnitCollection
{
   spineLine.Bounds.Top
};

List<SvgText> svgText = new List<SvgText>();

for (int i = 0; i < exemplosParagem.Count; i++)
{
    SvgText aux = new SvgText
    {
        Text = exemplosParagem[i],
        Color = new SvgColourServer(Color.Black),
        FontSize = 20,
        X = paragensPositionX,
        Y = paragensPositionY
    };

    SvgTransformCollection transCollect = new SvgTransformCollection();
    SvgRotate rotation = new SvgRotate(-35, aux.Bounds.X, aux.Bounds.Y);
    transCollect.Add(rotation);

    aux.Transforms = transCollect;

    paragensPositionX[0] += (spineLine.Bounds.Width / exemplosParagem.Count);

    svgText.Add(aux);
}


foreach (var stop in svgText)
{
    spineLine.Children.Add(stop);
}

x.Children.Add(title);
x.Children.Add(spineLine);
x.Children.Add(rect);

var bitmap = x.Draw();

以下是这段代码分别在.png和中生成的图像.svgPNG图片是正确的。

PNG图像 SVG 图像的打印

4

1 回答 1

0

原来我必须paragensPositionX在循环内创建。

SvgUnitCollection paragensPositionX = new SvgUnitCollection();
if (i == 0)
{
    paragensPositionX.Add(spineLine.Bounds.X);
}
else
{
    paragensPositionX.Add(auxSpacing + spacing);
}
于 2019-07-23T11:43:25.497 回答