Flattening PDF

Help
brum17
2011-11-04
2013-01-26
  • brum17
    brum17
    2011-11-04

    Hi, is it possible to flatten PDF using PDF clown ? ..if so, how can I achieve that please ?

    Grazzie mille! :)

     
  • tjuni
    tjuni
    2012-05-01

    Hi Stefano,

    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?

    Thanks!

    tjuni

     
  • Hi tjuni,

    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.

    Stefano

     
  • tjuni
    tjuni
    2012-06-05

    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.SetFont(font, 12)
            foreground.SetFillColor(fontColor)

            foreground.ShowText(fld.Value.ToString, New PointF(fld.Widgets(0).Box.X, fld.Widgets(0).Box.Y))
            stamper.Flush()
            fld.Delete()
        End Sub

     
  • Seth
    Seth
    2012-10-17

    stechio,

    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;
                      stamper.Foreground.ShowXObject(FormXObject.Wrap(widget.Appearance.Normal.BaseObject));
                      stamper.Flush();
                   }
                   field.Delete();
                }
    

    Thanks,
    Seth

     
  • Seth
    Seth
    2012-10-17

    I found out that my code snippet above does work, but only for TextFields.  It is choking on all other form field types.