I am having an issue when trying to print a hard copy of a MigraDoc document. I am using PDFsharp-MigraDoc-gdi 1.50.4000-beta3b. None of my footer is ever shown. Now if I render my document to a PDF the footer is always there. I have created a content section. From there I create a header and footer that get added to the document:
Code:
public override void DefineContentSection(object contentSectionData)
{
Section section = _reportDocument.AddSection();
section.PageSetup.OddAndEvenPagesHeaderFooter = true;
section.PageSetup.StartingNumber = 1;
section.PageSetup.LeftMargin = Unit.FromCentimeter(1.5);
section.PageSetup.RightMargin = Unit.FromCentimeter(1.5);
section.PageSetup.TopMargin = Unit.FromCentimeter(3.1);
section.PageSetup.BottomMargin = Unit.FromCentimeter(2);
}
public override void BuildHeaderFooter(object headerFooterData)
{
ResultsHeaderData headerData = headerFooterData as ResultsHeaderData;
#region Header
Table table = _reportDocument.LastSection.Headers.Primary.AddTable();
table.Borders.Visible = false;
table.AddColumn(Unit.FromCentimeter(6)).Format.Alignment = ParagraphAlignment.Left;
table.AddColumn(Unit.FromCentimeter(6)).Format.Alignment = ParagraphAlignment.Center;
table.AddColumn(Unit.FromCentimeter(6)).Format.Alignment = ParagraphAlignment.Right;
Row row = table.AddRow()
row.HeadingFormat = true;
row.VerticalAlignment = VerticalAlignment.Center;
row.Cells[0].AddParagraph(ReportingResources.TestNameFormat(headerData.TestName));
row.Cells[2].AddParagraph(ReportingResources.ReportDateFormat(DateTime.Now.ToString(DATE_TIME_FORMAT)));
row = table.AddRow();
row.HeadingFormat = true;
row.VerticalAlignment = VerticalAlignment.Center;
row.Cells[0].AddParagraph(ReportingResources.PlateIDFormat(headerData.Plate));
row.Cells[0].MergeRight = 1;
row.Cells[2].AddParagraph(ReportingResources.SoftwareVersionFormat(ProductInformation.Instance.ProductNameAndVersionNumber));
//Note: Add a paragraph to create the separator line between the header and rest of document
const double height = (1.0);
Paragraph headerParagraph = new Paragraph();
Border headerBorder = new Border { Style = BorderStyle.Single, Color = Colors.Black };
headerParagraph.Format = new ParagraphFormat
{
Font = new Font("Courier New", new Unit(height)),
Shading = new Shading { Visible = true, Color = Colors.Transparent },
Borders = new Borders
{
Bottom = headerBorder,
}
};
_reportDocument.LastSection.Headers.Primary.Add(headerParagraph);
_reportDocument.LastSection.Headers.EvenPage.Add(table.Clone());
_reportDocument.LastSection.Headers.EvenPage.Add(headerParagraph.Clone());
#endregion
#region Footer
//TODO: Uncomment this if a separator is desired between report and footer
//Paragraph footerParagraph = new Paragraph();
//Border footerBorder = new Border { Style = BorderStyle.Single, Color = Colors.Black };
//footerParagraph.Format = new ParagraphFormat
//{
// Font = new Font("Courier New", new Unit(height)),
// Shading = new Shading { Visible = true, Color = Colors.Transparent },
// Borders = new Borders
// {
// Top = footerBorder,
// }
//};
//_reportDocument.LastSection.Headers.Primary.Add(footerParagraph);
// Create a paragraph with centered page number. See definition of style "Footer".
Table footerTable = _reportDocument.LastSection.Footers.Primary.AddTable();
footerTable.Borders.Visible = false;
footerTable.AddColumn(Unit.FromCentimeter(6)).Format.Alignment = ParagraphAlignment.Left;
footerTable.AddColumn(Unit.FromCentimeter(6)).Format.Alignment = ParagraphAlignment.Center;
footerTable.AddColumn(Unit.FromCentimeter(6)).Format.Alignment = ParagraphAlignment.Right;
Row footerRow = footerTable.AddRow();
footerRow.HeadingFormat = true;
footerRow.Borders.Visible = false;
footerRow.VerticalAlignment = VerticalAlignment.Center;
footerRow.Cells[0].MergeRight = 2;
footerRow.Cells[0].Format.Alignment = ParagraphAlignment.Left;
footerRow.Cells[0].AddParagraph(string.Format("Report Fingerprint: {0}", RandomString(64)));
footerRow = footerTable.AddRow();
footerRow.HeadingFormat = true;
footerRow.Borders.Visible = false;
footerRow.VerticalAlignment = VerticalAlignment.Center;
footerRow[1].Format.Alignment = ParagraphAlignment.Center;
Paragraph paragraph = new Paragraph();
paragraph.AddTab();
paragraph.AddPageField();
paragraph.AddText(ReportingResources.PageOfPage);
paragraph.AddNumPagesField();
footerRow.Cells[1].Add(paragraph.Clone());
_reportDocument.LastSection.Footers.EvenPage.Add(footerTable.Clone());
// Add paragraph to footer for odd pages.
//_reportDocument.LastSection.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.
// _reportDocument.LastSection.Footers.EvenPage.Add(paragraph.Clone());
#endregion
}
To print the document I have the following:
Code:
public MigraDocPrintDocument(Document document)
: this()
{
_renderer = MakeRenderer(document);
}
private static DocumentRenderer MakeRenderer(Document document)
{
DocumentRenderer renderer = new DocumentRenderer(document);
renderer.PrepareDocument();
return renderer;
}
protected override void OnPrintPage(PrintPageEventArgs e)
{
base.OnPrintPage(e);
if (!e.Cancel)
{
PageSettings settings = e.PageSettings;
try
{
Graphics graphics = e.Graphics;
IntPtr hdc = graphics.GetHdc();
int xOffset = GetDeviceCaps(hdc, PHYSICALOFFSETX);
int yOffset = GetDeviceCaps(hdc, PHYSICALOFFSETY);
graphics.ReleaseHdc(hdc);
graphics.TranslateTransform(-xOffset * 100 / graphics.DpiX, -yOffset * 100 / graphics.DpiY);
// Recall: Width and Height are exchanged when settings.Landscape is true.
XSize size = new XSize(e.PageSettings.Bounds.Width / 100.0 * 72, e.PageSettings.Bounds.Height / 100.0 * 72);
const float scale = 100f / 72f;
graphics.ScaleTransform(scale, scale);
// draw line A4 portrait
//graphics.DrawLine(Pens.Red, 0, 0, 21f / 2.54f * 72, 29.7f / 2.54f * 72);
XGraphics gfx = XGraphics.FromGraphics(graphics, size);
_renderer.RenderPage(gfx, _pageNumber);
}
catch
{
e.Cancel = true;
}
_pageNumber++;
_pageCount--;
e.HasMorePages = _pageCount > 0;
}
}
MigraDocPrintDocument printDocument = new MigraDocPrintDocument(GetMigraDocDocument());
printDocument.PrinterSettings = _documentBuilder.PrinterSettings;
printDocument.Print();
From another thread from several years back (
http://stackoverflow.com/questions/20081169/migradocprintdocument-not-printing-footers) I found that setting the Section.PageSetup.FooterDistance to the same amount as my bottom margin will make my footer appear but if I have a table that splits the page my footer will not be shown.
Any ideas why the footer will not print?