我是第一次使用 MigraDoc 库,需要帮助。我正在使用 pdf 格式打印表格MigraDoc
。在我的表中,3 行组成一个组。所以每 3 行组必须在同一页面上,但在我当前的代码中,这没有发生。
它将在同一页上打印第一行或第二行,如果页面结束,第三行将进入下一页。
public void CreateDocument()
{
this.document = new Document { Info = { Title = "Time Sheet Report By Job", } };
this.DefineStyles();
this.CreatePage();
this.FillContent();
}
public void FillContent()
{
int x = 1;
foreach (XElement r in this.xReport.Element(ElrepTable).Elements(ElrepRow))
{
Row row1 = this.table.AddRow();
int i = 0;
row1.Cells[0].Borders.Visible = false;
foreach (XElement c in r.Elements(ElrepCell))
{
row1.Cells[i++].AddParagraph(c.Value);
}
if (x++ % 3 != 0)
{
continue;
}
row1.Borders.Bottom.Width = 1;
row1.Cells[0].Borders.Bottom.Visible = true;
row1.Cells[0].Borders.Bottom.Width = 1;
}
int colCount = this.GetColCount();
int rowCount = this.GetRowCount();
this.table.SetEdge(1, 1, colCount - 1, rowCount - 1, Edge.Box, BorderStyle.Single, 0.05);
}
private void CreatePage()
{
Section section = this.document.AddSection();
section.PageSetup.LeftMargin = "1cm";
section.PageSetup.RightMargin = "1cm";
section.PageSetup.BottomMargin = "2cm";
section.PageSetup.TopMargin = "4.5cm";
// Put a logo in the header
string imgPath = this.SaveImg(Resource1.MMS_Logo, "logo");
Image image = section.Headers.Primary.AddImage(imgPath);
image.Height = "2cm";
image.LockAspectRatio = true;
image.RelativeVertical = RelativeVertical.Line;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.Top = ShapePosition.Top;
image.Left = ShapePosition.Center;
image.WrapFormat.Style = WrapStyle.Through;
imgPath = this.SaveImg(Resource1.shadow, "shadow");
Image image2 = section.Headers.Primary.AddImage(imgPath); // .Headers.Primary
image2.Height = "1.5cm";
image2.Width = "5cm";
image2.RelativeVertical = RelativeVertical.Line;
image2.RelativeHorizontal = RelativeHorizontal.Margin;
image2.Top = ShapePosition.Top;
image2.Left = ShapePosition.Right;
image2.WrapFormat.Style = WrapStyle.Through;
this.leftTextFrame = section.Headers.Primary.AddTextFrame();
this.leftTextFrame.Height = "10.0cm";
this.leftTextFrame.Width = "7.0cm";
this.leftTextFrame.RelativeHorizontal = RelativeHorizontal.Margin;
this.leftTextFrame.RelativeVertical = RelativeVertical.Page;
this.leftTextFrame.Left = ShapePosition.Left;
this.leftTextFrame.Top = "1.75cm";
this.centerTextFrame = section.Headers.Primary.AddTextFrame();
this.centerTextFrame.Height = "5.0cm";
this.centerTextFrame.Width = "6.0cm";
this.centerTextFrame.RelativeHorizontal = RelativeHorizontal.Margin;
this.centerTextFrame.RelativeVertical = RelativeVertical.Page;
this.centerTextFrame.Left = ShapePosition.Center;
this.centerTextFrame.Top = "3.5cm";
this.rightTextFrame = section.Headers.Primary.AddTextFrame();
this.rightTextFrame.Height = "3.0cm";
this.rightTextFrame.Width = "5.0cm";
this.rightTextFrame.RelativeHorizontal = RelativeHorizontal.Margin;
this.rightTextFrame.RelativeVertical = RelativeVertical.Page;
this.rightTextFrame.Left = ShapePosition.Right;
this.rightTextFrame.Top = "1.75cm";
Dictionary<string, string> jobdet = this.GetJobDetails();
// Put sender in address frame
Paragraph p1 = this.leftTextFrame.AddParagraph();
var f = new Font { Bold = true, Size = 8 };
p1.AddFormattedText(this.GetTitle(), f);
p1.AddLineBreak();
p1.AddLineBreak();
p1.AddFormattedText("Account Information", new Font { Bold = true, Italic = true, Size = 7 });
p1.AddLineBreak();
Paragraph p2 = this.leftTextFrame.AddParagraph();
p2.Format.LeftIndent = "2cm";
p2.AddFormattedText("Job #:", new Font { Bold = true, Size = 6 });
p2.AddTab();
p1.AddTab();
p2.AddFormattedText(this.GetJobNo(), new Font { Size = 6 });
p2.AddLineBreak();
p2.AddFormattedText("Job Name:", new Font { Bold = true, Size = 6 });
p2.AddTab();
p2.AddFormattedText(jobdet["Name"], new Font { Size = 6 });
p2.AddLineBreak();
p2.AddFormattedText("Address:", new Font { Bold = true, Size = 6 });
p2.AddTab();
p2.AddFormattedText(jobdet["Address"], new Font { Size = 6 });
p2.AddLineBreak();
Paragraph cp1 = this.centerTextFrame.AddParagraph();
cp1.AddFormattedText("Supervisor:", new Font { Bold = true, Size = 6 });
cp1.AddTab();
cp1.AddFormattedText(jobdet["Supervisor"], new Font { Size = 6 });
cp1.AddLineBreak();
cp1.AddLineBreak();
cp1.AddFormattedText("Location:", new Font { Bold = true, Size = 6 });
cp1.AddTab();
cp1.AddTab();
cp1.AddFormattedText(jobdet["Location"], new Font { Size = 6 });
Paragraph rp1 = this.rightTextFrame.AddParagraph();
rp1.Format.Alignment = ParagraphAlignment.Center;
rp1.AddFormattedText("Timesheet by Job", new Font { Bold = true, Size = 9 });
rp1.AddLineBreak();
rp1.AddFormattedText("Pay Period End " + this.GetEdate(), new Font { Bold = true, Size = 8 });
rp1.AddLineBreak();
rp1.AddLineBreak();
rp1.AddFormattedText(this.GetOnDate(), new Font { Size = 6 });
rp1.AddTab();
FormattedText ft = rp1.AddFormattedText("Page ", new Font { Bold = true, Size = 7 });
ft.AddPageField();
ft.Font.Size = 7;
ft.Font.Bold = true;
ft = rp1.AddFormattedText(" of ", new Font { Bold = true, Size = 7 });
ft.AddNumPagesField();
ft.Font.Size = 7;
ft.Font.Bold = true;
section.AddParagraph();
cp1.AddLineBreak();
cp1.AddLineBreak();
this.table = section.AddTable();
this.table.Style = "Table";
this.table.Borders.Color = Color.Parse("Black");
this.table.Rows.LeftIndent = 0;
int cols = this.GetColCount();
Column column = this.table.AddColumn("4cm");
column.Format.Alignment = ParagraphAlignment.Left;
column = this.table.AddColumn("0.6cm");
column.Format.Alignment = ParagraphAlignment.Left;
for (int i = 2; i < cols; i++)
{
column = this.table.AddColumn("0.85cm");
column.Format.Alignment = ParagraphAlignment.Left;
}
// Create the header of the table
Row row = this.table.AddRow();
row.HeadingFormat = true;
row.Format.Alignment = ParagraphAlignment.Center;
row.Format.Font.Bold = true;
row.Cells[0].Borders.Top.Visible = false;
row.Cells[0].Borders.Left.Visible = false;
row.Cells[0].Borders.Right.Visible = false;
row.Cells[1].Borders.Top.Visible = false;
row.Cells[1].Borders.Left.Visible = false;
string[] colNames = this.GetColNames();
for (int i = 0; i < cols; i++)
{
row.Cells[i].AddParagraph(colNames[i]);
row.Cells[i].Format.Font.Bold = false;
row.Cells[i].Format.Alignment = ParagraphAlignment.Left;
row.Cells[i].VerticalAlignment = VerticalAlignment.Center;
row.Cells[i].Borders.Width = 1.5;
}
}
我不知道天气 MigraDoc 有一些功能可以直接做这种事情。否则我想知道页面是否会在 3 行之间中断,如果是,我将不得不自己进行分页。
任何伪代码片段都会非常有帮助。