PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Thu Mar 28, 2024 3:08 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Wed Nov 28, 2012 10:04 am 
Offline

Joined: Wed Nov 28, 2012 9:23 am
Posts: 1
Hi
I am generating a PDF document from a object model. The object model is split into sections with groups of controls inside each section. The group of controls contain different types of controls e.g texts, radiobuttons (lists), bullet points. Each control has positioning values relative to the container (the group). I have been able to create sections using TextFrames and then and then textFrames for the text control and images for the radiobuttons. Using the following code. I have also done the same using Table for each section in the code further below. however the problem I have with both is positioning of the controls. The sections render fine but the controls , in particular the radiobuttons and their respective labels do not position relative to the group container.
I think I noticed a bug where by the relativehorizontal does not seem to work when within a cell. RelativeVertical works fine.


Code using TextFrames
Code:
       void CreatePage(WebFormStep model)
        {
            // Each MigraDoc document needs at least one section.
            Section section = this.document.AddSection();
            createPDFHeader(section, model);     
            if (model != null)
            {
                if (model.Type != WebFormStepType.Summary)
                {
                    return;
                }
                else
                {
                    var sections = new List<WebFormSection>(model.Content.Sections);

                    foreach (var modelSection in sections)
                    {
                        TextFrame pdfSectionHeader = section.AddTextFrame();
                        Paragraph pdfTitle = section.AddParagraph(modelSection.Title);
                        pdfTitle.Format.Borders.Visible = true;
                        pdfTitle.Style = "Heading1";
                        var modelGroups = modelSection.Groups;
                       
                        BuildPDFGroups(modelGroups, section);
                    }
                }
            }
           
         }

        private void BuildPDFGroups(IEnumerable<WebFormGroup> modelGroups, Section section)
        {
            foreach (var modelGroup in modelGroups)
            {
                switch (modelGroup.Type)
                {
                    case WebFormGroupType.Standard:
                        {
                            if (modelGroup.IsVisible)
                            {
                                TextFrame pdfGroupFrame = section.AddTextFrame();
                                if (modelGroup.Controls != null)
                                {
                                    var modelControls = modelGroup.Controls;
                                    BuildPDFControls(pdfGroupFrame, modelControls, section);
                                }

                                if (modelGroup.Dimension != null)
                                {
                                    if (modelGroup.Dimension.Height.HasValue)
                                    {
                                        pdfGroupFrame.Height = modelGroup.Dimension.Height.Value.ToCM();
                                    }
                                    if (modelGroup.Dimension.Width.HasValue)
                                    {
                                        pdfGroupFrame.Width = modelGroup.Dimension.Width.Value.ToCM();
                                    }
                                }
                                if (modelGroup.Position != null)
                                {
                                    if (modelGroup.Position.X.HasValue)
                                    {
                                        pdfGroupFrame.Left = modelGroup.Position.X.Value.ToCM();
                                    }
                                    if (modelGroup.Position.Y.HasValue)
                                    {
                                        pdfGroupFrame.Top = modelGroup.Position.Y.Value.ToCM();
                                    }
                                }

                                if (modelGroup.Freetexts != null)
                                {
                                    var FreeTexts = modelGroup.Freetexts;

                                    foreach (WebFormFreetext freetext in FreeTexts)
                                    {
                                        if (freetext.Type != WebFormFreetextType.Help)
                                        {
                                            TextFrame pdfTextFrame = section.AddTextFrame();
                                            pdfTextFrame.Width = "17.00cm";
                                            Paragraph pdftext = pdfTextFrame.AddParagraph(freetext.Text.ToString());
                                            pdftext.Format.SpaceAfter = "0.00cm";
                                            pdftext.Format.SpaceBefore = "0.00cm";
                                            pdfTextFrame.Height = modelGroup.Dimension.Height.Value.ToCM();
                                            pdfTextFrame.MarginTop = "0.00cm";
                                            pdfTextFrame.MarginBottom = "0.00cm";
                                            pdfTextFrame.FillFormat.Color = Highlighter;
                                            pdfTextFrame.LineFormat.Color = HeadingBackgroudColor;
                                            pdfTextFrame.LineFormat.DashStyle = DashStyle.Solid;
                                           
                                        }
                                    }
                                }
                            }
                            break;
                        }

                    case WebFormGroupType.RepeatableContainer:
                        {
                            if (modelGroup.IsVisible)
                            {
                                if (modelGroup != null)
                                {
                                    TextFrame pdfGroupFrame = section.AddTextFrame();
                                   
                                    if (modelGroup.Controls != null)
                                    {
                                        var modelControls = modelGroup.Controls;
                                        BuildPDFControls(pdfGroupFrame, modelControls, section);
                                    }
                                    if (modelGroup.Groups != null)
                                    {
                                         //recursion
                                         BuildPDFGroups(modelGroup.Groups, section);
                                     }

                                    if (modelGroup.Dimension != null)
                                    {
                                        if (modelGroup.Dimension.Height.HasValue)
                                        {
                                            pdfGroupFrame.Height = modelGroup.Dimension.Height.Value.ToCM();
                                        }
                                        if (modelGroup.Dimension.Width.HasValue)
                                        {
                                            pdfGroupFrame.Width = modelGroup.Dimension.Width.Value.ToCM();
                                        }
                                    }
                                    if (modelGroup.Position != null)
                                    {
                                        if (modelGroup.Position.X.HasValue)
                                        {
                                            pdfGroupFrame.Left = modelGroup.Position.X.Value.ToCM();
                                        }
                                        if (modelGroup.Position.Y.HasValue)
                                        {
                                            pdfGroupFrame.Top = modelGroup.Position.Y.Value.ToCM();
                                        }
                                    }

                                }
                            }
                            break;
                        }

                    case WebFormGroupType.RepeatableInstance:
                        {
                            if (modelGroup.IsVisible)
                            {
                                if (modelGroup != null)
                                {
                                    TextFrame pdfGroupFrame = section.AddTextFrame();
                                    if (modelGroup.Controls != null)
                                    {
                                        BuildPDFControls(pdfGroupFrame, modelGroup.Controls, section);
                                    }

                                            if (modelGroup.Groups != null)
                                            {
                                                //recursion
                                                BuildPDFGroups(modelGroup.Groups, section);
                                            }
                                  }
                                   
                            }
                            break;
                        }   
                }
            }
        }

        private void BuildPDFControls(TextFrame pdfGroupFrame, IEnumerable<WebFormControl> modelControls, Section section)
        {
            if (modelControls != null)
            {
                foreach (var control in modelControls)
                {
                    //render control text
                    if (control.IsVisible)
                    {
                        switch (control.Type)
                            {
                            case WebFormControlType.RadioButton:
                                    {
                                        if (control.Position != null)
                                        {
                                            if (control.Position.X.HasValue)
                                            {
                                                pdfGroupFrame.Left = control.Position.X.Value.ToCM();
                                               
                                            }
                                            if (control.Position.Y.HasValue)
                                            {
                                                pdfGroupFrame.Top = control.Position.Y.Value.ToCM();
                                            }
                                        }
                                        if (control.Dimension != null)
                                        {
                                            if (control.Dimension.Height.HasValue)
                                            {
                                                pdfGroupFrame.Height = control.Dimension.Height.Value.ToCM();
                                            }
                                            if (control.Dimension.Width.HasValue)
                                            {
                                                pdfGroupFrame.Width = control.Dimension.Width.Value.ToCM();
                                            }
                                        }
                                        if (control.Label.Position != null)
                                        {
                                            pdfGroupFrame.AddParagraph(control.Label.Text.ToString());
                                        }
                                       if (control.Value != null)
                                        {
                                            if (control.Value == control.Data)
                                            {
                                                Image pdfRadioButtonImg = pdfGroupFrame.AddImage(@"C:\Development\Projects\B2CCaseTracker\B2CCaseTracker.UI.Web\Content\img\wf-controls\check-box-checked.png");
                                                pdfRadioButtonImg.Left = control.Position.X.Value.ToCM();
                                                pdfRadioButtonImg.Top = control.Position.Y.Value.ToCM();
                                            }
                                            else
                                            {
                                                Image pdfRadioButtonImg = pdfGroupFrame.AddImage(@"C:\Development\Projects\B2CCaseTracker\B2CCaseTracker.UI.Web\Content\img\wf-controls\check-box-unchecked.png");
                                                pdfRadioButtonImg.Left = control.Position.X.Value.ToCM();
                                                pdfRadioButtonImg.Top = control.Position.Y.Value.ToCM();
                                            }
                                       }

                                        break;
                                    }

                            case WebFormControlType.RadioButtonList:
                                    {
                                        BuildPDFControls(pdfGroupFrame, control.Controls, section);
                                        break;
                                    }
                            case WebFormControlType.Text:
                                    {
                                       
                                        if (control.Data != null)
                                        {
                                            pdfGroupFrame.AddParagraph(control.Data.ToString());
                                        }
                                        if (control.Position != null)
                                        {
                                            if (control.Position.X.HasValue)
                                            {
                                                pdfGroupFrame.Left = control.Position.X.Value.ToCM();
                                            }
                                            if (control.Position.Y.HasValue)
                                            {
                                                pdfGroupFrame.Top = control.Position.Y.Value.ToCM();
                                            }
                                        }
                                        break;
                                    }
                            case WebFormControlType.TextBox:
                                    {
                                        if (control.Dimension != null)
                                        {
                                            if (control.Dimension.Height.HasValue)
                                            {
                                                pdfGroupFrame.Height = control.Dimension.Height.Value.ToCM();
                                            }
                                            if (control.Dimension.Width.HasValue)
                                            {
                                                pdfGroupFrame.Width = control.Dimension.Width.Value.ToCM();
                                            }
                                        }
                                            if (control.Label != null)
                                            {
                                                TextFrame pdfLabel = section.AddTextFrame();
                                                pdfLabel.AddParagraph(control.Label.Text.ToString());
                                                pdfLabel.RelativeHorizontal = pdfGroupFrame.RelativeHorizontal;
                                            }
                                       
                                           
                                            if (control.Data != null)
                                            {
                                                TextFrame pdfData = section.AddTextFrame();
                                                pdfData.AddParagraph(control.Data.ToString());
                                                pdfData.RelativeHorizontal = pdfGroupFrame.RelativeHorizontal;
                                            }

                                       
                                        break;
                                    }
                           
                        }
                        }

                    if (control.Controls != null)
                    {
                        //recursion
                        BuildPDFControls(pdfGroupFrame, control.Controls, section);
                    }

                }
            }
        }






