Hi, is it possible to flatten PDF using PDF clown ? ..if so, how can I achieve that please ?
Grazzie mille! :)
yes, form flattening is possible, although no helper tool has been written yet; you have basically to merge the page content stream with the current appearance (FormXObject) of the widgets associated to field annotations, minding to update the page resource dictionary accordingly. Here they are, some of the classes involved:
Acroform fields : obtain them through File.getDocument().getForm().getFields();
Widgets : obtain them through Field.getWidgets();
Appearance : obtain it through Widget.getAppearance();
FormXObject : this is the content stream representation of an appearance state of a widget - obtain it through Appearance.getNormal();
PageStamper : inject the widget appearance into the page through it.
As it seems to be a quite popular task, I'll write a tool in the next weeks, after the 0.1.1 release.
Thanks so much for the PdfClown library - it's great! I downloaded the code from branches -> 0.1.1 fix and can now successfully fill the form fields in my pdf. However, I don't know how to flatten them. Did you have a chance to write the tool you mentioned above? Alternatively, do you have a code sample that would show how to flatten fields in a form?
the tool is waiting for volunteering - in the past years I worked hard to make PDF Clown a rich and flexible library ready for advanced applications; now it's time that someone else gets his/her hands dirty and contributes some honest piece of code.
Here's a basic function to flatten form fields using the pdfClown library. (Sorry - it's VB.NET because that's the language I'm using right now.) It's somewhat primitive in the sense that it expects the field to only have one widget. Also, it may not reflect a proper understanding of the PDFClown library since I fully admit that I don't have one. However, it works and may be a good starting point for anyone looking to do the same thing.
Private Sub FlattenField(ByVal fld As org.pdfclown.documents.interaction.forms.Field, ByVal page As org.pdfclown.documents.Page)
Dim stamper As New org.pdfclown.tools.PageStamper(page)
Dim font As New StandardType1Font(fld.Document, StandardType1Font.FamilyEnum.Courier, False, False)
Dim fontColor As DeviceRGBColor = DeviceRGBColor.Black
Dim foreground As PrimitiveComposer = stamper.Foreground
foreground.ShowText(fld.Value.ToString, New PointF(fld.Widgets(0).Box.X, fld.Widgets(0).Box.Y))
I attempted to follow your directions above, however, I have been unsuccessful. Can you please take a look at the code snippet and point me in the right direction? This code snipped throws an "InvalidCastException" inside the FormXObject.Wrap function.
stamper = new PageStamper();
foreach (Field field in pdf.Form.Fields.Values)
field.Value = "X";
foreach (Widget widget in field.Widgets)
stamper.Page = widget.Page;
I found out that my code snippet above does work, but only for TextFields. It is choking on all other form field types.
Sign up for the SourceForge newsletter:
You seem to have CSS turned off.
Please don't fill out this field.