using Text = DocumentFormat.OpenXml.Wordprocessing.Text;
private void BtnWord_Click(object sender, EventArgs e)
{
try
{
string filePath = "C:\\Varie\\DocumentoWordOpenXML.docx";
//Creazione del documento documento Word
using (WordprocessingDocument wordDocument
= WordprocessingDocument.Create(filePath,
DocumentFormat.OpenXml.WordprocessingDocumentType.Document))
{
//Gestione del documento principale
MainDocumentPart mainPart = wordDocument.AddMainDocumentPart();
mainPart.Document
= new Document();
Body body = new Body();
//Creazione
del paragrafo con del testo
Paragraph paragraph = new Paragraph();
Run run = new Run();
Text text = new Text("Benvenuti nella creazione
di documenti Word");
//Struttura del documento
run.Append(text);
paragraph.Append(run);
body.Append(paragraph);
mainPart.Document.Append(body);
mainPart.Document.Save();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void CreaDocxConFormattazione(string NomeFile)
{
try
{
using (WordprocessingDocument wordDocument = WordprocessingDocument.Create(
NomeFile, WordprocessingDocumentType.Document))
{
MainDocumentPart mainPart =
wordDocument.AddMainDocumentPart();
mainPart.Document = new Document();
Body body = new Body();
//Gestione
del paragrafo con formattazione al centro
ParagraphProperties paragraphProperties = new ParagraphProperties(
new Justification() { Val = JustificationValues.Center });
//Imposta lo stile del testo
RunProperties runProperties = new RunProperties(
new Bold(), // Grassetto
new Italic(), // Corsivo
new FontSize() { Val = "28" }, // 28 = 14pt (misura in
half-points)
new RunFonts() { Ascii = "Calibri" } // Font Calibri
);
//Imposta il testo
Text text = new Text("Benvenuti nella creazione
di documenti Word");
//Prepara il
documento
Run run = new Run();
run.Append(runProperties);
run.Append(text);
Paragraph paragraph = new Paragraph();
paragraph.Append(paragraphProperties);
paragraph.Append(run);
body.Append(paragraph);
mainPart.Document.Append(body);
mainPart.Document.Save();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void BtnWord_Click(object sender, EventArgs e)
{
try
{
string filePath2 = "C:\\Varie\\DocumentoWordOpenXML2.docx";
CreaDocxConFormattazione(filePath2);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

Nessun commento:
Posta un commento