How to determine iTextSharp Basefont default style - asp.net

I am creating a PDF using iTextSharp.
Part of the process adds straightforward text to the PDF and another part creates an outline of the same text.
For the straighforward text aspect I am using the ShowTextAligned method in iTextSharp; which requires a Basefont and has no way of setting the style.
For the outline I am creating a GDI GraphicsPath to get points, which are then translated into PDF curves/lines etc. The AddString method requires a Drawing.FontStyle
Now my issue is that I need to set the style of AddString to the same one being used by the BaseFont. I.E if the Basefont is rendering Regular, i need to set the AddString fontstyle to regular
How do I determine what style is being used by the BaseFont in ShowTextAligned?
Further info:
I have also tried using ColumnText; which allows you to set an iTextSharp Font, along with it's style. Though using this method results in a font that is rendered with the approximate style; I.E a font usually in regular and set to bold gives a slightly malformed bold font.
EDIT
When I say i need to pass in the BaseFont to ShowTextAligned, What I mean to say is that in order to use this method you have to set the font and size first using SetFontAndSize of the PdfContentByte - it is this method that requires the BaseFont.
I know I must be missing something obvious.. I just can't see the wood for the trees :)

You can try defining your base font beforehand and use it throughout your PDF doc.
See iTextSharp - Working with Fonts
Sample code:
BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false);
Font times = new Font(bfTimes, 12, Font.ITALIC, Color.RED);
The above lines create a BaseFont object and uses the built-in constant values to set the font family and encoding. It also specifies false for embedding the font within the PDF document. A new Font object is created using the BaseFont object, and further setting the font size in points, the style and the color - again, using iTextSharp's constants for these values.
For more info on BaseFont, see its class definition.

You can use SetColorFill method. Like this
content.BeginText();
BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252,BaseFont.NOT_EMBEDDED);
content.SetFontAndSizebaseFont, 12);
BaseColor baseColor = new BaseColor(255, 0, 0);
content.SetColorFill(baseColor);
//or use predefined colors
content.SetColorFill(BaseColor.RED);
// use ShowTextAligned method
content.EndText();

Related

MigraDoc / PdfSharpCore - Requesting a fontface "New"

table.Rows[0].Cells[4].AddParagraph("0123456789ABCDEFGHIJKLMNOPQRSTUVWYXZ")
.Format.Font.ApplyFont(new Font("barcode", 36));
...
var documentRenderer = new DocumentRenderer(_document);
documentRenderer.PrepareDocument(); //<--- crash here
It comes through my custom font resolver ResolveTypeface("barcode", false, false) and GetFont("barcode.ttf") as expected.
It then comes through with ResolveTypeface("New", false, false). I don't know where "New" is coming from.
If I change new Font("barcode", 36) to new Font("Arial", 36) all is well and it never askes for a fontface of "New". I've tried another font, just in case something was messed up there. I've looked through the MigraDoc / PdfSharpCore source but see no reference to a hardcoded "New" string.
My best guess is that MigraDoc to PDF conversion looks up the FontFamily from the font that it used to resolve and then uses that font family (not the original one) when generating the PDF.
This means 2 things:
You better use the exact font family name that is in the font when resolving it.
You font needs to be correct / not corrupted. Somewhere it's grabbing "New" from the font I was using.

Why does my OBJ / MTL model material show up as black?

Why does my OBJ model have has no material and display as black?
I have an OBJ:
<a-obj-model id="gorilla" src="#gorilla-obj" mtl="#gorilla-mtl"></a-obj-model>
I can see the geometry, but the material shows up as black.
If you check your MTL, you might notice it is trying to use TGA or some other sort of textures that aren't plain images. In this case, you need to include additional three.js loaders.
You could try including all the necessary loaders like including https://github.com/mrdoob/three.js/blob/dev/examples/js/loaders/TGALoader.js and THREE.Loader.Handlers.add( /\.tga$/i, new THREE.TGALoader() );
However, it might be simplest to just batch convert all the TGAs to just use images like PNGs using a converter, and replace all instances of 'tga' with 'png'.

