Making text bold using Stream Writer in C# - c#

How to make text bold using stream writer,here is my code:
string path = Application.StartupPath + "\\WZ.PNR";
StreamWriter writer = new StreamWriter(path);
textPrint.ToText(writer, Width, FSection, FAlign, DSection, DAlign, Format);
writer.WriteLine();
writer.Close();
I am writing some text and i need to make some text BOLD, How to do it?
Thanks

StreamWriter is for writing plain text. You need markup of some kind to make text bold. Options include:
RTF
HTML
TeX
How are you expecting to open the generated file? The application will need to understand whatever file format you choose. There's no general concept of "a bold character" - the letter E is the letter E; if you want it styled that styling data is separate.
Given your file extension, are you trying to create a PeerNet Label Designer file? If so, you'll need to find out the appropriate file format - I don't know whether it's a text format, binary etc.

At first you should create your path in that way:
string path = Path.Combine(Application.StartupPath, "WZ.PNR");
After this little improvement let's take a look at your pnr file...
So you open this file and like to write some bold text to it?
So do you have some kind of program, that is already able to create and view such a .pnr file?
I think you'll have, or from where do you know, that it is possible to have bold text within such a file?
In case you have this program to generate such a file with bold text. Just make a new file, enter three words: "one two three" and make the 'two' bold. Save this file and open it with a good plain text editor (e.g. notepad++) or a good Hex-Editor and try to find out how this will be accomplished.
For example, open WordPad create a new rtf-File and insert the above example. After saving it and re-opening in a plain text editor you'll get:
{\rtf1\ansi\ansicpg1252\deff0\deflang1031{\fonttbl{\f0\fswiss\fcharset0 Arial;}}
\viewkind4\uc1\pard\f0\fs20 one \b two\b0 three\par
}
And as you can see, the bold is been made by using '\b ' to enable and '\b0 ' to disable the bold text. Also there is plenty of other informations like used font, charset, etc.
That's called reverse-engineering if you don't have any specs. ;-)

The extension PNR suggests some sort of Printer file. That means you'll have to look up the escape codes for that particular kind of printer.

Related

Can not read turkish characters from text file to string array

