CellFormat

CellFormat class

Rappresenta tutta la formattazione per una cella della tabella.

Per saperne di più, visita ilLavorare con le tabelle articolo di documentazione.

public class CellFormat

Proprietà

NomeDescrizione
Borders { get; }Ottiene la raccolta dei bordi della cella.
BottomPadding { get; set; }Restituisce o imposta la quantità di spazio (in punti) da aggiungere sotto il contenuto della cella.
FitText { get; set; }SeVERO , adatta il testo alla cella, comprimendo ogni paragrafo alla larghezza della cella.
HideMark { get; set; }Restituisce o imposta la visibilità del contrassegno di cella.
HorizontalMerge { get; set; }Specifica come la cella viene unita orizzontalmente con le altre celle nella riga.
LeftPadding { get; set; }Restituisce o imposta la quantità di spazio (in punti) da aggiungere a sinistra del contenuto della cella.
Orientation { get; set; }Restituisce o imposta l’orientamento del testo in una cella di tabella.
PreferredWidth { get; set; }Restituisce o imposta la larghezza preferita della cella.
RightPadding { get; set; }Restituisce o imposta la quantità di spazio (in punti) da aggiungere a destra del contenuto della cella.
Shading { get; }Restituisce unShading oggetto che fa riferimento alla formattazione dell’ombreggiatura per la cella.
TopPadding { get; set; }Restituisce o imposta la quantità di spazio (in punti) da aggiungere sopra il contenuto della cella.
VerticalAlignment { get; set; }Restituisce o imposta l’allineamento verticale del testo nella cella.
VerticalMerge { get; set; }Specifica come la cella viene unita verticalmente con altre celle.
Width { get; set; }Ottiene la larghezza della cella in punti.
WrapText { get; set; }SeVERO , inserisci il testo nella cella.

Metodi

NomeDescrizione
ClearFormatting()Ripristina la formattazione predefinita della cella. Non modifica la larghezza della cella.
SetPaddings(double, double, double, double)Imposta la quantità di spazio (in punti) da aggiungere a sinistra/in alto/a destra/in basso al contenuto della cella.

Esempi

Mostra come modificare la formattazione di una cella di una tabella.

Document doc = new Document(MyDir + "Tables.docx");
Table table = doc.FirstSection.Body.Tables[0];
Cell firstCell = table.FirstRow.FirstCell;

// Utilizzare la proprietà "CellFormat" di una cella per impostare la formattazione che modifica l'aspetto di quella cella.
firstCell.CellFormat.Width = 30;
firstCell.CellFormat.Orientation = TextOrientation.Downward;
firstCell.CellFormat.Shading.ForegroundPatternColor = Color.LightGreen;

doc.Save(ArtifactsDir + "Table.CellFormat.docx");

Mostra come modificare il formato di righe e celle in una tabella.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

Table table = builder.StartTable();
builder.InsertCell();
builder.Write("City");
builder.InsertCell();
builder.Write("Country");
builder.EndRow();
builder.InsertCell();
builder.Write("London");
builder.InsertCell();
builder.Write("U.K.");
builder.EndTable();

// Utilizzare la proprietà "RowFormat" della prima riga per modificare la formattazione
// del contenuto di tutte le celle in questa riga.
RowFormat rowFormat = table.FirstRow.RowFormat;
rowFormat.Height = 25;
rowFormat.Borders[BorderType.Bottom].Color = Color.Red;

// Utilizzare la proprietà "CellFormat" della prima cella nell'ultima riga per modificare la formattazione del contenuto di quella cella.
CellFormat cellFormat = table.LastRow.FirstCell.CellFormat;
cellFormat.Width = 100;
cellFormat.Shading.BackgroundPatternColor = Color.Orange;

doc.Save(ArtifactsDir + "Table.RowCellFormat.docx");

Mostra come creare una tabella con bordi personalizzati.

Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);

builder.StartTable();

// Impostazione delle opzioni di formattazione della tabella per un generatore di documenti
// li applicheremo a ogni riga e cella che aggiungeremo.
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;

builder.CellFormat.ClearFormatting();
builder.CellFormat.Width = 150;
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
builder.CellFormat.Shading.BackgroundPatternColor = Color.GreenYellow;
builder.CellFormat.WrapText = false;
builder.CellFormat.FitText = true;

builder.RowFormat.ClearFormatting();
builder.RowFormat.HeightRule = HeightRule.Exactly;
builder.RowFormat.Height = 50;
builder.RowFormat.Borders.LineStyle = LineStyle.Engrave3D;
builder.RowFormat.Borders.Color = Color.Orange;

builder.InsertCell();
builder.Write("Row 1, Col 1");

builder.InsertCell();
builder.Write("Row 1, Col 2");
builder.EndRow();

// La modifica della formattazione verrà applicata alla cella corrente,
// e tutte le nuove celle che creeremo in seguito con il builder.
// Ciò non influirà sulle celle aggiunte in precedenza.
builder.CellFormat.Shading.ClearFormatting();

builder.InsertCell();
builder.Write("Row 2, Col 1");

builder.InsertCell();
builder.Write("Row 2, Col 2");

builder.EndRow();

// Aumenta l'altezza della riga per adattarla al testo verticale.
builder.InsertCell();
builder.RowFormat.Height = 150;
builder.CellFormat.Orientation = TextOrientation.Upward;
builder.Write("Row 3, Col 1");

builder.InsertCell();
builder.CellFormat.Orientation = TextOrientation.Downward;
builder.Write("Row 3, Col 2");

builder.EndRow();
builder.EndTable();

doc.Save(ArtifactsDir + "DocumentBuilder.InsertTable.docx");

Guarda anche