GraphicsMagick and FontMetrics

My aim is to create a text image and put it on top of other images. This is the command I use to create the text image:
String[] cp_img_arguments = {"convert",
"-size", width_size + "x",
"-background", background_colour,
"-pointsize", pointsize.toString(),
"-font", font_path,
"-fill",text_colour,
"caption:" + text,
destination};
In order to calculate the size of the image I need, I make use of FontsMetrics like this:
Font font = new Font(fontName, Font.PLAIN, pointsize);
Canvas canvas = new Canvas();
fontMetrics = canvas.getFontMetrics(font);
width_size = fontMetrics.stringWidth(toString());
Although I often generate images with the text truncated or multi-line, which means the width I calculate is not actually enough for the text.
The font comes from a .ttf file. I believe this miss-calculation happens because of the density/DPI parameter in GraphicsMagick. I am not currently using it (default is 55 I believe), but I wonder how that affects the actual font size, compared to what FontMetrics calculate.
Does anyone have experience generating images with text using gm? Any ideas how I can make the image dimension calculation more accurate?

Use text instead of marker in nokia HERE map

I want to use text instead of StandardMarker in Nokia HERE map.
I tried using TextMarker but didn't work. May be I used incorrectly. Can you suggest me that how to do this?
TextMarker is not a built-in class of the API, rather the example in the API playground (http://developer.here.com/apiexplorer/examples/public/api-for-js/markers/extending-marker-to-text-marker.html) introduces this class by extending the Marker class. Take a look at the source code of the API explorer example.
The general idea is that you will need to produce a graphics (or graphics image) for the marker icon to use. There are multiple ways to do this but creating an SVG markup string with text is probably the easiest way to do it (as shown in the example code).
What the code in the example does it takes some variables and replaces parts of the SVG template string with the values.
var iconSVG = '<svg width="__WIDTH__" height="__HEIGHT__" xmlns="http://www.w3.org/2000/svg">' +
'<rect x="__X__" y="__Y__" rx="5" ry="5" width="__RECT_WIDTH__" height="__RECT_HEIGHT__" ' +
'style="fill:__BRUSH__;stroke:__STROKE__;stroke-width:__STROKEWIDTH__;"/>' +
'<text x="__OFFSETX__" y="__OFFSETY__" fill="__TEXT_PEN__" style="font-weight:normal; ' +
'font-family:__FONTFAMILY__; font-size:__FONTSIZE__;" textContent="__TEXT__">__TEXT__</text>' +
'</svg>',
From the SVG markup string you can produce a graphics image:
icon = new nokia.maps.gfx.GraphicsImage(that.svgParser.parseSvg(iconSVG));
And this graphics image can be used as a marker icon.
marker.set("icon", icon)
The most important thing to note is that you need to figure out the size of your text (width and height) manually.

Strange problem about conversion between GDI+ to GDI: Bitmap and HBitmap

I want to convert gdi+ Bitmap into gdi object HBitmap.
I am using the following method:
Bitmap* img = new Bitmap(XXX);
// lots of codes...
HBITMAP temp;
Color color;
img->GetHBITMAP(color, &temp);
the img object is drawing on a dialog.
when this part of method is called, strange thing happens!
the img displaying in the window changed!
It become a bit clearer or sharper.
My question is what happens?
The bitmap pixel format may be the reason. Do you specify it explicitly in the Bitmap constructor?
Gdiplus::Bitmap bmp(WIDTH, HEIGHT, PixelFormat24bppRGB);
Try making sure that all the pixel formats you use are the same.
Another reason may be the differences in Gdiplus::Graphics interpolation modes in your code. That attribute determines how the images are resized, how the lines are drawn, etc.
m_pViewPortImage = new Gdiplus::Bitmap(
observedWidth,
observedHeight,
PixelFormat24bppRGB
);
Gdiplus::Graphics gr(m_pViewPortImage);
gr.SetInterpolationMode(Gdiplus::InterpolationModeHighQualityBicubic);

Resources