PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Sep 24, 2020 3:14 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 5 posts ] 
Author Message
PostPosted: Wed Dec 18, 2019 12:10 pm 
Offline

Joined: Mon Dec 16, 2019 2:46 pm
Posts: 4
Hi,
I would like to create an invoice. I have a page header with a table.
Code:
var ht = section.Headers.Primary.AddTable();

After that I have 4 more tables. One table is inside another.

How do I get a page break made?

Thanks for every hint and tip.

regards Ingo


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 18, 2019 12:22 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3047
Location: Cologne, Germany
How about section.AddPageBreak()?

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 18, 2019 3:14 pm 
Offline

Joined: Mon Dec 16, 2019 2:46 pm
Posts: 4
Hi,
thanks for your help.
In have add:
Code:
 private void CreatePage(string imageFile)
{
    Section section = this.document.AddSection();
    section.AddPageBreak();
    section.PageSetup.PageFormat = PageFormat.A4;
    if (!String.IsNullOrEmpty(imageFile) || File.Exists(imageFile))
    {
        var image = section.Headers.Primary.AddImage(imageFile);
        image.Height = "2.5cm";
        image.LockAspectRatio = true;
        image.RelativeVertical = RelativeVertical.Line;
        image.RelativeHorizontal = RelativeHorizontal.Margin;
        image.Top = ShapePosition.Top;
        image.Left = ShapePosition.Right;
        image.WrapFormat.Style = WrapStyle.Through;
    }
    // Create footer
    Paragraph paragraph = section.Footers.Primary.AddParagraph();
    if (_invoiceHeader.FooterText.StartsWith('#'))
    {
        var textArray = _invoiceHeader.FooterText.Substring(1, _invoiceHeader.FooterText.Length - 1).Replace("\t", String.Empty).Replace(' ', ' ').Split('\n');
        for (int i = 0; i < textArray.Length; i++)
        {
            if (i == 0)
            {
                paragraph.AddFormattedText(textArray[i] + Environment.NewLine, TextFormat.Bold);
                continue;
            }
            paragraph.AddText(textArray[i] + Environment.NewLine);
        }
             
               
    }
    else
    {
        paragraph.AddText(_invoiceHeader.FooterText);
    }
    // Create the header
    paragraph = section.AddParagraph();
    paragraph.Format.SpaceBefore = "2cm";
    paragraph.Style = "Normal";
    paragraph.Format.Font.Size = 18;
    paragraph.Format.Font.Bold = true;
    paragraph.Format.Alignment = ParagraphAlignment.Right;
    if (_invoiceHeader.Credit)
    {
        paragraph.AddFormattedText(_localizer["InvoiceCredit"].Value, TextFormat.Bold);
    }
    else
    {
        paragraph.AddFormattedText(_localizer["Invoice"].Value, TextFormat.Bold);

    }

    var  ht = section.AddTable();
    ht.Style = "Table";
    ht.Rows.LeftIndent = 0;
    Column column = ht.AddColumn("10.0cm");
    column.Format.Alignment = ParagraphAlignment.Left;
    column = ht.AddColumn("4.0cm");
    column.Format.Alignment = ParagraphAlignment.Left;
    column = ht.AddColumn("2.5cm");
    column.Format.Alignment = ParagraphAlignment.Right;
    addressRow = ht.AddRow();
    addressRow.HeadingFormat = true;
    paragraph = addressRow.Cells[0].AddParagraph(String.Format("{0}· {1} · {2} {3}", _invoiceCompany.Name, _invoiceCompany.Street, _invoiceCompany.ZipCode, _invoiceCompany.Location));
    paragraph.Format.Font.Name = "Times New Roman";
    paragraph.Format.Font.Size = 7;
    paragraph.Format.SpaceAfter = 3;
    paragraph.AddLineBreak();

    paragraph = addressRow.Cells[1].AddParagraph();
    paragraph.Style = "Normal";
    paragraph.AddLineBreak();
    paragraph.AddText(_localizer["Invoice number"].Value);
    paragraph.AddLineBreak();
    paragraph.AddText(_localizer["Invoice date"].Value);
    paragraph.AddLineBreak();
    paragraph.AddText(_localizer["Customer number"].Value);
    paragraph = addressRow.Cells[2].AddParagraph();
    paragraph.Style = "Normal";
    paragraph.Format.Alignment = ParagraphAlignment.Right;
    paragraph.AddLineBreak();
    paragraph.AddText(_invoiceHeader.InvoiceNumber.ToString("N00"));
    paragraph.AddLineBreak();
    paragraph.AddText(_invoiceHeader.InvoiceDate.ToShortDateString());
    paragraph.AddLineBreak();
    paragraph.AddText(_invoiceHeader.CustomerNumber.ToString("N00"));

    // create Table for items.
    paragraph = section.AddParagraph();
    paragraph.Format.SpaceBefore = "2cm";
    paragraph.AddText(" ");
    table = section.AddTable();
    table.Style = "Table";
    table.Borders.Color = new Color(0, 0, 0);
    table.Borders.Width = 0.25;
    table.Rows.LeftIndent = 0;
    column = table.AddColumn("9.5cm");
    column.Format.Alignment = ParagraphAlignment.Left;
    column = table.AddColumn("2.0cm");
    column.Format.Alignment = ParagraphAlignment.Right;
    column = table.AddColumn("2.5cm");
    column.Format.Alignment = ParagraphAlignment.Right;
    column = table.AddColumn("2.5cm");
    column.Format.Alignment = ParagraphAlignment.Right;
    Row row = table.AddRow();
    row.HeadingFormat = true;
    row.Format.Font.Bold = true;
    row.Cells[0].AddParagraph(_localizer["Article"].Value);
    row.Cells[0].Borders.Top.Visible = false;
    row.Cells[0].Borders.Left.Visible = false;
    row.Cells[0].Borders.Right.Visible = false;
    row.Cells[1].AddParagraph(_localizer["Number"].Value);
    row.Cells[1].Borders.Top.Visible = false;
    row.Cells[1].Borders.Left.Visible = false;
    row.Cells[1].Borders.Right.Visible = false;
    row.Cells[2].AddParagraph(_localizer["UnitPrice"].Value);
    row.Cells[2].Borders.Top.Visible = false;
    row.Cells[2].Borders.Left.Visible = false;
    row.Cells[2].Borders.Right.Visible = false;
    row.Cells[3].AddParagraph(_localizer["Total"].Value);
    row.Cells[3].Borders.Top.Visible = false;
    row.Cells[3].Borders.Left.Visible = false;
    row.Cells[3].Borders.Right.Visible = false;

    // table for VAT
    if (_invoiceHeader.VatRates.Count > 0)
    {
        vatTable = section.AddTable();
        vatTable.Style = "Table";
        vatTable.Borders.Visible = false;
        column = vatTable.AddColumn("7.5cm");
        column = vatTable.AddColumn("2.0cm");
        column.Format.Alignment = ParagraphAlignment.Right;
        column = vatTable.AddColumn("2.0cm");
        column.Format.Alignment = ParagraphAlignment.Right;
        column = vatTable.AddColumn("2.5cm");
        column.Format.Alignment = ParagraphAlignment.Right;
        column = vatTable.AddColumn("2.5cm");
        column.Format.Alignment = ParagraphAlignment.Right;
        Row vatRow = vatTable.AddRow();
        vatRow.HeadingFormat = true;
        vatRow.Format.Font.Bold = true;
        vatRow.Cells[1].AddParagraph("Netto");
        vatRow.Cells[2].AddParagraph("% USt.");
        vatRow.Cells[3].AddParagraph("USt.");
        vatRow.Cells[4].AddParagraph("Brutto");
    }
    sumTable = section.AddTable();
    sumTable.Style = "Table";
    sumTable.Borders.Color = new Color(0, 0, 0);
    sumTable.Borders.Width = 0.25;
    sumTable.Rows.LeftIndent = 0;
    column = sumTable.AddColumn("9.5cm");
    column.Format.Alignment = ParagraphAlignment.Left;
    column = sumTable.AddColumn("2.0cm");
    column.Format.Alignment = ParagraphAlignment.Right;
    column = sumTable.AddColumn("2.5cm");
    column.Format.Alignment = ParagraphAlignment.Right;
    column = sumTable.AddColumn("2.5cm");
    column.Format.Alignment = ParagraphAlignment.Right;

    if (!String.IsNullOrEmpty(_invoiceHeader.InvoiceText))
    {
        paragraph = section.AddParagraph();
        paragraph.AddLineBreak();
        paragraph.AddText(_invoiceHeader.InvoiceText);
    }
}   