Code using Tables

Code:
#region MigraDoc - Creating Documents on the Fly
//
// Authors:
//   PDFsharp Team (mailto:PDFsharpSupport@pdfsharp.de)
//
// Copyright (c) 2001-2009 empira Software GmbH, Cologne (Germany)
//
// http://www.pdfsharp.com
// http://www.migradoc.com
// http://sourceforge.net/projects/pdfsharp
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Globalization;
using System.Collections.Generic;
using System.Xml;
using System.Xml.XPath;
using MigraDoc.DocumentObjectModel;
using MigraDoc.DocumentObjectModel.Tables;
using MigraDoc.DocumentObjectModel.Shapes;
using MigraDoc.Rendering;
using B2CCaseTracker.Model;
using B2CCaseTracker.Model.Entities;
using System.Diagnostics;
using System.Collections.ObjectModel;
using B2CCaseTracker.Model.Enums;
using B2CCaseTracker.Model.Repositories;
using MakePDF;

namespace Invoice
{
    /// <summary>
    /// Creates the invoice form.
    /// </summary>
    public class InvoiceForm
    {
        // Some pre-defined colors
#if true
        // RGB colors
        readonly static Color TableBorder = new Color(81, 125, 192);
        readonly static Color TableBlue = new Color(235, 240, 249);
        readonly static Color TableGray = new Color(242, 242, 242);
        readonly static Color Red = new Color(255, 0, 0);
        readonly static Color White = new Color(255, 255, 255);
        readonly static Color Yellow = new Color(255, 255, 0);
        readonly static Color Black = new Color(0, 0, 0);
#else
    // CMYK colors
    readonly static Color tableBorder = Color.FromCmyk(100, 50, 0, 30);
    readonly static Color tableBlue = Color.FromCmyk(0, 80, 50, 30);
    readonly static Color tableGray = Color.FromCmyk(30, 0, 0, 0, 100);
#endif
        //readonly static String PageWidthInCm = "20.00cm";
        /// <summary>
        /// The MigraDoc document that represents the invoice.
        /// </summary>
        MigraDoc.DocumentObjectModel.Document document;

