aspose.wordsはwordを生成し、表と段落のいくつかのセグメントコードを作成します.
12679 ワード
class Program
{
static void Main(string[] args)
{
Document doc = new Document();
DocumentBuilder builder = new DocumentBuilder(doc);
//
PageSetup ps = builder.PageSetup;
ps.PaperSize = Aspose.Words.PaperSize.A4;
ps.Orientation = Orientation.Portrait;
ps.TopMargin = ConvertUtil.InchToPoint(1.0);
ps.BottomMargin = ConvertUtil.InchToPoint(1.0);
ps.LeftMargin = ConvertUtil.InchToPoint(0.5);
ps.RightMargin = ConvertUtil.InchToPoint(0.5);
ps.HeaderDistance = ConvertUtil.InchToPoint(0.2);
ps.FooterDistance = ConvertUtil.InchToPoint(0.2);
//ps.DifferentFirstPageHeaderFooter = true; //
ps.HeaderDistance = 50;
ps.FooterDistance = 60;
//
DataTable data = new DataTable();
data.Columns.Add("Column1");
data.Columns.Add("Column2");
data.Columns.Add("Column3");
data.Columns.Add("Column4");
data.Columns.Add("Column5");
Random rand = new Random();
for (int i = 0; i < 20; i++)
{
DataRow row = data.NewRow();
foreach (DataColumn column in data.Columns)
{
row[column.ColumnName] = rand.Next();
}
data.Rows.Add(row);
}
// DocumentBuilder
CreateTable(builder, data, "This is table title");
builder.InsertBreak(BreakType.PageBreak);
// Table
Table table = CreateTable(doc, data, "This is table title");
builder.CurrentSection.Body.AppendChild(table);
builder.MoveToDocumentEnd();
builder.InsertBreak(BreakType.PageBreak);
builder.InsertBreak(BreakType.PageBreak);
builder.InsertBreak(BreakType.PageBreak);
builder.InsertBreak(BreakType.PageBreak);
builder.InsertBreak(BreakType.PageBreak);
#region
// ( )
builder.Font.ClearFormatting();
builder.Font.Bold = true;
builder.Font.Name = " ";
builder.Font.Size = 14;
builder.ParagraphFormat.ClearFormatting();
builder.ParagraphFormat.LineSpacing = 19;
builder.Writeln(" 、 ");
builder.Font.ClearFormatting();
builder.Font.Name = " ";
builder.Font.Size = 12;
builder.Write(" :");
builder.Font.ClearFormatting();
builder.Font.Underline = Underline.Single;
builder.Write(" ");
builder.InsertImage(" .png", RelativeHorizontalPosition.LeftMargin, 80, RelativeVerticalPosition.Line, -10, 50, 20, WrapType.None);
builder.Font.ClearFormatting();
builder.Font.Name = " ";
builder.Font.Size = 12;
builder.Write(" / :");
builder.Font.ClearFormatting();
builder.Font.Underline = Underline.Single;
builder.Write(" ");
builder.InsertImage(" .png", RelativeHorizontalPosition.LeftMargin, 270, RelativeVerticalPosition.Line, -10, 50, 20, WrapType.None);
builder.Font.ClearFormatting();
builder.Font.Name = " ";
builder.Font.Size = 12;
builder.Write(" :");
builder.Font.ClearFormatting();
builder.Font.Underline = Underline.Single;
builder.Write(" ."); //bug: , ,
builder.InsertImage(" .png", RelativeHorizontalPosition.LeftMargin, 460, RelativeVerticalPosition.Line, -10, 50, 20, WrapType.None);
builder.Font.ClearFormatting();
builder.MoveToDocumentEnd();
#endregion
#region
// Create the headers.
builder.MoveToHeaderFooter(HeaderFooterType.HeaderPrimary);
builder.ParagraphFormat.Borders.Bottom.LineStyle = LineStyle.Single;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
builder.Write("Header Text goes here...");
//add footer having current date
//builder.MoveToHeaderFooter(HeaderFooterType.FooterPrimary);
////builder.InsertField("Date", ""); //
//builder.InsertField("Page");
// Get the primary footer
// page/numpages
foreach (Section section in doc.Sections)
{
HeaderFooter hf = new HeaderFooter(section.Document, HeaderFooterType.FooterEven);
section.HeadersFooters.Add(hf);
// Get the first paragraph of footer
hf.AppendChild(new Paragraph(section.Document));
Paragraph para = hf.FirstParagraph;
// Add current page number field
para.AppendField("PAGE");
HeaderFooter hf2 = new HeaderFooter(section.Document, HeaderFooterType.FooterPrimary);
section.HeadersFooters.Add(hf2);
// Get the first paragraph of footer
hf2.AppendChild(new Paragraph(section.Document));
Paragraph para2 = hf2.FirstParagraph;
// Add current page number field
para2.AppendField("PAGE");
// Add a / character
para2.AppendChild(new Run(doc, "/"));
// Add total page numbers field
para2.AppendField("NUMPAGES");
para2.ParagraphFormat.Borders.Top.LineStyle = LineStyle.Single;
}
#endregion
doc.UpdateFields();
//
doc.Save("text.doc", SaveFormat.Doc);
doc.Save("text.pdf", SaveFormat.Pdf);
doc.Save("text.rtf", SaveFormat.Rtf);
}
// DocumentBuilder
static void CreateTable(DocumentBuilder builder, DataTable data, string title)
{
if (data == null || data.Rows.Count <= 0 || string.IsNullOrWhiteSpace(title))
{
return;
}
//
builder.StartTable();
//
builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.First;
builder.CellFormat.SetPaddings(0, 10, 0, 10);
builder.CellFormat.FitText = false; //
builder.CellFormat.VerticalAlignment = CellVerticalAlignment.Center; //
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //
builder.Font.ClearFormatting();
builder.Font.Spacing = 0;
builder.Font.Bold = true;
builder.Write(title);
for (int i = 0; i < data.Columns.Count - 1; i++)
{
builder.InsertCell();
builder.CellFormat.HorizontalMerge = CellMerge.Previous; //
}
//builder.CellFormat.VerticalMerge = CellMerge.First;
builder.EndRow();//
//
foreach (DataColumn column in data.Columns)
{
//
builder.InsertCell();
//
builder.CellFormat.ClearFormatting(); //
builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.Font.ClearFormatting();
builder.Font.Size = 12;
builder.Font.Bold = true;
builder.ParagraphFormat.Alignment = ParagraphAlignment.Center;
//
builder.Write(column.ColumnName);
}
builder.EndRow();
foreach (DataRow dataRow in data.Rows)
{
foreach (DataColumn column in data.Columns)
{
//
builder.InsertCell();
//
builder.CellFormat.ClearFormatting(); //
builder.CellFormat.HorizontalMerge = CellMerge.None;
builder.Font.ClearFormatting();
builder.Font.Size = 12;
//
builder.Write(dataRow[column.ColumnName].ToString());
}
//
builder.EndRow();
}
//
builder.EndTable();
}
// Table
static Table CreateTable(Document doc, DataTable data, string title)
{
Table table = new Table(doc);
Row row;
Cell cell;
Run run;
row = table.NewRow();
cell = row.NewCell();
cell.CellFormat.HorizontalMerge = CellMerge.First;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.FirstParagraph.ParagraphFormat.LineSpacing = 30;
run = new Run(doc);
run.Font.Bold = true;
run.Text = title;
cell.FirstParagraph.ParagraphFormat.Alignment = ParagraphAlignment.Center; //
cell.FirstParagraph.AppendChild(run);
for (int i = 0; i < data.Columns.Count - 1; i++)
{
cell = row.NewCell();
cell.CellFormat.ClearFormatting();
cell.CellFormat.HorizontalMerge = CellMerge.Previous;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
}
if (data != null && data.Rows.Count > 0 && data.Columns.Count > 0)
{
//
row = table.NewRow();
//row.RowFormat.ClearFormatting();
row.RowFormat.HeightRule = HeightRule.AtLeast; // :
row.RowFormat.Height = 20;
foreach (DataColumn column in data.Columns)
{
//cell=row.NewCell(column.ColumnName);
cell = row.NewCell();
cell.CellFormat.ClearFormatting();
cell.CellFormat.HorizontalMerge = CellMerge.None;
run = new Run(doc);
run.Font.Bold = true;
run.Text = column.ColumnName;
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
cell.FirstParagraph.AppendChild(run);
}
foreach (DataRow dr in data.Rows)
{
row = table.NewRow();
//row.RowFormat.ClearFormatting();
row.RowFormat.HeightRule = HeightRule.AtLeast; // :
row.RowFormat.Height = 20;
foreach (DataColumn dataColumn in data.Columns)
{
cell = row.NewCell(dr[dataColumn.ColumnName].ToString());
cell.CellFormat.ClearFormatting();
cell.CellFormat.VerticalAlignment = CellVerticalAlignment.Center;
}
}
}
//table.ClearBorders(); //
//table.AutoFit(AutoFitBehavior.FixedColumnWidths); // ,
return table;
}
}
他のいくつかの拡張方法のコードは次のとおりです.
public static class MyExtendMethod
{
public static Row NewRow(this Table table)
{
Row row = new Row(table.Document);
table.AppendChild(row);
return row;
}
public static Cell NewCell(this Row row)
{
Cell cell = new Cell(row.Document);
row.AppendChild(cell);
cell.AppendChild(new Paragraph(row.Document));
return cell;
}
public static Cell NewCell(this Row row,string cellText)
{
Cell cell = new Cell(row.Document);
cell.AppendChild(new Paragraph(row.Document));
row.AppendChild(cell);
cell.FirstParagraph.AppendChild(new Run(row.Document, cellText));
return cell;
}
}