There are many kinds of areas in a chart, such as chart area, plot area, legend area. Spire.XLS offers properties to set the performance of each area easily in C# and VB.NET. We have already shown you how to set the background color and image for chart area and plot area in C#. This article will show you how to set the background color for chart legend in C# with the help of Spire.XLS 7.8.43 or above.

Firstly, please check the original screenshot of excel chart with the automatic setting for chart legend.

How to Set the Background Color of Legend in an Excel Chart

Code Snippet of how to set the background color of legend in a Chart:

Step 1: Create a new workbook and load from file.

Workbook workbook = new Workbook();
workbook.LoadFromFile("sample.xlsx");

Step 2: Get the first worksheet from workbook and then get the first chart from the worksheet.

Worksheet ws = workbook.Worksheets[0];
Chart chart = ws.Charts[0];

Step 3: Change the background color of the legend in a chart and specify a Solid Fill of SkyBlue.

XlsChartFrameFormat x = chart.Legend.FrameFormat as XlsChartFrameFormat;
x.Fill.FillType = ShapeFillType.SolidColor;
x.ForeGroundColor = Color.SkyBlue;

Step 4: Save the document to file.

workbook.SaveToFile("result.xlsx",ExcelVersion.Version2010);

Effective screenshot after fill the background color for Excel chart legend:

How to Set the Background Color of Legend in an Excel Chart

Full codes:

using Spire.Xls;
using Spire.Xls.Core.Spreadsheet.Charts;
using System.Drawing;
namespace SetBackgroundColor
{
    class Program
    {

        static void Main(string[] args)
        {

            Workbook workbook = new Workbook();
            workbook.LoadFromFile("sample.xlsx");

            Worksheet ws = workbook.Worksheets[0];
            Chart chart = ws.Charts[0];

            XlsChartFrameFormat x = chart.Legend.FrameFormat as XlsChartFrameFormat;
            x.Fill.FillType = ShapeFillType.SolidColor;
            x.ForeGroundColor = Color.SkyBlue;

            workbook.SaveToFile("result.xlsx", ExcelVersion.Version2010);
        }
    }
}

The format of data marker in a line, scatter and radar chart can be changed and customized, which makes it more attractive and distinguishable. We could set markers' built-in type, size, background color, foreground color and transparency in Excel. This article is going to introduce how to achieve those features in C# using Spire.XLS.

Note: before start, please download the latest version of Spire.XLS and add the .dll in the bin folder as the reference of Visual Studio.

Step 1: Create a workbook with sheet and add some sample data.

            Workbook workbook = new Workbook();
            workbook.CreateEmptySheets(1);
            Worksheet sheet = workbook.Worksheets[0];
            sheet.Name = "Demo";
            sheet.Range["A1"].Value = "Tom";
            sheet.Range["A2"].NumberValue = 1.5;
            sheet.Range["A3"].NumberValue = 2.1;
            sheet.Range["A4"].NumberValue = 3.6;
            sheet.Range["A5"].NumberValue = 5.2;
            sheet.Range["A6"].NumberValue = 7.3;
            sheet.Range["A7"].NumberValue = 3.1;
            sheet.Range["B1"].Value = "Kitty";
            sheet.Range["B2"].NumberValue = 2.5;
            sheet.Range["B3"].NumberValue = 4.2;
            sheet.Range["B4"].NumberValue = 1.3;
            sheet.Range["B5"].NumberValue = 3.2;
            sheet.Range["B6"].NumberValue = 6.2;
            sheet.Range["B7"].NumberValue = 4.7;

Step 2: Create a Scatter-Markers chart based on the sample data.

            Chart chart = sheet.Charts.Add(ExcelChartType.ScatterMarkers);
            chart.DataRange = sheet.Range["A1:B7"];
            chart.PlotArea.Visible=false;
            chart.SeriesDataFromRange = false;
            chart.TopRow = 5;
            chart.BottomRow = 22;
            chart.LeftColumn = 4;
            chart.RightColumn = 11;
            chart.ChartTitle = "Chart with Markers";
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 10;

Step 3: Format the markers in the chart by setting the background color, foreground color, type, size and transparency.

            Spire.Xls.Charts.ChartSerie cs1 = chart.Series[0];
            cs1.DataFormat.MarkerBackgroundColor = Color.RoyalBlue;
            cs1.DataFormat.MarkerForegroundColor = Color.WhiteSmoke;
            cs1.DataFormat.MarkerSize = 7;
            cs1.DataFormat.MarkerStyle = ChartMarkerType.PlusSign;
            cs1.DataFormat.MarkerTransparencyValue = 0.8;

            Spire.Xls.Charts.ChartSerie cs2 = chart.Series[1];
            cs2.DataFormat.MarkerBackgroundColor = Color.Pink;
            cs2.DataFormat.MarkerSize = 9;
            cs2.DataFormat.MarkerStyle = ChartMarkerType.Diamond;
            cs2.DataFormat.MarkerTransparencyValue = 0.9;