        /// <summary>
        /// An XML invoice based on a sample created with Microsoft InfoPath.
        /// </summary>
        readonly XmlDocument invoice;

        /// <summary>
        /// The root navigator for the XML document.
        /// </summary>
        readonly XPathNavigator navigator;

        /// <summary>
        /// Initializes a new instance of the class BillFrom and opens the specified XML document.
        /// </summary>
        public InvoiceForm()
        {
            this.invoice = new XmlDocument();
            this.invoice.Load(@"c:\tests\invoice.xml");
            this.navigator = this.invoice.CreateNavigator();
        }

        /// <summary>
        /// Creates the invoice document.
        /// </summary>
        public MigraDoc.DocumentObjectModel.Document CreateDocument(WebFormStep model)
        {
            // Create a new MigraDoc document
            this.document = new MigraDoc.DocumentObjectModel.Document();
            this.document.Info.Title = "A sample B2C document";
            this.document.Info.Subject = "B2C";
            this.document.Info.Author = "Enact";

            DefineStyles();
                       
            CreatePage(model);

            return this.document;
        }

        /// <summary>
        /// Defines the styles used to format the MigraDoc document.
        /// </summary>
        void DefineStyles()
        {
            // Get the predefined style Normal.
            Style style = this.document.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 = "Arial";
            style.Font.Size= 10;
            style.ParagraphFormat.Shading.Color = TableGray;

            Style TableStyle = this.document.AddStyle("TableStyle","Normal");
            TableStyle.Font.Name = "Arial";
            TableStyle.Font.Size = 10;
            TableStyle.Font.Bold = true;
            TableStyle.Font.Color = White;
            TableStyle.ParagraphFormat.Shading.Color = Red;
            TableStyle.ParagraphFormat.Borders.Color = Black;

            Style TableContentStyle = this.document.AddStyle("TableContentStyle", "TableStyle");
            TableStyle.Font.Bold = false;
            TableStyle.Font.Color = Black;
            TableStyle.ParagraphFormat.Shading.Color = White;
            TableStyle.ParagraphFormat.Borders.Color = Black;
        }

