├── .gitattributes ├── .gitignore ├── Invoicer.Console ├── App.config ├── Invoicer.Console.csproj ├── Process.cs ├── Program.cs ├── Properties │ └── AssemblyInfo.cs └── images │ └── vodafone.jpg ├── Invoicer.sln ├── Invoicer ├── Helpers │ ├── DateTimeExtensions.cs │ ├── MigraDocHelpers.cs │ └── Utils.cs ├── Invoicer.csproj ├── Models │ ├── Address.cs │ ├── DetailRow.cs │ ├── Invoice.cs │ ├── ItemRow.cs │ ├── Options.cs │ └── TotalRow.cs ├── Properties │ └── AssemblyInfo.cs ├── Services │ ├── IInvoicerApi.cs │ ├── Impl │ │ ├── PdfInvoice.Sections.cs │ │ └── PdfInvoice.cs │ └── InvoicerApi.cs └── packages.config ├── Readme.md └── license.txt /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | [Dd]ebug/ 11 | [Dd]ebugPublic/ 12 | [Rr]elease/ 13 | x64/ 14 | build/ 15 | bld/ 16 | [Bb]in/ 17 | [Oo]bj/ 18 | 19 | # Roslyn cache directories 20 | *.ide/ 21 | 22 | # MSTest test Results 23 | [Tt]est[Rr]esult*/ 24 | [Bb]uild[Ll]og.* 25 | 26 | #NUNIT 27 | *.VisualState.xml 28 | TestResult.xml 29 | 30 | # Build Results of an ATL Project 31 | [Dd]ebugPS/ 32 | [Rr]eleasePS/ 33 | dlldata.c 34 | 35 | *_i.c 36 | *_p.c 37 | *_i.h 38 | *.ilk 39 | *.meta 40 | *.obj 41 | *.pch 42 | *.pdb 43 | *.pgc 44 | *.pgd 45 | *.rsp 46 | *.sbr 47 | *.tlb 48 | *.tli 49 | *.tlh 50 | *.tmp 51 | *.tmp_proj 52 | *.log 53 | *.vspscc 54 | *.vssscc 55 | .builds 56 | *.pidb 57 | *.svclog 58 | *.scc 59 | 60 | # Chutzpah Test files 61 | _Chutzpah* 62 | 63 | # Visual C++ cache files 64 | ipch/ 65 | *.aps 66 | *.ncb 67 | *.opensdf 68 | *.sdf 69 | *.cachefile 70 | 71 | # Visual Studio profiler 72 | *.psess 73 | *.vsp 74 | *.vspx 75 | 76 | # TFS 2012 Local Workspace 77 | $tf/ 78 | 79 | # Guidance Automation Toolkit 80 | *.gpState 81 | 82 | # ReSharper is a .NET coding add-in 83 | _ReSharper*/ 84 | *.[Rr]e[Ss]harper 85 | *.DotSettings.user 86 | 87 | # JustCode is a .NET coding addin-in 88 | .JustCode 89 | 90 | # TeamCity is a build add-in 91 | _TeamCity* 92 | 93 | # DotCover is a Code Coverage Tool 94 | *.dotCover 95 | 96 | # NCrunch 97 | _NCrunch_* 98 | .*crunch*.local.xml 99 | 100 | # MightyMoose 101 | *.mm.* 102 | AutoTest.Net/ 103 | 104 | # Web workbench (sass) 105 | .sass-cache/ 106 | 107 | # Installshield output folder 108 | [Ee]xpress/ 109 | 110 | # DocProject is a documentation generator add-in 111 | DocProject/buildhelp/ 112 | DocProject/Help/*.HxT 113 | DocProject/Help/*.HxC 114 | DocProject/Help/*.hhc 115 | DocProject/Help/*.hhk 116 | DocProject/Help/*.hhp 117 | DocProject/Help/Html2 118 | DocProject/Help/html 119 | 120 | # Click-Once directory 121 | publish/ 122 | 123 | # Publish Web Output 124 | *.[Pp]ublish.xml 125 | *.azurePubxml 126 | ## TODO: Comment the next line if you want to checkin your 127 | ## web deploy settings but do note that will include unencrypted 128 | ## passwords 129 | #*.pubxml 130 | 131 | # NuGet Packages Directory 132 | packages/* 133 | ## TODO: If the tool you use requires repositories.config 134 | ## uncomment the next line 135 | #!packages/repositories.config 136 | 137 | # Enable "build/" folder in the NuGet Packages folder since 138 | # NuGet packages use it for MSBuild targets. 139 | # This line needs to be after the ignore of the build folder 140 | # (and the packages folder if the line above has been uncommented) 141 | !packages/build/ 142 | 143 | # Windows Azure Build Output 144 | csx/ 145 | *.build.csdef 146 | 147 | # Windows Store app package directory 148 | AppPackages/ 149 | 150 | # Others 151 | sql/ 152 | *.Cache 153 | ClientBin/ 154 | [Ss]tyle[Cc]op.* 155 | ~$* 156 | *~ 157 | *.dbmdl 158 | *.dbproj.schemaview 159 | *.pfx 160 | *.publishsettings 161 | node_modules/ 162 | 163 | # RIA/Silverlight projects 164 | Generated_Code/ 165 | 166 | # Backup & report files from converting an old project file 167 | # to a newer Visual Studio version. Backup files are not needed, 168 | # because we have git ;-) 169 | _UpgradeReport_Files/ 170 | Backup*/ 171 | UpgradeLog*.XML 172 | UpgradeLog*.htm 173 | 174 | # SQL Server files 175 | *.mdf 176 | *.ldf 177 | 178 | # Business Intelligence projects 179 | *.rdl.data 180 | *.bim.layout 181 | *.bim_*.settings 182 | 183 | # Microsoft Fakes 184 | FakesAssemblies/ 185 | 186 | # LightSwitch generated files 187 | GeneratedArtifacts/ 188 | _Pvt_Extensions/ 189 | ModelManifest.xml -------------------------------------------------------------------------------- /Invoicer.Console/App.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Invoicer.Console/Invoicer.Console.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {E7CD7592-86C8-447E-BE01-BF979D7B4894} 8 | Exe 9 | Properties 10 | Invoicer.Console 11 | Invoicer.Console 12 | v4.5.2 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | {e5ba227e-4afe-448a-b879-a2784b3fc97c} 55 | Invoicer 56 | 57 | 58 | 59 | 60 | 61 | 62 | 69 | -------------------------------------------------------------------------------- /Invoicer.Console/Process.cs: -------------------------------------------------------------------------------- 1 | using Invoicer.Models; 2 | using Invoicer.Services; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Text; 7 | using System.Threading.Tasks; 8 | 9 | namespace Invoicer.Console 10 | { 11 | public class Process 12 | { 13 | public void Go() 14 | { 15 | new InvoicerApi(SizeOption.A4, OrientationOption.Landscape, "£") 16 | .TextColor("#CC0000") 17 | .BackColor("#FFD6CC") 18 | .Image(@"..\..\images\vodafone.jpg", 125, 27) 19 | .Company(Address.Make("FROM", new string[] { "Vodafone Limited", "Vodafone House", "The Connection", "Newbury", "Berkshire RG14 2FN" }, "1471587", "569953277")) 20 | .Client(Address.Make("BILLING TO", new string[] { "Isabella Marsh", "Overton Circle", "Little Welland", "Worcester", "WR## 2DJ" })) 21 | .Items(new List { 22 | ItemRow.Make("Nexus 6", "Midnight Blue", (decimal)1, 20, (decimal)166.66, (decimal)199.99), 23 | ItemRow.Make("24 Months (£22.50pm)", "100 minutes, Unlimited texts, 100 MB data 3G plan with 3GB of UK Wi-Fi", (decimal)1, 20, (decimal)360.00, (decimal)432.00), 24 | ItemRow.Make("Special Offer", "Free case (blue)", (decimal)1, 0, (decimal)0, (decimal)0), 25 | }) 26 | .Totals(new List { 27 | TotalRow.Make("Sub Total", (decimal)526.66), 28 | TotalRow.Make("VAT @ 20%", (decimal)105.33), 29 | TotalRow.Make("Total", (decimal)631.99, true), 30 | }) 31 | .Details(new List { 32 | DetailRow.Make("PAYMENT INFORMATION", "Make all cheques payable to Vodafone UK Limited.", "", "If you have any questions concerning this invoice, contact our sales department at sales@vodafone.co.uk.", "", "Thank you for your business.") 33 | }) 34 | .Footer("http://www.vodafone.co.uk") 35 | .Save(); 36 | } 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /Invoicer.Console/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Invoicer.Console 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | new Process().Go(); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Invoicer.Console/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Invoicer.Console")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Invoicer.Console")] 13 | [assembly: AssemblyCopyright("Copyright © 2014")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("f0fc9248-4a0c-4eb6-8599-c6b345c0248c")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Invoicer.Console/images/vodafone.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/simonray/Invoicer/a8870faf35a7113d37785007cf344b8f795b6e6f/Invoicer.Console/images/vodafone.jpg -------------------------------------------------------------------------------- /Invoicer.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3FF5ED00-B1D5-44C1-87A9-249DF0566D2A}" 7 | ProjectSection(SolutionItems) = preProject 8 | Readme.md = Readme.md 9 | EndProjectSection 10 | EndProject 11 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Invoicer", "Invoicer\Invoicer.csproj", "{E5BA227E-4AFE-448A-B879-A2784B3FC97C}" 12 | EndProject 13 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Invoicer.Console", "Invoicer.Console\Invoicer.Console.csproj", "{E7CD7592-86C8-447E-BE01-BF979D7B4894}" 14 | EndProject 15 | Global 16 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 17 | Debug|Any CPU = Debug|Any CPU 18 | Release|Any CPU = Release|Any CPU 19 | EndGlobalSection 20 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 21 | {E5BA227E-4AFE-448A-B879-A2784B3FC97C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 22 | {E5BA227E-4AFE-448A-B879-A2784B3FC97C}.Debug|Any CPU.Build.0 = Debug|Any CPU 23 | {E5BA227E-4AFE-448A-B879-A2784B3FC97C}.Release|Any CPU.ActiveCfg = Release|Any CPU 24 | {E5BA227E-4AFE-448A-B879-A2784B3FC97C}.Release|Any CPU.Build.0 = Release|Any CPU 25 | {E7CD7592-86C8-447E-BE01-BF979D7B4894}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 26 | {E7CD7592-86C8-447E-BE01-BF979D7B4894}.Debug|Any CPU.Build.0 = Debug|Any CPU 27 | {E7CD7592-86C8-447E-BE01-BF979D7B4894}.Release|Any CPU.ActiveCfg = Release|Any CPU 28 | {E7CD7592-86C8-447E-BE01-BF979D7B4894}.Release|Any CPU.Build.0 = Release|Any CPU 29 | EndGlobalSection 30 | GlobalSection(SolutionProperties) = preSolution 31 | HideSolutionNode = FALSE 32 | EndGlobalSection 33 | EndGlobal 34 | -------------------------------------------------------------------------------- /Invoicer/Helpers/DateTimeExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Globalization; 4 | using System.Linq; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace Invoicer.Helpers 9 | { 10 | public static class DateTimeExtensions 11 | { 12 | public static string GetShortYear(this DateTime date) 13 | { 14 | return date.Year.ToString().Substring(2); 15 | } 16 | 17 | public static int GetWeekNumber(this DateTime date) 18 | { 19 | return GetWeekNumber(date, CultureInfo.CurrentCulture.Name); 20 | } 21 | 22 | public static int GetWeekNumber(this DateTime date, string culture) 23 | { 24 | CultureInfo info = new CultureInfo(culture); 25 | return info.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Invoicer/Helpers/MigraDocHelpers.cs: -------------------------------------------------------------------------------- 1 | using Invoicer.Helpers; 2 | using MigraDoc.DocumentObjectModel; 3 | using MigraDoc.DocumentObjectModel.Shapes; 4 | using MigraDoc.DocumentObjectModel.Tables; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Linq; 8 | using System.Text; 9 | using System.Threading.Tasks; 10 | 11 | namespace Invoicer.Helpers 12 | { 13 | public static class MigraDocHelpers 14 | { 15 | public static Color BackColorFromHtml(string hex) 16 | { 17 | if (hex == null) 18 | return Colors.LightYellow; 19 | else 20 | return new Color((uint)System.Drawing.ColorTranslator.FromHtml(hex).ToArgb()); 21 | } 22 | 23 | public static Color TextColorFromHtml(string hex) 24 | { 25 | if (hex == null) 26 | return Colors.Black; 27 | else 28 | return new Color((uint)System.Drawing.ColorTranslator.FromHtml(hex).ToArgb()); 29 | } 30 | 31 | public static double PageWidth(this Section section) 32 | { 33 | return (PageWidth(section.Document)); 34 | } 35 | 36 | public static double PageWidth(this Document document) 37 | { 38 | Unit width, height; 39 | 40 | PageSetup.GetPageSize(document.DefaultPageSetup.PageFormat, out width, out height); 41 | if (document.DefaultPageSetup.Orientation == Orientation.Landscape) 42 | Utils.Swap(ref width, ref height); 43 | 44 | return (width.Point - document.DefaultPageSetup.LeftMargin.Point - document.DefaultPageSetup.RightMargin.Point); 45 | } 46 | 47 | public static Column AddColumn(this Table table, ParagraphAlignment align, Unit unit = new Unit()) 48 | { 49 | Column column = table.AddColumn(); 50 | column.Width = unit; 51 | column.Format.Alignment = align; 52 | return column; 53 | } 54 | 55 | public static Paragraph AddParagraph(this TextFrame frame, string text, ParagraphAlignment align, string style = null) 56 | { 57 | return frame.AddParagraph().AddText(text, align, style); 58 | } 59 | 60 | public static Paragraph AddParagraph(this Cell cell, string text, ParagraphAlignment align, string style = null) 61 | { 62 | return cell.AddParagraph().AddText(text, align, style); 63 | } 64 | 65 | private static Paragraph AddText(this Paragraph paragraph, string text, ParagraphAlignment align, string style = null) 66 | { 67 | paragraph.Format.Alignment = align; 68 | if (style == null) 69 | paragraph.AddText(text); 70 | else 71 | paragraph.AddFormattedText(text, style); 72 | return paragraph; 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Invoicer/Helpers/Utils.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Invoicer.Helpers 8 | { 9 | public static class Utils 10 | { 11 | public static string ToCurrency(this decimal number) 12 | { 13 | return ToCurrency(number, string.Empty); 14 | } 15 | 16 | public static string ToCurrency(this decimal number, string symbol) 17 | { 18 | return string.Format("{0}{1:N2}", symbol, number); 19 | } 20 | 21 | public static void Swap(ref T lhs, ref T rhs) 22 | { 23 | T tmp = lhs; lhs = rhs; rhs = tmp; 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Invoicer/Invoicer.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {E5BA227E-4AFE-448A-B879-A2784B3FC97C} 8 | Library 9 | Properties 10 | Invoicer 11 | Invoicer 12 | v4.5.2 13 | 512 14 | 15 | 16 | true 17 | full 18 | false 19 | bin\Debug\ 20 | DEBUG;TRACE 21 | prompt 22 | 4 23 | 24 | 25 | pdbonly 26 | true 27 | bin\Release\ 28 | TRACE 29 | prompt 30 | 4 31 | 32 | 33 | 34 | ..\packages\PDFsharp-MigraDoc-GDI.1.32.4334.0\lib\net20\MigraDoc.DocumentObjectModel.dll 35 | 36 | 37 | ..\packages\PDFsharp-MigraDoc-GDI.1.32.4334.0\lib\net20\MigraDoc.Rendering.dll 38 | 39 | 40 | ..\packages\PDFsharp-MigraDoc-GDI.1.32.4334.0\lib\net20\MigraDoc.RtfRendering.dll 41 | 42 | 43 | ..\packages\PDFsharp-MigraDoc-GDI.1.32.4334.0\lib\net20\PdfSharp.dll 44 | 45 | 46 | ..\packages\PDFsharp-MigraDoc-GDI.1.32.4334.0\lib\net20\PdfSharp.Charting.dll 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 85 | -------------------------------------------------------------------------------- /Invoicer/Models/Address.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Invoicer.Models 8 | { 9 | public class Address 10 | { 11 | public string Title { get; set; } 12 | public string[] AddressLines { get; set; } 13 | public string VatNumber { get; set; } 14 | public string CompanyNumber { get; set; } 15 | 16 | public bool HasCompanyNumber { get { return !string.IsNullOrEmpty(CompanyNumber); } } 17 | public bool HasVatNumber { get { return !string.IsNullOrEmpty(VatNumber); } } 18 | 19 | public static Address Make(string title, string[] address, string company = null, string vat = null) 20 | { 21 | return new Address 22 | { 23 | Title = title, 24 | AddressLines = address, 25 | CompanyNumber = company, 26 | VatNumber = vat, 27 | }; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /Invoicer/Models/DetailRow.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | 5 | namespace Invoicer.Models 6 | { 7 | public class DetailRow 8 | { 9 | public string Title { get; set; } 10 | public List Paragraphs { get; set; } 11 | 12 | public static DetailRow Make(string title, params string[] paragraphs) 13 | { 14 | return new DetailRow 15 | { 16 | Title = title, 17 | Paragraphs = paragraphs.ToList(), 18 | }; 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /Invoicer/Models/Invoice.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Drawing; 4 | using System.Linq; 5 | 6 | namespace Invoicer.Models 7 | { 8 | public class Invoice 9 | { 10 | public SizeOption PageSize { get; set; } 11 | public OrientationOption PageOrientation { get; set; } 12 | public string Currency { get; set; } 13 | 14 | public string BackColor { get; set; } 15 | public string TextColor { get; set; } 16 | public string Image { get; set; } 17 | public Size ImageSize { get; set; } 18 | public string Title { get; set; } 19 | public string Reference { get; set; } 20 | public DateTime BillingDate { get; set; } 21 | public DateTime DueDate { get; set; } 22 | public Address Client { get; set; } 23 | public Address Company { get; set; } 24 | public PositionOption CompanyOrientation { get; set; } 25 | public List Details { get; set; } 26 | public string Footer { get; set; } 27 | public List Items { get; set; } 28 | public List Totals { get; set; } 29 | 30 | /// 31 | /// Do any of the items have a discount specified. If there are no discounts then 32 | /// the column will be ommited from the invoice. 33 | /// 34 | public bool HasDiscount { get { return Items.Any(row => row.HasDiscount); } } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Invoicer/Models/ItemRow.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Invoicer.Models 5 | { 6 | public class ItemRow 7 | { 8 | public string Name { get; set; } 9 | public string Description { get; set; } 10 | public decimal Amount { get; set; } 11 | public decimal VAT { get; set; } 12 | public decimal Price { get; set; } 13 | public string Discount { get; set; } 14 | public decimal Total { get; set; } 15 | 16 | public bool HasDiscount 17 | { 18 | get 19 | { 20 | return (!string.IsNullOrEmpty(Discount)); 21 | } 22 | } 23 | 24 | public static ItemRow Make(string name, string description, decimal amount, decimal vat, decimal price, decimal total) 25 | { 26 | return Make(name, description, amount, vat, price, "", total); 27 | } 28 | 29 | public static ItemRow Make(string name, string description, decimal amount, decimal vat, decimal price, string discount, decimal total) 30 | { 31 | return new ItemRow() 32 | { 33 | Name = name, 34 | Description = description, 35 | Amount = amount, 36 | VAT = vat, 37 | Price = price, 38 | Discount = discount, 39 | Total = total, 40 | }; 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /Invoicer/Models/Options.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | using System.Threading.Tasks; 6 | 7 | namespace Invoicer.Models 8 | { 9 | public enum OrientationOption 10 | { 11 | Landscape, 12 | Portrait, 13 | } 14 | 15 | public enum PositionOption 16 | { 17 | Left, 18 | Right, 19 | } 20 | 21 | public enum SizeOption 22 | { 23 | A4, 24 | Letter, 25 | Legal, 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Invoicer/Models/TotalRow.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Invoicer.Models 5 | { 6 | public class TotalRow 7 | { 8 | public string Name { get; set; } 9 | public decimal Value { get; set; } 10 | public bool Inverse { get; set; } 11 | 12 | public static TotalRow Make(string name, decimal value, bool inverse = false) 13 | { 14 | return new TotalRow() 15 | { 16 | Name = name, 17 | Value = value, 18 | Inverse = inverse, 19 | }; 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /Invoicer/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | // General Information about an assembly is controlled through the following 6 | // set of attributes. Change these attribute values to modify the information 7 | // associated with an assembly. 8 | [assembly: AssemblyTitle("Invoicer")] 9 | [assembly: AssemblyDescription("")] 10 | [assembly: AssemblyConfiguration("")] 11 | [assembly: AssemblyCompany("")] 12 | [assembly: AssemblyProduct("Invoicer")] 13 | [assembly: AssemblyCopyright("Copyright © 2014")] 14 | [assembly: AssemblyTrademark("")] 15 | [assembly: AssemblyCulture("")] 16 | 17 | // Setting ComVisible to false makes the types in this assembly not visible 18 | // to COM components. If you need to access a type in this assembly from 19 | // COM, set the ComVisible attribute to true on that type. 20 | [assembly: ComVisible(false)] 21 | 22 | // The following GUID is for the ID of the typelib if this project is exposed to COM 23 | [assembly: Guid("aef0a2da-8729-4160-94bf-f92df64db371")] 24 | 25 | // Version information for an assembly consists of the following four values: 26 | // 27 | // Major Version 28 | // Minor Version 29 | // Build Number 30 | // Revision 31 | // 32 | // You can specify all the values or you can default the Build and Revision Numbers 33 | // by using the '*' as shown below: 34 | // [assembly: AssemblyVersion("1.0.*")] 35 | [assembly: AssemblyVersion("1.0.0.0")] 36 | [assembly: AssemblyFileVersion("1.0.0.0")] 37 | -------------------------------------------------------------------------------- /Invoicer/Services/IInvoicerApi.cs: -------------------------------------------------------------------------------- 1 | using Invoicer.Models; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace Invoicer.Services 7 | { 8 | public interface IInvoicerApi : IInvoicerOptions 9 | { 10 | 11 | } 12 | 13 | public interface IInvoicerOptions : IInvoicerActions 14 | { 15 | /// 16 | /// Set a custom html color to personalize the document background 17 | /// 18 | IInvoicerOptions BackColor(string color); 19 | 20 | /// 21 | /// Set a custom html color to personalize the document text 22 | /// 23 | IInvoicerOptions TextColor(string color); 24 | 25 | /// 26 | /// Add a logo to the left corner of the document. 27 | /// 28 | IInvoicerOptions Image(string image, int width, int height); 29 | 30 | /// 31 | /// Set the title used on the document (e.g. 'invoice' or 'quote'). 32 | /// 33 | /// Title. Default is 'Invoice'. 34 | IInvoicerOptions Title(string title); 35 | 36 | /// 37 | /// A unique reference number for the document. 38 | /// 39 | /// Reference (e.g. '123456-1'). If ommited the default is year, week, weekday. 40 | IInvoicerOptions Reference(string reference); 41 | 42 | /// 43 | /// Set the document billing date 44 | /// 45 | /// Date, if ommited this is set to todays date. 46 | IInvoicerOptions BillingDate(DateTime date); 47 | 48 | /// 49 | /// Set the due date. 50 | /// 51 | /// Date, if ommited this is set to todays date + 14 days. 52 | IInvoicerOptions DueDate(DateTime dueDate); 53 | 54 | /// 55 | /// Set the company address 56 | /// 57 | IInvoicerOptions Company(Address address); 58 | 59 | /// 60 | /// Set the client address. 61 | /// 62 | IInvoicerOptions Client(Address address); 63 | 64 | /// 65 | /// Switch the position of the company address (default left). 66 | /// 67 | /// Orientation left or right. 68 | IInvoicerOptions CompanyOrientation(PositionOption position); 69 | 70 | /// 71 | /// You can add titles and paragraphs to display information on the bottom area 72 | /// of the document such as payment details or shipping information. 73 | /// 74 | IInvoicerOptions Details(List details); 75 | 76 | /// 77 | /// Set a footer, this would typically be a web url. 78 | /// 79 | IInvoicerOptions Footer(string title); 80 | 81 | /// 82 | /// Add a new product or service row to your document below the company and 83 | /// client information, paging is automatic. 84 | /// 85 | IInvoicerOptions Items(List items); 86 | 87 | /// 88 | /// Add a row below the products and services for calculations and totals. 89 | /// 90 | IInvoicerOptions Totals(List totals); 91 | }; 92 | 93 | public interface IInvoicerActions 94 | { 95 | /// 96 | /// Save the document with a password 97 | /// 98 | /// Filename of the PDF that will be created 99 | /// Leave null (default) or set a password 100 | void Save(string filename = null, string password = null); 101 | } 102 | } 103 | -------------------------------------------------------------------------------- /Invoicer/Services/Impl/PdfInvoice.Sections.cs: -------------------------------------------------------------------------------- 1 | using Invoicer.Helpers; 2 | using Invoicer.Models; 3 | using MigraDoc.DocumentObjectModel; 4 | using MigraDoc.DocumentObjectModel.Shapes; 5 | using MigraDoc.DocumentObjectModel.Tables; 6 | using System; 7 | using System.Linq; 8 | 9 | namespace Invoicer.Services.Impl 10 | { 11 | public partial class PdfInvoice 12 | { 13 | private void HeaderSection() 14 | { 15 | HeaderFooter header = Pdf.LastSection.Headers.Primary; 16 | 17 | Table table = header.AddTable(); 18 | double thirdWidth = Pdf.PageWidth() / 3; 19 | 20 | table.AddColumn(ParagraphAlignment.Left, thirdWidth * 2); 21 | table.AddColumn(); 22 | 23 | Row row = table.AddRow(); 24 | 25 | if (!string.IsNullOrEmpty(Invoice.Image)) 26 | { 27 | Image image = row.Cells[0].AddImage(Invoice.Image); 28 | row.Cells[0].VerticalAlignment = VerticalAlignment.Center; 29 | 30 | image.Height = Invoice.ImageSize.Height; 31 | image.Width = Invoice.ImageSize.Width; 32 | } 33 | 34 | TextFrame frame = row.Cells[1].AddTextFrame(); 35 | 36 | Table subTable = frame.AddTable(); 37 | subTable.AddColumn(thirdWidth / 2); 38 | subTable.AddColumn(thirdWidth / 2); 39 | 40 | row = subTable.AddRow(); 41 | row.Cells[0].MergeRight = 1; 42 | row.Cells[0].AddParagraph(Invoice.Title, ParagraphAlignment.Right, "H1-20"); 43 | 44 | row = subTable.AddRow(); 45 | row.Cells[0].AddParagraph("REFERENCE:", ParagraphAlignment.Left, "H2-9B-Color"); 46 | row.Cells[1].AddParagraph(Invoice.Reference, ParagraphAlignment.Right, "H2-9"); 47 | row.Cells[0].AddParagraph("BILLING DATE:", ParagraphAlignment.Left, "H2-9B-Color"); 48 | row.Cells[1].AddParagraph(Invoice.BillingDate.ToShortDateString(), ParagraphAlignment.Right, "H2-9"); 49 | row.Cells[0].AddParagraph("DUE DATE:", ParagraphAlignment.Left, "H2-9B-Color"); 50 | row.Cells[1].AddParagraph(Invoice.DueDate.ToShortDateString(), ParagraphAlignment.Right, "H2-9"); 51 | } 52 | 53 | public void FooterSection() 54 | { 55 | HeaderFooter footer = Pdf.LastSection.Footers.Primary; 56 | 57 | Table table = footer.AddTable(); 58 | table.AddColumn(footer.Section.PageWidth() / 2); 59 | table.AddColumn(footer.Section.PageWidth() / 2); 60 | Row row = table.AddRow(); 61 | if (!string.IsNullOrEmpty(Invoice.Footer)) 62 | { 63 | Paragraph paragraph = row.Cells[0].AddParagraph(Invoice.Footer, ParagraphAlignment.Left, "H2-8-Blue"); 64 | Hyperlink link = paragraph.AddHyperlink(Invoice.Footer, HyperlinkType.Web); 65 | } 66 | 67 | Paragraph info = row.Cells[1].AddParagraph(); 68 | info.Format.Alignment = ParagraphAlignment.Right; 69 | info.Style = "H2-8"; 70 | info.AddText("Page "); 71 | info.AddPageField(); 72 | info.AddText(" of "); 73 | info.AddNumPagesField(); 74 | } 75 | 76 | private void AddressSection() 77 | { 78 | Section section = Pdf.LastSection; 79 | 80 | Address leftAddress = Invoice.Company; 81 | Address rightAddress = Invoice.Client; 82 | 83 | if (Invoice.CompanyOrientation == PositionOption.Right) 84 | Utils.Swap
(ref leftAddress, ref rightAddress); 85 | 86 | Table table = section.AddTable(); 87 | table.AddColumn(ParagraphAlignment.Left, section.Document.PageWidth() / 2 - 10); 88 | table.AddColumn(ParagraphAlignment.Center, Unit.FromPoint(20)); 89 | table.AddColumn(ParagraphAlignment.Left, section.Document.PageWidth() / 2 - 10); 90 | 91 | Row row = table.AddRow(); 92 | row.Style = "H2-10B-Color"; 93 | row.Shading.Color = Colors.White; 94 | 95 | row.Cells[0].AddParagraph(leftAddress.Title, ParagraphAlignment.Left); 96 | row.Cells[0].Format.Borders.Bottom = BorderLine; 97 | row.Cells[2].AddParagraph(rightAddress.Title, ParagraphAlignment.Left); 98 | row.Cells[2].Format.Borders.Bottom = BorderLine; 99 | 100 | row = table.AddRow(); 101 | AddressCell(row.Cells[0], leftAddress.AddressLines); 102 | AddressCell(row.Cells[2], rightAddress.AddressLines); 103 | 104 | row = table.AddRow(); 105 | } 106 | 107 | private void AddressCell(Cell cell, string[] address) 108 | { 109 | foreach (string line in address) 110 | { 111 | Paragraph name = cell.AddParagraph(); 112 | if (line == address[0]) 113 | name.AddFormattedText(line, "H2-10B"); 114 | else 115 | name.AddFormattedText(line, "H2-9-Grey"); 116 | } 117 | } 118 | 119 | private void BillingSection() 120 | { 121 | Section section = Pdf.LastSection; 122 | 123 | Table table = section.AddTable(); 124 | 125 | double width = section.PageWidth(); 126 | double productWidth = Unit.FromPoint(150); 127 | double numericWidth = (width - productWidth) / (Invoice.HasDiscount ? 5 : 4); 128 | table.AddColumn(productWidth); 129 | table.AddColumn(ParagraphAlignment.Center, numericWidth); 130 | table.AddColumn(ParagraphAlignment.Center, numericWidth); 131 | table.AddColumn(ParagraphAlignment.Center, numericWidth); 132 | if (Invoice.HasDiscount) 133 | table.AddColumn(ParagraphAlignment.Center, numericWidth); 134 | table.AddColumn(ParagraphAlignment.Center, numericWidth); 135 | 136 | BillingHeader(table); 137 | 138 | foreach (ItemRow item in Invoice.Items) 139 | { 140 | BillingRow(table, item); 141 | } 142 | 143 | if (Invoice.Totals != null) 144 | { 145 | foreach (TotalRow total in Invoice.Totals) 146 | { 147 | BillingTotal(table, total); 148 | } 149 | } 150 | table.AddRow(); 151 | } 152 | 153 | private void BillingHeader(Table table) 154 | { 155 | Row row = table.AddRow(); 156 | row.HeadingFormat = true; 157 | row.Style = "H2-10B-Color"; 158 | row.Shading.Color = Colors.White; 159 | row.TopPadding = 10; 160 | row.Borders.Bottom = BorderLine; 161 | 162 | row.Cells[0].AddParagraph("PRODUCT", ParagraphAlignment.Left); 163 | row.Cells[1].AddParagraph("AMOUNT", ParagraphAlignment.Center); 164 | row.Cells[2].AddParagraph("VAT %", ParagraphAlignment.Center); 165 | row.Cells[3].AddParagraph("UNIT PRICE", ParagraphAlignment.Center); 166 | if (Invoice.HasDiscount) 167 | { 168 | row.Cells[4].AddParagraph("DISCOUNT", ParagraphAlignment.Center); 169 | row.Cells[5].AddParagraph("TOTAL", ParagraphAlignment.Center); 170 | } 171 | else 172 | { 173 | row.Cells[4].AddParagraph("TOTAL", ParagraphAlignment.Center); 174 | } 175 | } 176 | 177 | private void BillingRow(Table table, ItemRow item) 178 | { 179 | Row row = table.AddRow(); 180 | row.Style = "TableRow"; 181 | row.Shading.Color = MigraDocHelpers.BackColorFromHtml(Invoice.BackColor); 182 | 183 | Cell cell = row.Cells[0]; 184 | cell.AddParagraph(item.Name, ParagraphAlignment.Left, "H2-9B"); 185 | cell.AddParagraph(item.Description, ParagraphAlignment.Left, "H2-9-Grey"); 186 | 187 | cell = row.Cells[1]; 188 | cell.VerticalAlignment = VerticalAlignment.Center; 189 | cell.AddParagraph(item.Amount.ToCurrency(), ParagraphAlignment.Center, "H2-9"); 190 | 191 | cell = row.Cells[2]; 192 | cell.VerticalAlignment = VerticalAlignment.Center; 193 | cell.AddParagraph(item.VAT.ToCurrency(), ParagraphAlignment.Center, "H2-9"); 194 | 195 | cell = row.Cells[3]; 196 | cell.VerticalAlignment = VerticalAlignment.Center; 197 | cell.AddParagraph(item.Price.ToCurrency(Invoice.Currency), ParagraphAlignment.Center, "H2-9"); 198 | 199 | if (Invoice.HasDiscount) 200 | { 201 | cell = row.Cells[4]; 202 | cell.VerticalAlignment = VerticalAlignment.Center; 203 | cell.AddParagraph(item.Discount, ParagraphAlignment.Center, "H2-9"); 204 | 205 | cell = row.Cells[5]; 206 | cell.VerticalAlignment = VerticalAlignment.Center; 207 | cell.AddParagraph(item.Total.ToCurrency(Invoice.Currency), ParagraphAlignment.Center, "H2-9"); 208 | } 209 | else 210 | { 211 | cell = row.Cells[4]; 212 | cell.VerticalAlignment = VerticalAlignment.Center; 213 | cell.AddParagraph(item.Total.ToCurrency(Invoice.Currency), ParagraphAlignment.Center, "H2-9"); 214 | } 215 | } 216 | 217 | private void BillingTotal(Table table, TotalRow total) 218 | { 219 | if (Invoice.HasDiscount) 220 | { 221 | table.Columns[4].Format.Alignment = ParagraphAlignment.Left; 222 | table.Columns[5].Format.Alignment = ParagraphAlignment.Left; 223 | } 224 | else 225 | { 226 | table.Columns[4].Format.Alignment = ParagraphAlignment.Left; 227 | } 228 | 229 | Row row = table.AddRow(); 230 | row.Style = "TableRow"; 231 | 232 | string font; Color shading; 233 | if (total.Inverse == true) 234 | { 235 | font = "H2-9B-Inverse"; 236 | shading = MigraDocHelpers.TextColorFromHtml(Invoice.TextColor); 237 | } 238 | else 239 | { 240 | font = "H2-9B"; 241 | shading = MigraDocHelpers.BackColorFromHtml(Invoice.BackColor); 242 | } 243 | 244 | if (Invoice.HasDiscount) 245 | { 246 | Cell cell = row.Cells[4]; 247 | cell.Shading.Color = shading; 248 | cell.AddParagraph(total.Name, ParagraphAlignment.Left, font); 249 | 250 | cell = row.Cells[5]; 251 | cell.Shading.Color = shading; 252 | cell.AddParagraph(total.Value.ToCurrency(Invoice.Currency), ParagraphAlignment.Center, font); 253 | } 254 | else 255 | { 256 | Cell cell = row.Cells[3]; 257 | cell.Shading.Color = shading; 258 | cell.AddParagraph(total.Name, ParagraphAlignment.Left, font); 259 | 260 | cell = row.Cells[4]; 261 | cell.Shading.Color = shading; 262 | cell.AddParagraph(total.Value.ToCurrency(Invoice.Currency), ParagraphAlignment.Center, font); 263 | } 264 | } 265 | 266 | private void PaymentSection() 267 | { 268 | Section section = Pdf.LastSection; 269 | 270 | Table table = section.AddTable(); 271 | table.AddColumn(Unit.FromPoint(section.Document.PageWidth())); 272 | Row row = table.AddRow(); 273 | 274 | if (Invoice.Details != null && Invoice.Details.Count > 0) 275 | { 276 | foreach (DetailRow detail in Invoice.Details) 277 | { 278 | row.Cells[0].AddParagraph(detail.Title, ParagraphAlignment.Left, "H2-9B-Color"); 279 | row.Cells[0].Borders.Bottom = BorderLine; 280 | 281 | row = table.AddRow(); 282 | TextFrame frame = null; 283 | foreach (string line in detail.Paragraphs) 284 | { 285 | if (line == detail.Paragraphs[0]) 286 | { 287 | frame = row.Cells[0].AddTextFrame(); 288 | frame.Width = section.Document.PageWidth(); 289 | } 290 | frame.AddParagraph(line, ParagraphAlignment.Left, "H2-9"); 291 | } 292 | } 293 | } 294 | 295 | if (Invoice.Company.HasCompanyNumber || Invoice.Company.HasVatNumber) 296 | { 297 | row = table.AddRow(); 298 | 299 | Color shading = MigraDocHelpers.TextColorFromHtml(Invoice.TextColor); 300 | 301 | if (Invoice.Company.HasCompanyNumber && Invoice.Company.HasVatNumber) 302 | { 303 | row.Cells[0].AddParagraph(string.Format("Company Number: {0}, VAT Number: {1}", 304 | Invoice.Company.CompanyNumber, Invoice.Company.VatNumber), 305 | ParagraphAlignment.Center, "H2-9B-Inverse") 306 | .Format.Shading.Color = shading; 307 | } 308 | else 309 | { 310 | if (Invoice.Company.HasCompanyNumber) 311 | row.Cells[0].AddParagraph(string.Format("Company Number: {0}", Invoice.Company.CompanyNumber), 312 | ParagraphAlignment.Center, "H2-9B-Inverse") 313 | .Format.Shading.Color = shading; 314 | else 315 | row.Cells[0].AddParagraph(string.Format("VAT Number: {0}", Invoice.Company.VatNumber), 316 | ParagraphAlignment.Center, "H2-9B-Inverse") 317 | .Format.Shading.Color = shading; 318 | } 319 | } 320 | } 321 | } 322 | } 323 | -------------------------------------------------------------------------------- /Invoicer/Services/Impl/PdfInvoice.cs: -------------------------------------------------------------------------------- 1 | using Invoicer.Helpers; 2 | using Invoicer.Models; 3 | using MigraDoc.DocumentObjectModel; 4 | using MigraDoc.Rendering; 5 | using PdfSharp.Pdf.Security; 6 | using System; 7 | using System.Linq; 8 | 9 | namespace Invoicer.Services.Impl 10 | { 11 | public partial class PdfInvoice 12 | { 13 | public Document Pdf { get; private set; } 14 | public Invoice Invoice { get; private set; } 15 | 16 | private PageFormat InvoiceFormat 17 | { 18 | get 19 | { 20 | switch (Invoice.PageSize) 21 | { 22 | case SizeOption.A4: 23 | return PageFormat.A4; 24 | case SizeOption.Legal: 25 | return PageFormat.Legal; 26 | case SizeOption.Letter: 27 | return PageFormat.Letter; 28 | default: 29 | throw new ArgumentException("Unable to find matching size."); 30 | } 31 | } 32 | } 33 | 34 | private Orientation InvoiceOrientation 35 | { 36 | get 37 | { 38 | switch (Invoice.PageOrientation) 39 | { 40 | case OrientationOption.Landscape: 41 | return Orientation.Landscape; 42 | case OrientationOption.Portrait: 43 | return Orientation.Portrait; 44 | default: 45 | throw new ArgumentException("Unable to find matching orientation."); 46 | } 47 | } 48 | } 49 | 50 | private Border BorderLine 51 | { 52 | get 53 | { 54 | Border bottomLine = new Border(); 55 | bottomLine.Width = new Unit(0.5); 56 | bottomLine.Color = MigraDocHelpers.TextColorFromHtml(Invoice.TextColor); 57 | return bottomLine; 58 | } 59 | } 60 | 61 | public PdfInvoice(Invoice invoice) 62 | { 63 | Pdf = new Document(); 64 | Invoice = invoice; 65 | } 66 | 67 | public void Save(string filename, string password = null) 68 | { 69 | CreateDocument(); 70 | 71 | PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always); 72 | renderer.Document = Pdf; 73 | renderer.RenderDocument(); 74 | if (!string.IsNullOrEmpty(password)) 75 | SetPassword(renderer, password); 76 | renderer.PdfDocument.Save(filename); 77 | } 78 | 79 | private void CreateDocument() 80 | { 81 | Pdf.DefaultPageSetup.PageFormat = InvoiceFormat; 82 | Pdf.DefaultPageSetup.Orientation = InvoiceOrientation; 83 | Pdf.DefaultPageSetup.TopMargin = 125; 84 | Pdf.Info.Title = Invoice.Title; 85 | 86 | DefineStyles(); 87 | 88 | Pdf.AddSection(); 89 | HeaderSection(); 90 | AddressSection(); 91 | BillingSection(); 92 | PaymentSection(); 93 | FooterSection(); 94 | } 95 | 96 | private void SetPassword(PdfDocumentRenderer renderer, string password) 97 | { 98 | PdfSecuritySettings securitySettings = renderer.PdfDocument.SecuritySettings; 99 | securitySettings.OwnerPassword = password; 100 | securitySettings.UserPassword = password; 101 | securitySettings.PermitAccessibilityExtractContent = false; 102 | securitySettings.PermitAnnotations = false; 103 | securitySettings.PermitAssembleDocument = false; 104 | securitySettings.PermitExtractContent = false; 105 | securitySettings.PermitFormsFill = false; 106 | securitySettings.PermitFullQualityPrint = false; 107 | securitySettings.PermitModifyDocument = false; 108 | securitySettings.PermitPrint = false; 109 | } 110 | 111 | private void DefineStyles() 112 | { 113 | MigraDoc.DocumentObjectModel.Style style = Pdf.Styles["Normal"]; 114 | style.Font.Name = "Calibri"; 115 | 116 | style = Pdf.Styles.AddStyle("H1-20", "Normal"); 117 | style.Font.Size = 20; 118 | style.Font.Bold = true; 119 | 120 | style = Pdf.Styles.AddStyle("H2-8", "Normal"); 121 | style.Font.Size = 8; 122 | 123 | style = Pdf.Styles.AddStyle("H2-8-Blue", "H2-8"); 124 | style.ParagraphFormat.Font.Color = Colors.Blue; 125 | 126 | style = Pdf.Styles.AddStyle("H2-8B", "H2-8"); 127 | style.Font.Bold = true; 128 | 129 | style = Pdf.Styles.AddStyle("H2-9", "Normal"); 130 | style.Font.Size = 9; 131 | 132 | style = Pdf.Styles.AddStyle("H2-9-Grey", "H2-9"); 133 | style.Font.Color = Colors.Gray; 134 | 135 | style = Pdf.Styles.AddStyle("H2-9B", "H2-9"); 136 | style.Font.Bold = true; 137 | 138 | style = Pdf.Styles.AddStyle("H2-9B-Inverse", "H2-9B"); 139 | style.ParagraphFormat.Font.Color = Colors.White; 140 | 141 | style = Pdf.Styles.AddStyle("H2-9B-Color", "H2-9B"); 142 | style.Font.Color = MigraDocHelpers.TextColorFromHtml(Invoice.TextColor); 143 | 144 | style = Pdf.Styles.AddStyle("H2-10", "Normal"); 145 | style.Font.Size = 10; 146 | 147 | style = Pdf.Styles.AddStyle("H2-10B", "H2-10"); 148 | style.Font.Bold = true; 149 | 150 | style = Pdf.Styles.AddStyle("H2-10B-Color", "H2-10B"); 151 | style.Font.Color = MigraDocHelpers.TextColorFromHtml(Invoice.TextColor); 152 | } 153 | } 154 | } 155 | -------------------------------------------------------------------------------- /Invoicer/Services/InvoicerApi.cs: -------------------------------------------------------------------------------- 1 | using Invoicer.Helpers; 2 | using Invoicer.Models; 3 | using Invoicer.Services.Impl; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Drawing; 7 | using System.Linq; 8 | 9 | namespace Invoicer.Services 10 | { 11 | public class InvoicerApi : IInvoicerApi 12 | { 13 | public Invoice Invoice { get; protected set; } 14 | 15 | public static string DefaultReference 16 | { 17 | get 18 | { 19 | DateTime now = DateTime.Now; 20 | return string.Format("{0}{1}{2}", now.GetShortYear(), now.GetWeekNumber(), (int)now.DayOfWeek); 21 | } 22 | } 23 | 24 | public InvoicerApi( 25 | SizeOption size = SizeOption.A4, 26 | OrientationOption orientation = OrientationOption.Portrait, 27 | string currency = "£" 28 | ) 29 | { 30 | Invoice = new Invoice(); 31 | Invoice.Title = "Invoice"; 32 | Invoice.PageSize = size; 33 | Invoice.PageOrientation = orientation; 34 | Invoice.Currency = currency; 35 | Invoice.BillingDate = DateTime.Now; 36 | Invoice.DueDate = Invoice.BillingDate.AddDays(14); 37 | Invoice.Reference = DefaultReference; 38 | } 39 | 40 | public IInvoicerOptions BackColor(string color) 41 | { 42 | Invoice.BackColor = color; 43 | return this; 44 | } 45 | 46 | public IInvoicerOptions TextColor(string color) 47 | { 48 | Invoice.TextColor = color; 49 | return this; 50 | } 51 | 52 | public IInvoicerOptions Image(string image, int width, int height) 53 | { 54 | Invoice.Image = image; 55 | Invoice.ImageSize = new Size(width, height); 56 | return this; 57 | } 58 | 59 | 60 | public IInvoicerOptions Title(string title) 61 | { 62 | Invoice.Title = title; 63 | return this; 64 | } 65 | 66 | public IInvoicerOptions Reference(string reference) 67 | { 68 | Invoice.Reference = reference; 69 | return this; 70 | } 71 | 72 | public IInvoicerOptions BillingDate(DateTime date) 73 | { 74 | Invoice.BillingDate = date; 75 | return this; 76 | } 77 | 78 | public IInvoicerOptions DueDate(DateTime dueDate) 79 | { 80 | Invoice.DueDate = dueDate; 81 | return this; 82 | } 83 | 84 | public IInvoicerOptions Company(Address address) 85 | { 86 | Invoice.Company = address; 87 | return this; 88 | } 89 | 90 | public IInvoicerOptions Client(Address address) 91 | { 92 | Invoice.Client = address; 93 | return this; 94 | } 95 | 96 | public IInvoicerOptions CompanyOrientation(PositionOption position) 97 | { 98 | Invoice.CompanyOrientation = position; 99 | return this; 100 | } 101 | 102 | public IInvoicerOptions Details(List details) 103 | { 104 | Invoice.Details = details; 105 | return this; 106 | } 107 | 108 | public IInvoicerOptions Footer(string title) 109 | { 110 | Invoice.Footer = title; 111 | return this; 112 | } 113 | 114 | public IInvoicerOptions Items(List items) 115 | { 116 | Invoice.Items = items; 117 | return this; 118 | } 119 | 120 | public IInvoicerOptions Totals(List totals) 121 | { 122 | Invoice.Totals = totals; 123 | return this; 124 | } 125 | 126 | public void Save(string filename, string password = null) 127 | { 128 | if (filename == null || !System.IO.Path.HasExtension(filename)) 129 | filename = System.IO.Path.ChangeExtension(Invoice.Reference, "pdf"); 130 | new PdfInvoice(Invoice).Save(filename, password); 131 | } 132 | } 133 | } 134 | -------------------------------------------------------------------------------- /Invoicer/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /Readme.md: -------------------------------------------------------------------------------- 1 | # Invoicer 2 | 3 | A simple PDF invoice generator for .NET. 4 | 5 | ### Usage 6 | 7 | new InvoicerApi(SizeOption.A4, OrientationOption.Landscape, "£") 8 | .TextColor("#CC0000") 9 | .BackColor("#FFD6CC") 10 | .Image(@"vodafone.jpg", 125, 27) 11 | .Company(Address.Make("FROM", "Vodafone Limited", "Vodafone House", "The Connection", "Newbury", "Berkshire RG14 2FN")) 12 | .Client(Address.Make("BILLING TO", "Isabella Marsh", "Overton Circle", "Little Welland", "Worcester", "WR## 2DJ")) 13 | .Items(new List { 14 | ItemRow.Make("Nexus 6", "Midnight Blue", (decimal)1, 20, (decimal)166.66, (decimal)199.99), 15 | ItemRow.Make("24 Months (£22.50pm)", "100 minutes, Unlimited texts, 100 MB data 3G plan with 3GB of UK Wi-Fi", (decimal)1, 20, (decimal)360.00, (decimal)432.00), 16 | ItemRow.Make("Special Offer", "Free case (blue)", (decimal)1, 0, (decimal)0, (decimal)0), 17 | }) 18 | .Totals(new List { 19 | TotalRow.Make("Sub Total", (decimal)526.66), 20 | TotalRow.Make("VAT @ 20%", (decimal)105.33), 21 | TotalRow.Make("Total", (decimal)631.99, true), 22 | }) 23 | .Details(new List { 24 | DetailRow.Make("PAYMENT INFORMATION", "Make all cheques payable to Vodafone UK Limited.", "", "If you have any questions concerning this invoice, contact our sales department at sales@vodafone.co.uk.", "", "Thank you for your business.") 25 | }) 26 | .Footer("http://www.vodafone.co.uk") 27 | .Save(); 28 | 29 | ![Alt text](http://s14.postimg.cc/525eovuep/invoice.png "Sample Invoice") 30 | 31 | ### References 32 | * [PDFsharp/MigraDoc](http://pdfsharp.com) 33 | * [HTML Color Picker](http://www.w3schools.com/tags/ref_colorpicker.asp) 34 | -------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 simonray (https://github.com/simonray) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | --------------------------------------------------------------------------------