![]() |
Delphi-Version: XE8
Broken TBytes
Delphi-Quellcode:
2nd file is corrupted. there is a bunch of wrong bytes at the end of the file. if you notepad the 2nd pdf and remove said bytes. everything is normal
procedure WhyIsItNotWorking;
var Stream : TBytesStream; b : TBytes ; begin Stream := TBytesStream.Create; Stream.LoadFromFile( AttachedPDF ); b := Stream.Bytes ; Stream := TBytesStream.Create( b ); Stream.SaveToFile( AttachedPDF + '2' ); FreeAndNil(Stream); end; ![]() this is a form/fillable pdf above code works with 'regular' pdf |
AW: Broken TBytes
Please format your code using the Delphi tags.
Edit your post, select your Code and press that little helmet button. This will surround your Code with [ CODE="Delphi"] [ /CODE] Tags. Then save. |
AW: Broken TBytes
Hi,
your TBytes Array doesn't have a defined size. May this will help:
Delphi-Quellcode:
Cheers Klaus
setlength(b,stream.size);
stream.ReadBuffer(b,stream.size); |
AW: Broken TBytes
Just use the debugger to inspect your
Delphi-Quellcode:
after you've done your
b: TBytes
Delphi-Quellcode:
.
b := Stream.Bytes;
It already contains more bytes than your original file. Since the Bytes property just reads out an ordinary field, something must have gone wrong in
Delphi-Quellcode:
.
LoadFromFile
The root cause is the protected method
Delphi-Quellcode:
of
Realloc
Delphi-Quellcode:
, the parent of your
TMemoryStream
Delphi-Quellcode:
. It makes sure the internal byte array is always aligned to a multiple of 8KB.
TBytesStream
This way, the Byte property is garbage, you will have to truncate it afterwards, just as Klaus already said:
Delphi-Quellcode:
procedure WhyIsItNotWorking();
const path = 'x:\cake.pdf'; var Stream : TBytesStream; b : TBytes ; begin Stream := TBytesStream.Create(); Stream.LoadFromFile(path); b := Stream.Bytes; SetLength(b, Stream.Size); // This is new Stream := TBytesStream.Create(b); Stream.SaveToFile( path.Replace('cake', 'cake2') ); FreeAndNil(Stream); // You know that you did not destroy the first TStream instance? end; I would have made the same mistake. I don't think this was to be expected. |
AW: Broken TBytes
It is documented and works as designed:
Zitat:
![]() |
AW: Broken TBytes
Thanks everyone. Please note that the 'issue' didn't happen on android
|
AW: Broken TBytes
Zitat:
|
Alle Zeitangaben in WEZ +1. Es ist jetzt 06:25 Uhr. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz