dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] Generate PDF from binary?

  1. #1
    Join Date
    Jan 2010
    Posts
    130

    [RESOLVED] Generate PDF from binary?

    I'm trying to create a PDF file from binary data retrieved from a webservice. The data is successfully retrieved but it is displayed on the page in binary instead of being converted into a PDF file. I am not sure how to make the data appear in the form of a PDF file instead of just as a string. I tried using iTextSharp to create a PDF but am not sure how to get the PDFWriter to write the data from the BinaryWriter??!

    Code:
            protected void GenerateArchivePDF(int value)
            {
                try {
                    // Retrieve data
                    PDFbinary = archives[value].PDFBitArray;
    
                    // Create a new temp file called "test.dat" which contains data imported from webservice
                    FileStream readStream = File.Create(Server.MapPath("test.dat"));
                    BinaryWriter binaryWriter = new BinaryWriter(readStream);
    
                    // Write the binary data to the file
                    binaryWriter.Write(PDFbinary);
                    binaryWriter.Close();
                    readStream.Close(); 
    
                    // Create a new PDF document
                    doc = new Document(PageSize.A4, 80, 50, 30, 65);
                    FileStream pdfStream = new FileStream("~/Beurteilungsbogen.pdf", FileMode.Create);
                    PdfWriter pdfWriter = PdfWriter.GetInstance(doc, pdfStream);
    
                    // Import data from newly created file named "test.dat"
                    stream = File.OpenRead(Server.MapPath("test.dat"));
                    binaryReader = new BinaryReader(stream);
    
                    // Read from file - INSTEAD OF WRITING ON PAGE SHOULD WRITE IN PDF!
                    Response.Write(binaryReader.ReadInt32());
                    //pdfWriter.Add(Response.Write(binaryReader.ReadInt32())); // trying to write to PDF!
               
                    // Close
                    binaryReader.Close();
                    stream.Close();
                }
                catch (DocumentException de)
                {
                    errorPage.DisplayErrorMsg(de.Message);
                }
                catch (IOException ioe)
                {
                    errorPage.DisplayErrorMsg(ioe.Message);
                }
                finally
                {
                    doc.Close();
                    Response.Redirect("~/Beurteilungsbogen.pdf");
                }

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

    Re: Generate PDF from binary?

    If the binary data is the PDF (which seems to), than you can use following code
    Code:
    byte[] buffer = ... // here is conversion from PDFbinary; sorry I omit it
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.ContentType = application/pdf;
    Response.AddHeader("Content-Disposition", "attachment;filename=yourfile.pdf");
    Response.AddHeader("Content-Length", buffer.Length.ToString());
    Response.OutputStream.Write(buffer, 0, buffer.Length);
    Response.Flush();
    Response.End();
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  3. #3
    Join Date
    Jan 2010
    Posts
    130

    Question Re: Generate PDF from binary?

    Yes it is. So it's not possible to use my code then? Instead of using a byte[] buffer (as I originally wanted to do) I imported binary data in the form of an integer and then wrote it to a new file using the BinaryWriter. I then tried to read this file using the BinaryReader but I dont know how to make this appear in the PDF I created??

  4. #4
    Join Date
    Jul 2019
    Posts
    2

    Re: [RESOLVED] Generate PDF from binary?

    I have been using ZetPDF for a while now for generating PDF from the library and it is been awesome. ZetPDF is a .net SDK for adding PDF render and print support in .net applications. It is designed to solve most developer’s needs with regards to PDF rendering.

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




On-Demand Webinars (sponsored)