        /// <summary>
        /// Creates the static parts of the invoice.
        /// </summary>
        void CreatePage(WebFormStep model)
        {
            // Each MigraDoc document needs at least one section.
            Section section = this.document.AddSection();
            createPDFHeader(section, model);     
            if (model != null)
            {
                if (model.Type != WebFormStepType.Summary)
                {
                    return;
                }
                else
                {
                    var sections = new List<WebFormSection>(model.Content.Sections);

                    foreach (var modelSection in sections)
                    {

                        Table SummaryHeaders = section.AddTable();
                        SummaryHeaders.Style = "TableStyle";
                        //SummaryHeaders.AddColumn(PageWidthInCm);
                        SummaryHeaders.AddColumn("7.00cm");
                        SummaryHeaders.AddColumn("3.50cm");
                        SummaryHeaders.AddColumn("1.50cm");
                        SummaryHeaders.AddColumn("3.50cm");
                        SummaryHeaders.AddColumn("1.50cm");
                        Row SummaryRow = SummaryHeaders.AddRow();
                        SummaryRow[0].MergeRight = 3;
                        SummaryRow[0].AddParagraph(modelSection.Title);
                        SummaryRow[0].Format.Shading.Color = Red;
                        SummaryRow[0].Format.Font.Color = White;
                       
                        var modelGroups = modelSection.Groups;
                       
                        BuildPDFGroups(SummaryHeaders, modelGroups, section);
                    }
                }
            }
           
         }

