I have a Word document imported into the resouce file of my project.
Is it possible to extract this document and display it in the RichTextBox control in my application?
I was able to extract the string and image objects from the resource file of my project using the below class.
namespace TestProject
{
public class Utilities
{
private static ResourceManager _resource = new ResourceManager("TestProject.Resource1", Assembly.GetExecutingAssembly());
public static string GetString(string name)
{
return (System.String)(_resource.GetString(name));
}
public static Image GetImage(string name)
{
return (System.Drawing.Image) (_resource.GetObject(name));
}
}
}
RTF is formatted as a string and if you add it to the Files section of the resources file, it will wrap it with a property to read the string.
That is:
Properties.Resources.YourDocument;
is implemented as:
internal static string YourDocument {
get {
return ResourceManager.GetString("YourDocument", resourceCulture);
}
}
and return rich text looking something like this:
{\rtf1\ansi\ansicpg1252\deff0\deflang3081{\fonttbl{\f0\fnil\fcharset0
Calibri;}} {\colortbl ;\red255\green255\blue0;} {*\generator Msftedit
5.41.21.2510;}\viewkind4\uc1\pard\sa200\sl276\slmult1\cf1\lang9\f0\fs22
Rich\cf0 , multiline text.\par \par Is \b\fs32 here\b0\fs22\par }
Leaving you just needing to do:
richTextBox1.Rtf = RichTextResource.Properties.Resources.YourDocument
That assumes the document is actually saved as rich text. A word doc will show up as garbage.
Finally, if your resource is stored as a byte[], you'll need to convert to a string first. I.e.
richTextBox1.Rtf = System.Text.Encoding.UTF8.GetString(bytes), assuming its UTF8 encoded.
Related
I'm working on a project where I have to extract specific text from a pdf so that I can send these info into an excel file.
I tried at first to convert my pdf into a .txt file thinking a .txt file format would be easier to convert into json.
But the result is not at all what I need (dictionary-style Json format) but instead a kind of giant messy string .
The pdf sample looks like this:
Analysis
Some text
Reference Date (Big space) 11/17/2021
Reference Price (Big space) USD 745
Client id (Big space) 4572845
I'd like to have something like this at the end:
{Analysis:Some text, Reference Date:11/17/2021, Reference Price:USD 745, Client id:4572845}
Currently the results give all the info mixed up between each others.
Here is my code:
First, I created a "Global" class where I will create the method "Extract_Row_Info_TS that will basically load the first page of the document (called a TS or Termsheet) and extract the text from the PDF and store it into a txt file called "result.txt":
class Global
{
public static void Extract_RowInfo_TS(string doc_Type, string docPath, int? nbrPage = null)
{
switch (doc_Type)
{
case "Pdf":
Spire.Pdf.PdfDocument doc = new Spire.Pdf.PdfDocument();
doc.LoadFromFile(docPath);
StringBuilder buffer = new StringBuilder();
//Extract text from the first page only
Spire.Pdf.PdfPageBase pagefirst = doc.Pages[0];
buffer.Append(pagefirst.ExtractText());
doc.Close();
//save text
String fileName = #"my_disk:\my_path\result.txt";
File.WriteAllText(fileName, buffer.ToString());
//Load File
System.Diagnostics.Process.Start(fileName);
break;
case "Excel":
Spire.Xls.Workbook Wb = new Spire.Xls.Workbook();
break;
case "Word":
Spire.Doc.Document doc_word = new Spire.Doc.Document();
break;
}
}
}
Come back to my main page, I call the above method "Extract_RowInfo_TS" from above Global class and when it created "result.txt" from the pdf infos, I'll try to convert this "result.txt" into a json format:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_Extract_PDF_Click(object sender, EventArgs e)
{
Global.Extract_RowInfo_TS("Pdf", #"my_disk:\my_path\my_doc.pdf");
Convert_To_Json_Format(#"my_disk:\my_path\result.txt");
}
private void Convert_To_Json_Format(string baseTextFile)
{
string streamText = new StreamReader(baseTextFile).ReadToEnd();
//Serialize Json Data.
string serializeData = Serialize_into_Json(streamText);
string newFile = #"my_disk:\my_path\NEW_text_file_2.txt";
File.WriteAllText(newFile, serializeData);
System.Diagnostics.Process.Start(newFile);
}
private static string Serialize_into_Json(string json)
{
string jsonData = JsonConvert.SerializeObject(json);
return jsonData;
}
}
I'm stuck here trying to create a proper json format file (or anything alike actually, I just want to group info between them, maybe create a table first ? I don't know...) that I can use for sending into my Excel file. Any help would be much appreciated ! I'm using the Free version of Spire Nuget package v4.3.1 that contains Free Spire.PDF, Spire.Xls, Spire.Doc and more of them. But maybe there are some others solutions out there to achieve the goal I'm looking for.
Thanks in advance for helping and have a great day.
I currently am making a UI for a note keeper and was just going to preview documents etc, but i was wondering what file type i would need to create if instead i wanted to do things like tag the file etc, preferably in c#, basically make my own evernote, how do these programs store the notes?
I dont know how to directly tag the file, but you could create your own system to do it. I mentioned two ways to do it:
The first way is to format the note's / file's contents so that there are two parts, the tags and the actual text. When the program loads the note / file, it seperates the tags and the text. This has the downside that the program have to load the whole file to just find the tags.
The second way is to have a database with the filename and it's associated tags. In this way the program doesn't have to load the whole file just to find the tags.
The first way
In this solution you need to format your files in a specific way
<Tags>
tag1,tag2,tag3
</Tags>
<Text>
The text you
want in here
</Text>
By setting up the file like this, the program can separate the tags from the text. To load it's tags you'd need this code:
public List<string> GetTags(string filePath)
{
string fileContents;
// read the file if it exists
if (File.Exists(filePath))
fileContents = File.ReadAllText(filePath);
else
return null;
// Find the place where "</Tags>" is located
int tagEnd = fileContents.IndexOf("</Tags>");
// Get the tags
string tagString = fileContents.Substring(6, tagEnd - 6).Replace(Environment.NewLine, ""); // 6 comes from the length of "<Tags>"
return tagString.Split(',').ToList();
}
Then to get the text you'd need this:
public string GetText(string filePath)
{
string fileContents;
// read the file if it exists
if (File.Exists(filePath))
fileContents = File.ReadAllText(filePath);
else
return null;
// Find the place where the text content begins
int textStart = fileContents.IndexOf("<Text>") + 6 + Environment.NewLine.Length; // The length on newLine is neccecary because the line shift after "<Text>" shall NOT be included in the text content
// Find the place where the text content ends
int textEnd = fileContents.LastIndexOf("</Text>");
return fileContents.Substring(textStart, textEnd - textStart - Environment.NewLine.Length); // The length again to NOT include a line shift added earlier by code
}
Then I'll let you find out how you do the rest.
The second way
In this solution you have a database file over all your files and their associated tags. This database file would look like this:
[filename]:[tags]
file.txt:tag1, tag2, tag3
file2.txt:tag4, tag5, tag6
The program will then read the file name and the tags in this way:
public static void LoadDatabase(string databasePath)
{
string[] fileContents;
// End process if database doesn't exist
if (File.Exists(databasePath))
return;
fileContents = File.ReadAllLines(databasePath); // Read all lines seperately and put them into an array
foreach (string str in fileContents)
{
string fileName = str.Split(':')[0]; // Get the filename
string tags = str.Split(':')[1]; // Get the tags
// Do what you must with the information
}
}
I hope this helps.
I have a folder named Folderć, which contains smth.jpg. As folder has letter ć in name, filepath is saved in database as Folder%C0%01%/smth.jpg.
Letter ć is saved as Hex code. This is not a problem while previewing image on website.
Problem happens when i am trying to make a subfolder in Folderć via C# function. Function gets filepath string, finds folder name and creates subfolder in it. As my string contains hex code instead of letter ć function cant find that path thus cant create subfolder.
That string is in UTF-8 format, so changing the encoding doesnt change anything.
Anyone knows where is problem and how to solve it?
You can encode the name with Base64:
public string ToBase64String(string text)
{
byte[] data = Encoding.UTF8.GetBytes(text);
return Convert.ToBase64String(data);
}
and use it string myEncFolderPath = ToBase64String(myFolderPath); before saving the string in the DB.
After you receive the string from the DB, you can decode it back to a normal string with:
public string FromBase64String(string base64)
{
byte[] data = Convert.FromBase64String(base64);
return Encoding.UTF8.GetString(data);
}
by using string myFolderPath = FromBase64String(myEncFolderPath);.
That way, you can save strings freely in the DB.
I am having below string and filename, the filename is the output of another function, so it look like below
string filename = "maindestination.jpg"; //It can .png, .gif etc depend on the type of file.
Then I have got another function which gives the output as below
string id = "tcm:123-3455";
Now I want to write a function something like below which will do the merging of both the outputted values as below:
public static string newFileName(string filename, string strID)
{
string newFileName = "maindestination_tcm:123-3455.jpg"
return newFileName;
}
public static string GetFileNameWithId(string fileName, string strId) {
return string.Format(
"{0}_{1}{2}",
Path.GetFileNameWithoutExtension(fileName),
strId,
Path.GetExtension(fileName));
}
Use the methods in the Path class, in the System.IO namespace:
string newFileName=Path.GetFileNameWithoutExtension(filename)+"_"+strID+Path.GetExtension(filename);
By the way, this will work on any version of the .NET Framework, not just in C# 2.0.
I'm writing a program about trying different images in picturebox, but the image must correspond to the text. Plus it must come from "Resources" folder of the project.
this is what i want to do if the text "apple" displays to the screen then the image with a filename "apple" would also display in the picturebox..
I can do it in "if-else" like this
string word="apple";
if(word==apple)
pictureBox1.Image= WindowsFormsApplication4.Properties.Resources.apple;
but What if I have a thousand images, I still think there's an easy way for this,..
i'm trying this one,,
string word=label1.Text;//label1.text changes from time to time
pictureBox1.Image= WindowsFormsApplication4.Properties.Resources.word;
but I know "word" is string....It's not possible...I cannot append string to the syntax....
You can pass in a string using the GetObject method of the ResourceManager class:
string itemName = label1.Text;
this.pictureBox1.Image =
(Image)Properties.Resources.ResourceManager.GetObject(itemName);
If you open resources .Designer.cs file code , you will see something like:
internal static System.Drawing.Bitmap apple {
get {
object obj = ResourceManager.GetObject("apple", resourceCulture);
return ((System.Drawing.Bitmap)(obj));
}
}
so you can do the same thing:
string word=label1.Text;//label1.text changes from time to time
pictureBox1.Image= (System.Drawing.Bitmap)WindowsFormsApplication4
.Properties
.Resources
.ResourceManager.GetObject(word);