Please have a look to the images.
What do I have to do with it on the second page? Either the image is not taken over or the same header is printed as on the first page. In addition, the text is written in the footers on the first page. What am I doing wrong??


Attachments:
image2.png
image2.png [ 190.14 KiB | Viewed 1707 times ]
image1.png
image1.png [ 87.03 KiB | Viewed 1707 times ]
Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 18, 2019 4:22 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3047
Location: Cologne, Germany
The Primary header will be used on every page by default.
You can set a FirstPage header to override the Primary header on the first page.
You can set an EvenPage header to override the Primary header on even pages.

If you only set the Primary header you will have it on every page.

The PageSetup must reserve the space needed for headers and footers or they may overlap with the floating text from the main body.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Wed Dec 18, 2019 5:51 pm 
Offline

Joined: Tue Aug 06, 2019 10:45 am
Posts: 41
Thomas Hoevel wrote:
The PageSetup must reserve the space needed for headers and footers or they may overlap with the floating text from the main body.


To give further help on this:
- You have to set the header or foother distance, that, from what I could tell, was the distance from the top or bottom edge of the of the page.
- Then you have to set the margins, the margin should take into account both the content height of the header or footer, and the header or footer distance.

I have saved a template that supposedly gives a rough estimate of the heigth of the header or footer, by their elements.

This method doesn't work always and the code here in just a method that worked for me, just adding it here in case you need/want it.

Code:
        public static int GetHeaderFooterHeight(DocumentElements headerElements)
        {
            double height = 0;
            var docAux = new Document();
            var elements = headerElements.Clone();

            if (elements.Count == 0)
                return 0;

            docAux.LastSection.Elements = headerElements.Clone();

            DocumentRenderer _renderer = new DocumentRenderer(docAux);
            _renderer.PrepareDocument();

            var docObjects = _renderer.GetDocumentObjectsFromPage(1);
            var info = _renderer.GetRenderInfoFromPage(1);

            for (int i = 0; i < docObjects.Length; ++i)
            {
                if (docObjects[i] is Table)
                {
                    var tableInfo = (MigraDoc.Rendering.TableRenderInfo)info[i];
                    height += tableInfo.LayoutInfo.ContentArea.Height;
                }
                else if (docObjects[i] is Paragraph)
                {
                    var paragraphInfo = (MigraDoc.Rendering.ParagraphRenderInfo)info[i];
                    height += paragraphInfo.LayoutInfo.ContentArea.Height;
                }
            }

            return Double.IsNaN(height) ? 0 : Convert.ToInt32(height);
        }


Hope it helps


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 18 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