how to force a justified alignment on a pdfpcell - asp.net

to justify a text on a pdfpcell i know these commands
PdfPCell Example= new PdfPCell(new paragraph("Some text here",MyFont));//previous created font
Example.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
but i have a hmtl string , and i need to decode, and im doing like this
string txt = "long html string text"
PdfPCell Example2= new PdfPCell();
List<IElement> sr = HTMLWorker.ParseToList(new StringReader(txt ), style);//style was previous created
foreach (IElement element in sr)
{
Example2.AddElement(element);
}
Example2.SetLeading(0f, 1.5f);
Example2.Border = 0;
Example2.PaddingBottom = 20;
Table1.AddCell(Example2);//table declared previous
ok , until this point all is working and my pdfp document is fine but i need to ignore the alignment of the html string and force all text to be justified.
im tried this
Example2.HorizontalAlignment = Element.ALIGN_JUSTIFIED;
but doesnt work .

In the first example, you're working in "text mode" and the properties of the cell are respected.
As soon as you use AddElement (), you're working in "composite mode" and the properties of the cell are ignored (as documented in my book). Instead, the properties of the separate elements are used.
In answer to your question: you shouldn't define the alignment at the level of the cell, but at the level of the element.

Related

How do you insert HTML into a text node in AngleSharp?

I'm parsing a document with AngleSharp. I have a text node (NodeName: "#text") and I want to insert some HTML in it. I can certainly reset NodeValue to whatever I want, but it's still a text node, so all the brackets are escaped.
How do I take the string value of a text node, inject some HTML into it, then have a parsed DOM representation that that HTML take the place of the original text node?
I guess what you want is to replace a single text node by multiple nodes.
For instance <div>foo</div>, i.e.,
+ root
+ textnode
becomes
+ root
+ textnode (1)
+ element
+ textnode (2)
which could <div>f<b>o</b>o</div>. The simplest way I can think of is just replacing the node.
var source = #"<div>foo</div>";
var parser = new HtmlParser();
var document = parser.Parse(source);
var div = document.QuerySelector("div");
div.InnerHtml = div.InnerHtml.Replace("foo", "f<b>o</b>o");
Now you can argue that just replacing the text may not be what you want. You maybe have already elements that you want to insert. Therefore a better (yet more complex) way would be to split the text node and insert the remaining contents.
var source = #"<div>foo</div>";
var parser = new HtmlParser();
var document = parser.Parse(source);
var div = document.QuerySelector("div");
var text = div.TextContent;
div.RemoveChild(div.FirstChild); // assuming there is only one child
var bold = document.CreateElement("b");
bold.TextContent = text.Substring(1, 1); //o
div.Append(
document.CreateTextNode(text.Substring(0, 1)), //f
bold,
document.CreateTextNode(text.Substring(2, 1)));//o
Depending in your use-case there may be a more simple solution.

Unable to Create Image of Special Characters

I have written a imageHandler.ashx in c# which draw image of a text on a fly . Its takes a parameter string txt. The Handler is called from Asp.net page. At the time of calling, if text contains the character # or any special character .it Does not create the images of #,or any special character's given to it.
According to my R&D the problem is with a Graphics.DrawString() method some how its is unable to write special characters.so please tell me how to create a image of a special characters
Code I am using :
String signatureText = HttpUtility.HtmlDecode(signatureText);
System.Drawing.Text.PrivateFontCollection fontCollection = new System.Drawing.Text.PrivateFontCollection();
FontFamily ff1 = fontCollection.Families[0];
// Initialize graphics
RectangleF rectF = new RectangleF(0, 0, imgWidth, imgHeight);
Bitmap pic = new Bitmap((int) rectF.Width,imgHeight, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(pic);
//g.SmoothingMode = SmoothingMode.Default;
// g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
Font font1 = new Font(ff1, imgHeight, FontStyle.Regular, GraphicsUnit.Pixel);
float bestFontSize = BestFontSize(g, new Size(imgWidth, imgHeight), font1, signatureText);
Font font2 = new Font(ff1, bestFontSize, FontStyle.Regular, GraphicsUnit.Pixel);
// Set colors
Color fontColor = Color.Black;
Color rectColor = Color.White;
SolidBrush fgBrush = new SolidBrush(fontColor);
SolidBrush bgBrush = new SolidBrush(rectColor);
// Rectangle or ellipse?
g.FillRectangle(bgBrush, rectF);
// Set font direction & alignment
StringFormat format = new StringFormat();
//format.FormatFlags = StringFormatFlags.DirectionRightToLeft;
format.Alignment = StringAlignment.Center;
format.LineAlignment = StringAlignment.Center;
// Finally, draw the font
g.DrawString(signatureText, font2, fgBrush, rectF, format);
pic = (Bitmap) ImageUtility.ResizeImageWithAspectRatio(pic, imgWidth, imgHeight, true);
pic.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Any help is Appreciated Thanks in Advance
DrawString() don't have any problem while writing special characters if they are provided in font file (.ttf)
Problem can be in the following line
System.Drawing.Text.PrivateFontCollection fontCollection = new System.Drawing.Text.PrivateFontCollection();
Specially with the font you get in ff1
FontFamily ff1 = fontCollection.Families[0];
In your case it means Font ff1 don't have representation for some or all special characters.
Since I don't know how many font files you are getting from fontCollection.Families, if fontCollection.Families contain more than one font file try next one.
FontFamily ff1 = fontCollection.Families[1]; // and so on.
Or try to add font file with all special characters (if System.Drawing.Text.PrivateFontCollection is absolutely necessary ).
Alternatively you can use InstalledFontCollection instead of PrivateFontCollection
System.Drawing.Text.InstalledFontCollection fontCollection = new System.Drawing.Text.InstalledFontCollection();
Which will give you all installed fonts of the system on which application is running through its Families property InstalledFontCollection.
Easiest way is (if only one font is required, as you are using only one font in your provided code)
using constructor of FontFamily class with a string type parameter FontFamily.
Consider replacing this line
FontFamily ff1 = fontCollection.Families[0];
With this one
FontFamily fontFamily1 = new FontFamily("Arial");
Arial must be installed on the system where application is running.
Hope this will help.
DrawString doesn't have any problem with drawing special characters like # or copyright (I just tested it and produced the image below).
The problem might be from your URL parameter. Try encoding your text using Server.UrlEncode when passing the text, and then Server.UrlDecode before giving it to DrawString.

Asp.net itextsharp issue with row's text close to bottomline

I am working on generating pdf using itextsharp ,In my table i have multiple columns such as sr.ni. name .],quantity ,mrp ,price tax etc.
Name column has round about 40% of total width all values in other column comes right in middle of row but name's value is somehow close to bottom line ,Everything is same for all columns ie. style,font etc.
code
var cell=new PdfPcell();
cell = new PdfPCell(new Phrase(price, font));
cell.HorizontalAlignment = Element.ALIGN_CENTER;
cell.VerticleAlignMent= Element.ALIGN_CENTER;
producttable.AddCell(cell);
same code for all values
any solutions.
also tried
cell.AddElement(new Chunk(name, font));
cell.HorizontalAlignment = Element.ALIGN_LEFT;
cell.VerticalAlignment = Element.ALIGN_MIDDLE;
productsTable.AddCell(cell);
didnt work
You can assign vertical alignment to your particular cell as shown below:
pdfCell2.VerticalAlignment = Element.ALIGN_BOTTOM;
pdfCell3.VerticalAlignment = Element.ALIGN_MIDDLE;
pdfCell4.VerticalAlignment = Element.ALIGN_TOP;