Step 4: Save the document and launch to see effects.

            workbook.SaveToFile("S3.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("S3.xlsx");

Effects:

How to set customized data marker for charts in C#

Full Codes:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spire.Xls;
using System.Drawing;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {

            Workbook workbook = new Workbook();
            workbook.CreateEmptySheets(1);
            Worksheet sheet = workbook.Worksheets[0];

            sheet.Name = "Demo";
            sheet.Range["A1"].Value = "Tom";
            sheet.Range["A2"].NumberValue = 1.5;
            sheet.Range["A3"].NumberValue = 2.1;
            sheet.Range["A4"].NumberValue = 3.6;
            sheet.Range["A5"].NumberValue = 5.2;
            sheet.Range["A6"].NumberValue = 7.3;
            sheet.Range["A7"].NumberValue = 3.1;
            sheet.Range["B1"].Value = "Kitty";
            sheet.Range["B2"].NumberValue = 2.5;
            sheet.Range["B3"].NumberValue = 4.2;
            sheet.Range["B4"].NumberValue = 1.3;
            sheet.Range["B5"].NumberValue = 3.2;
            sheet.Range["B6"].NumberValue = 6.2;
            sheet.Range["B7"].NumberValue = 4.7;
           
            Chart chart = sheet.Charts.Add(ExcelChartType.ScatterMarkers);
            chart.DataRange = sheet.Range["A1:B7"];
            chart.PlotArea.Visible=false;
            chart.SeriesDataFromRange = false;
            chart.TopRow = 5;
            chart.BottomRow = 22;
            chart.LeftColumn = 4;
            chart.RightColumn = 11;
            chart.ChartTitle = "Chart with Markers";
            chart.ChartTitleArea.IsBold = true;
            chart.ChartTitleArea.Size = 10;

            Spire.Xls.Charts.ChartSerie cs1 = chart.Series[0];
            cs1.DataFormat.MarkerBackgroundColor = Color.RoyalBlue;
            cs1.DataFormat.MarkerForegroundColor = Color.WhiteSmoke;
            cs1.DataFormat.MarkerSize = 7;
            cs1.DataFormat.MarkerStyle = ChartMarkerType.PlusSign;
            cs1.DataFormat.MarkerTransparencyValue = 0.8;

            Spire.Xls.Charts.ChartSerie cs2 = chart.Series[1];
            cs2.DataFormat.MarkerBackgroundColor = Color.Pink;
            cs2.DataFormat.MarkerSize = 9;
            cs2.DataFormat.MarkerStyle = ChartMarkerType.Diamond;
            cs2.DataFormat.MarkerTransparencyValue = 0.9;
 
            workbook.SaveToFile("S3.xlsx", ExcelVersion.Version2010);
            System.Diagnostics.Process.Start("S3.xlsx");
        }
    }
}

C#/VB.NET: Rotate Pages in PDF

2022-06-16 08:42:00 Written by Koohji

In some cases, you might need to rotate PDF pages. For example, when you receive a PDF document that contains disoriented pages, you may wish to rotate the pages so you can read the document easier. In this article, you will learn how to rotate pages in PDF in C# and VB.NET using Spire.PDF for .NET.

Install Spire.PDF for .NET

To begin with, you need to add the DLL files included in the Spire.PDF for.NET package as references in your .NET project. The DLL files can be either downloaded from this link or installed via NuGet.

PM> Install-Package Spire.PDF

Rotate a Specific Page in PDF using C# and VB.NET

Rotation is based on 90-degree increments. You can rotate a PDF page by 0/90/180/270 degrees. The following are the steps to rotate a PDF page:

  • Create an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Get the desired page by its index (zero-based) through PdfDocument.Pages[pageIndex] property.
  • Get the original rotation angle of the page through PdfPageBase.Rotation property.
  • Increase the original rotation angle by desired degrees.
  • Apply the new rotation angle to the page through PdfPageBase.Rotation property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace RotatePdfPage
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile("Sample.pdf");

            //Get the first page
            PdfPageBase page = pdf.Pages[0];

            //Get the original rotation angle of the page
            int rotation = (int)page.Rotation;

            //Rotate the page 180 degrees clockwise based on the original rotation angle
            rotation += (int)PdfPageRotateAngle.RotateAngle180;
            page.Rotation = (PdfPageRotateAngle)rotation;

            //Save the result document
            pdf.SaveToFile("Rotate.pdf");
        }
    }
}

C#/VB.NET: Rotate Pages in PDF

Rotate All Pages in PDF using C# and VB.NET

The following are the steps to rotate all pages in a PDF document:

  • Create an instance of PdfDocument class.
  • Load a PDF document using PdfDocument.LoadFromFile() method.
  • Loop through each page in the document.
  • Get the original rotation angle of the page through PdfPageBase.Rotation property.
  • Increase the original rotation angle by desired degrees.
  • Apply the new rotation angle to the page through PdfPageBase.Rotation property.
  • Save the result document using PdfDocument.SaveToFile() method.
  • C#
  • VB.NET
using Spire.Pdf;

namespace RotateAllPdfPages
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create a PdfDocument instance
            PdfDocument pdf = new PdfDocument();
            //Load a PDF document
            pdf.LoadFromFile("Sample.pdf");

            foreach (PdfPageBase page in pdf.Pages)
            {
                //Get the original rotation angle of the page
                int rotation = (int)page.Rotation;
                //Rotate the page 180 degrees clockwise based on the original rotation angle
                rotation += (int)PdfPageRotateAngle.RotateAngle180;
                page.Rotation = (PdfPageRotateAngle)rotation;
            }

            //Save the result document
            pdf.SaveToFile("RotateAll.pdf");
        }
    }
}

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

page 235