Hello I am working in windows application "winforms" in this I have successfully save pdf file in database now I want to display that in adobe reader which i have been added from toolbox
what should I need to do, thats why I can able to view pdf in adobe reader.
I already try to display pdf file to reader but nothing display.
Here is my code:
{
con.Open();
SqlCommand command1 = new SqlCommand("select PDFFILE from help ;", con);
DataTable dt = new DataTable();
byte[] img = (byte[])command1.ExecuteScalar();
MemoryStream ms = new MemoryStream(img);
string s = System.Text.Encoding.UTF8.GetString(img);
axAcroPDF1.LoadFile(s);
con.Close();
}
If the adobe control only takes a filename, you will need to save the pdf as a file first. System.IO.Path will get you a temporary path. Using a guid will also prove useful for random names.
If you don't want that, you may use the PdfiumViewer, which is Googles Pdfium wrapped as a .NET control. Works fine, takes streams as load parameter and is easy to use. You can get both the PdfiumViewer and the required Pdfium-DLLs via NuGet. Plus, you won't be dependent on the installed adobe version any more.
Related
I am using iText to fill a pdf. This pdf contains a xfa from, I fill this form through a xml file . After filling the form user need to download it and sign it manually. So far so good everything works fine every field in form filled properly. If user sign the file adobe reader make a new copy of file with signature.
But when user try to sign that pdf it gives following error and the newly generated file with signature doesn't save data, It make all field blank.
At least one signature has problems
When user sign pdf adobe reader also give a popup on signature verification
But if i fill the same pdf manually without using itext it allow me to sign pdf successfully
this is the code that i am using to fill pdf with xml data :
public static byte[] FillXfaForm(byte[] byteArray, String xmlFilePath)
{
PdfReader reader = new PdfReader(byteArray);
using (MemoryStream ms = new MemoryStream())
{
using (PdfStamper stamper = new PdfStamper(reader, ms,'\0',true))
{
stamper.Writer.CloseStream = false;
stamper.AcroFields.Xfa.FillXfaForm(xmlFilePath);
}
if(File.Exists(xmlFilePath))
{
File.Delete(xmlFilePath);
}
return ms.ToArray();
}
}
And here is the screen shot of error message
Please help me to solve this issue.
Finally I fix this problem
The cause of this problem is that i update whole xml document to fill xfa from but when i only update the data part not the whole xml it works without any error.
I don't know what is the difference it really create as in this similar question "Bruno Lowagie" state that you can either use full xml replace or you can change data part only.
How can I set XFA data in a static XFA form in iTextSharp and get it to save?
But for me it allow me to sign the document only if I replace data part not the whole xml data.
I hope it will help someone facing similar problem.
I have one question and I'm hoping it's an easy one for you.
(Windows Forms Application, C#, Framework 3.5, SQL Server 2008 R2)
I don't know how to open Excel (it is loaded in byte[] type) via OleDB.
So, what have I done:
I have uploaded Excel (.xls(x)) file via form, and saved it in a database as varbinary(max). Now I need to read that Excel file via oleDB. I've managed to load that file from database and saved it into byte[] variable. How can I open byte[] in oleDB?
When I uploaded file for the first time (before saving it to the database), I've opened it via OleDB with just passing file path. How can I access Excel's data when it is already stored in memory as byte[]?
If you want to read using OleDB, then you have to write the bytes to disk. For example, you could do this:
var filename = System.IO.Path.GetTempFileName();
// Assuming that fileBytes is a byte[] containing what you read from your database
System.IO.File.WriteAllBytes(filename, fileBytes);
var connection = #"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filename + ";Extended Properties=\"Excel 12.0;HDR=YES\"";
// Do your work on excel
using (System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connection))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT * FROM [Sheet1$]";
using (var rdr = cmd.ExecuteReader())
{
while (rdr.Read())
{
System.Diagnostics.Debug.WriteLine(rdr["ColumnName"]);
}
}
}
conn.Close();
}
//Cleanup
System.IO.File.Delete(filename);
If you don't want to write the file to disk, you could look into using a third party library that can read excel files from a memory stream. Tools like SpreadsheetGear or Aspose are commercial tools that can accomplish this.
I don't know anything about what library you're using to deal with your Excel data, but the first thing that comes to mind is that it almost certainly has a LoadFromFile method. It might not be called that, but that's what it does. See if it also has a LoadFromStream method, and if so, take your byte[] data and load it into a MemoryStream, and load the XLS from there.
emp.upload_file = Path.GetFileName(file.FileName);
emp as your table object created and file is HttpPostedFileBase file
I have a strange behavior when i try to read an XML worksheet using this code :
string CONNEC_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=\"Excel 8.0;HDR=No;IMEX=2;\"";
string fullFilePath = #"C:\Tmp\TestFile.xls";
using (OleDbConnection objCon = new OleDbConnection(string.Format(CONNEC_STRING, fullFilePath)))
{
using (OleDbCommand cm = new OleDbCommand("Select * From [MYCELLSRANGE]", objCon))
using (OleDbDataAdapter da = new OleDbDataAdapter(cm))
{
DataTable dt = new DataTable();
objCon.Open();
da.Fill(dt);
objCon.Close();
}
}
If the Excel file is closed, I receive the error 'External table is not in the expected format.'
When I open the file, if I execute the above code, it works fine and I can read data contained in MYCELLSRANGE.
So, has anyone any idea about this problem ?
Thanks for answers.
I believe its because the Jet Engine uses the Excel app to interpret the file. I ran into this issue once myself. When you use the Excel.dll reference and you interpret the Excel file from that you have to create a new Application instance to read the file. Jet just does this very thing to get the data. I suggest using that code instead.
C# Corner example of how to Import Excel to a ListView
C# Corner example of how to Create an Excel Worksheet in C#
MSDN users discussing the same topic
If you would like further assistance, let me know. I have successfully done this in VB.NET in various applications of mine.
I have an Excel 2007 file "my.xlsx" and a sheet named "States", and I have the following code
using (OleDbConnection con = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\my.xlsx;Extended Properties='Excel 12.0 Xml;HDR=NO'"))
{
OleDbCommand cmd = new OleDbCommand("select * from [States]", con);
con.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while(reader.Read())
Console.WriteLine(reader[0]);
}
It keeps throwing exception saying "The Microsoft Office Access database engine could not find the object 'States'. Make sure the object exists and that you spell its name and the path name correctly.".
Could someone help to see what's wrong with my code please?
I know this may be not exactly what you want to hear, but you're in a long line of people who have had struggles trying to read excel files by using oledb...
I've had a lot more luck using libraries such as NPOI to read Excel files from C#:
http://npoi.codeplex.com/ (recommended)
http://nexcel.sourceforge.net/
http://sourceforge.net/projects/koogra/
I think that you need to open the connection before creating the command - not sure if it's a big deal...
Then add a $ at the end of the sheet name:
OleDbCommand cmd = new OleDbCommand("select * from [States$]", con);
Basically [Name] refers to a named range, whereas [Name$] refers to a sheet.
For more information see this KB: http://support.microsoft.com/kb/316934
I just got it working by visiting this page
http://www.davidhayden.com/blog/dave/archive/2006/05/26/2973.aspx
For my requirement, I only need to read an Excel file. Most of the solutions I searched show you to use some kind of library, which is overkill for me. I was really looking for someone to just post a code snippet on how to read the file, but I only found that on the page I put the link with.
I have to automate something for the finance dpt. I've got an Excel file which I want to read using OleDb:
string connectionString = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=A_File.xls;Extended Properties=""HTML Import;IMEX=1;""";
using (OleDbConnection connection = new OleDbConnection())
{
using (DbCommand command = connection.CreateCommand())
{
connection.ConnectionString = connectionString;
connection.Open();
DataTable dtSchema = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if( (null == dtSchema) || ( dtSchema.Rows.Count <= 0 ) )
{
//raise exception if needed
}
command.CommandText = "SELECT * FROM [NameOfTheWorksheet$]";
using (DbDataReader dr = command.ExecuteReader())
{
while (dr.Read())
{
//do something with the data
}
}
}
}
Normally the connectionstring would have an extended property "Excel 8.0", but the file can't be read that way because it seems to be an html file renamed to .xls.
when I copy the data from the xls to a new xls, I can read the new xls with the E.P. set to "Excel 8.0".
Yes, I can read the file by creating an instance of Excel, but I rather not..
Any idea how I can read the xls using OleDb without making manual changes to the xls or by playing with ranges in a instanciated Excel?
Regards,
Michel
I asked this same question on another forum and got the answer so I figured I'd share it here. As per this article: http://ewbi.blogs.com/develops/2006/12/reading_html_ta.html
Instead of using the sheetname, you must use the page title in the select statement without the $. SELECT * FROM [HTMLPageTitle]
I've been searching so many solution, end up I found something really simple and easy -
to import XML file to Excel file, I tried to convert XML to HTML first, use -
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=63
then I found I could easily change my output file as .xls, instead of .html
//create the output stream
XmlTextWriter myWriter = new XmlTextWriter
("result.html", null);
then the output is perfect Excel file from my XML data file.
hope this will save ur work.
I have run into the same problem. As previously mentioned, it seems to be an html file renamed to .xls. When I copy the data from the xls to a new xls, I can read the new xls with the E.P. set to "Excel 8.0".
In this scenario, the file couldn't be saved in the correct format. So we have to convert that file to the correct format. To do this, use MS Office Excel 2007, Click File -> Convert. The file will be converted to the right format automatically.