-1

I have two strings, each string is a PDF etiquette, I must to write this two etiquette into a PDF file. To do this I convert a each string to a byte array (I don't know if this is the best way) and each I Write into a PDF file. When I write one etiquette into PDF file all is good, I see the etiquette, but then I try to appending the second, the result is the same, into the file is only the first etiquette. For example this code write first etiquette and all working good:

Dim fs As FileStream = New FileStream(fullFileName, FileMode.CreateNew)
fs.Close()
fs = New FileStream(fullFileName, FileMode.Append)
Dim str As String = GetPDFString(27)
Dim binaryData As Byte() = ConvertStringToByte(str)
fs.Write(binaryData, 0, binaryData.Length)
fs.Close()

but if I want to append the second etiquette in the same PDF file using this code ... this not appending.

Dim fs As FileStream = New FileStream(fullFileName, FileMode.CreateNew)
fs.Close()
fs = New FileStream(fullFileName, FileMode.Append)
Dim str As String = GetPDFString(25)
Dim str1 As String = GetPDFString(27)
Dim binaryData As Byte() = ConvertStringToByte(str)
Dim binaryData1 As Byte() = ConvertStringToByte(str1)
fs.Write(binaryData, 0, binaryData.Length)
fs.Write(binaryData1, 0, binaryData1.Length)
fs.Close()

both have the same result, and I don't understand why the second etiquette isn't appending? Thank you a lot.

Silvia Parfeni
  • 518
  • 1
  • 6
  • 17
  • If you are so intelligent, why you are not help me, but are down voting? – Silvia Parfeni Sep 14 '15 at 14:06
  • 1
    For the record, I did not downvote you, but I can explain why people are doing it. There are certain rules for asking a question on StackOverflow, part of which is research. Yes, you need to do your own research, i.e. spend 1-2 hours of your own time, trying to figure our the problem yourself and only then ask a question, but be sure to provide some steps you have tried, and what went wrong. You are expected to provide a reduced test case for the problem in question, which is usually a result of your research. I.e. shrink 100 steps down to 3, and post them as a question. – Victor Zakharov Sep 14 '15 at 17:26
  • If you want to get upvotes, remove mention of PDF and Base64 conversion from your question. See what's left, and if you still have a problem. If yes, good, you've now reduced your test case. If no, you probably need to make a new question having PDF and/or Base64 in the title, nothing to do with Bytes or FileStreams. – Victor Zakharov Sep 14 '15 at 17:28
  • It's never too late to edit your question. Include what you found. Explain your way of thought a bit more. – Victor Zakharov Sep 15 '15 at 11:23
  • ok I try to edit my question – Silvia Parfeni Sep 15 '15 at 21:05
  • You cannot simply append to a PDF file and expect text to appear, you need to be using some 3rd party library to manipulate PDF documents. Check these links: [link1](http://stackoverflow.com/questions/465433/creating-pdf-files-at-runtime-in-c-sharp), [link2](http://stackoverflow.com/questions/2937797/best-c-sharp-api-to-create-pdf), [link3](http://www.codeproject.com/Articles/570682/PDF-File-Writer-Csharp-Class-Library-Version-Unico), most promising among those listed is [PDFSharp](http://www.pdfsharp.com/PDFsharp/). [Hello world example](http://www.pdfsharp.net/wiki/HelloWorld-sample-VB.ashx) – Victor Zakharov Sep 16 '15 at 00:18

2 Answers2

5

Your question title suggests that you are asking about how to append a byte to a FileStream, not about PDF, and not about Base64 string conversion (which you are using in your code).

Before asking a question on StackOverflow, you need to ensure you are conveying only one problem at a time. Remove everything that is not relevant, and prepare a code sample we can use in a brand new VS project, in order to reproduce your problem and help you solve it.

Now, if your question is really about appending a byte (or a byte array) to a file, it's as simple as one line of code (or two, if you keep FileStream approach). See below link:

Also copy-pasted for your convenience here (and converted from C# to VB.NET):

Dim appendMe As Byte() = New Byte(999) {}
File.AppendAllBytes("C:\test.exe", appendMe)

Or, to avoid memory overflow, if your byte array is expected to be large enough:

Public Shared Sub AppendAllBytes(path As String, bytes As Byte())
   'argument-checking here.

   Using stream = New FileStream(path, FileMode.Append)
       stream.Write(bytes, 0, bytes.Length)
   End Using
End Sub
Community
  • 1
  • 1
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • @SilviaParfeni: I'm sorry, this was C#, I'm so used to using both languages now, so I often use them interchangeably. Fixed my mistake just now. – Victor Zakharov Sep 14 '15 at 13:51
  • sorry but in my pdf I see only the second bytes array – Silvia Parfeni Sep 14 '15 at 14:16
  • @SilviaParfeni: What do you mean by `the second bytes array` ? It does not matter which bytes array to append, first, second and even third will work in exact same way. – Victor Zakharov Sep 14 '15 at 14:18
  • I don't now if is a good Idea to send you my string, but here I can't, is to large length = 62372. normally I have 2 strings, this is tow pdf files, then I write in 2 different files all is ok, but I must to write this in one file, but I can not do do this – Silvia Parfeni Sep 14 '15 at 14:31
  • @SilviaParfeni: 62KB is nothing, not to be pasted here, but for processing purposes. You can easily use `AppendAllBytes` and won't notice any performance impact. Can you append 1 byte? 2 bytes? ... 62372 bytes? Where did the problem occur? If you cannot append 1 byte, please provide this byte code and how to reproduce the problem. If you can append 20 bytes, but not 21, for example, please provide both strings (or byte codes). This is called making a reduced test case, part of standard troubleshooting in development, and is something you need to do *before* asking a question on SO. – Victor Zakharov Sep 14 '15 at 15:06
2

With this line:

fs.Write(binaryData1, binaryData.Length + 1, binaryData1.Length)

Specifically the second argument (binaryData.Length + 1), you're telling it to start appending from the wrong position of binaryData1. If it's 3 bytes long, for example, and so is binaryData, it won't append anything. It should be similar to the first .Write line:

fs.Write(binaryData1, 0, binaryData1.Length)

So it appends all of binaryData1. It will still append it after binaryData - you don't need to specify the length of the preceeding binaryData in this line.


Alternatively, bypassing the above entirely, concatenate your two strings before encoding/writing them to the file:

Dim fs As FileStream = New FileStream(fullFileName, FileMode.CreateNew)
fs.Close()
fs = New FileStream(fullFileName, FileMode.Append)
Dim str As String = GetPDFString(id, token, depot, 25)
Dim str1 As String = GetPDFString(id, token, depot, 27)
Dim binaryData As Byte() = Convert.FromBase64String(str & str1) 'concatenate strings
fs.Write(binaryData, 0, binaryData.Length)
fs.Close()
James Thorpe
  • 31,411
  • 5
  • 72
  • 93
  • I tried to write also so: fs.Write(binaryData1, 0, binaryData1.Length), but is the same result, the second string is not appending, if I write in the different files, all ist ok – Silvia Parfeni Sep 14 '15 at 13:20
  • How are you checking if it's being appended? Are you viewing the raw bytes in the file, or opening the PDF? It could be that appending this information at this position in the file isn't correct as per the PDF spec, so is being ignored etc. If this is the case, my edit may help. – James Thorpe Sep 14 '15 at 13:23
  • I don't now where is the problem, but the result is not that I need :(, thank you for help – Silvia Parfeni Sep 14 '15 at 13:32
  • if I concatenated the strings binary data is Nothing – Silvia Parfeni Sep 14 '15 at 13:56