        private void BuildPDFGroups(Table SectionTable, IEnumerable<WebFormGroup> modelGroups, Section section)
        {
            foreach (var modelGroup in modelGroups)
            {
                switch (modelGroup.Type)
                {
                    case WebFormGroupType.Standard:
                        {
                            if (modelGroup.IsVisible)
                            {
                                if (modelGroup.Controls != null)
                                {
                                    BuildPDFControls(SectionTable, modelGroup.Controls, section);
                                }

                                if (modelGroup.Freetexts != null)
                                {
                                    var FreeTexts = modelGroup.Freetexts;

                                    foreach (WebFormFreetext freetext in FreeTexts)
                                    {
                                        if (freetext.Type != WebFormFreetextType.Help)
                                        {

                                            SectionTable.Style = "TableContentStyle";
                                           
                                            SectionTable.LeftPadding = "0.00";
                                            SectionTable.RightPadding = "0.00";
                                            SectionTable.Borders.BordersCleared = true;
                                                                                       
                                            Row SectionHeaderRow = SectionTable.AddRow();
                                            SectionHeaderRow[0].MergeRight = 3;
                                            SectionHeaderRow.Cells[0].AddParagraph(freetext.Text);
                                           
                                        }
                                    }
                                }
                            }
                            break;
                        }

                    case WebFormGroupType.RepeatableContainer:
                        {
                            if (modelGroup.IsVisible)
                            {
                                if (modelGroup != null)
                                {

                                    if (modelGroup.Controls != null)
                                    {
                                        var modelControls = modelGroup.Controls;
                                        BuildPDFControls(SectionTable, modelControls, section);
                                    }
                                   
                                    if (modelGroup.Groups != null)
                                    {
                                         //recursion
                                         BuildPDFGroups(SectionTable, modelGroup.Groups, section);
                                     }
                                   
                                }
                            }
                            break;
                        }

                    case WebFormGroupType.RepeatableInstance:
                        {
                            if (modelGroup.IsVisible)
                            {
                                if (modelGroup != null)
                                {
                                    //Table SectionTable = section.AddTable();

                                    if (modelGroup.Controls != null)
                                    {
                                        BuildPDFControls(SectionTable, modelGroup.Controls, section);
                                    }

                                            if (modelGroup.Groups != null)
                                            {
                                                //recursion
                                                BuildPDFGroups(SectionTable, modelGroup.Groups, section);
                                            }
                                  }
                                   
                            }
                            break;
                        }   
                }
            }
        }


        private void BuildPDFControls(Table SectionTable, IEnumerable<WebFormControl> modelControls, Section section)
        {
            if (modelControls != null)
            {
                foreach (var control in modelControls)
                {
                    //render control text
                    if (control.IsVisible)
                    {
                        switch (control.Type)
                            {
                            case WebFormControlType.RadioButton:
                                    {
                                        Row ControlRow = SectionTable.AddRow();
                                       
                                       if (control.Value != null)
                                        {

                                            if (control.Value == control.Data)
                                            {
                                                if (control.Label.Text != null)
                                                    {
                                                        ControlRow[1].AddParagraph(control.Label.Text.ToString());
                                                    }
                                                ControlRow[2].AddImage(@"C:\Development\Projects\B2CCaseTracker\B2CCaseTracker.UI.Web\Content\img\wf-controls\check-box-checked.png");
                                            }   
                                            else
                                            {
                                                if (control.Label.Text != null)
                                                    {
                                                        ControlRow[1].AddParagraph(control.Label.Text.ToString());
                                                    }
                                                ControlRow[2].AddImage(@"C:\Development\Projects\B2CCaseTracker\B2CCaseTracker.UI.Web\Content\img\wf-controls\check-box-unchecked.png");                                            }
                                        }
                                        break;
                                    }

                            case WebFormControlType.RadioButtonList:
                                    {
                                        BuildPDFControls(SectionTable, control.Controls, section);
                                        break;
                                    }
                            case WebFormControlType.Text:
                                    {
                                        Row ControlRow = SectionTable.AddRow();
                                        if (control.Data != null)
                                        {
                                            ControlRow[0].AddParagraph(control.Data.ToString());
                                        }
                                        break;
                                    }
                            case WebFormControlType.TextBox:
                                    {
                                        Row ControlRow = SectionTable.AddRow();
                                            if (control.Label != null)
                                            {
                                                ControlRow[0].AddParagraph(control.Label.Text.ToString());
                                             }
                                       
                                           
                                            if (control.Data != null)
                                            {
                                                ControlRow[0].AddParagraph(control.Data.ToString());
                                            }

                                       
                                        break;
                                    }
                           
                        }
                        }

                    if (control.Controls != null)
                    {
                        //recursion
                        BuildPDFControls(SectionTable, control.Controls, section);
                    }

                }
            }
        }
       



        void createPDFHeader(Section section, WebFormStep model)
        {
            TextFrame pdfSectionHeader = section.AddTextFrame();
            //pdfSectionHeader.AddParagraph(model.Name.ToString());
            pdfSectionHeader.AddParagraph(model.Title.ToString());
        }   

    }
}


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 138 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