Load a document.
using (DocX document = DocX.Load(@"Test.docx"))
{
// All Paragraphs in this document.
]]> documentParagraphs = document.Paragraphs;
// Make sure this document contains at least one Table.
if (document.Tables.Count() > 0)
{
// Get the first Table in this document.
Table t = document.Tables[0];
// All Paragraphs in this Table.
]]> tableParagraphs = t.Paragraphs;
// Make sure this Table contains at least one Row.
if (t.Rows.Count() > 0)
{
// Get the first Row in this document.
Row r = t.Rows[0];
// All Paragraphs in this Row.
]]> rowParagraphs = r.Paragraphs;
// Make sure this Row contains at least one Cell.
if (r.Cells.Count() > 0)
{
// Get the first Cell in this document.
Cell c = r.Cells[0];
// All Paragraphs in this Cell.
]]> cellParagraphs = c.Paragraphs;
}
}
}
// Save all changes to this document.
document.Save();
}// Release this document from memory.
// Load a document.
using (DocX document = DocX.Load(@"Test.docx"))
{
// Get the first Paragraph from this document.
Paragraph p = document.InsertParagraph();
// Set the Direction of this Paragraph.
p.Direction = Direction.RightToLeft;
// Make sure the document contains at lest one Table.
if (document.Tables.Count() > 0)
{
// Get the first Table from this document.
Table t = document.Tables[0];
/*
* Set the direction of the entire Table.
* Note: The same function is available at the Row and Cell level.
*/
t.SetDirection(Direction.RightToLeft);
}
// Save all changes to this document.
document.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Load(@"Test.docx"))
{
// Get all of the hyperlinks in this document
List<Hyperlink> hyperlinks = document.Hyperlinks;
// Change the first hyperlinks text and Uri
Hyperlink h0 = hyperlinks[0];
h0.Text = "DocX";
h0.Uri = new Uri("http://docx.codeplex.com");
// Save this document.
document.Save();
}
hyperlinks = document.Hyperlinks;
// Change the first hyperlinks text and Uri
Hyperlink h0 = hyperlinks[0];
h0.Text = "DocX";
h0.Uri = new Uri("http://docx.codeplex.com");
// Save this document.
document.Save();
}
]]>
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add a hyperlink to this document.
Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
// Add a Paragraph to this document and insert the hyperlink
Paragraph p1 = document.InsertParagraph();
p1.Append("This is a cool ").AppendHyperlink(h).Append(" .");
/*
* Remove the hyperlink from this Paragraph only.
* Note a reference to the hyperlink will still exist in the document and it can thus be reused.
*/
p1.Hyperlinks[0].Remove();
// Add a new Paragraph to this document and reuse the hyperlink h.
Paragraph p2 = document.InsertParagraph();
p2.Append("This is the same cool ").AppendHyperlink(h).Append(" .");
document.Save();
}// Release this document from memory.
pictures = t.Pictures;
// Save this document.
document.Save();
}
]]>
// Create a document.
using (DocX document = DocX.Load(@"Test.docx"))
{
// Get the first Table in this document.
Table t = document.Tables[0];
// Get a list of all Hyperlinks in this Table.
List<Hyperlink> hyperlinks = t.Hyperlinks;
// Save this document.
document.Save();
}
// Load a document into memory.
using (DocX document = DocX.Load(@"Test.docx"))
{
// Get the first Table in this document.
Table t = document.Tables[0];
// Get the character index of Table t in this document.
int index = t.Index;
// Remove Table t.
t.Remove();
// Insert a new Table at the original index of Table t.
Table newTable = document.InsertTable(index, 4, 4);
// Set the design of this new Table, so that we can see it.
newTable.Design = TableDesign.LightShadingAccent1;
// Save all changes made to the document.
document.Save();
} // Release this document from memory.
// Load a document.
using (DocX document = DocX.Load(@"Test.docx"))
{
// Get the first table in a document.
Table table = document.Tables[0];
// Set the content direction for all content in this table to RightToLeft.
table.SetDirection(Direction.RightToLeft);
// Save all changes made to this document.
document.Save();
}
// Load a document into memory.
using (DocX document = DocX.Load(@"Test.docx"))
{
// Get the first Table in this document.
Table t = d.Tables[0];
// Remove this Table.
t.Remove();
// Save all changes made to the document.
document.Save();
} // Release this document from memory.
// Load a document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Get the first table in this document.
Table table = document.Tables[0];
// Insert a new row at the end of this table.
Row row = table.InsertRow();
// Loop through each cell in this new row.
foreach (Cell c in row.Cells)
{
// Set the text of each new cell to "Hello".
c.Paragraphs[0].InsertText("Hello", false);
}
// Save the document to a new file.
document.SaveAs(@"C:\Example\Test2.docx");
}// Release this document from memory.
// Load a document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Get the first Table in this document.
Table table = document.Tables[0];
// Insert a new column to this right of this table.
table.InsertColumn();
// Set the new columns text to "Row no."
table.Rows[0].Cells[table.ColumnCount - 1].Paragraph.InsertText("Row no.", false);
// Loop through each row in the table.
for (int i = 1; i < table.Rows.Count; i++)
{
// The current row.
Row row = table.Rows[i];
// The cell in this row that belongs to the new column.
Cell cell = row.Cells[table.ColumnCount - 1];
// The first Paragraph that this cell houses.
Paragraph p = cell.Paragraphs[0];
// Insert this rows index.
p.InsertText(i.ToString(), false);
}
document.Save();
}// Release this document from memory.
// Load a document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Get the first table in this document.
Table table = document.Tables[0];
// Remove the last row from this table.
table.RemoveRow();
// Save the document.
document.Save();
}// Release this document from memory.
// Load a document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Get the first table in this document.
Table table = document.Tables[0];
// Remove the first row from this table.
table.RemoveRow(0);
// Save the document.
document.Save();
}// Release this document from memory.
// Load a document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Get the first table in this document.
Table table = document.Tables[0];
// Remove the last column from this table.
table.RemoveColumn();
// Save the document.
document.Save();
}// Release this document from memory.
// Load a document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Get the first table in this document.
Table table = document.Tables[0];
// Remove the first column from this table.
table.RemoveColumn(0);
// Save the document.
document.Save();
}// Release this document from memory.
// Load a document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Get the first table in this document.
Table table = document.Tables[0];
// Insert a new row at index 1 in this table.
Row row = table.InsertRow(1);
// Loop through each cell in this new row.
foreach (Cell c in row.Cells)
{
// Set the text of each new cell to "Hello".
c.Paragraphs[0].InsertText("Hello", false);
}
// Save the document to a new file.
document.SaveAs(@"C:\Example\Test2.docx");
}// Release this document from memory.
// Load a document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Get the first Table in this document.
Table table = document.Tables[0];
// Insert a new column to this left of this table.
table.InsertColumn(0, false);
// Set the new columns text to "Row no."
table.Rows[0].Cells[table.ColumnCount - 1].Paragraph.InsertText("Row no.", false);
// Loop through each row in the table.
for (int i = 1; i < table.Rows.Count; i++)
{
// The current row.
Row row = table.Rows[i];
// The cell in this row that belongs to the new column.
Cell cell = row.Cells[table.ColumnCount - 1];
// The first Paragraph that this cell houses.
Paragraph p = cell.Paragraphs[0];
// Insert this rows index.
p.InsertText(i.ToString(), false);
}
document.Save();
}// Release this document from memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p1 = document.InsertParagraph("Paragraph", false);
// Insert a new Table.
Table t1 = document.InsertTable(2, 2);
t1.Design = TableDesign.LightShadingAccent1;
// Insert a page break before this Table.
t1.InsertPageBreakBeforeSelf();
// Save this document.
document.Save();
}// Release this document from memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Table.
Table t1 = document.InsertTable(2, 2);
t1.Design = TableDesign.LightShadingAccent1;
// Insert a page break after this Table.
t1.InsertPageBreakAfterSelf();
// Insert a new Paragraph.
Paragraph p1 = document.InsertParagraph("Paragraph", false);
// Save this document.
document.Save();
}// Release this document from memory.
// Place holder for a Table.
Table t;
// Load document a.
using (DocX documentA = DocX.Load(@"a.docx"))
{
// Get the first Table from this document.
t = documentA.Tables[0];
}
// Load document b.
using (DocX documentB = DocX.Load(@"b.docx"))
{
// Get the first Table in document b.
Table t2 = documentB.Tables[0];
// Insert the Table from document a before this Table.
Table newTable = t2.InsertTableBeforeSelf(t);
// Save all changes made to document b.
documentB.Save();
}// Release this document from memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
//Insert a Table into this document.
Table t = document.InsertTable(2, 2);
t.Design = TableDesign.LightShadingAccent1;
t.Alignment = Alignment.center;
// Insert a new Table before this Table.
Table newTable = t.InsertTableBeforeSelf(2, 2);
newTable.Design = TableDesign.LightShadingAccent2;
newTable.Alignment = Alignment.center;
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Place holder for a Table.
Table t;
// Load document a.
using (DocX documentA = DocX.Load(@"a.docx"))
{
// Get the first Table from this document.
t = documentA.Tables[0];
}
// Load document b.
using (DocX documentB = DocX.Load(@"b.docx"))
{
// Get the first Table in document b.
Table t2 = documentB.Tables[0];
// Insert the Table from document a after this Table.
Table newTable = t2.InsertTableAfterSelf(t);
// Save all changes made to document b.
documentB.Save();
}// Release this document from memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
//Insert a Table into this document.
Table t = document.InsertTable(2, 2);
t.Design = TableDesign.LightShadingAccent1;
t.Alignment = Alignment.center;
// Insert a new Table after this Table.
Table newTable = t.InsertTableAfterSelf(2, 2);
newTable.Design = TableDesign.LightShadingAccent2;
newTable.Alignment = Alignment.center;
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Place holder for a Paragraph.
Paragraph p;
// Load document a.
using (DocX documentA = DocX.Load(@"a.docx"))
{
// Get the first paragraph from this document.
p = documentA.Paragraphs[0];
}
// Load document b.
using (DocX documentB = DocX.Load(@"b.docx"))
{
// Get the first Table in document b.
Table t = documentB.Tables[0];
// Insert the Paragraph from document a before this Table.
Paragraph newParagraph = t.InsertParagraphBeforeSelf(p);
// Save all changes made to document b.
documentB.Save();
}// Release this document from memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a Table into this document.
Table t = document.InsertTable(2, 2);
t.InsertParagraphBeforeSelf("I was inserted before the next Table.");
// Save all changes made to this new document.
document.Save();
}// Release this new document form memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a Table into this document.
Table t = document.InsertTable(2, 2);
t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false);
// Save all changes made to this new document.
document.Save();
}// Release this new document form memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a Table into this document.
Table t = document.InsertTable(2, 2);
Formatting boldFormatting = new Formatting();
boldFormatting.Bold = true;
t.InsertParagraphBeforeSelf("I was inserted before the next Table.", false, boldFormatting);
// Save all changes made to this new document.
document.Save();
}// Release this new document form memory.
// Place holder for a Paragraph.
Paragraph p;
// Load document a.
using (DocX documentA = DocX.Load(@"a.docx"))
{
// Get the first paragraph from this document.
p = documentA.Paragraphs[0];
}
// Load document b.
using (DocX documentB = DocX.Load(@"b.docx"))
{
// Get the first Table in document b.
Table t = documentB.Tables[0];
// Insert the Paragraph from document a after this Table.
Paragraph newParagraph = t.InsertParagraphAfterSelf(p);
// Save all changes made to document b.
documentB.Save();
}// Release this document from memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a Table into this document.
Table t = document.InsertTable(2, 2);
Formatting boldFormatting = new Formatting();
boldFormatting.Bold = true;
t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false, boldFormatting);
// Save all changes made to this new document.
document.Save();
}// Release this new document form memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a Table into this document.
Table t = document.InsertTable(2, 2);
t.InsertParagraphAfterSelf("I was inserted after the previous Table.", false);
// Save all changes made to this new document.
document.Save();
}// Release this new document form memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a Table into this document.
Table t = document.InsertTable(2, 2);
t.InsertParagraphAfterSelf("I was inserted after the previous Table.");
// Save all changes made to this new document.
document.Save();
}// Release this new document form memory.
// Create a new document.
using (DocX document = DocX.Create("Test.docx"))
{
// Insert a table into this document.
Table t = document.InsertTable(3, 3);
// Create a large blue border.
Border b = new Border(BorderStyle.Tcbs_single, BorderSize.seven, 0, Color.Blue);
// Set the tables Top, Bottom, Left and Right Borders to b.
t.SetBorder(TableBorderType.Top, b);
t.SetBorder(TableBorderType.Bottom, b);
t.SetBorder(TableBorderType.Left, b);
t.SetBorder(TableBorderType.Right, b);
// Save the document.
document.Save();
}
// Create a new document.
using (DocX document = DocX.Create("Test.docx"))
{
// Insert table into this document.
Table t = document.InsertTable(3, 3);
t.Design = TableDesign.TableGrid;
// Get the center cell.
Cell center = t.Rows[1].Cells[1];
// Insert some text so that we can see the effect of the Margins.
center.Paragraphs[0].Append("Center Cell");
// Set the center cells Left, Margin to 10.
center.MarginLeft = 25;
// Save the document.
document.Save();
}
// Create a new document.
using (DocX document = DocX.Create("Test.docx"))
{
// Insert table into this document.
Table t = document.InsertTable(3, 3);
t.Design = TableDesign.TableGrid;
// Get the center cell.
Cell center = t.Rows[1].Cells[1];
// Insert some text so that we can see the effect of the Margins.
center.Paragraphs[0].Append("Center Cell");
// Set the center cells Right, Margin to 10.
center.MarginRight = 25;
// Save the document.
document.Save();
}
// Create a new document.
using (DocX document = DocX.Create("Test.docx"))
{
// Insert table into this document.
Table t = document.InsertTable(3, 3);
t.Design = TableDesign.TableGrid;
// Get the center cell.
Cell center = t.Rows[1].Cells[1];
// Insert some text so that we can see the effect of the Margins.
center.Paragraphs[0].Append("Center Cell");
// Set the center cells Top, Margin to 10.
center.MarginTop = 25;
// Save the document.
document.Save();
}
// Create a new document.
using (DocX document = DocX.Create("Test.docx"))
{
// Insert table into this document.
Table t = document.InsertTable(3, 3);
t.Design = TableDesign.TableGrid;
// Get the center cell.
Cell center = t.Rows[1].Cells[1];
// Insert some text so that we can see the effect of the Margins.
center.Paragraphs[0].Append("Center Cell");
// Set the center cells Top, Margin to 10.
center.MarginBottom = 25;
// Save the document.
document.Save();
}
using (DocX document = DocX.Create("Test.docx"))
{
// Add an image to the document.
Image i = document.AddImage(@"Image.jpg");
// Create a picture i.e. (A custom view of an image)
Picture p = i.CreatePicture();
p.FlipHorizontal = true;
p.Rotation = 10;
// Create a new Paragraph.
Paragraph par = document.InsertParagraph();
// Append content to the Paragraph.
par.Append("Here is a cool picture")
.AppendPicture(p)
.Append(" don't you think so?");
// Save all changes made to this document.
document.Save();
}
pictures = p.Pictures;
// Save this document.
document.Save();
}
]]>
// Create a document.
using (DocX document = DocX.Load(@"Test.docx"))
{
// Get the first Paragraph in this document.
Paragraph p = document.Paragraphs[0];
// Get all of the hyperlinks in this Paragraph.
]]> hyperlinks = paragraph.Hyperlinks;
// Change the first hyperlinks text and Uri
Hyperlink h0 = hyperlinks[0];
h0.Text = "DocX";
h0.Uri = new Uri("http://docx.codeplex.com");
// Save this document.
document.Save();
}
// Create a new document.
using (DocX document = DocX.Create("Test.docx"))
{
// Create a new Paragraph with the text "Hello World".
Paragraph p = document.InsertParagraph("Hello World.");
// Make this Paragraph flow right to left. Default is left to right.
p.Direction = Direction.RightToLeft;
// Save all changes made to this document.
document.Save();
}
// 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 = 2.0f;
// Save all changes made to this document.
document.Save();
}
// 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();
}
// 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 = 2.0f;
// Save all changes made to this document.
document.Save();
}
// 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 = 2.0f;
// Save all changes made to this document.
document.Save();
}
// Load a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph with russian text and set curent local culture to it.
Paragraph p = document.InsertParagraph("Привет мир!").CurentCulture();
// Save this document.
document.Save();
}
// Place holder for a Table.
Table t;
// Load document a.
using (DocX documentA = DocX.Load(@"a.docx"))
{
// Get the first Table from this document.
t = documentA.Tables[0];
}
// Load document b.
using (DocX documentB = DocX.Load(@"b.docx"))
{
// Get the first Paragraph in document b.
Paragraph p2 = documentB.Paragraphs[0];
// Insert the Table from document a before this Paragraph.
Table newTable = p2.InsertTableBeforeSelf(t);
// Save all changes made to document b.
documentB.Save();
}// Release this document from memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
//Insert a Paragraph into this document.
Paragraph p = document.InsertParagraph("Hello World", false);
// Insert a new Table before this Paragraph.
Table newTable = p.InsertTableBeforeSelf(2, 2);
newTable.Design = TableDesign.LightShadingAccent2;
newTable.Alignment = Alignment.center;
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Place holder for a Table.
Table t;
// Load document a.
using (DocX documentA = DocX.Load(@"a.docx"))
{
// Get the first Table from this document.
t = documentA.Tables[0];
}
// Load document b.
using (DocX documentB = DocX.Load(@"b.docx"))
{
// Get the first Paragraph in document b.
Paragraph p2 = documentB.Paragraphs[0];
// Insert the Table from document a after this Paragraph.
Table newTable = p2.InsertTableAfterSelf(t);
// Save all changes made to document b.
documentB.Save();
}// Release this document from memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
//Insert a Paragraph into this document.
Paragraph p = document.InsertParagraph("Hello World", false);
// Insert a new Table after this Paragraph.
Table newTable = p.InsertTableAfterSelf(2, 2);
newTable.Design = TableDesign.LightShadingAccent2;
newTable.Alignment = Alignment.center;
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Place holder for a Paragraph.
Paragraph p;
// Load document a.
using (DocX documentA = DocX.Load(@"a.docx"))
{
// Get the first paragraph from this document.
p = documentA.Paragraphs[0];
}
// Load document b.
using (DocX documentB = DocX.Load(@"b.docx"))
{
// Get the first Paragraph in document b.
Paragraph p2 = documentB.Paragraphs[0];
// Insert the Paragraph from document a before this Paragraph.
Paragraph newParagraph = p2.InsertParagraphBeforeSelf(p);
// Save all changes made to document b.
documentB.Save();
}// Release this document from memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a Paragraph into this document.
Paragraph p = document.InsertParagraph("I am a Paragraph", false);
p.InsertParagraphBeforeSelf("I was inserted before the next Paragraph.");
// Save all changes made to this new document.
document.Save();
}// Release this new document form memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a Paragraph into this document.
Paragraph p = document.InsertParagraph("I am a Paragraph", false);
p.InsertParagraphBeforeSelf("I was inserted before the next Paragraph.", false);
// Save all changes made to this new document.
document.Save();
}// Release this new document form memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a Paragraph into this document.
Paragraph p = document.InsertParagraph("I am a Paragraph", false);
Formatting boldFormatting = new Formatting();
boldFormatting.Bold = true;
p.InsertParagraphBeforeSelf("I was inserted before the next Paragraph.", false, boldFormatting);
// Save all changes made to this new document.
document.Save();
}// Release this new document form memory.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p1 = document.InsertParagraph("Paragraph 1", false);
// Insert a new Paragraph.
Paragraph p2 = document.InsertParagraph("Paragraph 2", false);
// Insert a page break before Paragraph two.
p2.InsertPageBreakBeforeSelf();
// Save this document.
document.Save();
}// Release this document from memory.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p1 = document.InsertParagraph("Paragraph 1", false);
// Insert a page break after this Paragraph.
p1.InsertPageBreakAfterSelf();
// Insert a new Paragraph.
Paragraph p2 = document.InsertParagraph("Paragraph 2", false);
// Save this document.
document.Save();
}// Release this document from memory.
// Crete a new document.
using (DocX document = DocX.Create("Test.docx"))
{
// Add a Hyperlink into this document.
Hyperlink h = document.AddHyperlink("link", new Uri("http://www.google.com"));
// Insert a new Paragraph into the document.
Paragraph p1 = document.InsertParagraph("AC");
// Insert the hyperlink into this Paragraph.
p1.InsertHyperlink(1, h);
Assert.IsTrue(p1.Text == "AlinkC"); // Make sure the hyperlink was inserted correctly;
// Remove the hyperlink
p1.RemoveHyperlink(0);
Assert.IsTrue(p1.Text == "AC"); // Make sure the hyperlink was removed correctly;
}
// Place holder for a Paragraph.
Paragraph p;
// Load document a.
using (DocX documentA = DocX.Load(@"a.docx"))
{
// Get the first paragraph from this document.
p = documentA.Paragraphs[0];
}
// Load document b.
using (DocX documentB = DocX.Load(@"b.docx"))
{
// Get the first Paragraph in document b.
Paragraph p2 = documentB.Paragraphs[0];
// Insert the Paragraph from document a after this Paragraph.
Paragraph newParagraph = p2.InsertParagraphAfterSelf(p);
// Save all changes made to document b.
documentB.Save();
}// Release this document from memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a Paragraph into this document.
Paragraph p = document.InsertParagraph("I am a Paragraph", false);
Formatting boldFormatting = new Formatting();
boldFormatting.Bold = true;
p.InsertParagraphAfterSelf("I was inserted after the previous Paragraph.", false, boldFormatting);
// Save all changes made to this new document.
document.Save();
}// Release this new document form memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a Paragraph into this document.
Paragraph p = document.InsertParagraph("I am a Paragraph", false);
p.InsertParagraphAfterSelf("I was inserted after the previous Paragraph.", false);
// Save all changes made to this new document.
document.Save();
}// Release this new document form memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a Paragraph into this document.
Paragraph p = document.InsertParagraph("I am a Paragraph", false);
p.InsertParagraphAfterSelf("I was inserted after the previous Paragraph.");
// Save all changes made to this new document.
document.Save();
}// Release this new document form memory.
// Create a document using a relative filename.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Create and Insert a new Paragraph into this document.
Paragraph p = document.InsertParagraph("Hello", false);
// Remove the Paragraph and track this as a change.
p.Remove(true);
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Create a document using a relative filename.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Create a text formatting.
Formatting f = new Formatting();
f.FontColor = Color.Red;
f.Size = 30;
// Iterate through the Paragraphs in this document.
foreach (Paragraph p in document.Paragraphs)
{
// Insert the string "Start: " at the begining of every Paragraph and flag it as a change.
p.InsertText("Start: ", true, f);
}
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Create a document using a relative filename.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Create a text formatting.
Formatting f = new Formatting();
f.FontColor = Color.Red;
f.Size = 30;
// Iterate through the paragraphs in this document.
foreach (Paragraph p in document.Paragraphs)
{
// Insert the string "\tEnd" at the end of every paragraph and flag it as a change.
p.InsertText("\tEnd", true, f);
}
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Create a document using a relative filename.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Create a text formatting.
Formatting f = new Formatting();
f.FontColor = Color.Red;
f.Size = 30;
// Iterate through the Paragraphs in this document.
foreach (Paragraph p in document.Paragraphs)
{
// Insert the string "Start: " at the begining of every Paragraph and flag it as a change.
p.InsertText(0, "Start: ", true, f);
}
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Create a document using a relative filename.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Create a text formatting.
Formatting f = new Formatting();
f.FontColor = Color.Red;
f.Size = 30;
// Iterate through the paragraphs in this document.
foreach (Paragraph p in document.Paragraphs)
{
// Insert the string "\tStart:\t" at the begining of every paragraph and flag it as a change.
p.InsertText(0, "\tStart:\t", true, f);
}
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Load a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph with russian text and set specific culture to it.
Paragraph p = document.InsertParagraph("Привет мир").Culture(CultureInfo.CreateSpecificCulture("ru-RU"));
// Save this document.
document.Save();
}
// Load a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph and Append some text to it.
Paragraph p = document.InsertParagraph().Append("Hello World!!!");
// Save this document.
document.Save();
}
// Load a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Prepare format to use
Formatting format = new Formatting();
format.Bold = true;
format.Size = 18;
format.FontColor = Color.Blue;
// Insert a new Paragraph and append some text to it with the custom format
Paragraph p = document.InsertParagraph().Append("Hello World!!!", format);
// Save this document.
document.Save();
}
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add a hyperlink to this document.
Hyperlink h = document.AddHyperlink("Google", new Uri("http://www.google.com"));
// Add a new Paragraph to this document.
Paragraph p = document.InsertParagraph();
p.Append("My favourite search engine is ");
p.AppendHyperlink(h);
p.Append(", I think it's great.");
// Save all changes made to this document.
document.Save();
}
using (DocX document = DocX.Create("Test.docx"))
{
// Add an image to the document.
Image i = document.AddImage(@"Image.jpg");
// Create a picture i.e. (A custom view of an image)
Picture p = i.CreatePicture();
p.FlipHorizontal = true;
p.Rotation = 10;
// Create a new Paragraph.
Paragraph par = document.InsertParagraph();
// Append content to the Paragraph.
par.Append("Here is a cool picture")
.AppendPicture(p)
.Append(" don't you think so?");
// Save all changes made to this document.
document.Save();
}
using (DocX document = DocX.Create("Test.docx"))
{
// Add an equation to the document.
document.AddEquation("x=y+z");
// Save all changes made to this document.
document.Save();
}
Load test document.
using (DocX document = DocX.Create("Test.docx"))
{
// Add Headers and Footers into this document.
document.AddHeaders();
document.AddFooters();
document.DifferentFirstPage = true;
document.DifferentOddAndEvenPages = true;
// Add an Image to this document.
Xceed.Words.NET.Image img = document.AddImage(directory_documents + "purple.png");
// Create a Picture from this Image.
Picture pic = img.CreatePicture();
// Main document.
Paragraph p0 = document.InsertParagraph("Hello");
p0.InsertPicture(pic, 3);
// Header first.
Paragraph p1 = document.Headers.first.InsertParagraph("----");
p1.InsertPicture(pic, 2);
// Header odd.
Paragraph p2 = document.Headers.odd.InsertParagraph("----");
p2.InsertPicture(pic, 2);
// Header even.
Paragraph p3 = document.Headers.even.InsertParagraph("----");
p3.InsertPicture(pic, 2);
// Footer first.
Paragraph p4 = document.Footers.first.InsertParagraph("----");
p4.InsertPicture(pic, 2);
// Footer odd.
Paragraph p5 = document.Footers.odd.InsertParagraph("----");
p5.InsertPicture(pic, 2);
// Footer even.
Paragraph p6 = document.Footers.even.InsertParagraph("----");
p6.InsertPicture(pic, 2);
// Save this document.
document.Save();
}
// Load a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph and Append a new line with some text to it.
Paragraph p = document.InsertParagraph().AppendLine("Hello World!!!");
// Save this document.
document.Save();
}
// Load a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph and Append a new line with some text to it.
Paragraph p = document.InsertParagraph().AppendLine();
// Save this document.
document.Save();
}
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p = document.InsertParagraph();
p.Append("I am ")
.Append("Bold").Bold()
.Append(" I am not");
// Save this document.
document.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p = document.InsertParagraph();
p.Append("I am ")
.Append("Italic").Italic()
.Append(" I am not");
// Save this document.
document.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p = document.InsertParagraph();
p.Append("I am ")
.Append("Blue").Color(Color.Blue)
.Append(" I am not");
// Save this document.
document.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p = document.InsertParagraph();
p.Append("I am ")
.Append("Underlined").UnderlineStyle(UnderlineStyle.doubleLine)
.Append(" I am not");
// Save this document.
document.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p = document.InsertParagraph();
p.Append("I am ")
.Append("Big").FontSize(20)
.Append(" I am not");
// Save this document.
document.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p = document.InsertParagraph();
p.Append("I am ")
.Append("Times new roman").Font(new FontFamily("Times new roman"))
.Append(" I am not");
// Save this document.
document.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p = document.InsertParagraph();
p.Append("I am ")
.Append("Capitalized").CapsStyle(CapsStyle.caps)
.Append(" I am not");
// Save this document.
document.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p = document.InsertParagraph();
p.Append("I am ")
.Append("superscript").Script(Script.superscript)
.Append(" I am not");
// Save this document.
document.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p = document.InsertParagraph();
p.Append("I am ")
.Append("highlighted").Highlight(Highlight.green)
.Append(" I am not");
// Save this document.
document.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p = document.InsertParagraph();
p.Append("I am ")
.Append("outlined").Misc(Misc.outline)
.Append(" I am not");
// Save this document.
document.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p = document.InsertParagraph();
p.Append("I am ")
.Append("striked").StrikeThrough(StrikeThrough.doubleStrike)
.Append(" I am not");
// Save this document.
document.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p = document.InsertParagraph();
p.Append("I am ")
.Append("color underlined").UnderlineStyle(UnderlineStyle.dotted).UnderlineColor(Color.Orange)
.Append(" I am not");
// Save this document.
document.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Insert a new Paragraph.
Paragraph p = document.InsertParagraph();
p.Append("I am ")
.Append("hidden").Hide()
.Append(" I am not");
// Save this document.
document.Save();
}// Release this document from memory.
// Load a document.
using (DocX document = DocX.Create("CustomProperty_Add.docx"))
{
// Add a few Custom Properties to this document.
document.AddCustomProperty(new CustomProperty("fname", "cathal"));
document.AddCustomProperty(new CustomProperty("age", 24));
document.AddCustomProperty(new CustomProperty("male", true));
document.AddCustomProperty(new CustomProperty("newyear2012", new DateTime(2012, 1, 1)));
document.AddCustomProperty(new CustomProperty("fav_num", 3.141592));
// Insert a new Paragraph and append a load of DocProperties.
Paragraph p = document.InsertParagraph("fname: ")
.AppendDocProperty(document.CustomProperties["fname"])
.Append(", age: ")
.AppendDocProperty(document.CustomProperties["age"])
.Append(", male: ")
.AppendDocProperty(document.CustomProperties["male"])
.Append(", newyear2012: ")
.AppendDocProperty(document.CustomProperties["newyear2012"])
.Append(", fav_num: ")
.AppendDocProperty(document.CustomProperties["fav_num"]);
// Save the changes to the document.
document.Save();
}
// Load a document
using (DocX document = DocX.Create(@"Test.docx"))
{
// Create a custom property.
CustomProperty name = new CustomProperty("name", "Cathal Coffey");
// Add this custom property to this document.
document.AddCustomProperty(name);
// Create a text formatting.
Formatting f = new Formatting();
f.Bold = true;
f.Size = 14;
f.StrikeThrough = StrickThrough.strike;
// Insert a new paragraph.
Paragraph p = document.InsertParagraph("Author: ", false, f);
// Insert a field of type document property to display the custom property name and track this change.
p.InsertDocProperty(name, true, f);
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Create a document using a relative filename.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Iterate through the paragraphs
foreach (Paragraph p in document.Paragraphs)
{
// Remove the first two characters from every paragraph
p.RemoveText(0, 2, false);
}
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Create a document using a relative filename.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Iterate through the paragraphs
foreach (Paragraph p in document.Paragraphs)
{
// Remove all but the first 2 characters from this Paragraph.
p.RemoveText(2, false);
}
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Load a document using a relative filename.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// The formatting to match.
Formatting matchFormatting = new Formatting();
matchFormatting.Size = 10;
matchFormatting.Italic = true;
matchFormatting.FontFamily = new FontFamily("Times New Roman");
// The formatting to apply to the inserted text.
Formatting newFormatting = new Formatting();
newFormatting.Size = 22;
newFormatting.UnderlineStyle = UnderlineStyle.dotted;
newFormatting.Bold = true;
// Iterate through the paragraphs in this document.
foreach (Paragraph p in document.Paragraphs)
{
/*
* Replace all instances of the string "wrong" with the string "right" and ignore case.
* Each inserted instance of "wrong" should use the Formatting newFormatting.
* Only replace an instance of "wrong" if it is Size 10, Italic and Times New Roman.
* SubsetMatch means that the formatting must contain all elements of the match formatting,
* but it can also contain additional formatting for example Color, UnderlineStyle, etc.
* ExactMatch means it must not contain additional formatting.
*/
p.ReplaceText("wrong", "right", false, RegexOptions.IgnoreCase, newFormatting, matchFormatting, MatchFormattingOptions.SubsetMatch);
}
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Load a document
using (DocX document = DocX.Load(@"Test.docx"))
{
// Loop through the paragraphs in this document.
foreach(Paragraph p in document.Paragraphs)
{
// Find all instances of 'go' in this paragraph.
]]> gos = document.FindAll("go");
/*
* Insert 'don't' in frount of every instance of 'go' in this document to produce 'don't go'.
* An important trick here is to do the inserting in reverse document order. If you inserted
* in document order, every insert would shift the index of the remaining matches.
*/
gos.Reverse();
foreach (int index in gos)
{
p.InsertText(index, "don't ", false);
}
}
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Load a document
using (DocX document = DocX.Load(@"Test.docx"))
{
// Loop through the paragraphs in this document.
foreach(Paragraph p in document.Paragraphs)
{
// Find all instances of 'go' in this paragraph (Ignore case).
]]> gos = document.FindAll("go", RegexOptions.IgnoreCase);
/*
* Insert 'don't' in frount of every instance of 'go' in this document to produce 'don't go'.
* An important trick here is to do the inserting in reverse document order. If you inserted
* in document order, every insert would shift the index of the remaining matches.
*/
gos.Reverse();
foreach (int index in gos)
{
p.InsertText(index, "don't ", false);
}
}
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add Headers to the document.
document.AddHeaders();
// Get the default Header.
Header header = document.Headers.odd;
// Insert a Paragraph into the Header.
Paragraph p0 = header.InsertParagraph("Page ( of )");
// Insert place holders for PageNumber and PageCount into the Header.
// Word will replace these with the correct value for each Page.
p0.InsertPageNumber(PageNumberFormat.normal, 6);
p0.InsertPageCount(PageNumberFormat.normal, 11);
// Save the document.
document.Save();
}
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add Headers to the document.
document.AddHeaders();
// Get the default Header.
Header header = document.Headers.odd;
// Insert a Paragraph into the Header.
Paragraph p0 = header.InsertParagraph();
// Appemd place holders for PageNumber and PageCount into the Header.
// Word will replace these with the correct value for each Page.
p0.Append("Page (");
p0.AppendPageNumber(PageNumberFormat.normal);
p0.Append(" of ");
p0.AppendPageCount(PageNumberFormat.normal);
p0.Append(")");
// Save the document.
document.Save();
}
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add Headers to the document.
document.AddHeaders();
// Get the default Header.
Header header = document.Headers.odd;
// Insert a Paragraph into the Header.
Paragraph p0 = header.InsertParagraph("Page ( of )");
// Insert place holders for PageNumber and PageCount into the Header.
// Word will replace these with the correct value for each Page.
p0.InsertPageNumber(PageNumberFormat.normal, 6);
p0.InsertPageCount(PageNumberFormat.normal, 11);
// Save the document.
document.Save();
}
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add Headers to the document.
document.AddHeaders();
// Get the default Header.
Header header = document.Headers.odd;
// Insert a Paragraph into the Header.
Paragraph p0 = header.InsertParagraph();
// Appemd place holders for PageNumber and PageCount into the Header.
// Word will replace these with the correct value for each Page.
p0.Append("Page (");
p0.AppendPageNumber(PageNumberFormat.normal);
p0.Append(" of ");
p0.AppendPageCount(PageNumberFormat.normal);
p0.Append(")");
// Save the document.
document.Save();
}
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
if(document.isProtected)
Console.WriteLine("Protected");
else
Console.WriteLine("Not protected");
// Save the document.
document.Save();
}
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add header support to this document.
document.AddHeaders();
// Get a collection of all headers in this document.
Headers headers = document.Headers;
// The header used for the first page of this document.
Header first = headers.first;
// The header used for odd pages of this document.
Header odd = headers.odd;
// The header used for even pages of this document.
Header even = headers.even;
}
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add footer support to this document.
document.AddFooters();
// Get a collection of all footers in this document.
Footers footers = document.Footers;
// The footer used for the first page of this document.
Footer first = footers.first;
// The footer used for odd pages of this document.
Footer odd = footers.odd;
// The footer used for even pages of this document.
Footer even = footers.even;
}
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add header support to this document.
document.AddHeaders();
// Get a collection of all headers in this document.
Headers headers = document.Headers;
// The header used for odd pages of this document.
Header odd = headers.odd;
// The header used for even pages of this document.
Header even = headers.even;
// Force the document to use a different header for odd and even pages.
document.DifferentOddAndEvenPages = true;
// Content can be added to the Headers in the same manor that it would be added to the main document.
Paragraph p1 = odd.InsertParagraph();
p1.Append("This is the odd pages header.");
Paragraph p2 = even.InsertParagraph();
p2.Append("This is the even pages header.");
// Save all changes to this document.
document.Save();
}// Release this document from memory.
// Load a document.
DocX document = DocX.Load(@"C:\Example\Test.docx");
// Loop through each Image in this document.
foreach (Xceed.Words.NET.Image i in document.Images)
{
// Get the unique Id which identifies this Image.
string uniqueId = i.Id;
}
// Load Example.docx
DocX document = DocX.Load(@"C:\Example\Test.docx");
/*
* No two custom properties can have the same name,
* so a Dictionary is the perfect data structure to store them in.
* Each custom property can be accessed using its name.
*/
foreach (string name in document.CustomProperties.Keys)
{
// Grab a custom property using its name.
CustomProperty cp = document.CustomProperties[name];
// Write this custom properties details to Console.
Console.WriteLine(string.Format("Name: '{0}', Value: {1}", cp.Name, cp.Value));
}
Console.WriteLine("Press any key...");
// Wait for the user to press a key before closing the Console.
Console.ReadKey();
// Load Example.docx
DocX document = DocX.Load(@"C:\Example\Test.docx");
/*
* No two custom properties can have the same name,
* so a Dictionary is the perfect data structure to store them in.
* The values of this Dictionary are CustomProperties.
*/
foreach (CustomProperty cp in document.CustomProperties.Values)
{
// Write this custom properties details to Console.
Console.WriteLine(string.Format("Name: '{0}', Value: {1}", cp.Name, cp.Value));
}
Console.WriteLine("Press any key...");
// Wait for the user to press a key before closing the Console.
Console.ReadKey();
// Load a document
DocX document = DocX.Load(@"C:\Example\Test.docx");
// Get the text of this document.
string text = document.Text;
// Write the text of this document to Console.
Console.Write(text);
// Wait for the user to press a key before closing the console window.
Console.ReadKey();
Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Make sure the document is protected before checking the protection type.
if (document.isProtected)
{
EditRestrictions protection = document.GetProtectionType();
Console.WriteLine("Document is protected using " + protection.ToString());
}
else
Console.WriteLine("Document is not protected.");
// Save the document.
document.Save();
}
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Allow no editing, only the adding of comment.
document.AddProtection(EditRestrictions.comments);
// Save the document.
document.Save();
}
// Create a new document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Remove any editing restrictions that are imposed on this document.
document.RemoveProtection();
// Save the document.
document.Save();
}
// Create a new document.
using (DocX newDocument = DocX.Create(@"NewDocument.docx"))
{
// Load an old document.
using (DocX oldDocument = DocX.Load(@"OldDocument.docx"))
{
// Insert the old document into the new document.
newDocument.InsertDocument(oldDocument);
// Save the new document.
newDocument.Save();
}// Release the old document from memory.
}// Release the new document from memory.
// Create a document.
using (DocX document = DocX.Create(@"C:\Example\Test.docx"))
{
// Create a new Table with 2 columns and 3 rows.
Table newTable = document.InsertTable(2, 3);
// Set the design of this Table.
newTable.Design = TableDesign.LightShadingAccent2;
// Set the column names.
newTable.Rows[0].Cells[0].Paragraph.InsertText("Ice Cream", false);
newTable.Rows[0].Cells[1].Paragraph.InsertText("Price", false);
// Fill row 1
newTable.Rows[1].Cells[0].Paragraph.InsertText("Chocolate", false);
newTable.Rows[1].Cells[1].Paragraph.InsertText("€3:50", false);
// Fill row 2
newTable.Rows[2].Cells[0].Paragraph.InsertText("Vanilla", false);
newTable.Rows[2].Cells[1].Paragraph.InsertText("€3:00", false);
// Save all changes made to document b.
document.Save();
}// Release this document from memory.
// Place holder for a Table.
Table t;
// Load document a.
using (DocX documentA = DocX.Load(@"C:\Example\a.docx"))
{
// Get the first Table from this document.
t = documentA.Tables[0];
}
// Load document b.
using (DocX documentB = DocX.Load(@"C:\Example\b.docx"))
{
/*
* Insert the Table that was extracted from document a, into document b.
* This creates a new Table that is now associated with document b.
*/
Table newTable = documentB.InsertTable(10, t);
// Save all changes made to document b.
documentB.Save();
}// Release this document from memory.
// Place holder for a Table.
Table t;
// Load document a.
using (DocX documentA = DocX.Load(@"C:\Example\a.docx"))
{
// Get the first Table from this document.
t = documentA.Tables[0];
}
// Load document b.
using (DocX documentB = DocX.Load(@"C:\Example\b.docx"))
{
/*
* Insert the Table that was extracted from document a, into document b.
* This creates a new Table that is now associated with document b.
*/
Table newTable = documentB.InsertTable(t);
// Save all changes made to document b.
documentB.Save();
}// Release this document from memory.
// Create a document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Create a new Table with 3 rows and 2 columns. Insert this Table at index 37.
Table newTable = document.InsertTable(37, 3, 2);
// Set the design of this Table.
newTable.Design = TableDesign.LightShadingAccent3;
// Set the column names.
newTable.Rows[0].Cells[0].Paragraph.InsertText("Ice Cream", false);
newTable.Rows[0].Cells[1].Paragraph.InsertText("Price", false);
// Fill row 1
newTable.Rows[1].Cells[0].Paragraph.InsertText("Chocolate", false);
newTable.Rows[1].Cells[1].Paragraph.InsertText("€3:50", false);
// Fill row 2
newTable.Rows[2].Cells[0].Paragraph.InsertText("Vanilla", false);
newTable.Rows[2].Cells[1].Paragraph.InsertText("€3:00", false);
// Save all changes made to document b.
document.Save();
}// Release this document from memory.
// Use a FileStream fs to create a new document.
using(FileStream fs = new FileStream(@"C:\Example\Test.docx", FileMode.Create))
{
// Load the document using fs
using (DocX document = DocX.Create(fs))
{
// Do something with the document here.
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
}
using(SPSite mySite = new SPSite("http://server/sites/site"))
{
// Open a connection to the SharePoint site
using(SPWeb myWeb = mySite.OpenWeb())
{
// Create a MemoryStream ms.
using (MemoryStream ms = new MemoryStream())
{
// Create a document using ms.
using (DocX document = DocX.Create(ms))
{
// Do something with the document here.
// Save all changes made to this document.
document.Save();
}// Release this document from memory
// Add the document to the SharePoint site
web.Files.Add("filename", ms.ToArray(), true);
}
}
}
// Create a document using a relative filename.
using (DocX document = DocX.Create(@"..\Test.docx"))
{
// Do something with the document here.
// Save all changes made to this document.
document.Save();
}// Release this document from memory
// Create a document using a relative filename.
using (DocX document = DocX.Create(@"..\Test.docx"))
{
// Do something with the document here.
// Save all changes made to this document.
document.Save();
}// Release this document from memory
// Open a FileStream fs to a document.
using (FileStream fs = new FileStream(@"C:\Example\Test.docx", FileMode.Open))
{
// Load the document using fs.
using (DocX document = DocX.Load(fs))
{
// Do something with the document here.
// Save all changes made to the document.
document.Save();
}// Release this document from memory.
}
// Get the SharePoint site that you want to access.
using (SPSite mySite = new SPSite("http://server/sites/site"))
{
// Open a connection to the SharePoint site
using (SPWeb myWeb = mySite.OpenWeb())
{
// Grab a document stored on this site.
SPFile file = web.GetFile("Source_Folder_Name/Source_File");
// DocX.Load requires a Stream, so open a Stream to this document.
Stream str = new MemoryStream(file.OpenBinary());
// Load the file using the Stream str.
using (DocX document = DocX.Load(str))
{
// Do something with the document here.
// Save all changes made to the document.
document.Save();
}// Release this document from memory.
}
}
// Load a document using its fully qualified filename
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Do something with the document here
// Save all changes made to document.
document.Save();
}// Release this document from memory.
// Load a document using its relative filename.
using(DocX document = DocX.Load(@"..\..\Test.docx"))
{
// Do something with the document here.
// Save all changes made to document.
document.Save();
}// Release this document from memory.
// Load a document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Add an Image from a file.
document.AddImage(@"C:\Example\Image.png");
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Open a FileStream fs to an Image.
using (FileStream fs = new FileStream(@"C:\Example\Image.jpg", FileMode.Open))
{
// Load a document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Add an Image from a filestream fs.
document.AddImage(fs);
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
}
// Create a document.
using (DocX document = DocX.Create(@"Test.docx"))
{
// Add a hyperlink to this document.
Hyperlink h = document.AddHyperlink("Google", new Uri("http://www.google.com"));
// Add a new Paragraph to this document.
Paragraph p = document.InsertParagraph();
p.Append("My favourite search engine is ");
p.AppendHyperlink(h);
p.Append(", I think it's great.");
// Save all changes made to this document.
document.Save();
}
// Load a document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// Add an Image from a file.
document.AddImage(@"C:\Example\Image.jpg");
// Save all changes made to this document.
document.Save();
}// Release this document from memory.
// Load a document using its fully qualified filename.
DocX document = DocX.Load(@"C:\Example\Test1.docx");
// Insert a new Paragraph
document.InsertParagraph("Hello world!", false);
// Save the document to a new location.
document.SaveAs(@"C:\Example\Test2.docx");
DocX document;
using (FileStream fs1 = new FileStream(@"C:\Example\Test1.docx", FileMode.Open))
{
// Load a document using a stream.
document = DocX.Load(fs1);
// Insert a new Paragraph
document.InsertParagraph("Hello world again!", false);
}
// Save the document to a new location.
document.SaveAs(@"C:\Example\Test2.docx");
// Place holder for a document.
DocX document;
using (FileStream fs1 = new FileStream(@"C:\Example\Test1.docx", FileMode.Open))
{
// Load a document using a stream.
document = DocX.Load(fs1);
// Insert a new Paragraph
document.InsertParagraph("Hello world again!", false);
}
using (FileStream fs2 = new FileStream(@"C:\Example\Test2.docx", FileMode.Create))
{
// Save the document to a different stream.
document.SaveAs(fs2);
}
// Release this document from memory.
document.Dispose();
DocX document;
using (FileStream fs1 = new FileStream(@"C:\Example\Test1.docx", FileMode.Open))
{
// Load a document using a stream.
document = DocX.Load(fs1);
// Insert a new Paragraph
document.InsertParagraph("Hello world again!", false);
}
using (FileStream fs2 = new FileStream(@"C:\Example\Test2.docx", FileMode.Create))
{
// Save the document to a different stream.
document.SaveAs(fs2);
}
// Load Example.docx
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// If this document does not contain a core property called 'forename', create one.
if (!document.CoreProperties.ContainsKey("forename"))
{
// Create a new core property called 'forename' and set its value.
document.AddCoreProperty("forename", "Cathal");
}
// Get this documents core property called 'forename'.
string forenameValue = document.CoreProperties["forename"];
// Print all of the information about this core property to Console.
Console.WriteLine(string.Format("Name: '{0}', Value: '{1}'\nPress any key...", "forename", forenameValue));
// Save all changes made to this document.
document.Save();
} // Release this document from memory.
// Wait for the user to press a key before exiting.
Console.ReadKey();
// Load Example.docx
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// A CustomProperty called forename which stores a string.
CustomProperty forename;
// If this document does not contain a custom property called 'forename', create one.
if (!document.CustomProperties.ContainsKey("forename"))
{
// Create a new custom property called 'forename' and set its value.
document.AddCustomProperty(new CustomProperty("forename", "Cathal"));
}
// Get this documents custom property called 'forename'.
forename = document.CustomProperties["forename"];
// Print all of the information about this CustomProperty to Console.
Console.WriteLine(string.Format("Name: '{0}', Value: '{1}'\nPress any key...", forename.Name, forename.Value));
// Save all changes made to this document.
document.Save();
} // Release this document from memory.
// Wait for the user to press a key before exiting.
Console.ReadKey();
// Load document.
using (DocX document = DocX.Load(@"C:\Example\Test.docx"))
{
// The document is only in memory while in this scope.
}// Dispose() is automatically called at this point.
// Load document.
DocX document = DocX.Load(@"C:\Example\Test.docx");
// Do something with the document here.
// Dispose of the document.
document.Dispose();