get value from Linq and put it in the tex box

i have this code
MasterSoapClient sp = new MasterSoapClient();
MasterData[] lstMasterData = sp.GetActivityType(stid, null, 1);
grdEditActivityType.DataSource = lstMasterData;
grdEditActivityType.DataBind();
Session["opType"] = 2;
txtActivityCode.Text = lstMasterData.ToString();
txtActivityCode.DataBind();
here i called web service and put all data in this Gridview "grdEditActivityType "
and already workin
but there is column of lstMasterData i want to put it in the text box out of the grid
how i can do this ?
txtActivityCode.Text is a Property that you can use to assign a text value to the TextBox.
If you use txtActivity.Text = " some input "; Your textbox will contain the text " some input ".
You don't need to Bind txtActivityCode afterwards.
Having this clarified the next step is to create the string you want using to assign to the text box.
string s = "";
foreach( var masterData in lstMasterData )
{
s += masterData.SomeProperty; // s += masterData.ToString(); maybe, it depends on what do you want to put in the textbox;
}
txtActivityCode.Text = s;
And that's all.
I would suggest to start look more over ASP.NET tutorials to understand better how this framework works.

Removing last char from textfield without destroying textformat

i have a problem where i want to remove the last character from a textfield (including linebreaks) that has multiple textformats without removing the formats.
so far i have:
textfield.replaceText(textField.length-1,textField.length-1,'');
i guess this doesn't remove linebreaks, and is very slow, seems to destroy my textformats.
or:
textfield.text = textfield.text.slice(0,-1);
this is faster but removes all textformats as well.
It is a bit tedious, but you can use the htmlText-property of TextField, even though you are not formatting your text with StyleSheets: Flash will transform all your formatting information into HTML text internally, so even though you set textField.text, you can still get xml formatted text to work with:
textField.text = "A test.";
trace (textField.htmlText);
will actually return:
<P ALIGN="LEFT"><FONT FACE="Times Roman" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">A test.</FONT></P>
Text will always appear within <FONT> tags reflecting the changes you made using setTextFormat(). You can, therefore, iterate over the XML contained in this line, and remove only the last character in the last TextNode:
private function removeLastCharacter (textField:TextField) : void {
var xml:XML = new XML (textField.htmlText);
for ( var i : int = xml.children().length()-1; i >= 0; i-- ){
var node:XML = xml.children()[i];
if ( node.name() == "FONT") {
var tx:String = node.text()[0].toString();
node.setChildren (tx.substr (0, tx.length-1));
break;
}
}
textField.htmlText = xml;
trace (textField.text); // In the above example, output will be: "A test";
}
I hope I understand your problem correctly. If you keep your formatting in htmlText, I have one possible solution:
The idea is to keep the formatted text in an XML format, and modify the XML. XML will keep your formatting intact, you don't have to do string aerobatics to maintain them. The downsides are of course having to keep the formatting XML valid, and the extra variable.
Here's an example:
var tf:TextField = new TextField();
var t:XML = new XML("<html><p>lalala</p><font color='#ff0000'> lol</font></html>");
tf.htmlText = t.toXMLString();
t.font[0] = t.font[0].text().slice(0, -1);
tf.htmlText = t.toXMLString();
addChild(tf);

Resources