This project has moved and is read-only. For the latest updates, please go here.

Indent

Jun 2, 2010 at 1:40 PM
Hi, Thanks for a useful tool! I wonder if there is any way one can indent text or paragraphs without using tables?
Jun 4, 2010 at 2:47 PM
Hi Sqees,

sorry it took me so long to reply. I have been surfing for the week.
You could use tabs to indent your paragraphs

// Load a document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Insert a new 'indented' Paragraph at the end of this document.
document.InsertParagraph("\tNew text", false);

// Save all changes made to this document.
document.Save();
}// Release this document from memory

There are other ways to do this in Word such as paragraph margins, but
DocX does not currently support them.

I hope this helps.
Kind regards,
Cathal
Jun 8, 2010 at 12:52 PM
Hey Sqees,

I just added support for Paragraph indentation. I am still adding extra features to the dll, so if you send me your email address I will give you what I have thus far. Below is an example of how to use this new feature.

Indent only the first line of a Paragraph

// Create a new document.
using (DocX document = DocX.Create("Test.docx"))
{
    // Create a new Paragraph.
    Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
    
    // Indent only the first line of the Paragraph.
    p.IndentationFirstLine = 1.0f;
    
    // Save all changes made to this document.
    document.Save();
}

Indent all but the first line of a Paragraph

 // Create a new document.
 using (DocX document = DocX.Create("Test.docx"))
 {
     // Create a new Paragraph.
     Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
 
     // Indent all but the first line of the Paragraph.
     p.IndentationHanging = 1.0f;
     
     // Save all changes made to this document.
     document.Save();
 }

Indent an entire Paragraph from the left

 // Create a new document.
 using (DocX document = DocX.Create("Test.docx"))
 {
    // Create a new Paragraph.
    Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");

    // Indent this entire Paragraph from the left.
    p.IndentationBefore = 1.0f;
    
    // Save all changes made to this document.
    document.Save();
}

Indent an entire Paragraph from the right

 // Create a new document.
 using (DocX document = DocX.Create("Test.docx"))
 {
     // Create a new Paragraph.
     Paragraph p = document.InsertParagraph("Line 1\nLine 2\nLine 3");
 
     // Make the content of this Paragraph flow right to left.
     p.Direction = Direction.RightToLeft;
 
     // Indent this entire Paragraph from the right.
     p.IndentationAfter = 1.0f;
     
     // Save all changes made to this document.
     document.Save();
 }

kind regards,
Cathal
Jul 7, 2014 at 11:17 AM
Edited Jul 7, 2014 at 11:19 AM
Hi, great tool you craft.

I have some suggestions about indentation.

From open xml
.....
Indentation is defined with the <w:ind> element.

<w:pPr>
  <w:ind w:left="1440" w:right="1440" />
</w:pPr>

Values are in twentieths of a point: 1440 twips = 1 inch; 567 twips = 1 centimeter. To specify units in hundreths of a character, use attributes leftChars/endChars, rightChars/endChars, etc. Negative values are possible. See attributes below.
.......

I am not sure if the "twips" value should be a round number.
For example if IndentationFirstLine = 1.3F , the result is w:firstLine="740,999972820282", and the document cannot be opened.

There is line of code in Paragraph.cs for each indentation type setter that converts float value to string and then to XAttribute.

string indentation = ((indentationHanging / 0.1) * 57).ToString();

Here a value of 570 twips per 1 centimeter is used.
Math.Round seems to do the job, and maybe extracting a function, because it is used in several places.