Falls es jemand interessiert: Hier das Workaround. Hässlich, geht aber.
static bool ReplaceBinary(ref byte[] fileBytes, byte[] oldBytes, byte[] newBytes)
{
int index = IndexOfBytes(fileBytes, oldBytes);
if (index < 0)
{
// Text was not found
return false;
}
byte[] newFileBytes =
new byte[fileBytes.Length + newBytes.Length - oldBytes.Length];
Buffer.BlockCopy(fileBytes, 0, newFileBytes, 0, index);
Buffer.BlockCopy(newBytes, 0, newFileBytes, index, newBytes.Length);
Buffer.BlockCopy(fileBytes, index + oldBytes.Length,
newFileBytes, index + newBytes.Length,
fileBytes.Length - index - oldBytes.Length);
fileBytes = new byte[newFileBytes.Length];
Buffer.BlockCopy(newFileBytes, 0, fileBytes, 0, newFileBytes.Length);
return true;
}
static int ReplaceTextInFile(string pdfFileName, string tempFileName,
string oldText, string newText)
{
byte[] fileBytes = File.ReadAllBytes(pdfFileName),
// https://stackoverflow.com/questions/28062565/how-can-i-replace-a-unicode-string-in-a-binary-file
oldBytes = Encoding.UTF8.GetBytes(oldText),
newBytes = Encoding.UTF8.GetBytes(newText);
int replaceCount = 0;
while (ReplaceBinary(ref fileBytes, oldBytes, newBytes)) {
replaceCount++;
};
Trace.WriteLine("Replace date formats replaced " +replaceCount + " repeats");
if (replaceCount!= 0)
File.WriteAllBytes(tempFileName, fileBytes);
return replaceCount;
}
static int IndexOfBytes(byte[] searchBuffer, byte[] bytesToFind)
{
for (int i = 0; i < searchBuffer.Length - bytesToFind.Length; i++)
{
bool success = true;
for (int j = 0; j < bytesToFind.Length; j++)
{
if (searchBuffer[i + j] != bytesToFind[j])
{
success = false;
break;
}
}
if (success)
{
return i;
}
}
return -1;
}
private void ReplaceDateFormats(string src)
{
// Brute force method to get around a bug in LibreOffice
string dest = Path.GetTempFileName();
// Check if replace yyyy is also required
if (ReplaceTextInFile(src, dest, "dd/mm/yy", "dd.mm.yyyy") > 0)
File.Move(dest, src, true);
}
}