PrintableAreaHeight & PrintableAreaWidth - c#

How to adjust System.Windows.Controls.PrintDialog.PrintableAreaHeight & System.Windows.Controls.PrintDialog.PrintableAreaWidth since they are read only?

PrintableAreaHeight and PrintableAreaWidth is calculated based on the PrintDialog.PrintTicket that is used. In other words they can't be adjusted because the printer that is printing the document specifies what values to use. If you really want to change the print area, which can cause the printer to print ink on it rollers if the size is larger than what can actually be printed, you could do:
var pd = new PrintDialog();
if(pd.ShowDialog() == true)
{
pd.PrintTicket.PageMediaSize = new PageMediaSize(newWidth, newHeight);
pd.PrintDocument(...);
}

In WPF you can use the PrintTicket Class of System.Windows.Controls.PrintDialog.
This class has a lot of properties to change the aspect of the page.
In Windows Forms you can use YourPrintDialog.PrinterSettings.DefaultPageSettings.PaperSize.
Here is a link to MSDN: PrinterSettings.PaperSizes Property

Related

C# Zxing Encode 1d EAN8

I want to generate a 1D EAN8 barcode using c# Zxing. I have only been able to find code examples and documentation for generating 2D QR-code
var writer = new BarcodeWriter
{
Format = BarcodeFormat.QR_CODE,
Options = new QrCodeEncodingOptions
{
Height = height,
Width = width
}
};
return writer.Write(textForEncoding);
which I can run and works fine, but there is no "1DCodeEncodingOptions" or similarly named function. I tried
var writer = new BarcodeWriter
{
Format = BarcodeFormat.EAN_8
};
return writer.Write("1234567");
but it throughs an index error.
edit: I have the syntax correct now but it is not producing a proper barcode because I do not know the size it expects, and there seems to be no default.
using ZXing;
Using ZXing.OneD
var writer = new BarcodeWriter
{
Format = BarcodeFormat.EAN_8,
Options = new ZXing.Common.EncodingOptions
{
Height = 100,
Width = 300
}
};
return writer.Write("12345678");
12345678 is not a valid EAN8 barcode. The check digit for 1234567 is 0. See EAN 8 : How to calculate checksum digit? for how to calculate checksums.
ZXing won't stop you creating invalid barcodes (at least in the version 0.11 I'm using, although the current source on Codeplex looks like it does), but they won't scan. The scanner uses the checksum to ensure that it has read the data correctly.
If you intend to use an EAN8 in commerce, you will need to get a GTIN-8 from your national GS1 Member Organization. If you only intend to sell them in your store, you should use one of the restricted distribution prefixes.
If you don't need to put your products in the retail supply chain, I'd recommend a different barcode format.
Interleaved 2 of 5 (BarcodeFormat.ITF) is very compact but can only contain digits; it doesn't have any self-checking and there's a design flaw that allows the barcode to be misread from some angles. It's recommended that you put black bars (called Bearer Bars) across the top and bottom of the barcode. I can't see an option for this in ZXing.
The next most compact format is Code 128 (BarcodeFormat.CODE_128), using Code Set C. This encodes two digits in one module (one block of six bars and spaces). The format is self-checking (there is always a check character, which is stripped off by the scanner). Some scanners don't handle Code Set C properly. To force Code Set B, use Code128EncodingOptions in place of Common.EncodingOptions and set ForceCodesetB to true.

Using Imageresizer Watermark plugin to write text: Centered, width and line feeds

I'm trying to use the watermark plugin to write text on images for my project. Right now I'm trying to find out how to set a "width" for a writing box so I can get automatic line returns. Is there a way to do this with the watermark plugin?
Also I'm trying to see if I can get a "text-align: center" effect when I'm writing my text (possibliy in relation to that set width), how could I get that setup?
I'm thinking that the alternative to this would be to have code driven line returns and centering, but this would mean that I would have to count the width of my characters and this seems like a world of pain hehe
Here is a code sample that shows what I'm doing (this currently works):
var c = Config.Current;
var wp = c.Plugins.Get<WatermarkPlugin>();
var t = new TextLayer();
t.Text = panty.Message;
t.TextColor = (System.Drawing.Color) color;
t.Font = fonts[myFunObject.Font];
t.FontSize = fontSize[myFunObject.LogoPosition];
t.Left = new DistanceUnit(5, DistanceUnit.Units.Pixels);
t.Top = new DistanceUnit(5, DistanceUnit.Units.Pixels);
wp.NamedWatermarks["myFunObjectMessage"] = new Layer[] { t };
EDIT: I also have to mention that the text I'm writing is user submitted so it's different everytime. If you want a similar case, think about thos funny cat images with funny text captions on them. This project is quite similar to that. (Minus the cats)
Thanks for the help!
Basically, System.Drawing (and therefore the current version of Watermark) are very primitive about line wrapping.
As you mentioned, you can do hacky stuff with character counting and separate MeasureString calls with loops, but the results are only barely acceptable.
You may try to fork the Watermark source code and hack support for your use case. I don't see a way to improve Watermark in a generic way without replacing the underlying graphics engine first (which may happen anyway).
System.Drawing has unsurpassed image resampling quality. Text wrapping, though, it kind of stinks at.

