PDFsharp & MigraDoc Foundation

Page 1 of 1

Author:  James Coyle [ Sun Apr 15, 2018 5:07 pm ]
Post subject:  MigraDocXML

Hi everyone, first time here on the forums, though I've been using MigraDoc for probably around a year now as a part of my job to automate PDF file generation from a system I'm working on. As much as I think MigraDoc is a great library for creating PDFs, I've been finding it to be getting quite limiting recently due to the fact that all the design work has to be done in .NET code, so this weekend I started working on a library to design files for MigraDoc in XML instead.

There's still loads that hasn't been done yet, but the fundamentals for creating quite simple PDFs are in place and I thought you might find it interesting/useful for your own projects.

I've got a project page set up on gitlab over here that you can take a look at:

And here's an example of some of the XML:
<Document ImagePath="{Model.ImagePath}">

    <Style Target="Paragraph">
        <Setters Font.Name="Calibri" SpaceBefore="2mm" SpaceAfter="2mm"/>

    <Style Target="Paragraph" Name="Title">
        <Setters Font.Size="20" Font.Bold="true" Alignment="Center"/>

    <Style Target="Paragraph" Name="Address">
        <Setters SpaceBefore="0" SpaceAfter="0"/>

    <Section PageSetup.DifferentFirstPageHeaderFooter="true"

        <Header IsPrimary="true" IsFirstPage="true">
            <p Alignment="Right">
                <Text>Page </Text><PageField/><Text> of </Text><NumPagesField/>

        <Footer IsFirstPage="true">
                <Style Target="Row">
                    <Setters Alignment="Center"/>

                <Style Target="Paragraph">
                    <Setters SpaceBefore="0" SpaceAfter="0"/>

                <Column Width="15cm"/>

                    <Cell Index="0">
                        <p><Image Name="Generic Bank Logo.jpeg" Width="4cm"></Image></p>
                <Row C0="For all your non-specific banking needs"/>

        <p Style="Title">{Model.AccountHolder}'s Monthly Statement</p>

        <p Style="Address">{Model.Address.Line1}</p>
        <p Style="Address">{Model.Address.Town}</p>
        <p Style="Address">{Model.Address.County}</p>
        <p Style="Address">{Model.Address.Country}</p>
        <p Style="Address" SpaceAfter="1cm">{Model.Address.Postcode}</p>

        <If Var="Model.AccountBalance" Criteria="&lt;0">
            <p Font.Color="Red" Font.Bold="true">Your account is currently overdrawn, you need to either earn more money, or spend less</p>

        <p>Your monthly transactions</p>
        <Table Borders.Color="Gray">
            <Column Width="5cm"/>
            <Column Width="7cm"/>
            <Column Width="3cm"/>

            <Row Heading="true" Font.Bold="true" C0="Date" C1="Description" C2="Price"/>

            <ForEach Var="tx" In="Model.Transactions">
                <Row Font.Color="{tx.Colour}" C0="{tx.Date#dd MMM yyyy}" C1="{tx.Name}" C2="{tx.Value#C}"/>



Let me know your thoughts

Author:  TH-Soft [ Sun Apr 15, 2018 7:16 pm ]
Post subject:  Re: MigraDocXML

James Coyle wrote:
As much as I think MigraDoc is a great library for creating PDFs, I've been finding it to be getting quite limiting recently due to the fact that all the design work has to be done in .NET code, so this weekend I started working on a library to design files for MigraDoc in XML instead.[...]
Let me know your thoughts
MigraDoc always aupported MigraDoc DDL. I do not (yet) see the point of re-inventing the wheel using XML syntax when MDDDL existed previously.
In the old days there even was an editor that allowed mixing MDDDL and C# in a way similar to the razor syntax that allows mixing HTML and C#.
We got used to creating documents from code and the technology that allows mixing MDDDL and C# was abandoned many years ago.

Are there advantages of XML over MDDDL? A schema file could make the difference, allowing VS to help with syntax checks and IntelliSense. I don't see a schema file in your sample code.

Author:  James Coyle [ Sun Apr 15, 2018 10:25 pm ]
Post subject:  Re: MigraDocXML

Hi Thomas

I always got the impression that MDDDL was for saving a document that had already been designed for the purpose of debugging more than anything. I'll admit, I've not really done much with DDLs, but from the samples that are available I've never seen any way that they allow you to pass in data to be inserted within your document. It could be that this is supported and I've just missed it, there doesn't seem to be too much online regarding what you can do with DDLs.

You mention there used to be an editor for mixing MDDDL & C#, does that still exist in any form? I'd be interested to see how it was implemented. Also, did that then mean that the files were compiled? Because that was the one issue I had with razor pages when I wanted to make a small layout change to my pages and had to redeploy my web app to do so.

Regarding advantages of XML or MDDDL...I don't think the format of the file itself isn't really what's significant, the reason I chose to store files as xml is just because it's pretty much become industry standard format for defining document layouts. Also, you're right, I don't currently have a schema file, which could actually be very helpful. For the time being I've just been making sure the library actually works.

I wrote this project mainly for myself as I can really see this providing a huge boost to how I work with MigraDoc. I'll try and win you over to my library :D if only because I'd love to know that other people are getting use out of it too. The problem this is aiming to solve for me is that I've found the documents I produce for customers often seem to be running off pretty much the exact same data, but with each of them having different requests of how they want things laid out. With the way I'm using migradoc at the moment it means either lots of conditional logic or else customer-specific plugins just for document layout changes, neither of which are at all ideal. Storing the layout outside of my assembly along with information of how data should be populated into it means I just need to change one xml file.


Author:  marksu22 [ Wed Oct 03, 2018 6:43 pm ]
Post subject:  Re: MigraDocXML

Dear All,

I'm interested in the MDDDL also.

I'm looking for a solution to replace RDLC when I just need to generate a simple document with dynamic data.
After I found and looked into the MigraDoc, I want to compose a template of document and then insert data by code.

So I wonder if using MDDDL directly is a good solution.
There are several questions: Is there a documented specification of MDDDL? Is it easy to insert data later by code?

So maybe MigraDocXML will be an answer for me.

Author:  TH-Soft [ Wed Oct 03, 2018 8:21 pm ]
Post subject:  Re: MigraDocXML

marksu22 wrote:
I'm interested in the MDDDL also.
This is the wrong place to ask questions about MDDDL.

Info about MDDDL:
I think the comprehensive MDDDL documentation is not publicly available.
MDDDL is a textfile and you can define your own placeholders and use string.Replace() or similar techniques to add dynamic data.

Many years ago I used CMD.EXE to create MDDDL files from CMD batch files (without placeholders) and then create PDF from MDDDL.

An alternative XML serialization:

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group