C# Word Library
C# Word - Paragraph Processing in C#.NET
Provide Various Methods to Setup and Modify Paragraphs in Word Document for C# Users
C#.NET Word document paragraph processing Interface control (XDoc.Word). It is a tool which enable users to process paragraph on a new document by using our API.
Create Paragraph in Word
The following demo code will show you how to create an empty paragraph in body only. You can create paragraphs in other story in the same way.
String docFilePath = @"";
//Open the document
DOCXDocument document =DOCXDocument.Open(docFilePath);
//Get the main document
IDocument doc = document.GetDocument();
//Create a empty paragraph for document
IParagraph paragraph = doc.CreateParagraph();
//Save the document
doc.Save(@"");
Set Paragraph Properties in Word
In C# class programming, you can use specific APIs to process paragraph in Word document. C# users can set paragraph properties and create content such as run, footnote, endnote and picture in a paragraph. The following demo code shows you how to set paragraph properties.
String docFilePath = @"";
//Open the document
DOCXDocument document = DOCXDocument.Open(docFilePath);
//Get the main ducument
IDocument doc = document.GetDocument();
//Document clone
IDocument doc0 = doc.Clone();
//Create a paragraph for document
IParagraph para = doc0.CreateParagraph();
//Create a run for para
IRun run = paragraph.CreateARun();
//Create text for run
run.CreateText("ABCDEFG");
//Paragraph properties.
//Create borders for para
para.CreateBorders();
//Indent setup
para.SetLeftIndent(500);
//Spacing setup
para.SetBoforeSpacing(300);
para.SetLineSpacing(200);
//Shadow
para.SetShadowFillColor(false, Color.Blue);
//Alignment
para.SetParagraphAlignment(ParagraphAlign.Center);
//MORE TODO:
//
//
//Save the document
doc0.Save(@"");
Create Footnote & Endnote in Paragraph
If you need to create a footnote or endnote in a paragraph, you may use this sample code.
String docFilePath = @"";
//Open the document
DOCXDocument document = DOCXDocument.Open(docFilePath);
//Get the main ducument
IDocument doc = document.GetDocument();
//Document clone
IDocument doc0 = doc.Clone();
//Create a paragraph for document
IParagraph para = doc0.CreateParagraph();
//Create a run for para
IRun run = paragraph.CreateARun();
//Create text in run
run.CreateText("Run for footnote and endnote!");
//Create a footnote for paragraph
IFootNote footnote = para.CreateFootNote();
//Create a paragraph for footnote
IParagraph ftnPara = footnote.CreateParagraph(0);
//Create a Run and text for footnote content
IRun ftnRun = ftnPara.CreateARun();
ftnRun.CreateText("Footnote created!");
//Create a endnote for paragraph
IEndtNote endnote = para.CreateEndNote();
//Create a paragraph for endnote
IParagraph endnPara = endnote.CreateParagraph(0);
//Create a Run and text for endnote content
IRun endnRun = endnPara.CreateARun();
endnRun.CreateText("Endnote created!");
//Save the document
doc0.Save(@"");
Create Picture in Paragraph
If you want to insert picture in a paragraph, you can use demo code below.
String docFilePath = @"";
String imageSrcPath = @"";
//Open the document
DOCXDocument document = DOCXDocument.Open(docFilePath);
//Get the main ducument
IDocument doc = document.GetDocument();
//Document clone
IDocument doc0 = doc.Clone();
//Create a paragraph for document
IParagraph para = doc0.CreateParagraph();
//Create a picture for para
IPicture picture = para.CreatePicture(imageSrcPath);
//Save the document
doc0.Save(@"");
Run Processing in Paragraph
We know a paragraph is made up of runs, if you want to modify paragraph content, you can modify run firstly. The following demo code provide a method to create run in paragraph.
String docFilePath = @"";
String imageSourcePath = @"";
//Open the document on local disk by given path.
DOCXDocument document = DOCXDocument.Open(docFilePath);
//Get the main document for edit
IDocument doc = document.GetDocument();
//Create a paragraph for document.
IParagraph paragraph = doc.CreateParagraph();
//Create a run for paragraph, now no content in run before edit.
IRun run = paragraph.CreateARun();
//Edit run
run.CreateText("Text created in run");
//Save the document after edit
doc.Save(@"");