This is the code which I am trying to make a bold and underlined text.
Font header = new Font(Font.FontFamily.TIMES_ROMAN, 15f, Font.BOLD, BaseColor.BLACK);
header.SetStyle(Font.UNDERLINE);
But all I get is underline and not bold. Is there any way I can get both underline and bold font ?
Try the following:
Font header = new Font(Font.FontFamily.TIMES_ROMAN, 15f, Font.BOLD | Font.UNDERLINE, BaseColor.BLACK);
As an alternative to using the Font to underline text, you can also use the setUnderline() method that is available for the Chunk class. When you use the solution explained in the answer by Joachim Isaksson, you can choose the line width of the line, nor the distance from the baseline of the text. The setUnderline() method gives you all that freedom.
Read my answer to the question How to strike through text using iText? for more info.
Take a look at these examples:
Chunk chunk1 = new Chunk("0123456789");
chunk1.SetUnderline(2, -3);
document.Add(new Phrase(chunk1));
Chunk chunk2 = new Chunk("0123456789");
chunk2.SetUnderline(2, 3);
document.Add(new Phrase(chunk2));
In both cases, the line that is drawn will be 2 user units thick instead of the default 1 user unit. In chunk1 the line will be drawn 3 user units under the text (this is underline functionality). In chunk2, the line will be drawn above the baseline (this is strikethrough functionality).
I have used like this:
Dim font8Underline As Font = FontFactory.GetFont("ARIAL", 8, Font.BOLD)
font8Underline.SetStyle(Font.UNDERLINE)
Bold
Font1.SetStyle(1)
Italic
Font1.SetStyle(2)
Bold and Italic
Font1.SetStyle(3)
Underline
Font1.SetStyle(4)
Bold and Italic and Underline
Font1.SetStyle(7)
1+2+4=7
Related
I need to change the DrawString paragraph into different file formats like (Bold, Underline, color, etc).
Eg: Hi, This is RAM,
gfx.DrawString("Hi, This is RAM", fontRegular, XBrushes.DarkSlateGray, 0, 30);
You can use DrawString with regular, bold, italic, and bold italic fonts.
There are no paragraphs with PDFsharp. Just make the appropriate calls to DrawString with the right fonts and the correct positions. With your example, you need three calls to DrawString. Use MeasureString to find how much space a string needs so you can draw the next string at the correct position.
MigraDoc uses PDFsharp to create PDF files. MigraDoc has paragraphs and using AddFormattedText you can mix several fonts and font styles in one paragraph. So maybe consider using MigraDoc.
Information about MigraDoc on the PDFsharp web site:
http://pdfsharp.net/Overview.ashx
I would like to ask what units does column.width return when using Openxml? (IS it in EMU, metres, cm or dpi and how do i get its DPI equivalent?
From SpreadsheetOpenXmlFromScratch:
Note that the width of the digits depends on the font and the font size used. I'll be using the MeasureString() function of the Graphics class to measure pixel width.
Everything Vincent does is based on the font type and size. You will have to retrieve the font type and font size to run through the calculations.
EDIT: pp 40-50
No, it is using the font measurements, not the column text. Another excerpt:
Note that the width of the digits depends on the font and the font size used. I'll be using the MeasureString() function of the Graphics class to measure pixel width.
System.Drawing.Font drawfont = new System.Drawing.Font("Calibri", 11, FontStyle.Regular);
// I just need a Graphics object. Any reasonable bitmap size will do.
Graphics g = Graphics.FromImage(new Bitmap(256, 256));
Dim drawfont As New System.Drawing.Font("Calibri", 11, FontStyle.Regular)
' I just need a Graphics object. Any reasonable bitmap size will do.
Dim g As Graphics = Graphics.FromImage(New Bitmap(256, 256))
The font used should be the same as that used in your stylesheet. Otherwise you would be measuring for one font or font size, but using another font or font size to render your actual text in the Excel cell. I know it's obvious, but it's important to state here.
Specifically, it should be the font used in the Normal style. That word is capitalised for a reason. It refers to the cell style named Normal (together with "Good", "Bad" and "Neutral"). Get your friendly neighbourhood Excel geek to help you find the cell style option in Excel.
Some of his other methods do require text when he actually measures the width of a zero flanked by two underscores, then subtracts the width of the underscores to get a more accurate width, but this just requires the font name and style.
Now the MeasureString() function has a tiny problem. This is from the official documentation:
The MeasureString method is designed for use with individual strings and includes a small amount of extra space before and after the string to allow for overhanging glyphs.
What this means is that the following code will not exactly give you the pixel width of "0":
fWidthOfZero = (double)g.MeasureString("0", drawfont).Width;
fWidthOfZero = Convert.ToDouble(g.MeasureString("0", drawfont).Width)
Give it a try.
I have a template.docx, doc1.docx, doc2.docx. I am trying to merge doc1.docx and doc2.docx into template.docx using altChunk. The 3 documents have different font size and font styles. I want the resulting document base on the style in template.docx. Any ideas how to achieve that?
For example:
all the heading1 font size is 24, font color is blue
List paragraph font size is 12, font family is Times New Roman
I'm struggling to add background color on certain part of sentence(text), word with MIGRADOC and PDFSHARP. Any suggestions how to do ?
par.addText(coloredText);
This is how I tried to add text that should be colored but there is no way to setup for color, except for paragraph (paragraph.shading.color = Color.red) but I need part of the text in the paragraph.
Thanks
With FormattedText it is possible to determine the color of the text (unfortunately not the background)
With the piece of code below it is possible to do this:
Paragraph par = section.AddParagraph();
par.Format.Alignment = ParagraphAlignment.Left;
// Use formatted text to specify the color
FormattedText ftext = new FormattedText();
ftext.AddText("Coloured Text");
ftext.Color = Colors.Red;
par.AddText("normal Text");
par.AddSpace(1);
par.Add(ftext);
par.AddSpace(1);
par.AddText("rest of the normal Text");
Let's say I have 200 pixels of space and I want to draw two strings into it:
- the first string should be left justified
- right second string should be right justified
- But not overlap if they do not both fit (then do whatever my String.Trimming options say)
Am I going to have to measure each and draw this manually, or does DrawString have some way to support what I'm trying to do without me reinventing the wheel?
Imagine that \l and \r were escapes that did this, then I could say
graphics.Drawstring("\lfirst\rsecond", ...);
and I'd wind up with something like
"first second"
At least that's what I'd like to have happen (I know \l and \r do not exist). Is there a way?
I've ignored your flags and instead I'm showing you (roughly) how you can align text. It's easy enough to pick out your text, split it up and draw it as two separate strings!
string text2 = "Use TextFormatFlags and Rectangle objects to"
+ " align text in a rectangle.";
using (Font font2 = new Font("Arial", 12, FontStyle.Bold, GraphicsUnit.Point))
{
Rectangle rect2 = new Rectangle(150, 10, 130, 140);
// Create a TextFormatFlags with word wrapping, horizontal center and
// vertical center specified.
TextFormatFlags flags = TextFormatFlags.HorizontalLeft |
TextFormatFlags.VerticalCenter | TextFormatFlags.WordBreak;
// Draw the text and the surrounding rectangle.
TextRenderer.DrawText(e.Graphics, text2, font2, rect2, Color.Blue, flags);
e.Graphics.DrawRectangle(Pens.Black, rect2);
}
In the long run here's what I wound up doing:
Call MeasureString on left string
Call MeasureString on right string
Draw left string, left-justified
If the sum of the width of the two strings is less than width of the available space, draw the right string right-justified.
Pretty straightforward, though I was hoping there was something in the framework that would have done the work for me.