PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Aug 21, 2024 12:20 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: Footers Not Printing
PostPosted: Fri Feb 12, 2016 4:52 pm 
Offline

Joined: Fri Feb 12, 2016 4:26 pm
Posts: 1
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: Footers Not Printing
PostPosted: Mon Feb 15, 2016 8:42 am 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3109
Location: Cologne, Germany
Hi!

Everything you see in the PDF file will also be sent to the printer.

There are two things you have to pay attention to: The page size of the PDF file must be the same as the page size you use in the printer.
When printing the PDF file, make sure you use the 100% setting (original size, no automatic scaling).

There are different footers: the standard footer can be overridden by an even-page footer and a first-page footer.
If you only set the first page footer, you will only get a footer on the first page.
If you leave the even page footer empty, you may end up with footers only on odd pages.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 37 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group