Pages

Wednesday, January 28, 2015

ITextSharp PDF editing

In any case, you need a font that knows how to display Arabic glyphs:
BaseFont bf = BaseFont.CreateFont(ARIALUNI_TFF, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font f = new Font(bf, 12);
For Adding text
  Dim oldFile As String = "D:\PdfGenerator_VB\nf.pdf"
        Dim newFile As String = "D:\PdfGenerator_VB\newnf.pdf"

        ' open the reader
        Dim reader As New PdfReader(oldFile)
        Dim size As Rectangle = reader.GetPageSizeWithRotation(1)
        Dim document As New Document(size)

        ' open the writer
        Dim fs As New FileStream(newFile, FileMode.Create, FileAccess.Write)
        Dim writer As PdfWriter = PdfWriter.GetInstance(document, fs)
        document.Open()

        ' the pdf content
        Dim cb As PdfContentByte = writer.DirectContent

        ' select the font properties
        Dim bf As BaseFont = BaseFont.CreateFont("C:\Windows\Fonts\Arial.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED)
        cb.SetColorFill(BaseColor.DARK_GRAY)
        cb.SetFontAndSize(bf, 8)
      
        Dim f As New Font(bf, 8)
        Dim ct As New ColumnText(writer.DirectContent)
        ct.RunDirection = PdfWriter.RUN_DIRECTION_RTL
        ct.SetSimpleColumn(50, 50, 480, 270, 24, Element.ALIGN_RIGHT)
        Dim chunk = New Chunk("الأربعاء", f)
        ct.AddElement(chunk)
        ct.Go()


        cb.BeginText()
        text = "28"
        ' put the alignment and coordinates here
        cb.ShowTextAligned(2, text, 405, 260, 0)
        cb.EndText()

        cb.BeginText()
        text = "01"
        ' put the alignment and coordinates here
        cb.ShowTextAligned(2, text, 390, 260, 0)
        cb.EndText()

        cb.BeginText()
        text = "2015"
        ' put the alignment and coordinates here
        cb.ShowTextAligned(2, text, 370, 260, 0)
        cb.EndText()

        ' create the new page and add it to the pdf
        Dim page As PdfImportedPage = writer.GetImportedPage(reader, 1)
        cb.AddTemplate(page, 0, 0)

        ' close the streams and voilá the file should be changed :)
        document.Close()
        fs.Close()
        writer.Close()
        reader.Close()
For Fields using
  Dim pdfTemplate As String = "D:\PdfGenerator_VB\nf.pdf"

        ' title the form
        Me.Text += " - " + PdfTemplate

        ' create a new PDF reader based on the PDF template document
        Dim pdfReader As PdfReader = New PdfReader(pdfTemplate)

        ' create and populate a string builder with each of the 
        ' field names available in the subject PDF
        Dim sb As New StringBuilder()

        Dim de As New DictionaryEntry
        For Each kvp As KeyValuePair(Of String, AcroFields.Item) In pdfReader.AcroFields.Fields
            sb.Append(kvp.Key.ToString() + Environment.NewLine)
        Next

        ' Write the string builder's content to the form's textbox
        textBox1.Text = sb.ToString()
        textBox1.SelectionStart = 0
  Dim pdfTemplate As String = "D:\PdfGenerator_VB\nf.pdf"
        Dim newFile As String = "D:\PdfGenerator_VB\Final_nf.pdf"

        Dim pdfReader As New PdfReader(pdfTemplate)
        Dim pdfStamper As New PdfStamper(pdfReader, New FileStream( _
                    newFile, FileMode.Create))

        Dim pdfFormFields As AcroFields = pdfStamper.AcroFields

        ' set form pdfFormFields

        ' The first worksheet and W-4 form
        pdfFormFields.SetField("untitled1", "1")
        pdfFormFields.SetField("untitled2", "2")
        pdfFormFields.SetField("f1_03(0)", "1")
     ' report by reading values from completed PDF
        Dim sTmp As String = "W-4 Completed for " + pdfFormFields.GetField("f1_09(0)") + " " + _
        pdfFormFields.GetField("f1_10(0)")
        MessageBox.Show(sTmp, "Finished")

        ' flatten the form to remove editting options, set it to false
        ' to leave the form open to subsequent manual edits
        pdfStamper.FormFlattening = True

        ' close the pdf
        pdfStamper.Close()
http://www.dotnettips.info/Post/582/%D9%81%D8%A7%D8%B1%D8%B3%D9%89-%D9%86%D9%88%DB%8C%D8%B3%D9%89-%D9%88-itextsharp

How to Create a PDF with Editable Fields

PDFescape

http://smallbusiness.chron.com/create-pdf-editable-fields-55640.html