I am trying to do some kind of sentence processing in turkish, and I am using text file for database. But I can not read turkish characters from text file, because of that I can not process the data correctly.
string[] Tempdatabase = File.ReadAllLines(#"C:\Users\dialogs.txt");
textBox1.Text = Tempdatabase[5];
Output:
It's probably an encoding issue. Try using one of the Turkish code page identifiers.
var Tempdatabase =
File.ReadAllLines(#"C:\Users\dialogs.txt", Encoding.GetEncoding("iso-8859-9"));
You can fiddle around using Encoding as much as you like. This might eventually yield the expected result, but bear in mind that this may not work with other files.
Usually, C# processes strings and files using Unicode by default. So unless you really need something else, you should try this instead:
Open your text file in notepad (or any other program) and save it as an UTF-8 file. Then, you should get the expected results without any modifications in your code. This is because C# reads the file using the encoding you saved it with. This is default behavior, which should be preferred.
When you save your text file as UTF-8, then C# will interpret it as such.
This also applies to .html files inside Visual Studio, if you notice that they are displayed incorrectly (parsed with ASCII)
The file contains the text in a specific Turkish character set, not Unicode. If you don't specify any other behaviour, .net will assume Unicode text when reading text from a text file. You have two possible solutions:
Either change the text file to use Unicode (for example utf8) using an external text editor.
Or specify a specific character set to read for example:
string[] Tempdatabase = File.ReadAllLines(#"C:\Users\dialogs.txt", Encoding.Default);
This will use the local character set of the Windows system.
string[] Tempdatabase = File.ReadAllLines(#"C:\Users\dialogs.txt", Encoding.GetEncoding("Windows-1254");
This will use the Turkish character set defined by Microsoft.

Rtf to WordML Convert in C#

I have a windows application to generate report.
It has templates in RTF as "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}}\r\n\\viewkind4\\uc1\\pard\\fs20\\tab\\tab\\tab\\tab af\\par\r\n}\r\n", which is written to word doc file. then the word is Saved-As XML and close. Then, tags like (say) are extracted and some new
The problem here is Word, which is used as converter in the process and it consumes valuable time in Loop, where it opens word instance, save, close, delete.
Please correct any mistake if i have made and help me with an alternative to convert to WordML .
Use Aspose .Words
//your rtf string
string rtfStrx = "{\\rtf1\\ansi\\ansicpg1252\\deff0\\deflang2057{\\fonttbl{\\f0\\fnil\\fcharset0 Arial;}}\r\n\\viewkind4\\uc1\\pard\\fs20\\tab\\tab\\tab\\tab af\\par\r\n}\r\n"
//convert string to bytes for memory stream
byte[] rtfBytex = Encoding.UTF8.GetBytes(rtfStrx);
MemoryStream rtfStreamx = new MemoryStream(rtfBytex);
Document rtfDocx = new Document(rtfStreamx);
rtfDocx.Save(#"C:\Temp.xml", SaveFormat.WordML);
This saves your RTF text in new document as WordML. I cannot say about time it will take in loop. But it will surely have much less time then MS Word being physically opened and closed.
Unless I am missing something, I assume that you are trying to create Office XML file from RTF template? I think you can use Open XML SDK for creation of the xml file. Specifically, DocumentReflector that comes with that SDK seems to a good fit for that. See this example. Also, there is a http://www.codeguru.com/cpp/controls/richedit/conversions/article.php/c5377/ which shows how to convert from RTF to HTML that might guide you.
use wpf richtextbox. Rtf => xaml. Since xaml is xml_ use xslt or linq to convert it to your desired xml structure

Read a RTF File and Remove the dynamic text

My RTF File contains a text in the beginning like below:
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Trebuchet MS;}{\f1\fswiss\fprq2\fcharset0 Verdana;}{\f2\fnil\fcharset0 Tahoma;}{\f3\fnil\fcharset0 Arial;}{\f4\froman\fprq2\fcharset0 Times New Roman;}}
How can i Read the RTF File and replace with anything i wish?
If you want to generate the RTF Header yourself you should take a look at the RTF Spec.
Otherwise you might be able to simply use the RichTextBox-Control, set the font-style/color etc. and get the header from the RTFText-Property.

Best way to extracting only the bold text from a PDF

iTextSharp is a great tool, I can use
PdfTextExtractor.GetTextFromPage(reader, iPage) + " ";
and it works great, but is there a way to extract only the bold text (e.g. the headlines) from the pdf, and not everything?
Any solution is useful, regardless of the programing language. Thank you
From within iText, You need to use the classes from the com.itextpdf.text.pdf.parser package.
Specifically, you'll need to use a PdfTextExtractor with a custom TextExtractionStrategy that checks the font name. Bold fonts USUALLY have the world "bold" in their name.
Potential Issues:
1) Not everything that looks like text is rendered with fonts and letters. It can be paths or a bitmap. The only way to extract such text is with OCR, and there's no way to get font info.
2) Font Encoding. The bytes that map to the glyphs you're seeing in the PDF may not have a map from those bytes to actual character information.
3) Not all bold-looking text is made with a bold font. Some bold text is made by stroking the text outline with a fairly thin line as well as the usual filling. In this case, the text render mode will be set to "stroke & fill" instead of the usual "fill". This is pretty rare, but it does happen from time to time.
An easy way to test for problems 1 and 2 is to attempt to copy and paste the text within Reader/Acrobat. If you can't select it, it's almost certainly paths or an image. If you can select it but the characters come out as random junk when pasted, then iText will come up with the same junk.
Problem 3 isn't that hard to test for programattically, though you have to handle it on a case by case basis. You need to call TextRenderInfo.getTextRenderMode(). 0 is fill (the standard way of doing things), and 2 is "stroke and fill".
So your TextExtractionStrategy can stub out beginTextBlock, endTextBlock, renderImage, and getResultantText. In your renderText implementation, you'll have to check the font name (for "bold", case insensitive) and the text render mode. If either of those is the case, it's part of on of your headings.
All this is supposing that you are dealing with arbitrary PDF files. If all your PDFs come from the same source, you can start cutting corners. I'll leave that as an Exercise For The Reader.
One of your best bets for this job surely is TET by pdflib.com with its ability to extract to the TETML format. Available for Windows, Mac OS X, Linux, Solaris, AIX, HP-UX...
I'm not sure if it does indeed recognize "headlines" as such (because PDF does not know much of structural markups, only visual ones) -- but it surely can tell you exact position and font used by each string of characters.

Subscript text in pdf C#

How do I insert a subscript character in a string in C#?
I have problems appending a superscript "2" in the same string using char.ConvertFromUtf32(178);, but I struggle with finding a similar solution for the subscripted text. Actually, I'm struggling with finding any solution at all to this rather embarrassing issue.
Plain text doesn't have formatting, like superscript, subscript, bold, italic and/or colors.
You need to use some "rich text" format.
The type of "rich text" depends on where you want to use it. Examples: HTML, RTF.
For PDF you need to look into the formatting options provided by your PDF creation library.
The PDF creation library I'm using did not offer much.
One work around I could figure out was to pick equalent ascii values from charecter map and append it to the existing string.

Categories