Java (481)
This article demonstrates how to create a scatter chart and add a trendline to it in an Excel document by using Spire.XLS for Java.
import com.spire.xls.*;
import com.spire.xls.core.IChartTrendLine;
import java.awt.*;
public class ScatterChart {
public static void main(String[] args) {
//Create a a Workbook object and get the first worksheet
Workbook workbook = new Workbook();
Worksheet sheet = workbook.getWorksheets().get(0);
//Rename the first worksheet and set the column width
sheet.getCellRange("A1:B1").setColumnWidth(22f);;
sheet.setName("Scatter Chart");
//Insert data
sheet.getCellRange("A1").setValue("Advertising Expenditure");
sheet.getCellRange("A2").setValue("10429");
sheet.getCellRange("A3").setValue("95365");
sheet.getCellRange("A4").setValue("24085");
sheet.getCellRange("A5").setValue("109154");
sheet.getCellRange("A6").setValue("34006");
sheet.getCellRange("A7").setValue("84687");
sheet.getCellRange("A8").setValue("17560");
sheet.getCellRange("A9").setValue ("61408");
sheet.getCellRange("A10").setValue ("29402");
sheet.getCellRange("B1").setValue("Sales Revenue");
sheet.getCellRange("B2").setValue ("42519");
sheet.getCellRange("B3").setValue("184357");
sheet.getCellRange("B4").setValue ("38491");
sheet.getCellRange("B5").setValue ("214956");
sheet.getCellRange("B6").setValue ("75469");
sheet.getCellRange("B7").setValue ("134735");
sheet.getCellRange("B8").setValue("47935");
sheet.getCellRange("B9").setValue ("151832");
sheet.getCellRange("B10").setValue ("65424");
//Set cell style
sheet.getCellRange("A1:B1").getStyle().getFont().isBold(true);
sheet.getCellRange("A1:B1").getStyle().setColor(Color.darkGray);
sheet.getCellRange("A1:B1").getCellStyle().getExcelFont().setColor(Color.white);
sheet.getCellRange("A1:B10").getStyle().setHorizontalAlignment(HorizontalAlignType.Center);
sheet.getCellRange("A2:B10").getCellStyle().setNumberFormat("\"$\"#,##0") ;
//Create a scatter chart and set its data range
Chart chart = sheet.getCharts().add(ExcelChartType.ScatterMarkers);
chart.setDataRange(sheet.getCellRange("B2:B10"));
chart.setSeriesDataFromRange(false);
//Set position of the chart.
chart.setLeftColumn(4);
chart.setTopRow(1);
chart.setRightColumn(13);
chart.setBottomRow(22);
//Set chart title and series data label
chart.setChartTitle("Advertising & Sales Relationship");
chart.getChartTitleArea().isBold(true);
chart.getChartTitleArea().setSize(12);
chart.getSeries().get(0).setCategoryLabels(sheet.getCellRange("B2:B10"));
chart.getSeries().get(0).setValues(sheet.getCellRange("A2:A10"));
//Add a trendline
IChartTrendLine trendLine = chart.getSeries().get(0).getTrendLines().add(TrendLineType.Exponential);
trendLine.setName("Trendline");
//Set title of the x and y axis
chart.getPrimaryValueAxis().setTitle("Advertising Expenditure ($)");
chart.getPrimaryCategoryAxis().setTitle("Sales Revenue ($)");
//Save the document
workbook.saveToFile("ScatterChart.xlsx",ExcelVersion.Version2010);
workbook.dispose();
}
}

This article demonstrates how to use Spire. PDF for Java to add multiple headers to an existing PDF document in Java applications. The multiple headers on PDF means that different page has different header on the same PDF document.
import com.spire.pdf.*;
import com.spire.pdf.graphics.*;
import java.awt.*;
import java.awt.geom.*;
public class addDifferentHeaders {
public static void main(String[] args) {
String output = "output/addDifferentHeaders.pdf";
//load the sample PDF document
PdfDocument doc = new PdfDocument();
doc.loadFromFile("Sample.pdf");
String header1 = "Add header by Spire.PDF";
String header2 = "Different header";
//define style
PdfTrueTypeFont font = new PdfTrueTypeFont(new Font("Arial", Font.BOLD,12));
PdfBrush brush= PdfBrushes.getBlue();
Rectangle2D rect = new Rectangle2D.Float();
Dimension2D dimension2D = new Dimension();
dimension2D.setSize(doc.getPageSettings().getSize().getWidth(),50f);
rect.setFrame(new Point2D.Float(0, 20), dimension2D);
PdfStringFormat format=new PdfStringFormat();
format.setAlignment(PdfTextAlignment.Center);
//draw header string for the first page
doc.getPages().get(0).getCanvas().drawString(header1,font,brush,rect,format);
//draw header string for the second page
format.setAlignment( PdfTextAlignment.Left);
doc.getPages().get(1).getCanvas().drawString(header2, font, brush, rect, format);
//save the document
doc.saveToFile(output, FileFormat.PDF);
}
}

