I am trying to read a text from document file in C#
I can't create new Document in C#.
I try to using "using Microsoft.Office.Interop.Word"
or "using System.Windows.Documents" but it does'nt recognize the code "Document doc=new Document".
In addition how can I read a text from .docx file?
The code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Office.Interop.Word; //didnt recognize "Office"
namespace DocumentTest1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
Application word = new Application();
Document doc = new Document();
}
}
What could be the problem? tnx
Hi Please see below code
List<string> data = new List<string>();
Application app = new Application();
Document doc = app.Documents.Open(ref readFromPath);
foreach (Paragraph objParagraph in doc.Paragraphs)
data.Add(objParagraph.Range.Text.Trim());
Related
I have this assembly that basically creates an empty excel file using EPPLUS library. The file is created directly to desk, but i want first to write it in a memoerystream and then save the memoerystream to desk. I have this so far.
using OfficeOpenXml;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplicationXLSX
{
class Program
{
static void Main(string[] args)
{
Createxlsx("XLSX");
}
public static void Createxlsx(string filename)
{
FileInfo newFile = new FileInfo("C:\\ConsoleApplicationXLSX\\" + filename + ".xlsx");
MemoryStream ms = new MemoryStream();
//create a package
using (ExcelPackage package = new ExcelPackage(ms))
{
package.Workbook.Worksheets.Add("worksheet");
package.Save();
}
}
}
}
Try
package.SaveAs(ms)
That should fill your stream with the excelfile-content.
I am uploading 4000 zip files of size 85 KB of each using over Linux Server using SFTP in C# WPF application.
This whole process takes 30 minutes.
Is there any way to speed up the uploading using SFTP?
I'm using WinSCP .NET assembly:
https://winscp.net/eng/docs/library
I had also used Chilkat previously.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WinSCP;
namespace SFTP_Demo
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
string line;
SessionOptions sessionoptions = new SessionOptions()
{
Protocol = WinSCP.Protocol.Sftp,
HostName = "172.168.1.7",
PortNumber = 22,
UserName = "lduser",
Password = "lduser",
GiveUpSecurityAndAcceptAnySshHostKey = true
};
using (Session session = new Session())
{
session.Open(sessionoptions);
TransferOptions transferOptions = new TransferOptions();
transferOptions.TransferMode = TransferMode.Binary;
TransferOperationResult transferResult;
System.IO.StreamReader file = new System.IO.StreamReader(txtFile.Text);
while ((line = file.ReadLine()) != null)
{
transferResult = session.PutFiles(#"D:\Test\signature\ldoutput\"+line, "/SFTP/", false, transferOptions);
transferResult.Check();
counter++;
strbldr = strbldr.AppendLine(string.Format("{0} Upload of {1} succeeded", counter + 1.ToString(), line));
}
}
}
}
}
There's quite an overhead with each file (opening, closing, updating timestamps). So the transfer of a large number of small files is pretty inefficient.
What you can do, is to parallelize the transfer.
Collect the list of files using Session.ListDirectory (or Session.EnumerateRemoteFiles if you need recursion) and split the list to batches, transferring each in a separate thread.
See this example:
Automating download in parallel connections over SFTP/FTP protocol
Im tring to create a file using a System.Io.File namespace howeever im using it on MVC witch im new to and i get this error when i publish my proyect "A using namespace directive can only be applied to namespaces; 'System.IO.File' is a type not a namespace"
This is my using Statement:
using System;
using System.Reflection;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.IO.File;
using System.Text;
using (var reader = System.IO.File.CreateText(#"C:\inetpub\wwwroot\procedimiento.txt"))
{
// Starting outer json array
reader.WriteLine("[");
for (var rowIndex = 0; rowIndex < myTable.Rows.Count; rowIndex++)
{
var row = myTable.Rows[rowIndex];
var rowValues = new List<string>(); // can be reused if needed
foreach (DataColumn column in myTable.Columns)
rowValues.Add(row[column].ToString());
var jsonRow = JsonConvert.SerializeObject(rowValues);
// Write current row
reader.Write(jsonRow);
// Add separating comma
if (rowIndex != myTable.Rows.Count - 1)
reader.WriteLine(",");
}
// End outer json array
reader.WriteLine("]");
}
the using keyword have different semantics depending on where it is located.
When put directly in a file it's to tell which namespaces to import. In that context you can not have a using statement for a class directly. Well. You can, but the syntax is different. MSDN.
The other usage is to dispose an object when it goes out of scope. In this case you can enter the fully qualified class name (namespace + class name) or just the class name. MSDN
In your code you have mixed the two.
Alternative 1
Completely remove the using statement in the file and just specify the full class name in the statement.
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Text;
//in your method
using (var reader = System.IO.File.CreateText(#"C:\inetpub\wwwroot\procedimiento.txt"))
Alternative 2
Remove the namespace from the statement and the class name from the directive:
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Text;
//in your method
using (var reader = File.CreateText(#"C:\inetpub\wwwroot\procedimiento.txt"))
Alternative 3
Rename the class using a directive. You typically use this when the compiler can't distinguish between different identifiers (like having the same class name in different imported namespaces).
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.IO;
using IoFile = System.IO.File; //this
using System.Text;
//in your method
using (var reader = IoFile.CreateText(#"C:\inetpub\wwwroot\procedimiento.txt"))
Your code is missing class and method declarations. System.IO.File is in fact a type and you shouldn't be referencing it in your using statements. All you need to reference is System.IO and then you can call File.CreateText().
using System;
using System.IO;
public class MyClass
{
public void CreateFile()
{
string path = #"c:\temp\MyTest.txt";
if (!File.Exists(path))
{
// Create a file to write to.
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("Hello");
sw.WriteLine("And");
sw.WriteLine("Welcome");
}
}
}
}
MSDN on File.CreateText()
Try this.
using (StreamWriter sw = File.CreateText(fileName))
{
sw.WriteLine("New file created: {0}", DateTime.Now.ToString());
}
I was trying to write an automatic method that retrieve a value from an XML file.
as i wrote the following code:
XmlDocument xDoc = new XmlDocument();
I found out that when I'm trying to access xDoc object while typing xDoc. , it does nothing, means no option to manipulate the object...
my usings are:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using Microsoft.Office.Interop;
using System.IO;
using Excel = Microsoft.Office.Interop.Excel;
using System.Xml;
using System.Collections.Generic;
Any ideas ?
Sounds like you need to make a class to put the XMLDocument in if (per your comment) that is the next line you could have attached.
What I mean is, as Jon Skeet said, are you sure Adam that you're not declaring the XMLDocument inside of the namespace instead of inside of a class. Should be something like:
using System.Xml;
namespace SomeNameSpace
{
public class MyClass
{
XMLDocument xDoc = new XMLDocument();
public void MyMethod() {
xDoc.DocumentType;
}
}
}
I was trying to extract the data from pdf file using iTextSharp, but i go two errors. Actually I want to extract the data from pdf file and store it into database.
Here is my code:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.util.collections;
using System.Linq;
public partial class frm_CreatePDF : System.Web.UI.Page
{
public string P_InputStream3 = "~/My Documents/List Of Holidays 2012";
protected void Page_Load(object sender, EventArgs e)
{
ExtractText();
}
private string ExtractText()
{
PdfReader reader = new PdfReader(Server.MapPath(P_InputStream3));
string txt = PdfTextExtractor.GetTextFromPage(reader, 2, new LocationTextExtractionStrategy());
return txt;
}
}
And the error is:
The name 'PdfTextExtractor' does not exist in the current context
The type or namespace name 'LocationTextExtractionStrategy
Make sure you have brought the namespace in which those two classes are defined into scope by adding the using directive:
using iTextSharp.text.pdf.parser;
I guess you need to use another namespace where LocationTextExtractionStrategy is defined
First of all you need to add a reference to the ItextSharp dll, after wich you can add a using statement to acces the namespace that contains that static class
http://msdn.microsoft.com/en-us/library/wkze6zky%28v=vs.100%29.aspx
This is a late answer, but I've found what your issue is:
You are missing the iTextSharp.text.pdf.parser using statement. Add this below your using iTextSharp.text.pdf; code:
using iTextSharp.text.pdf.parser;
That should be able to find your LocationTextExtractionStrategy pretty well.
You can also just add iTextSharp.text.pdf.parser directly behind your LocationTextExtractionStrategy if you want.
string txt = PdfTextExtractor.GetTextFromPage(reader, 2, new iTextSharp.text.pdf.parser.LocationTextExtractionStrategy());
But I would recommend the former. It's cleaner and easier to read.