Image comparison to Properties.Resources.image

Could you tell me why is the following condition false?
List<Image> SelectedImages = new List<Image>();
SelectedImages.Add(Properties.Resources.my_image);
if (SelectedImages[0] == Properties.Resources.my_image) // false
{
...
}
Even if I write:
SelectedImages[0] = Properties.Resources.my_image;
if (SelectedImages[0] == Properties.Resources.my_image) // false
{
...
}
How can I make this comparison work?
They're not equal because Properties.Resources makes a copy of the image and returns that copy back to your code. Everytime you get that image, you're effectively creating an entirely new Image object. It makes a copy because resources should ordinarily behave like constants. You wouldn't want your code to change the picture if you (for example) modified the picture in some way.
So what happens in your code? You end up comparing two different image objects that happen to contain the same data. .NET sees that you have two different objects and returns false. It doesn't do a "deep" comparision to see if the objects have the same data/state.
Here's a link to an article that explains what is going on at a high level: https://navaneethkn.wordpress.com/2009/10/15/understanding-equality-and-object-comparison-in-net-framework/
A quick way to workaround your problem would be to compare the Resource image and your SelectedImage Image pixel by pixel to see if they are "equal". This would obviously perform poorly. Another, more efficient way might be to build a dictionary and use a key to track where the image originally came from.
Just use the Tag property in the Image object. You can store anything you want in there. Just set up your image like this...
Image img_dbActive = MyApp.Properties.Resources.tray_icon_database_active;
img_dbActive.Tag = "tray_icon_database_active";
Image img_dbIdle = MyApp.Properties.Resources.tray_icon_database_idle;
img_dbIdle.Tag = "tray_icon_database_idle";
ToolStripStatusLabel lbl = new ToolStripStatusLabel("Database is doing something...", img_dbActive);
lbl.Image.Tag = "tray_icon_database_active";
if (lbl.Image.Tag == img_dbActive.Tag)
{
//Images match
}

Get windows explorer font size of current user using C#

Is this possible with C# or do I need to do P/Invoke? I was thinking it's an easy way to determine which font size to set for my ListView.
Check under class SystemFonts. I don't think you need more than the default namespaces for this.
for example:
string name = SystemFonts.IconTitleFont.FontFamily.Name;
float size = SystemFonts.IconTitleFont.Size;

Get document vector count in a pdf document?

I have a problem with some user provided pdf documents. They are created from 3d packages and are basically a HUGE list of vector lines that take and age to render (over 60 secs).
How can I generate a report on the number of vector lines present in a pdf document using iTextSharp (5.0.5)?
I can get text and image data but can't see where to get a handle on vector. They don't seems to be represented as an image.
iText[Sharp]'s parser package doesn't yet handle lineTo or curveTo commands. It's a goal, but not one that's been important enough to implement as yet. Other Things are getting attention at the moment.
If you're feeling adventurous, you should check out PdfContentStreamProcessor. In a private function populateOperators, there's a long list of commands that are currently handled (in one fashion or another).
You'd need to write similar command classes for all the line art commands (moveTo, lineTo, rect, stroke, fill, clip), and expose them in some way.
Actually, if all you want to do is COUNT the number of paths, you could just implement stroke and fill to increment some static integer[s], then check them after parsing. Should be fairly simple (I'm writing in Java, but it's easy enough to translate):
private static class CountOps implements ContentOperator {
public static int operationCount = 0;
public void invoke(PdfContentStreamProcessor processor, PdfLiteral operator, ArrayList<PdfObject> operands) {
++operationCount;
}
}
Ah! registerContentOperator is a public function. You don't need to change iText's source at all:
PdfContentStreamProcessor proc = new PdfContentStreamProcessor(null);
CountOps counter = new CountOps();
proc.registerContentOperator("S", countOps); // stroke the path
proc.registerContentOperator("s", countOps); // close & stroke
proc.registerContentOperator("F", countOps); // fill, backward compat
proc.registerContentOperator("f", countOps); // fill
proc.registerContentOperator("f*", countOps); // fill with event-odd winding rule
proc.registerContentOperator("B", countOps); // fill & stroke
proc.registerContentOperator("B*", countOps); // fill & stroke with even-odd
proc.registerContentOperator("b", countOps); // close, fill, & stroke
proc.registerContentOperator("b*", countOps); // close, fill, & stroke with even-odd
proc.processContent( contentBytes, pageResourceDict );
int totalStrokesAndFills = CountOps.operationCount; // note that stroke&fill operators will be counted once, not twice.
Something like that. Only a null RenderListener will cause a null pointer exception if you run into any text or images. You could whip up a no-op listener yourself or use one of the existing ones and ignore its output.
PS: iTextSharp 5.0.6 should be released any day now if it isn't out already.
There is no specific Vector image. Normally it is just added to the content stream which is essentially a Vector data stream for drawing the whole page.
There is a blog article which you might find useful for understanding this at http://www.jpedal.org/PDFblog/2010/11/grow-your-own-pdf-file-%E2%80%93-part-5-path-objects/

Categories