Pārlūkot izejas kodu

Added example for manipulating a row as pattern in a table.

master
Olivier ROMAND pirms 9 gadiem
vecāks
revīzija
cad4eedf67
3 mainītis faili ar 148 papildinājumiem un 94 dzēšanām
  1. Binārs
      Examples/DocumentWithTemplateTable.docx
  2. 3
    0
      Examples/Examples.csproj
  3. 145
    94
      Examples/Program.cs

Binārs
Examples/DocumentWithTemplateTable.docx Parādīt failu


+ 3
- 0
Examples/Examples.csproj Parādīt failu

@@ -70,6 +70,9 @@
<None Include="DocumentWithBookmarks.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="DocumentWithTemplateTable.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Input.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>

+ 145
- 94
Examples/Program.cs Parādīt failu

@@ -1,4 +1,5 @@
using System;
using Novacode;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
@@ -6,7 +7,6 @@ using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Novacode;
using Image = Novacode.Image;
namespace Examples
@@ -39,6 +39,7 @@ namespace Examples
Chart3D();
DocumentMargins();
CreateTableWithTextDirection();
CreateTableRowsFromTemplate();
AddToc();
AddTocByReference();
@@ -256,6 +257,54 @@ namespace Examples
Console.WriteLine("\tCreated: docs\\DocumentHeading.docx\n");
}
}
/// <summary>
/// Loads a document having a table with a given line as template.
/// It avoids extra manipulation regarding style
/// </summary>
private static void CreateTableRowsFromTemplate()
{
Console.WriteLine("\tCreateTableFromTemplate()");
using (DocX docX = DocX.Load("DocumentWithTemplateTable.docx"))
{
//look for one specific table here
Table orderTable = docX.Tables.First(t => t.TableCaption == "ORDER_TABLE");
if (orderTable != null)
{
//Row 0 and 1 are Headers
//Row 2 is pattern
if (orderTable.RowCount >= 2)
{
//get the Pattern row for duplication
Row orderRowPattern = orderTable.Rows[2];
//Add 5 lines of product
for (int i = 0; i < 5; i++)
{
//InsertRow performs a copy, so we get markup in new line ready for replacements
Row newOrderRow = orderTable.InsertRow(orderRowPattern, 2 + i);
newOrderRow.ReplaceText("%PRODUCT_NAME%", "Product_" + i);
newOrderRow.ReplaceText("%PRODUCT_PRICE1%", "$ " + i * new Random().Next(1, 50));
newOrderRow.ReplaceText("%PRODUCT_PRICE2%", "$ " + i * new Random().Next(1, 50));
}
//pattern row is at the end now, can be removed from table
orderRowPattern.Remove();
}
docX.SaveAs(@"docs\CreateTableFromTemplate.docx");
}
else
{
Console.WriteLine("\tError, couldn't find table with caption ORDER_TABLE in document");
}
}
Console.WriteLine("\tCreated: docs\\CreateTableFromTemplate.docx");
}
private static void Bookmarks()
{
Console.WriteLine("\tBookmarks()");
@@ -272,7 +321,7 @@ namespace Examples
document.Save();
Console.WriteLine("\tCreated: docs\\Bookmarks.docx\n");
}
}
/// <summary>
@@ -465,7 +514,7 @@ namespace Examples
Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS"));
title.Alignment = Alignment.center;
// Insert a new Paragraph into the document.
Paragraph p1 = document.InsertParagraph();
@@ -496,7 +545,7 @@ namespace Examples
Paragraph p3 = document.InsertParagraph();
p3.AppendLine();
p3.AppendLine("Adding another table...");
// Adding another table
Table table1 = document.AddTable(2, 2);
table1.Design = TableDesign.ColorfulGridAccent2;
@@ -510,7 +559,7 @@ namespace Examples
p4.InsertTableBeforeSelf(table1);
p4.AppendLine();
// Insert numbered list after table
Paragraph p5 = document.InsertParagraph();
@@ -536,7 +585,7 @@ namespace Examples
using (DocX document = DocX.Create(@"docs\DocumentMargins.docx"))
{
// Create a float var that contains doc Margins properties.
// Create a float var that contains doc Margins properties.
float leftMargin = document.MarginLeft;
float rightMargin = document.MarginRight;
float topMargin = document.MarginTop;
@@ -551,8 +600,8 @@ namespace Examples
document.MarginLeft = leftMargin;
document.MarginRight = rightMargin;
document.MarginTop = topMargin;
document.MarginBottom = bottomMargin;
document.MarginBottom = bottomMargin;
// created bulleted lists
var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted);
@@ -563,7 +612,7 @@ namespace Examples
document.InsertList(bulletedList);
// Save this document.
document.Save();
@@ -579,30 +628,31 @@ namespace Examples
using (DocX document = DocX.Create(@"docs\DocumentsWithListsFontChange.docx"))
{
foreach ( FontFamily oneFontFamily in FontFamily.Families ) {
foreach (FontFamily oneFontFamily in FontFamily.Families)
{
FontFamily fontFamily = oneFontFamily;
double fontSize = 15;
FontFamily fontFamily = oneFontFamily;
double fontSize = 15;
// created numbered lists
var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered, 1);
document.AddListItem(numberedList, "First sub list item", 1);
document.AddListItem(numberedList, "Second List Item.");
document.AddListItem(numberedList, "Third list item.");
document.AddListItem(numberedList, "Nested item.", 1);
document.AddListItem(numberedList, "Second nested item.", 1);
// created numbered lists
var numberedList = document.AddList("First List Item.", 0, ListItemType.Numbered, 1);
document.AddListItem(numberedList, "First sub list item", 1);
document.AddListItem(numberedList, "Second List Item.");
document.AddListItem(numberedList, "Third list item.");
document.AddListItem(numberedList, "Nested item.", 1);
document.AddListItem(numberedList, "Second nested item.", 1);
// created bulleted lists
// created bulleted lists
var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted);
document.AddListItem(bulletedList, "Second bullet item");
document.AddListItem(bulletedList, "Sub bullet item", 1);
document.AddListItem(bulletedList, "Second sub bullet item", 1);
document.AddListItem(bulletedList, "Third bullet item");
var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted);
document.AddListItem(bulletedList, "Second bullet item");
document.AddListItem(bulletedList, "Sub bullet item", 1);
document.AddListItem(bulletedList, "Second sub bullet item", 1);
document.AddListItem(bulletedList, "Third bullet item");
document.InsertList(bulletedList);
document.InsertList(numberedList, fontFamily, fontSize);
document.InsertList(bulletedList);
document.InsertList(numberedList, fontFamily, fontSize);
}
// Save this document.
document.Save();
@@ -761,14 +811,14 @@ namespace Examples
// Insert a Paragraph into the first Header.
Paragraph p0 = header_first.InsertParagraph();
p0.Append("Hello First Header.").Bold();
// Insert a Paragraph into the odd Header.
Paragraph p1 = header_odd.InsertParagraph();
p1.Append("Hello Odd Header.").Bold();
// Insert a Paragraph into the even Header.
Paragraph p2 = header_even.InsertParagraph();
p2.Append("Hello Even Header.").Bold();
@@ -1696,21 +1746,21 @@ namespace Examples
static void HelloWorldKeepWithNext()
{
// Create a Paragraph that will stay on the same page as the paragraph that comes next
// Create a Paragraph that will stay on the same page as the paragraph that comes next
Console.WriteLine("\tHelloWorldKeepWithNext()");
// Create a new document.
// Create a new document.
using (DocX document = DocX.Create("docs\\HelloWorldKeepWithNext.docx"))
{
// Create a new Paragraph with the text "Hello World".
Paragraph p = document.InsertParagraph("Hello World.");
p.KeepWithNext();
document.InsertParagraph("Previous paragraph will appear on the same page as this paragraph");
// Save all changes made to this document.
document.Save();
Console.WriteLine("\tCreated: docs\\HelloWorldKeepWithNext.docx\n");
}
{
// Create a new Paragraph with the text "Hello World".
Paragraph p = document.InsertParagraph("Hello World.");
p.KeepWithNext();
document.InsertParagraph("Previous paragraph will appear on the same page as this paragraph");
// Save all changes made to this document.
document.Save();
Console.WriteLine("\tCreated: docs\\HelloWorldKeepWithNext.docx\n");
}
}
static void HelloWorldKeepLinesTogether()
{
@@ -1727,7 +1777,7 @@ namespace Examples
Console.WriteLine("\tCreated: docs\\HelloWorldKeepLinesTogether.docx\n");
}
}
static void LargeTable()
{
Console.WriteLine("\tLargeTable()");
@@ -1768,7 +1818,7 @@ namespace Examples
Console.WriteLine("\tCreated: docs\\LargeTable.docx\n");
}
static void TableWithSpecifiedWidths()
{
Console.WriteLine("\tTableSpecifiedWidths()");
@@ -1829,55 +1879,56 @@ namespace Examples
/// <summary>
/// Create a document with two pictures. One picture is inserted normal way, the other one with rotation
/// </summary>
static void HelloWorldAddPictureToWord() {
Console.WriteLine("\tHelloWorldAddPictureToWord()");
static void HelloWorldAddPictureToWord()
{
Console.WriteLine("\tHelloWorldAddPictureToWord()");
// Create a document.
using (DocX document = DocX.Create(@"docs\HelloWorldAddPictureToWord.docx"))
{
// Add an image into the document.
RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
rd.Up(2);
Image image = document.AddImage(rd.Path + @"\images\logo_template.png");
// Create a picture (A custom view of an Image).
Picture picture = image.CreatePicture();
picture.Rotation = 10;
picture.SetPictureShape(BasicShapes.cube);
// Insert a new Paragraph into the document.
Paragraph title = document.InsertParagraph().Append("This is a test for a picture").FontSize(20).Font(new FontFamily("Comic Sans MS"));
title.Alignment = Alignment.center;
// Insert a new Paragraph into the document.
Paragraph p1 = document.InsertParagraph();
// Append content to the Paragraph
p1.AppendLine("Just below there should be a picture ").Append("picture").Bold().Append(" inserted in a non-conventional way.");
p1.AppendLine();
p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
p1.AppendLine();
// Insert a new Paragraph into the document.
Paragraph p2 = document.InsertParagraph();
// Append content to the Paragraph.
p2.AppendLine("Is it correct?");
p2.AppendLine();
// Lets add another picture (without the fancy stuff)
Picture pictureNormal = image.CreatePicture();
Paragraph p3 = document.InsertParagraph();
p3.AppendLine("Lets add another picture (without the fancy rotation stuff)");
p3.AppendLine();
p3.AppendPicture(pictureNormal);
// Save this document.
document.Save();
Console.WriteLine("\tCreated: docs\\HelloWorldAddPictureToWord.docx\n");
}
}
// Create a document.
using (DocX document = DocX.Create(@"docs\HelloWorldAddPictureToWord.docx"))
{
// Add an image into the document.
RelativeDirectory rd = new RelativeDirectory(); // prepares the files for testing
rd.Up(2);
Image image = document.AddImage(rd.Path + @"\images\logo_template.png");
// Create a picture (A custom view of an Image).
Picture picture = image.CreatePicture();
picture.Rotation = 10;
picture.SetPictureShape(BasicShapes.cube);
// Insert a new Paragraph into the document.
Paragraph title = document.InsertParagraph().Append("This is a test for a picture").FontSize(20).Font(new FontFamily("Comic Sans MS"));
title.Alignment = Alignment.center;
// Insert a new Paragraph into the document.
Paragraph p1 = document.InsertParagraph();
// Append content to the Paragraph
p1.AppendLine("Just below there should be a picture ").Append("picture").Bold().Append(" inserted in a non-conventional way.");
p1.AppendLine();
p1.AppendLine("Check out this picture ").AppendPicture(picture).Append(" its funky don't you think?");
p1.AppendLine();
// Insert a new Paragraph into the document.
Paragraph p2 = document.InsertParagraph();
// Append content to the Paragraph.
p2.AppendLine("Is it correct?");
p2.AppendLine();
// Lets add another picture (without the fancy stuff)
Picture pictureNormal = image.CreatePicture();
Paragraph p3 = document.InsertParagraph();
p3.AppendLine("Lets add another picture (without the fancy rotation stuff)");
p3.AppendLine();
p3.AppendPicture(pictureNormal);
// Save this document.
document.Save();
Console.WriteLine("\tCreated: docs\\HelloWorldAddPictureToWord.docx\n");
}
}
}

Notiek ielāde…
Atcelt
Saglabāt