Paragraph and text background color is an important element of document design in Word documents. Appropriate paragraph and text background color can serve to highlight specific paragraphs or text, enhance the contrast of text so that it is easier to read, and fill in the gaps in the layout to assist in typography. Therefore, the reasonable use of paragraph and text background color is very important when creating Word documents. This article is going to show how to set background color for paragraphs and text in Java using Spire.Doc for Java.
- Set Background Color for Paragraphs in Word Documents
- Set Background Color for Existing Text in Word Documents
Install Spire.Doc for Java
First, you're required to add the Spire.Doc.jar file as a dependency in your Java program. The JAR file can be downloaded from this link. If you use Maven, you can easily import the JAR file in your application by adding the following code to your project's pom.xml file.
<repositories>
<repository>
<id>com.e-iceblue</id>
<name>e-iceblue</name>
<url>https://repo.e-iceblue.com/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>e-iceblue</groupId>
<artifactId>spire.doc</artifactId>
<version>14.1.3</version>
</dependency>
</dependencies>
Set Background Color for Paragraphs in Word Documents
To set the background color of a paragraph, we need to get the paragraph and use the Paragraph.getFormat().setBackColor() method to change its background color. The detailed steps are as follows.
- Create an object of Document.
- Load a Word document using Document.loadFromFile() method.
- Get the first section using Document.getSections().get() method.
- Get the third paragraph using Section.getParagraphs().get() method.
- Set the background color for the paragraph using Paragraph.getFormat().setBackColor() method.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.Section;
import com.spire.doc.documents.Paragraph;
import java.awt.*;
public class SetParagraphBackgroundColor {
public static void main(String[] args) {
//Create an object of Document
Document document = new Document();
//Load a Word document
document.loadFromFile("C:/Sample.docx");
//Get the first section
Section section = document.getSections().get(0);
//Get the third paragraph
Paragraph paragraph = section.getParagraphs().get(2);
//Set the background color of this paragraph as Light Gray
paragraph.getFormat().setBackColor(Color.LIGHT_GRAY);
//Save the document
document.saveToFile("ParagraphBackgroundColor.docx", FileFormat.Docx_2013);
document.dispose();
}
}

Set Background Color for Existing Text in Word Documents
Spire.Doc for Java provides Document.findAllString() method to find all the occurrences of specific text in a Word document and TextRange.getCharacterFormat().setTextBackgroundColor() to set the background color for specific text. The detailed steps for setting the background color for existing text are as follows.
- Create an object of Document.
- Load a Word document using Document.loadFromFile() method.
- Find all the occurrences of “timeless universe” using Document.findAllString() method.
- Loop through the occurrences.
- Get each occurrence as a text range using TextSelection.getAsOneRange() method.
- Set the background color for the text range using TextRange.getCharacterFormat().setTextBackgroundColor() method.
- You can also choose an occurrence by its index from the collection, get it as a text range, and set background color only for the occurrence.
- Save the document using Document.saveToFile() method.
- Java
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import com.spire.doc.documents.TextSelection;
import com.spire.doc.fields.TextRange;
import java.awt.*;
public class SetTextBackgroundColor {
public static void main(String[] args) {
//Create an object of Document
Document document = new Document();
//Load a Word document
document.loadFromFile("C:/Sample.docx");
//Find the text to set background color for
TextSelection[] textSelections = document.findAllString("timeless universe", false, true);
//Loop through the occurrences of the text
for (TextSelection selection : textSelections){
//Get an occurrence as a text range
TextRange textRange = selection.getAsOneRange();
//Set background color for the occurrence
textRange.getCharacterFormat().setTextBackgroundColor(Color.CYAN);
}
//Set the background color for the first occurrence of the text
//TextRange textRange = textSelections[0].getAsOneRange();
//textRange.getCharacterFormat().setTextBackgroundColor(Color.CYAN);
//Save the document
document.saveToFile("TextBackgroundColor.docx", FileFormat.Docx_2013);
document.dispose();
}
}

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.