-
January 28th, 2010, 10:55 AM
#1
[RESOLVED] Convert PDF to byte[] - what imports needed?
Never having converted an existing PDF to byte[] before I used some free source code. My problem is that "InputStream" isn't recognized. The MSDN Library seems to classify it under the import System.Web but I have already imported this (http://msdn.microsoft.com/de-de/libr...putstream.aspx)! I think there might also be a prob with "new File()". Here is my code:
Code:
public static byte[] convertDocToByteArray(String sourcePath)
{
byte[] byteArray=null;
try {
File file = new File(sourcePath);
InputStream inputStream = new FileInputStream(file);
byte[] bytes = new byte[file.length()];
inputStream.read(bytes);
} catch (FileNotFoundException e)
{
Console.Write(e);
} catch (IOException io)
{
Console.Write(io);
}
return byteArray;
}
And here are my imports:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Azubiportal.AZPService;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.xml;
using System.Net.Mail;
using System.IO;
using System.Data;
using System.ComponentModel;
using System.Collections;
-
January 28th, 2010, 11:16 AM
#2
Re: Convert PDF to byte[] - what imports needed?
Well...there doesn't seem to be a class called 'InputStream' in the framework. That link you shows that the 'HttpRequest' object has an 'InputStream' property of type 'Stream'. So you need to 'Stream' instead of 'InputStream'. Secondly what is 'FileInputStream'? I don't recognise that type. Thirdly where did you get the open source code that does that?
-
January 28th, 2010, 11:44 AM
#3
Re: Convert PDF to byte[] - what imports needed?
How about trying this code?
Code:
byte[] fileArray = File.ReadAllBytes(@"C:\test\test.pdf");
File.WriteAllBytes(@"C:\test\test2.pdf", fileArray);
This code reads in a pdf, then saves it with a different file name.
you will need to add a using for the "System.IO" namespace.
===============================
My Blog
-
January 29th, 2010, 03:09 AM
#4
Re: Convert PDF to byte[] - what imports needed?
Also keep in mind, that "import" (using) a namespace is not enought - you also have to reference the appropriate assembly. In case of common assemblies like System.dll, the referencies are prepared by VS if you create a project, but for 3rd party assemblies or assemblies like System.Messagind.dll or System.Web.dll you have to add them yourself.
- Make it run.
- Make it right.
- Make it fast.
Don't hesitate to rate my post.
-
January 29th, 2010, 04:15 AM
#5
Re: Convert PDF to byte[] - what imports needed?
Thank you so much!
I'm having trouble figuring out what datatype to use in the db, byte[] doesnt seem to exist! Should I convert the byte[] to binary to save it in the db? Or to varbinary??
-
January 29th, 2010, 04:50 AM
#6
Re: Convert PDF to byte[] - what imports needed?
Hi Stephsh,
I assume you're using SQL Server and ADO.NET. Go for varbinary and you can pass in the byte array without any problem. It will for both writing and reading.
-
January 29th, 2010, 05:03 AM
#7
Re: Convert PDF to byte[] - what imports needed?
Thanks!
I assume this is how one converts the byte[] back to PDF (where buffer = byte[])? I can't debug due to some other errors in my code but I desperately need to get this project finished by today! :P
Code:
protected void GenerateArchivePDF(int value)
{
try {
buffer = archive.PDFBitArray;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=Beurteilungsbogen.pdf");
Response.AddHeader("Content-Length", buffer.Length.ToString());
Response.OutputStream.Write(buffer, 0, buffer.Length);
//Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
}
catch (DocumentException de)
{
errorPage.DisplayErrorMsg(de.Message);
}
catch (IOException ioe)
{
errorPage.DisplayErrorMsg(ioe.Message);
}
finally
{
doc.Close();
Response.Redirect("~/Beurteilungsbogen.pdf");
}
-
January 29th, 2010, 05:58 AM
#8
Re: Convert PDF to byte[] - what imports needed?
That looks ok. You'll have to try it out before you submit the work though...
-
January 29th, 2010, 05:59 AM
#9
Re: Convert PDF to byte[] - what imports needed?
lol yes I will do! Thankkkkk you so much for all your help, I was so worried I wouldnt be able to submit anything which would be a disaster since this is my first project!!!
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|