├── .gitattributes ├── .gitignore ├── Huanent.Printer.sln ├── Huanent.Printer ├── Helper │ └── PrintQueueHelper.cs ├── Huanent.Printer.csproj ├── IPrinter.cs ├── Internal │ └── Printer.cs ├── Models │ ├── FontSize.cs │ └── paperWidth.cs └── PrinterFactory.cs ├── Huanent.PrinterTest ├── Huanent.PrinterTest.csproj ├── PrinterTest.cs ├── Properties │ └── AssemblyInfo.cs ├── packages.config └── qr.png ├── LICENSE ├── README.md └── docs ├── 1.1.md └── img └── bill.png /.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 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | *.VC.VC.opendb 85 | 86 | # Visual Studio profiler 87 | *.psess 88 | *.vsp 89 | *.vspx 90 | *.sap 91 | 92 | # TFS 2012 Local Workspace 93 | $tf/ 94 | 95 | # Guidance Automation Toolkit 96 | *.gpState 97 | 98 | # ReSharper is a .NET coding add-in 99 | _ReSharper*/ 100 | *.[Rr]e[Ss]harper 101 | *.DotSettings.user 102 | 103 | # JustCode is a .NET coding add-in 104 | .JustCode 105 | 106 | # TeamCity is a build add-in 107 | _TeamCity* 108 | 109 | # DotCover is a Code Coverage Tool 110 | *.dotCover 111 | 112 | # NCrunch 113 | _NCrunch_* 114 | .*crunch*.local.xml 115 | nCrunchTemp_* 116 | 117 | # MightyMoose 118 | *.mm.* 119 | AutoTest.Net/ 120 | 121 | # Web workbench (sass) 122 | .sass-cache/ 123 | 124 | # Installshield output folder 125 | [Ee]xpress/ 126 | 127 | # DocProject is a documentation generator add-in 128 | DocProject/buildhelp/ 129 | DocProject/Help/*.HxT 130 | DocProject/Help/*.HxC 131 | DocProject/Help/*.hhc 132 | DocProject/Help/*.hhk 133 | DocProject/Help/*.hhp 134 | DocProject/Help/Html2 135 | DocProject/Help/html 136 | 137 | # Click-Once directory 138 | publish/ 139 | 140 | # Publish Web Output 141 | *.[Pp]ublish.xml 142 | *.azurePubxml 143 | # TODO: Comment the next line if you want to checkin your web deploy settings 144 | # but database connection strings (with potential passwords) will be unencrypted 145 | *.pubxml 146 | *.publishproj 147 | 148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 149 | # checkin your Azure Web App publish settings, but sensitive information contained 150 | # in these scripts will be unencrypted 151 | PublishScripts/ 152 | 153 | # NuGet Packages 154 | *.nupkg 155 | # The packages folder can be ignored because of Package Restore 156 | **/packages/* 157 | # except build/, which is used as an MSBuild target. 158 | !**/packages/build/ 159 | # Uncomment if necessary however generally it will be regenerated when needed 160 | #!**/packages/repositories.config 161 | # NuGet v3's project.json files produces more ignoreable files 162 | *.nuget.props 163 | *.nuget.targets 164 | 165 | # Microsoft Azure Build Output 166 | csx/ 167 | *.build.csdef 168 | 169 | # Microsoft Azure Emulator 170 | ecf/ 171 | rcf/ 172 | 173 | # Windows Store app package directories and files 174 | AppPackages/ 175 | BundleArtifacts/ 176 | Package.StoreAssociation.xml 177 | _pkginfo.txt 178 | 179 | # Visual Studio cache files 180 | # files ending in .cache can be ignored 181 | *.[Cc]ache 182 | # but keep track of directories ending in .cache 183 | !*.[Cc]ache/ 184 | 185 | # Others 186 | ClientBin/ 187 | ~$* 188 | *~ 189 | *.dbmdl 190 | *.dbproj.schemaview 191 | *.pfx 192 | *.publishsettings 193 | node_modules/ 194 | orleans.codegen.cs 195 | 196 | # Since there are multiple workflows, uncomment next line to ignore bower_components 197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 198 | #bower_components/ 199 | 200 | # RIA/Silverlight projects 201 | Generated_Code/ 202 | 203 | # Backup & report files from converting an old project file 204 | # to a newer Visual Studio version. Backup files are not needed, 205 | # because we have git ;-) 206 | _UpgradeReport_Files/ 207 | Backup*/ 208 | UpgradeLog*.XML 209 | UpgradeLog*.htm 210 | 211 | # SQL Server files 212 | *.mdf 213 | *.ldf 214 | 215 | # Business Intelligence projects 216 | *.rdl.data 217 | *.bim.layout 218 | *.bim_*.settings 219 | 220 | # Microsoft Fakes 221 | FakesAssemblies/ 222 | 223 | # GhostDoc plugin setting file 224 | *.GhostDoc.xml 225 | 226 | # Node.js Tools for Visual Studio 227 | .ntvs_analysis.dat 228 | 229 | # Visual Studio 6 build log 230 | *.plg 231 | 232 | # Visual Studio 6 workspace options file 233 | *.opt 234 | 235 | # Visual Studio LightSwitch build output 236 | **/*.HTMLClient/GeneratedArtifacts 237 | **/*.DesktopClient/GeneratedArtifacts 238 | **/*.DesktopClient/ModelManifest.xml 239 | **/*.Server/GeneratedArtifacts 240 | **/*.Server/ModelManifest.xml 241 | _Pvt_Extensions 242 | 243 | # Paket dependency manager 244 | .paket/paket.exe 245 | paket-files/ 246 | 247 | # FAKE - F# Make 248 | .fake/ 249 | 250 | # JetBrains Rider 251 | .idea/ 252 | *.sln.iml 253 | -------------------------------------------------------------------------------- /Huanent.Printer.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27130.2010 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution item", "solution item", "{BBEAE50A-0958-426F-89EC-20196B5C32A3}" 7 | ProjectSection(SolutionItems) = preProject 8 | README.md = README.md 9 | EndProjectSection 10 | EndProject 11 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Huanent.Printer", "Huanent.Printer\Huanent.Printer.csproj", "{1EDAAA01-77EA-41D6-8378-42CFFEE7BC39}" 12 | EndProject 13 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Huanent.PrinterTest", "Huanent.PrinterTest\Huanent.PrinterTest.csproj", "{6B95CD99-56FF-4588-8545-107579ADEF6E}" 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 | {1EDAAA01-77EA-41D6-8378-42CFFEE7BC39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 22 | {1EDAAA01-77EA-41D6-8378-42CFFEE7BC39}.Debug|Any CPU.Build.0 = Debug|Any CPU 23 | {1EDAAA01-77EA-41D6-8378-42CFFEE7BC39}.Release|Any CPU.ActiveCfg = Release|Any CPU 24 | {1EDAAA01-77EA-41D6-8378-42CFFEE7BC39}.Release|Any CPU.Build.0 = Release|Any CPU 25 | {6B95CD99-56FF-4588-8545-107579ADEF6E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 26 | {6B95CD99-56FF-4588-8545-107579ADEF6E}.Debug|Any CPU.Build.0 = Debug|Any CPU 27 | {6B95CD99-56FF-4588-8545-107579ADEF6E}.Release|Any CPU.ActiveCfg = Release|Any CPU 28 | {6B95CD99-56FF-4588-8545-107579ADEF6E}.Release|Any CPU.Build.0 = Release|Any CPU 29 | EndGlobalSection 30 | GlobalSection(SolutionProperties) = preSolution 31 | HideSolutionNode = FALSE 32 | EndGlobalSection 33 | GlobalSection(ExtensibilityGlobals) = postSolution 34 | SolutionGuid = {F58EFA8F-86CE-409E-A187-AABA28C6B50D} 35 | EndGlobalSection 36 | EndGlobal 37 | -------------------------------------------------------------------------------- /Huanent.Printer/Helper/PrintQueueHelper.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Printing; 5 | using System.Text; 6 | using System.Threading.Tasks; 7 | 8 | namespace PrintCore 9 | { 10 | public static class PrintQueueHelper 11 | { 12 | public static IEnumerable GetPrintQueue() 13 | { 14 | var localPrintServer = new LocalPrintServer(); 15 | var printQueues = localPrintServer.GetPrintQueues().GetEnumerator(); 16 | while (printQueues.MoveNext()) 17 | { 18 | yield return printQueues.Current; 19 | } 20 | 21 | } 22 | 23 | public static IEnumerable GetPrintQueueName() 24 | { 25 | return GetPrintQueue().Select(s => s.Name); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Huanent.Printer/Huanent.Printer.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | net40 5 | 2.0.2 6 | https://github.com/huanent/Huanent.Printer 7 | huanent 8 | https://s.gravatar.com/avatar/7991b51c75f1b60cfbb6595315821c69?s=400 9 | 10 | 11 | 12 | bin\Debug\net40\Huanent.Printer.xml 13 | 1701;1702;1705;CS1591 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /Huanent.Printer/IPrinter.cs: -------------------------------------------------------------------------------- 1 | using Huanent.Printer.Models; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Drawing; 5 | using System.Linq; 6 | using System.Text; 7 | 8 | namespace PrintCore 9 | { 10 | public interface IPrinter 11 | { 12 | void PrintText( 13 | string content, 14 | FontSize fontSize = FontSize.Normal, 15 | StringAlignment stringAlignment = StringAlignment.Near, 16 | float width = 1, 17 | float offset = 0 18 | ); 19 | 20 | void PrintImage( 21 | Image image, 22 | StringAlignment stringAlignment = StringAlignment.Near); 23 | 24 | void PrintLine(FontSize fontSize = FontSize.Normal); 25 | 26 | void NewRow(); 27 | 28 | /// 29 | /// invoke this method when you ready to print document 30 | /// 31 | void Finish(); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /Huanent.Printer/Internal/Printer.cs: -------------------------------------------------------------------------------- 1 | using Huanent.Printer.Models; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Drawing; 5 | using System.Drawing.Printing; 6 | using System.Text; 7 | using System.Linq; 8 | 9 | namespace PrintCore 10 | { 11 | public class Printer : IPrinter 12 | { 13 | 14 | #region fields 15 | PrintDocument _printDoc = new PrintDocument(); 16 | 17 | /// 18 | /// 打印对象打印宽度(根据英寸换算而来,paperWidth * 3.937) 19 | /// 20 | readonly int _paperWidth; 21 | const float _charProportion = 0.7352f; 22 | const float _lineHeightProportion = 1.6f; 23 | const string _fontName = "SimHei"; 24 | 25 | IList> _printActions = new List>(); 26 | 27 | /// 28 | /// 当前的打印高度,当调用换行或者图片打印时会增加此字段值 29 | /// 30 | float _currentheight = 0; 31 | 32 | float NewLineOffset { get; set; } = (int)FontSize.Normal * _lineHeightProportion; 33 | 34 | #endregion 35 | 36 | #region ctor 37 | 38 | /// 39 | /// 初始化机打印对象 40 | /// 41 | /// 打印机名称 42 | /// 打印纸宽度 43 | /// 打印纸高度 44 | internal Printer(string PrinterName, double paperWidth, int paperHight) 45 | { 46 | //3.937为一个打印单位(打印单位:80(实际宽度72.1),58(实际宽度48)) 47 | _paperWidth = Convert.ToInt32(Math.Ceiling(paperWidth * 3.937)); 48 | _printDoc.PrinterSettings.PrinterName = PrinterName; 49 | _printDoc.PrintPage += PrintPageDetails; 50 | _printDoc.DefaultPageSettings.PaperSize = new PaperSize("", _paperWidth, paperHight); 51 | _printDoc.PrintController = new StandardPrintController(); 52 | } 53 | 54 | 55 | #endregion 56 | 57 | #region eventHandler 58 | void PrintPageDetails(object sender, PrintPageEventArgs e) 59 | { 60 | foreach (var item in _printActions) 61 | { 62 | item(e.Graphics); 63 | } 64 | } 65 | #endregion 66 | 67 | #region IPrinterImplement 68 | public void NewRow() 69 | { 70 | _printActions.Add((g) => 71 | { 72 | _currentheight += NewLineOffset; 73 | NewLineOffset = (int)FontSize.Normal * _lineHeightProportion; 74 | }); 75 | } 76 | 77 | public void PrintText(string content, FontSize fontSize = FontSize.Normal, StringAlignment stringAlignment = StringAlignment.Near, float width = 1, float offset = 0) 78 | { 79 | _printActions.Add((g) => 80 | { 81 | float contentWidth = width == 1 ? _paperWidth * (1 - offset) : width * _paperWidth; 82 | string newContent = ContentWarp(content, fontSize, contentWidth, out var rowNum); 83 | var font = new Font(_fontName, (int)fontSize, FontStyle.Regular); 84 | var point = new PointF(offset * _paperWidth, _currentheight); 85 | var size = new SizeF(contentWidth, (int)fontSize * _lineHeightProportion * rowNum); 86 | var layoutRectangle = new RectangleF(point, size); 87 | var format = new StringFormat 88 | { 89 | Alignment = stringAlignment, 90 | FormatFlags = StringFormatFlags.NoWrap 91 | }; 92 | g.DrawString(newContent, font, Brushes.Black, layoutRectangle, format); 93 | float thisHeightOffset = rowNum * (int)fontSize * _lineHeightProportion; 94 | if (thisHeightOffset > NewLineOffset) NewLineOffset = thisHeightOffset; 95 | }); 96 | } 97 | 98 | public void Finish() 99 | { 100 | _printDoc.Print(); 101 | _printDoc.Dispose(); 102 | _printDoc = new PrintDocument(); 103 | _printActions.Clear(); 104 | } 105 | 106 | public void PrintImage(Image image, StringAlignment stringAlignment = StringAlignment.Near) 107 | { 108 | _printActions.Add((g) => 109 | { 110 | int x = 0; 111 | switch (stringAlignment) 112 | { 113 | case StringAlignment.Near: 114 | break; 115 | case StringAlignment.Center: 116 | x = (_paperWidth - image.Width) / 2; 117 | break; 118 | case StringAlignment.Far: 119 | x = _paperWidth - image.Width; 120 | break; 121 | default: 122 | break; 123 | } 124 | var point = new Point(x, Convert.ToInt32(_currentheight)); 125 | var size = new Size(image.Width, image.Height); 126 | var rectangle = new Rectangle(point, size); 127 | g.DrawImage(image, rectangle); 128 | NewLineOffset = image.Height; 129 | }); 130 | } 131 | 132 | public void PrintLine(FontSize fontSize = FontSize.Normal) 133 | { 134 | int charNum = (int)(_paperWidth / ((int)fontSize * _charProportion)); 135 | var builder = new StringBuilder(); 136 | for (int i = 0; i < charNum; i++) 137 | { 138 | builder.Append('-'); 139 | } 140 | PrintText(builder.ToString(), fontSize, StringAlignment.Center); 141 | } 142 | #endregion 143 | 144 | #region methods 145 | /// 146 | /// 对内容进行分行,并返回行数 147 | /// 148 | /// 内容 149 | /// 文字大小 150 | /// 内容区宽度 151 | /// 行数 152 | static string ContentWarp(string content, FontSize fontSize, float width, out int row) 153 | { 154 | content = content.Replace(Environment.NewLine, string.Empty); 155 | 156 | //0.7282 字符比例 157 | var builder = new StringBuilder(); 158 | float nowWidth = 0; 159 | row = 1; 160 | foreach (char item in content) 161 | { 162 | int code = Convert.ToInt32(item); 163 | float charWidth = code < 128 ? _charProportion * (int)fontSize : _charProportion * (int)fontSize * 2; 164 | nowWidth += charWidth; 165 | if (nowWidth > width) 166 | { 167 | builder.Append(Environment.NewLine); 168 | nowWidth = charWidth; 169 | row++; 170 | } 171 | builder.Append(item); 172 | } 173 | return builder.ToString(); 174 | } 175 | 176 | #endregion 177 | } 178 | 179 | } 180 | -------------------------------------------------------------------------------- /Huanent.Printer/Models/FontSize.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text; 5 | 6 | namespace Huanent.Printer.Models 7 | { 8 | public enum FontSize 9 | { 10 | micro = 4, 11 | Small = 6, 12 | Normal = 10, 13 | Large = 14, 14 | Huge = 16 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Huanent.Printer/Models/paperWidth.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 PrintCore 8 | { 9 | public enum PaperWidth 10 | { 11 | /// 12 | /// 80mm 13 | /// 14 | Paper80mm = 80, 15 | /// 16 | /// 76mm 17 | /// 18 | Paper76mm = 76, 19 | /// 20 | /// 58mm 21 | /// 22 | Paper58mm = 58 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Huanent.Printer/PrinterFactory.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 PrintCore 8 | { 9 | public static class PrinterFactory 10 | { 11 | public static Printer GetPrinter(string printerName, double paperWidth, int? pagerHeight = null) 12 | { 13 | if (string.IsNullOrEmpty(printerName)) throw new ArgumentException(nameof(printerName)); 14 | return new Printer(printerName, paperWidth, pagerHeight ?? 10000); 15 | } 16 | 17 | public static Printer GetPrinter(string printerName, PaperWidth paperWidth, int? pagerHeight = null) 18 | { 19 | switch (paperWidth) 20 | { 21 | case PaperWidth.Paper80mm: 22 | //80打印纸扣去两边内距实际可打的宽度为72.1 23 | return GetPrinter(printerName, 72.1, pagerHeight); 24 | case PaperWidth.Paper76mm: 25 | //76打印纸扣去两边内距实际可打的宽度为63.5 26 | return GetPrinter(printerName, 63.5, pagerHeight); 27 | case PaperWidth.Paper58mm: 28 | //58打印纸扣去两边内距实际可打的宽度为48 29 | return GetPrinter(printerName, 48, pagerHeight); 30 | default: 31 | throw new ArgumentException(nameof(paperWidth)); 32 | } 33 | 34 | } 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /Huanent.PrinterTest/Huanent.PrinterTest.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {6B95CD99-56FF-4588-8545-107579ADEF6E} 8 | Library 9 | Properties 10 | Huanent.PrinterTest 11 | Huanent.PrinterTest 12 | v4.5 13 | 512 14 | {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} 15 | 15.0 16 | $(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion) 17 | $(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages 18 | False 19 | UnitTest 20 | 21 | 22 | 23 | 24 | true 25 | full 26 | false 27 | bin\Debug\ 28 | DEBUG;TRACE 29 | prompt 30 | 4 31 | 32 | 33 | pdbonly 34 | true 35 | bin\Release\ 36 | TRACE 37 | prompt 38 | 4 39 | 40 | 41 | 42 | ..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.dll 43 | 44 | 45 | ..\packages\MSTest.TestFramework.1.2.0\lib\net45\Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.dll 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | {1edaaa01-77ea-41d6-8378-42cffee7bc39} 61 | Huanent.Printer 62 | 63 | 64 | 65 | 66 | PreserveNewest 67 | 68 | 69 | 70 | 71 | 72 | 73 | 这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。 74 | 75 | 76 | 77 | 78 | 79 | -------------------------------------------------------------------------------- /Huanent.PrinterTest/PrinterTest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Drawing; 3 | using Microsoft.VisualStudio.TestTools.UnitTesting; 4 | using PrintCore; 5 | 6 | namespace Huanent.PrinterTest 7 | { 8 | [TestClass] 9 | public class PrinterTest 10 | { 11 | [TestMethod] 12 | public void FinishTest() 13 | { 14 | var printer = PrinterFactory.GetPrinter("Microsoft XPS Document Writer", PaperWidth.Paper80mm); 15 | printer.NewRow(); 16 | printer.NewRow(); 17 | printer.PrintText("永辉超市", Printer.Models.FontSize.Huge, StringAlignment.Center); 18 | printer.NewRow(); 19 | printer.NewRow(); 20 | printer.NewRow(); 21 | printer.PrintText("操作员:张三"); 22 | printer.PrintText(DateTime.Now.ToString("HH:mm:ss"), stringAlignment: StringAlignment.Far); 23 | printer.NewRow(); 24 | printer.PrintLine(); 25 | printer.NewRow(); 26 | printer.PrintText("商品"); 27 | printer.PrintText("单价", offset: 0.35f); 28 | printer.PrintText("数量", offset: 0.65f); 29 | printer.PrintText("总价", stringAlignment: StringAlignment.Far); 30 | printer.NewRow(); 31 | printer.PrintLine(); 32 | printer.NewRow(); 33 | printer.PrintText("**长白山大萝卜,跳楼吐血大甩卖,不甜不要钱**", width: 0.35f); 34 | printer.PrintText("6.00", width: 0.2f, offset: 0.35f); 35 | printer.PrintText("2.00", width: 0.2f, offset: 0.65F); 36 | printer.PrintText("12.00", stringAlignment: StringAlignment.Far); 37 | printer.NewRow(); 38 | printer.NewRow(); 39 | printer.PrintText("大螃蟹", width: 0.35f); 40 | printer.PrintText("6.000000000001", width: 0.2f, offset: 0.35f); 41 | printer.PrintText("1", width: 0.2f, offset: 0.65F); 42 | printer.PrintText("6.000000000001", offset: 0.8f, width: 0.2f); 43 | printer.NewRow(); 44 | printer.PrintLine(); 45 | printer.NewRow(); 46 | var bitmap = new Bitmap("qr.png"); 47 | printer.PrintImage(bitmap, StringAlignment.Center); 48 | printer.NewRow(); 49 | printer.PrintLine(); 50 | printer.NewRow(); 51 | printer.PrintText("感谢光临,欢迎下次再来!", stringAlignment: StringAlignment.Center); 52 | printer.NewRow(); 53 | printer.Finish(); 54 | } 55 | 56 | [TestMethod] 57 | public void TextStringAlignmentTest() 58 | { 59 | var printer = PrinterFactory.GetPrinter("Microsoft XPS Document Writer", PaperWidth.Paper80mm); 60 | printer.PrintText("正", Printer.Models.FontSize.Huge); 61 | printer.NewRow(); 62 | printer.PrintText("正"); 63 | printer.NewRow(); 64 | printer.PrintText("正", Printer.Models.FontSize.micro); 65 | printer.NewRow(); 66 | printer.Finish(); 67 | } 68 | 69 | [TestMethod] 70 | public void PrintImageTest() 71 | { 72 | var printer = PrinterFactory.GetPrinter("Microsoft XPS Document Writer", PaperWidth.Paper80mm); 73 | printer.PrintText("一二三四五六七八", width: 0.3f); 74 | printer.PrintText("一二三四五六七八", width: 0.2f, offset: 0.4f); 75 | printer.NewRow(); 76 | printer.PrintText("我是新的行"); 77 | printer.NewRow(); 78 | printer.Finish(); 79 | } 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /Huanent.PrinterTest/Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | using System.Runtime.CompilerServices; 3 | using System.Runtime.InteropServices; 4 | 5 | [assembly: AssemblyTitle("Huanent.PrinterTest")] 6 | [assembly: AssemblyDescription("")] 7 | [assembly: AssemblyConfiguration("")] 8 | [assembly: AssemblyCompany("")] 9 | [assembly: AssemblyProduct("Huanent.PrinterTest")] 10 | [assembly: AssemblyCopyright("Copyright © 2018")] 11 | [assembly: AssemblyTrademark("")] 12 | [assembly: AssemblyCulture("")] 13 | 14 | [assembly: ComVisible(false)] 15 | 16 | [assembly: Guid("6b95cd99-56ff-4588-8545-107579adef6e")] 17 | 18 | // [assembly: AssemblyVersion("1.0.*")] 19 | [assembly: AssemblyVersion("1.0.0.0")] 20 | [assembly: AssemblyFileVersion("1.0.0.0")] 21 | -------------------------------------------------------------------------------- /Huanent.PrinterTest/packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /Huanent.PrinterTest/qr.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huanent/Huanent.Printer/eb966acc91ad8acd9ad013f2594b1063e36e727f/Huanent.PrinterTest/qr.png -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 huanent 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 all 13 | 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 THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 旧版本文档 2 | [1.1版本文档](https://github.com/huanent/Huanent.Printer/blob/master/docs/1.1.md) 3 | 4 | # 当前版本 5 | 6 | huanent.printer2.0版本是对1.1版本的破坏性更新,两个版本不兼容,2.0版本提供了更好的打印控制,如自动换行,流式打印等。 7 | ## 基本使用教程 8 | 9 | ### 安装程序集 10 | 在nuget搜索[Huanent.Printer](https://www.nuget.org/packages/Huanent.Printer/)进行安装,考虑到市场上还存在xp系统的打印机,故最低支持到.net4.0版本 11 | ### 获取本机所有打印机列表 12 | ``` 13 | var list= PrintQueueHelper.GetPrintQueueName(); 14 | ``` 15 | 此方法获取到本机所有打印机驱动的名称,选择小票打印机的名字例如 xp58 16 | ### 获得打印机对象 17 | ``` 18 | var printer = PrinterFactory.GetPrinter("xp58", PaperWidth.Paper80mm); 19 | ``` 20 | 进行打印 21 | ``` 22 | PrintQueueHelper.GetPrintQueueName(); 23 | var printer = PrinterFactory.GetPrinter("Microsoft XPS Document Writer", PaperWidth.Paper80mm); 24 | printer.NewRow(); 25 | printer.NewRow(); 26 | printer.PrintText("永辉超市", FontSize.Huge, StringAlignment.Center); 27 | printer.NewRow(); 28 | printer.NewRow(); 29 | printer.NewRow(); 30 | printer.PrintText("操作员:张三"); 31 | printer.PrintText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"), stringAlignment: StringAlignment.Far); 32 | printer.NewRow(); 33 | printer.PrintLine(); 34 | printer.NewRow(); 35 | printer.PrintText("商品"); 36 | printer.PrintText("单价", offset: 0.35f); 37 | printer.PrintText("数量", offset: 0.65f); 38 | printer.PrintText("总价", stringAlignment: StringAlignment.Far); 39 | printer.NewRow(); 40 | printer.PrintLine(); 41 | printer.NewRow(); 42 | printer.PrintText("**长白山大萝卜,跳楼吐血大甩卖,不甜不要钱**", width: 0.35f); 43 | printer.PrintText("6.00", width: 0.2f, offset: 0.35f); 44 | printer.PrintText("2.00", width: 0.2f, offset: 0.65F); 45 | printer.PrintText("12.00", stringAlignment: StringAlignment.Far); 46 | printer.NewRow(); 47 | printer.NewRow(); 48 | printer.PrintText("大螃蟹", width: 0.35f); 49 | printer.PrintText("6.000000000001", width: 0.2f, offset: 0.35f); 50 | printer.PrintText("1", width: 0.2f, offset: 0.65F); 51 | printer.PrintText("6.000000000001", offset: 0.8f, width: 0.2f); 52 | printer.NewRow(); 53 | printer.PrintLine(); 54 | printer.NewRow(); 55 | var bitmap = new Bitmap("qr.png"); 56 | printer.PrintImage(bitmap, StringAlignment.Center); 57 | printer.PrintText("关注超市->"); 58 | printer.NewRow(); 59 | printer.PrintLine(); 60 | printer.NewRow(); 61 | printer.PrintText("感谢光临,欢迎下次再来!", stringAlignment: StringAlignment.Center); 62 | printer.NewRow(); 63 | printer.Finish(); 64 | 65 | ``` 66 | 最终成品为下图 67 | 68 | ![](https://github.com/huanent/Huanent.Printer/blob/master/docs/img/bill.png) 69 | 70 | -------------------------------------------------------------------------------- /docs/1.1.md: -------------------------------------------------------------------------------- 1 | ## window下的80,58小票打印工具 2 | ### 使用方法 3 | ``` 4 | string printName = "Microsoft XPS Document Writer"; 5 | var print = PrinterFactory.GetPrinter(printName, PrintWidth.Print80mm); 6 | print.Print(new PrintUnit[] { 7 | new PrintUnit{ 8 | Alignment =StringAlignment.Center, 9 | Content ="测试文本", 10 | UnitWidth=100, 11 | Y =10, 12 | Font =new Font("黑体",12) 13 | } 14 | }); 15 | ``` 16 | ##### printName可使用PrintQueueHelper.GetPrintQueueName();获取电脑所有打印机列表选择 -------------------------------------------------------------------------------- /docs/img/bill.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/huanent/Huanent.Printer/eb966acc91ad8acd9ad013f2594b1063e36e727f/docs/img/bill.png --------------------------------------------------------------------------------