[RESOLVED] Convert PDF to byte[] - what imports needed?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: [RESOLVED] Convert PDF to byte[] - what imports needed?

  1. #1
    Join Date
    Jan 2010
    Posts
    130

    [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;

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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?

  3. #3
    Join Date
    Nov 2007
    Location
    .NET 3.5 / VS2008 Developer
    Posts
    624

    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.

    Code:
    using System.IO;
    ===============================
    My Blog

  4. #4
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    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.

  5. #5
    Join Date
    Jan 2010
    Posts
    130

    Question 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??

  6. #6
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    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.

  7. #7
    Join Date
    Jan 2010
    Posts
    130

    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");
                  }

  8. #8
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Convert PDF to byte[] - what imports needed?

    That looks ok. You'll have to try it out before you submit the work though...

  9. #9
    Join Date
    Jan 2010
    Posts
    130

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center