Recent

Author Topic: [Solved] How to unpack a stream from a pdf object?  (Read 1773 times)

KoBraSoft

  • New Member
  • *
  • Posts: 22
[Solved] How to unpack a stream from a pdf object?
« on: May 20, 2024, 02:29:42 pm »
Hi,
i would like to unpack a stream in a pdf object (a part of a pdf file).
Please find attached a sample project and a sample invoice. (This sample project only compiles with trunk!)

This object (Doc.Objects[27]) should contain some xml data.

This xml data is used for Zugferd (Germany) and faktur-x (France) invoices.

Konrad
« Last Edit: May 21, 2024, 06:54:11 pm by KoBraSoft »

rvk

  • Hero Member
  • *****
  • Posts: 6587
Re: How to unpack a stream from a pdf object?
« Reply #1 on: May 21, 2024, 10:37:20 am »
With no error checking and assuming your stream is object 27 AND /FlateDecode ...

Code: Pascal  [Select][+][-]
  1. uses ZStream;
  2.  
  3. procedure Decompress(ASrcStream, ADestStream: TStream);
  4. var
  5.   decompressor: TDecompressionStream;
  6. begin
  7.   decompressor := TDecompressionStream.Create(ASrcStream, false);
  8.   try
  9.     ADestStream.CopyFrom(decompressor, 0);  // 0 --> entire src stream
  10.   finally
  11.     decompressor.Free;
  12.   end;
  13. end;
  14.  
  15. var
  16.   S: ansistring;
  17.   instream: TMemoryStream;
  18.   outstream: TStringStream;
  19.  
  20. //...
  21.  
  22.   instream := TMemoryStream.Create;
  23.   outstream := TStringStream.Create;
  24.   try
  25.     instream.WriteBuffer(TPdfIndirect(Doc.Objects[27]).Stream.Data,Length(TPdfIndirect(Doc.Objects[27]).Stream.Data));
  26.     instream.Position := 0;
  27.     Decompress(instream, outstream);
  28.     S := outstream.DataString;
  29.   finally
  30.     instream.free;
  31.     outstream.free;
  32.   end;
  33.  
  34.   Memo1.Append(S);

This will give you:

Quote
<?xml version='1.0' encoding='UTF-8' ?>
<!-- English disclaimer below.-->
<!--Nutzungsrechte
ZUGFeRD Datenformat Version 2.2.0, 14.02.2022
Beispiel Version 14.02.2022
 
Zweck des Forums elektronisch Rechnung Deutschland, welches am 31. März 2010 unter der Arbeitsgemeinschaft für
<snip> rest of XML

KoBraSoft

  • New Member
  • *
  • Posts: 22
[Solved] Re: How to unpack a stream from a pdf object?
« Reply #2 on: May 21, 2024, 06:53:34 pm »
Perfect.
This is exactly what I was looking for.
Thank you.

 

TinyPortal © 2005-2018