This is a migrated thread and some comments may be shown as answers.

Insert colored path in block

2 Answers 196 Views
PdfProcessing
This is a migrated thread and some comments may be shown as answers.
Dirk
Top achievements
Rank 1
Iron
Dirk asked on 19 Jan 2018, 02:49 PM

Hi,

I'm just evaluating this library.
I want to create a PDF with path objects, but I'm not able set any color.
I'm converting a WPF Path (System.Media.Windows.geometry) with the PdfGeometryHelper.ConvertPathGeometry (Telerik example)) to a Telerik Path geometry:

      var wpfWindpwsPath= Geometry.Parse("F1 M 7.5,11.500 L 21.500,11.500 L 21.500,15.500 L 7.500,15.500 L 7.500,11.500 Z");
      var wpfWindpwsPathGeometry = wpfWindpwsPath.GetFlattenedPathGeometry();
      var wpfTelerikPathGeometry = PdfGeometryHelper.ConvertPathGeometry(wpfWindpwsPathGeometry);

      var block = addTableCell.Blocks.AddBlock();
      Telerik.Windows.Documents.Fixed.Model.Graphics.Path path = new Telerik.Windows.Documents.Fixed.Model.Graphics.Path();
      path.Fill = new RgbColor(156, 156, 155);
      path.Geometry = wpfTelerikPathGeometry;

//not possible
      block.InsertPath(path);

// this is possible, but color is black

      block.InsertPath(path.Geometry);

 

How can I set a color?

2 Answers, 1 is accepted

Sort by
0
Dirk
Top achievements
Rank 1
Iron
answered on 23 Jan 2018, 12:06 PM

I've still found an answer.

I only have to set

block.GraphicProperties.StrokeThickness = 1;
block.GraphicProperties.StrokeColor = new RgbColor(156, 156, 155);

But I now have an addional question:

I want to add multiple paths with different colors to the same block.
For example a cross with a black and a red line.

0
Deyan
Telerik team
answered on 24 Jan 2018, 12:09 PM
Hello Dirk,

PathGeometry instances may have only a single color even with multiple figures. However, you may use FormSource in order to insert more complex content in a Block. The CreateBlockWithPaths method from the following code snippet shows how to create Block with simple geometry and with a FormSource. Sample export of such Block may be seen in the PDF file from the attached archive "PathGeometry_vs_FormSource.zip".
private static Block CreateBlockWithPaths()
{
    Block block = new Block();
 
    block.GraphicProperties.IsFilled = false;
    block.GraphicProperties.IsStroked = true;
    block.GraphicProperties.StrokeColor = new RgbColor(0, 0, 255);
    block.GraphicProperties.StrokeThickness = 1;
 
    block.InsertText("PathGeometry: ");
    PathGeometry pathWithTwoFigures = GetPathWithMultipleFigures();
    block.InsertPath(pathWithTwoFigures);
             
    block.InsertText(" vs FormSource: ");
    FormSource formWithTwoColors = GetFormWithTwoColoredPaths();
    block.InsertForm(formWithTwoColors);
 
    return block;
}
 
private static FormSource GetFormWithTwoColoredPaths()
{
    FormSource formSource = new FormSource();
    PathGeometry spiralGeometry, rectanglesGeometry;
    GetGoldenSpiralGeometry(10, out spiralGeometry, out rectanglesGeometry);
    formSource.Size = rectanglesGeometry.Bounds.Size;
 
    FixedContentEditor formEditor = new FixedContentEditor(formSource);
    formEditor.GraphicProperties.IsStroked = true;
 
    formEditor.GraphicProperties.IsFilled = true;
    formEditor.GraphicProperties.FillColor = new RgbColor(100, 0, 180, 0);
    formEditor.GraphicProperties.StrokeColor = new RgbColor(0, 180, 0);
    formEditor.GraphicProperties.StrokeThickness = 5;
    formEditor.DrawPath(rectanglesGeometry);
 
    formEditor.GraphicProperties.IsFilled = false;
    formEditor.GraphicProperties.StrokeColor = new RgbColor(255, 0, 0);
    formEditor.GraphicProperties.StrokeThickness = 2;
    formEditor.DrawPath(spiralGeometry);
 
    return formSource;
}
 
private static PathGeometry GetPathWithMultipleFigures()
{
    PathGeometry spiralGeometry, rectanglesGeometry;
    GetGoldenSpiralGeometry(10, out spiralGeometry, out rectanglesGeometry);
    rectanglesGeometry.Figures.Add(spiralGeometry.Figures[0]);      
 
    return rectanglesGeometry;
}
 
private static void GetGoldenSpiralGeometry(int arcsCount, out PathGeometry spiralGeometry, out PathGeometry rectanglesGeometry)
{
    double sin45 = 1 / Math.Sqrt(2);
 
    Vector[] directions =
    {
        new Vector(1, -1) * sin45,
        new Vector(1, 1) * sin45,
        new Vector(-1, 1) * sin45,
        new Vector(-1, -1) * sin45
    };
 
    double length = 100;
    double fi = (Math.Sqrt(5) - 1) / 2;
 
    spiralGeometry = new PathGeometry();
    rectanglesGeometry = new PathGeometry();
    PathFigure spiralFigure = spiralGeometry.Figures.AddPathFigure();
    spiralFigure.StartPoint = new Point(0, length);
    Point previousPoint = spiralFigure.StartPoint;
 
    for (int i = 0; i < arcsCount; i++)
    {
        Vector delta = directions[i % 4] * (length / sin45);
        Point point = previousPoint + delta;
 
        PathFigure rectanglePath = rectanglesGeometry.Figures.AddPathFigure();
        Rect rect = new Rect(previousPoint, point);
        rectanglePath.StartPoint = rect.TopLeft;
        rectanglePath.Segments.AddLineSegment().Point = rect.TopRight;
        rectanglePath.Segments.AddLineSegment().Point = rect.BottomRight;
        rectanglePath.Segments.AddLineSegment().Point = rect.BottomLeft;
        rectanglePath.IsClosed = true;
 
        ArcSegment arc = spiralFigure.Segments.AddArcSegment();
        arc.RadiusX = length;
        arc.RadiusY = length;
        arc.SweepDirection = SweepDirection.Clockwise;
        arc.RotationAngle = 45;
        arc.Point = point;
 
        previousPoint = point;
        length *= fi;
    }
}

I hope this is helpful. If you have any other questions or concerns do not hesitate to contact us again.

Regards,
Deyan
Progress Telerik

Tags
PdfProcessing
Asked by
Dirk
Top achievements
Rank 1
Iron
Answers by
Dirk
Top achievements
Rank 1
Iron
Deyan
Telerik team
Share this question
or