Procházet zdrojové kódy

Merge pull request #24 from oromand/master

Added example for manipulating a row as pattern in a table.
master
PrzemyslawKlys před 9 roky
rodič
revize
c298a1a700
3 změnil soubory, kde provedl 148 přidání a 94 odebrání
  1. binární
      Examples/DocumentWithTemplateTable.docx
  2. 3
    0
      Examples/Examples.csproj
  3. 145
    94
      Examples/Program.cs

binární
Examples/DocumentWithTemplateTable.docx Zobrazit soubor


+ 3
- 0
Examples/Examples.csproj Zobrazit soubor

<None Include="DocumentWithBookmarks.docx"> <None Include="DocumentWithBookmarks.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>
<None Include="DocumentWithTemplateTable.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Include="Input.docx"> <None Include="Input.docx">
<CopyToOutputDirectory>Always</CopyToOutputDirectory> <CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None> </None>

+ 145
- 94
Examples/Program.cs Zobrazit soubor

using System;
using Novacode;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Data; using System.Data;
using System.Drawing; using System.Drawing;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Novacode;
using Image = Novacode.Image; using Image = Novacode.Image;
namespace Examples namespace Examples
Chart3D(); Chart3D();
DocumentMargins(); DocumentMargins();
CreateTableWithTextDirection(); CreateTableWithTextDirection();
CreateTableRowsFromTemplate();
AddToc(); AddToc();
AddTocByReference(); AddTocByReference();
Console.WriteLine("\tCreated: docs\\DocumentHeading.docx\n"); 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() private static void Bookmarks()
{ {
Console.WriteLine("\tBookmarks()"); Console.WriteLine("\tBookmarks()");
document.Save(); document.Save();
Console.WriteLine("\tCreated: docs\\Bookmarks.docx\n"); Console.WriteLine("\tCreated: docs\\Bookmarks.docx\n");
} }
} }
/// <summary> /// <summary>
Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS")); Paragraph title = document.InsertParagraph().Append("Test").FontSize(20).Font(new FontFamily("Comic Sans MS"));
title.Alignment = Alignment.center; title.Alignment = Alignment.center;
// Insert a new Paragraph into the document. // Insert a new Paragraph into the document.
Paragraph p1 = document.InsertParagraph(); Paragraph p1 = document.InsertParagraph();
Paragraph p3 = document.InsertParagraph(); Paragraph p3 = document.InsertParagraph();
p3.AppendLine(); p3.AppendLine();
p3.AppendLine("Adding another table..."); p3.AppendLine("Adding another table...");
// Adding another table // Adding another table
Table table1 = document.AddTable(2, 2); Table table1 = document.AddTable(2, 2);
table1.Design = TableDesign.ColorfulGridAccent2; table1.Design = TableDesign.ColorfulGridAccent2;
p4.InsertTableBeforeSelf(table1); p4.InsertTableBeforeSelf(table1);
p4.AppendLine(); p4.AppendLine();
// Insert numbered list after table // Insert numbered list after table
Paragraph p5 = document.InsertParagraph(); Paragraph p5 = document.InsertParagraph();
using (DocX document = DocX.Create(@"docs\DocumentMargins.docx")) 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 leftMargin = document.MarginLeft;
float rightMargin = document.MarginRight; float rightMargin = document.MarginRight;
float topMargin = document.MarginTop; float topMargin = document.MarginTop;
document.MarginLeft = leftMargin; document.MarginLeft = leftMargin;
document.MarginRight = rightMargin; document.MarginRight = rightMargin;
document.MarginTop = topMargin; document.MarginTop = topMargin;
document.MarginBottom = bottomMargin;
document.MarginBottom = bottomMargin;
// created bulleted lists // created bulleted lists
var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted); var bulletedList = document.AddList("First Bulleted Item.", 0, ListItemType.Bulleted);
document.InsertList(bulletedList); document.InsertList(bulletedList);
// Save this document. // Save this document.
document.Save(); document.Save();
using (DocX document = DocX.Create(@"docs\DocumentsWithListsFontChange.docx")) 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. // Save this document.
document.Save(); document.Save();
// Insert a Paragraph into the first Header. // Insert a Paragraph into the first Header.
Paragraph p0 = header_first.InsertParagraph(); Paragraph p0 = header_first.InsertParagraph();
p0.Append("Hello First Header.").Bold(); p0.Append("Hello First Header.").Bold();
// Insert a Paragraph into the odd Header. // Insert a Paragraph into the odd Header.
Paragraph p1 = header_odd.InsertParagraph(); Paragraph p1 = header_odd.InsertParagraph();
p1.Append("Hello Odd Header.").Bold(); p1.Append("Hello Odd Header.").Bold();
// Insert a Paragraph into the even Header. // Insert a Paragraph into the even Header.
Paragraph p2 = header_even.InsertParagraph(); Paragraph p2 = header_even.InsertParagraph();
p2.Append("Hello Even Header.").Bold(); p2.Append("Hello Even Header.").Bold();
static void HelloWorldKeepWithNext() 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()"); Console.WriteLine("\tHelloWorldKeepWithNext()");
// Create a new document.
// Create a new document.
using (DocX document = DocX.Create("docs\\HelloWorldKeepWithNext.docx")) 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() static void HelloWorldKeepLinesTogether()
{ {
Console.WriteLine("\tCreated: docs\\HelloWorldKeepLinesTogether.docx\n"); Console.WriteLine("\tCreated: docs\\HelloWorldKeepLinesTogether.docx\n");
} }
} }
static void LargeTable() static void LargeTable()
{ {
Console.WriteLine("\tLargeTable()"); Console.WriteLine("\tLargeTable()");
Console.WriteLine("\tCreated: docs\\LargeTable.docx\n"); Console.WriteLine("\tCreated: docs\\LargeTable.docx\n");
} }
static void TableWithSpecifiedWidths() static void TableWithSpecifiedWidths()
{ {
Console.WriteLine("\tTableSpecifiedWidths()"); Console.WriteLine("\tTableSpecifiedWidths()");
/// <summary> /// <summary>
/// Create a document with two pictures. One picture is inserted normal way, the other one with rotation /// Create a document with two pictures. One picture is inserted normal way, the other one with rotation
/// </summary> /// </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");
}
}
} }

Načítá se…
Zrušit
Uložit