|
This is my first time trying to to print directly to a printer using the PrintDialog and PrintDocument. I thought it would be like creating a PDF, where I just compose and create a file and print, but it looks like I'm forced to use an event handler to compose my print document using Drawing.
I was on the internet, and the examples where very simple and basic, but my document is going to be huge, and used many times in my c# winforms .Net Core 7 app. So I used ChatGPT to help me design a class, that I can call as my event handler, but I'm missing something in my class, or got it wrong on how all of this works.
On my Print Dialog Form, when I hit the print button, my code example ...
The printDefault.Question is what I'm trying to call, where Question is where I'm stumped ..
if (result == DialogResult.OK)
{
this.PrintObject.PrintSettings.DefaultPrinter = printDialog.PrinterSettings.PrinterName;
if (this.PrintObject.PrintSettings.DefaultDocument != null)
{
var printDefault = new PrintDefault
{
Bol = this.PrintBol
};
printDocument.PrintPage += new PrintPageEventHandler(printDefault.Question);
printDocument.Print();
this.PrintComplete = true;
}
}
And my class ....
I figured that my class is an extension of PrintDocument, but I'm not sure. One of the dumb requirements is that I need the MongoDb collection document to generate the BOL form to print, because that has all my data I need to populate the document I'm trying to create. I'm not really sure if my design is sound or not, and may have to think of an alternative. But I feel like I'm really close, just missing something that I didn't think of.
namespace SimpleBol.Classes.DirectPrint
{
public class PrintDefault : PrintDocument
{
public BILLOFLADINGS Bol { get; set; } = null!;
public void PrintDocument_PrintPage(object sender, PrintPageEventArgs e)
{
Font fontTitleBold = new Font("Arial", 16, FontStyle.Bold);
Font fontTitleRegular = new Font("Arial", 16, FontStyle.Regular);
Font fontLargeBold = new Font("Arial", 12, FontStyle.Bold);
Font fontLargeRegular = new Font("Arial", 12, FontStyle.Regular);
Font fontMedium = new Font("Arial", 10);
Font fontSmall = new Font("Arial", 8);
Font fontTiny = new Font("Arial", 6, FontStyle.Italic);
SolidBrush brush = new SolidBrush(System.Drawing.Color.Black);
RectangleF printArea = e.PageSettings.PrintableArea;
if (e.Graphics != null)
{
SizeF textSize = e.Graphics.MeasureString("BILL OF LADING", fontTitleBold);
float x = printArea.Left + (printArea.Width - textSize.Width) / 2;
float y = printArea.Top + (printArea.Height - textSize.Height) / 2;
e.Graphics.DrawString("Date: ", fontMedium, brush, x, y);
if (Bol != null)
{
string date = Bol.BolDate.ToString("dd-MM-yyyy");
e.Graphics.DrawString("Date: " + date, fontMedium, brush, x, y);
e.Graphics.DrawString("Shipper: " + Bol.ShipperName, fontMedium, brush, x, y + 20);
e.Graphics.DrawString("Vendor: " + Bol.ShipFromVendor, fontMedium, brush, x, y + 40);
}
}
}
protected override void OnPrintPage(PrintPageEventArgs e)
{
base.OnPrintPage(e);
PrintDocument_PrintPage(this, e);
}
}
}
On another note; since this is my first time, what's the best way to test during document composition. I threw out my cheap printer with cheap toner, and can't see having to print a sheet a paper on every test. Should I build a temp print preview first, and get it close enough there, and then use paper to fine tune?
Now I'm wondering if I should change my print dialog to show the preview first, and then hit the print button. The downside to being a rogue programmer is that I have no team to ask for help, and any help or opinions would be appreciated.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I can't help you with the print class, but I do have an idea about the testing.
Do you have MS Office installed on your development machine? If so, I believe that also installs a "Print to PDF" printer driver. You could use that to print to a PDF file instead of the printer.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I didn't think of that! I have CutePDF that I print all my orders with, before I email them.
Thanks!
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
On top of that, ChatGPT should not be used to write code. It sucks at writing anything beyond "Hello World" and WILL lead you down incorrect paths.
|
|
|
|
|
I'm beginning to realize that now.
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
I'd like to put all of my custom classes into a .NET Framework DLL so that I can share them among many different executables.
If I install a NuGet package to the DLL, are the classes in that package supposed to be available to any EXE that holds a reference to the DLL?
Or NuGet packages can't be shared that way, meaning the package must be installed to the EXE, not the shared library?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
In my experience, NuGet packages are on a project basis (dll; exe), though you can manage them at the solution and project level.
You can "share" a dll's Nuget package via the dll as an "interface"; otherwise, I know of no way to surface "name spaces" from a dll other than the one it "owns".
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
Thanks for the response, Gerry. Can you elaborate on what you mean by sharing the package as an interface? Do you mean I'd have to write code (interfaces) that specifically expose the types in the NuGet package?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Yes. A proxy.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
When you use or deploy the .DLL to another .EXE project, you have to ship the .DLL's in the NuGet package with your .DLL. Your .DLL won't work without them.
As for being available to the .EXE, yes, the NuGet .DLL's will also be available to the executable project, just like they were for your .DLL project.
|
|
|
|
|
OK, thanks Dave.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
The classes and functionality from the NuGet package won't be automatically available to the executables (EXE files) that reference this DLL. Each executable project that uses the DLL must also have its own references to the required NuGet packages.
|
|
|
|
|
I have a small game executable (not my game) that, when I close it down, opens my default browser with the game's maker's web site. Presumably it is just opening an http and Windows 11 handles it from there. Is there a way to prevent this from happening? —either to enclose the app's environment from the outside or to create an isolated environment that then executes the game (or some other way)? It would seem a little weird to me if I couldn't prevent that from happening to a specific Windows process. Windows provides a vast array of services to apps, but in terms of security, it might make sense to be able to prohibit certain operations for a process at the OS level. Does Windows provide any control for this?
|
|
|
|
|
Does Windows provide any control for this? No, not directly. I don't know of any O/S that does.
If the app is just shelling out to a URL, you really cannot stop an app from doing that. The shell will take commands from anything and there's nothing you can do to stop it.
Firewall rules will not help because the URL is being launched by the shell and the browser is the registered handler for HTTP/HTTPS. Your game isn't involved in handling the URL itself so there's no process-based firewall rule that will prevent the launch of it.
Writing up a rule to prevent access to the URL or IP will still bring up a browser window, but with a failure message.
|
|
|
|
|
|
I know the way I stopped it from happening with the game that I had.
I paid for the game, rather than just continuing to use the free demo.
|
|
|
|
|
Are there any .onnx Modules that are included already or plans to have them included?
The only ones I'm interested in are a .pt file format and the YOLOv5 6.2 doesn't work very well at all. (Probably because I don't have a GPU)
|
|
|
|
|
What does your question have to do with this forum? If you're asking about something you saw in an article, you should ask in the article itself.
|
|
|
|
|
It's related to the YOLOv5 .NET model.
|
|
|
|
|
|
Thank you, I'll post it there instead.
|
|
|
|
|
Hello guys,
I have an issue related to dependency injection in the Program.cs of a .Net 7 application.
In this case it's an API, but that does not really matter.
Before calling the line:
var app = builder.Build();
I am adding a few singletons normally with :
builder.Services.AddSingleton<IMyConfig, MyConfig>();
builder.Services.AddSingleton<IMyHttpClient, MyHttpClient>();
My issue is that the HTTP client singleton requires info from the config singleton, and it's not a simple case of injecting the former into the latter.
We can't use appsettings.json to manage our configuration, that would be too easy, and because the second one is an HTTP client, I have this code :
builder.Services.AddSingleton<IMyHttpClient, MyHttpClient>();
builder.Services.AddHttpClient<IMyHttpClient, MyHttpClient>(client =>
{
client.BaseAddress = new Uri(myConfig.myURI);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
}).ConfigurePrimaryHttpMessageHandler(() =>
{
return new HttpClientHandler()
{
UseDefaultCredentials = true
};
});
.. all this still before I call builder.Build();
I have already googled and chatGPT'd the problem, but the solutions I get are for .Net 6 at best, and do not apply.
My current solution is to use builder.Services.BuildServiceProvider().GetService<imyconfig>() to get an instance of myConfig, but then the builder is called twice, so this instance is lost, and after the builder.Build(), I have to read my config again.
Technically, this works, but it's ugly and triggers my OCD.
If there's any DI god out there, what am I missing ?
Thanks in advance
Fred
modified 28-Apr-23 9:43am.
|
|
|
|
|
If you're "stuck" with a given framework, count yourself lucky if you get something to work. When it does, move on instead of thinking about "ugly"; it's a waste of time.
"Before entering on an understanding, I have meditated for a long time, and have foreseen what might happen. It is not genius which reveals to me suddenly, secretly, what I have to say or to do in a circumstance unexpected by other people; it is reflection, it is meditation." - Napoleon I
|
|
|
|
|
If you want a singleton then you have to inject it into a thread flow that does not depend on requests but rather something like the start up method.
But as with the other reply, the point it to deliver something. And you need to have a realistic view about what might actually need to be easily replaced in the future. So does it actually need to be injected. Configuration might be it but you can do that with a standard factory idiom also.
|
|
|
|
|
|