J
Jeffrey D. Gordon
I'm wanting to replace Field Values in an existing PDF, I've done this
with PHP by doing a replace in the file.
I've been able to read the file in a byte array in c# but all my
attempts to replace strings in a binary file have led to failure for the
last couple of days.
I've stared at this function for too long I've become crosseyed
Any suggestions, or is there an easier way to replace in a byte array og
which I'm not aware?
With thanks,
JDG
------------------ BEGIN CODE -------------------
private byte[] ReplaceInByteArray(byte[] OriginalArray, byte[] Find,
byte[] Replace)
{
byte[] ReturnValue = OriginalArray;
if(System.Array.BinarySearch(ReturnValue, Find) > -1 )
{
byte[] NewReturnValue;
int lFoundPosition;
int lCurrentPosition;
while(System.Array.IndexOf(ReturnValue, Find) > -1 )
{
NewReturnValue = new byte[ReturnValue.Length + Find.Length -
Replace.Length];
lFoundPosition = System.Array.IndexOf(ReturnValue, Find);
lCurrentPosition = 0;
for(int x = 0; x < lFoundPosition; x++)
{
NewReturnValue[x] = ReturnValue[x];
lCurrentPosition++;
}
for(int y = 0; y < Replace.Length; y++)
{
NewReturnValue[y + lCurrentPosition] = Replace[y];
}
lCurrentPosition = lCurrentPosition + Replace.Length;
while(lCurrentPosition < NewReturnValue.Length)
{
NewReturnValue[lCurrentPosition] = ReturnValue[lCurrentPosition -
Replace.Length + Find.Length];
lCurrentPosition++;
}
ReturnValue = NewReturnValue;
}
}
return ReturnValue;
}
------------------ END CODE -------------------
with PHP by doing a replace in the file.
I've been able to read the file in a byte array in c# but all my
attempts to replace strings in a binary file have led to failure for the
last couple of days.
I've stared at this function for too long I've become crosseyed

Any suggestions, or is there an easier way to replace in a byte array og
which I'm not aware?
With thanks,
JDG
------------------ BEGIN CODE -------------------
private byte[] ReplaceInByteArray(byte[] OriginalArray, byte[] Find,
byte[] Replace)
{
byte[] ReturnValue = OriginalArray;
if(System.Array.BinarySearch(ReturnValue, Find) > -1 )
{
byte[] NewReturnValue;
int lFoundPosition;
int lCurrentPosition;
while(System.Array.IndexOf(ReturnValue, Find) > -1 )
{
NewReturnValue = new byte[ReturnValue.Length + Find.Length -
Replace.Length];
lFoundPosition = System.Array.IndexOf(ReturnValue, Find);
lCurrentPosition = 0;
for(int x = 0; x < lFoundPosition; x++)
{
NewReturnValue[x] = ReturnValue[x];
lCurrentPosition++;
}
for(int y = 0; y < Replace.Length; y++)
{
NewReturnValue[y + lCurrentPosition] = Replace[y];
}
lCurrentPosition = lCurrentPosition + Replace.Length;
while(lCurrentPosition < NewReturnValue.Length)
{
NewReturnValue[lCurrentPosition] = ReturnValue[lCurrentPosition -
Replace.Length + Find.Length];
lCurrentPosition++;
}
ReturnValue = NewReturnValue;
}
}
return ReturnValue;
}
------------------ END CODE -------------------