PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Setting a PdfStream's Value does not respect existing Filter
https://forum.pdfsharp.net/viewtopic.php?f=3&t=3468
Page 1 of 1

Author:  (void) [ Thu Sep 29, 2016 5:16 pm ]
Post subject:  Setting a PdfStream's Value does not respect existing Filter

While working on AcroFields, i stumbled over a problem when setting a PdfStream's Value.

The problem manifested as described here: viewtopic.php?f=2&t=894
e.g. Field-Values did not show up in the Reader-Application (Foxit in my case), until you click inside the Field.

In my case the problem could be traced down to this:
- I opened an existing Document in modify-mode to fill existing Form-Fields
- Setting the Text-Property of a PdfTextField created a new Appearance-Stream with the RenderAppearance method
- Stream-Data was put into existing /AP - Entry

The last step is the important bit.
As the Field already had an Appearance-Dictionary, the existing value was simply overwritten.
The problem was, there was a /Filter - Entry present in the dictionary with a value of /FlateDecode...

I assume, the Reader-Application tried to decode the stream with the FlateDecode-Filter but failed, (because the data was NOT encoded) hence not displaying the Text written into the Field.

The proposed fix is to either respect the existing filter or to remove the /Filter - Entry.

I resorted to simply remove the /Filter - Entry from the parent-dictionary with this line at the end of the setter (PdfStream.Value)

Code:
this.owner.elements.Remove(Keys.Filter);


This fixed it at least for me, Field-Values were now showing up correctly when opening the document.

Author:  Thomas Hoevel [ Mon Oct 24, 2016 11:58 am ]
Post subject:  Re: Setting a PdfStream's Value does not respect existing Fi

(void) wrote:
The proposed fix is to either respect the existing filter or to remove the /Filter - Entry.
I think it is a feature, not a bug: when setting a property (e.g. Text) you have to make sure that all related properties (Filter in this case) also get set accordingly.
You can also assign a filtered object to the Text property.

I think it would be nice to have a helper method that sets both Text and Filter at the same time.

Author:  (void) [ Mon Nov 07, 2016 10:34 pm ]
Post subject:  Re: Setting a PdfStream's Value does not respect existing Fi

Quote:
I think it is a feature, not a bug: when setting a property (e.g. Text) you have to make sure that all related properties (Filter in this case) also get set accordingly.
You can also assign a filtered object to the Text property.

I think, we might have a misunderstanding here.
Why should the average user had to deal with filters and the like, when setting the "Text"-Property of a PdfTextField ?
What kind of "filtered object" should that be ?

To clarify my point, the code-flow that leads to the problem:
- user sets the Text-Property of a PdfTextField
- from the PdfSharp sources, the Setter of the Text-Property :
Code:
set { Elements.SetString(Keys.V, value); RenderAppearance(); } //HACK in PdfTextField

- RenderAppearance creates a new ApperanceStream (or uses the existing one) and sets its value :
Quote:
xobj.Stream.Value = new RawEncoding().GetBytes(s);

The last line may cause a problem when the Stream already exists with a filter present.

This all happens inside of PdfSharp, so i thought, i point this out.

Quote:
I think it would be nice to have a helper method that sets both Text and Filter at the same time.

I agree, but not so much for the Text-Property of a PdfTextField, but more for the Value-Property of a PdfStream.
Setting the Text-Property should be a transparent operation for the user.

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/