Hi, I think this is a rather easy problem to solve but I have yet not managed to do so.
I'm rather new to PDFsharp, so I use the code provided in your Wiki and modify it to meet my requirements.
I want the PDF to have on the first page a big header (i use a table for that). On all other pages the header will be different (smaller table).
However I did not manage to get the 'bodytext' to beign where I want. I tried to use margins but they only will be good for the first page. For the second page the margin will be too big and therefore space will be wasted on that page.
The worst case is setting the margin to fit for all other pages but the table on the first page will overlap with the 'bodytext'.
I made a C# WindowsForms application with just one form and one button. When the button is clicked, you guess it a pdf will be generated.
Here is my code so far:
Code:
private void cmdCreatePDF_click(object sender, EventArgs e)
{
Document document = CreateDocument();
MigraDoc.DocumentObjectModel.IO.DdlWriter.WriteToFile(document,"MigraDoc,mddl");
MigraDoc.Rendering.PdfDocumentRenderer renderer = new Migra.Redering.PdfRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
renderer.Document = document;
renderer.RenderDocument();
string filename = @"C:\PdfSharp\mypdf.pdf";
renderer.PdfDocument.Save(filename);
}
private Document CreateDocument()
{
Document Doc = new Document()
Doc.Info.title = "TEST"
DefineStyles(Doc);
DefineContentSection(Doc);
return(Doc);
}
private void DefineStyles(Document Doc)
{
// Get the predefined style Normal.
Style style = Doc.Styles["Normal"];
// Because all styles are derived from Normal, the next line changes the
// font of the whole document. Or, more exactly, it changes the font of
// all styles and paragraphs that do not redefine the font.
style.Font.Name = "Times New Roman";
// Heading1 to Heading9 are predefined styles with an outline level. An outline level
// other than OutlineLevel.BodyText automatically creates the outline (or bookmarks)
// in PDF.
style = Doc.Styles["Heading1"];
style.Font.Name = "Tahoma";
style.Font.Size = 14;
style.Font.Bold = true;
style.Font.Color = Colors.DarkBlue;
style.ParagraphFormat.PageBreakBefore = true;
style.ParagraphFormat.SpaceAfter = 6;
style = Doc.Styles["Heading2"];
style.Font.Size = 12;
style.Font.Bold = true;
style.ParagraphFormat.PageBreakBefore = false;
style.ParagraphFormat.SpaceBefore = 6;
style.ParagraphFormat.SpaceAfter = 6;
style = Doc.Styles["Heading3"];
style.Font.Size = 10;
style.Font.Bold = true;
style.Font.Italic = true;
style.ParagraphFormat.SpaceBefore = 6;
style.ParagraphFormat.SpaceAfter = 3;
style = Doc.Styles[StyleNames.Header];
style.ParagraphFormat.AddTabStop("16cm", MigraDoc.DocumentObjectModel.TabAlignment.Right);
style = Doc.Styles[StyleNames.Footer];
style.ParagraphFormat.AddTabStop("8cm", MigraDoc.DocumentObjectModel.TabAlignment.Center);
// Create a new style called TextBox based on style Normal
style = Doc.Styles.AddStyle("TextBox", "Normal");
style.ParagraphFormat.Alignment = ParagraphAlignment.Justify;
style.ParagraphFormat.Borders.Width = 2.5;
style.ParagraphFormat.Borders.Distance = "3pt";
style.ParagraphFormat.Shading.Color = Colors.SkyBlue;
// Create a new style called TOC based on style Normal
style = Doc.Styles.AddStyle("TOC", "Normal");
style.ParagraphFormat.AddTabStop("16cm", MigraDoc.DocumentObjectModel.TabAlignment.Right, TabLeader.Dots);
style.ParagraphFormat.Font.Color = Colors.Blue;
}
static void DefineContentSection(Document Doc)
{
Section section = Doc.AddSection();
section.pageSetup = Doc.DefaultPageSetup.Clone();
section.PageFormat = PageFormat.A4;
var width = section.Pagesetup.PageWidth;
section.PageSetup.LeftMargin = "2.5cm"
section.PageSetup.RightMargin = "2.5cm"
section.PageSetup.TopMargin = "8.0cm"
section.PageSetup.ButtomMargin = "5.0cm"
section.PageSetup.DifferentFirstpageHeaderFooter = true;
section.PageSetup.OddAndEvenPageHeaderFooter = true;
section.PageSetup.StartingNumber = 1;
Table table = new Table();
float sectionWidth = width - section.PageSetup.LeftMargin - section.PageSetup.RightMargin;
float colWidth = sectionWidth / 4;
table.Borders.Width = 0.75;
Column col = tabke.AddColumn();
col.Width = colWidth;
Column col = tabke.AddColumn();
col.Width = colWidth;
Column col = tabke.AddColumn();
col.Width = colWidth;
Column col = tabke.AddColumn();
col.Width = colWidth;
//Now filling with content
Row row = table.AddRow();
Cell cell = row.Cells[0];
cell.addParagraph("A cell 1.1");
Cell cell = row.Cells[1];
cell.addParagraph("A cell 1.2");
row = table.AddRow();
Cell cell = row.Cells[0];
cell.addParagraph("A cell 2.1");
Cell cell = row.Cells[1];
cell.addParagraph("A cell 2.2");
Cell cell = row.Cells[2];
cell.addParagraph("A cell 2.3");
Cell cell = row.Cells[3];
cell.addParagraph("A cell 2.4");
row = table.AddRow();
Cell cell = row.Cells[0];
cell.addParagraph("A cell 3.1");
Cell cell = row.Cells[1];
cell.addParagraph("A cell 3.2");
Cell cell = row.Cells[2];
cell.addParagraph("A cell 3.3");
Cell cell = row.Cells[3];
cell.addParagraph("A cell 3.4");
//I'm doing this eight times
.
.
.
//When the table is complete
section.Headers.FirstPage.Add(table);
//Now adding 100 lines of BodyText
for(int i = 0; i<99;i++)
{
section.AddParagraph("This is a test line.")
}
Paragraph footerpar = new Paragraph();
footerpar.AddTab();
footerpar.AddPageField();
section.Footers.FirstPage.Add(footerpar);
HeaderFooter header = section.Headers.Primary;
header = section.Headers.Primary;
header.AddParagraph("\tOdd Page Header");
header = section.Headers.EvenPage;
header.AddParagraph("Even Page Header");
// Create a paragraph with centered page number. See definition of style "Footer".
Paragraph paragraph = new Paragraph();
paragraph.AddTab();
paragraph.AddPageField();
// Add paragraph to footer for odd pages.
section.Footers.Primary.Add(paragraph);
// Add clone of paragraph to footer for odd pages. Cloning is necessary because an object must
// not belong to more than one other object. If you forget cloning an exception is thrown.
section.Footers.EvenPage.Add(paragraph.Clone());
}
I have not come to the point to add the smaller table for the other pages so the Even and Odd headers are used as a placeholder.
Can anyone give me a hint how I can solve the problem?
Thank you very much!