TextFormatting i.e Bold, Italic, Underline, Color Text etc

TextFormatting i.e Bold, Italic, Underline, Color Text etc

Post by trgarc » Fri Jul 29, 2011 7:46 pm

Hi,

Here is a code to try different text formatting options. For example, using Arctorus Documents, you can set text inside the cell as bold, italic, underline, colorful, superscript and subscript etc.

If you get any trouble trying this code, then let me know.

Output Screen
output-screen.jpg
output-screen.jpg (115.52 KiB) Viewed 71493 times


Output File
TextFormatting.zip
(6.19 KiB) Downloaded 1040 times


Example-Java
Code: Select all
import java.awt.Color;
import com.arctorus.documents.*;
import com.arctorus.documents.table.Cell;
import com.arctorus.documents.table.Table;

public class ArctorusDocuments {

   public static void main(String[] args) throws Exception {

      String OUT_DIR = "C:\\Exports\\";

      // Create a document for your xlsx file
      Document xlsxDoc = new Document();

      // Access the first worksheet
      Page xlsxSheet = xlsxDoc.getFirstPage();

      // Set the sheet name to anything you like
      xlsxSheet.setName("Text Formatting");

      // In order to access the sheet cells get the table first
      Table xlsxTable = xlsxSheet.getTable();

      // Add normal text in cell C3
      Cell c3Cell = xlsxTable.getCell("C3");
      c3Cell.addRun(new Run("Normal Text"));
      TextFormat fmt = c3Cell.getRuns().get(0).getTextFormat();
      fmt.setFontName("Calibri");
      fmt.setSize((short) 11);

      // Add bold text in cell C4
      Cell c4Cell = xlsxTable.getCell("C4");
      c4Cell.addRun(new Run("Bold Text"));
      fmt = c4Cell.getRuns().get(0).getTextFormat();
      fmt.setFontName("Calibri");
      fmt.setSize((short) 11);
      fmt.setBold(true);

      // Add italic text in cell C5
      Cell c5Cell = xlsxTable.getCell("C5");
      c5Cell.addRun(new Run("Italic Text"));
      fmt = c5Cell.getRuns().get(0).getTextFormat();
      fmt.setFontName("Calibri");
      fmt.setSize((short) 11);
      fmt.setItalic(true);

      // Add underline text in cell C6
      Cell c6Cell = xlsxTable.getCell("C6");
      c6Cell.addRun(new Run("Underline Text"));
      fmt = c6Cell.getRuns().get(0).getTextFormat();
      fmt.setFontName("Calibri");
      fmt.setSize((short) 11);
      fmt.setUnderline(true);

      // Add color text in cell c7
      Cell c7Cell = xlsxTable.getCell("C7");
      c7Cell.addRun(new Run("Colored Text"));
      fmt = c7Cell.getRuns().get(0).getTextFormat();
      fmt.setFontName("Calibri");
      fmt.setSize((short) 11);
      fmt.setColor(Color.GREEN);

      // Add superscript text in cell C8 i.e X raise to power Y
      Cell c8Cell = xlsxTable.getCell("C8");
      c8Cell.addRun(new Run("X"));
      c8Cell.addRun(new Run("Y"));
      // Set X normal
      fmt = c8Cell.getRuns().get(0).getTextFormat();
      fmt.setFontName("Calibri");
      fmt.setSize((short) 11);
      fmt.setBaseline(BaselineType.NORMAL);
      // Set Y superscript
      fmt = c8Cell.getRuns().get(1).getTextFormat();
      fmt.setFontName("Calibri");
      fmt.setSize((short) 11);
      fmt.setBaseline(BaselineType.SUPERSCRIPT);

      // Add subscript text in cell C9 i.e X base Y
      Cell c9Cell = xlsxTable.getCell("C9");
      c9Cell.addRun(new Run("X"));
      c9Cell.addRun(new Run("Y"));
      // Set X normal
      fmt = c9Cell.getRuns().get(0).getTextFormat();
      fmt.setFontName("Calibri");
      fmt.setSize((short) 11);
      fmt.setBaseline(BaselineType.NORMAL);
      // Set Y subscript
      fmt = c9Cell.getRuns().get(1).getTextFormat();
      fmt.setFontName("Calibri");
      fmt.setSize((short) 11);
      fmt.setBaseline(BaselineType.SUBSCRIPT);

      // Save the document in xlsx format
      xlsxDoc.save(ExportType.XLSX, OUT_DIR + "TextFormatting.xlsx");

   }

}

trgarc
 
Posts: 4
Joined: Mon Nov 15, 2010 4:38 am
Top

Return to Arctorus Documents for .NET and Java

Who is online

Users browsing this forum: No registered users and 3 guests

cron
SSL Certificate Authority
SSL Certificate Authority
Credit Card Processing