Ejemplo n.º 1
0
        public object save()
        {
            string templateFile = "/Users/user/Downloads/1022out.pdf";
            string outFile      = "/Users/user/Downloads/1022out.html";

            //  ConvertPdfStreamToHtml();

            iTextSharp.text.pdf.PdfReader pdfReader = null;
            PdfStamper pdfStamper = null;

            try
            {
                pdfReader = new iTextSharp.text.pdf.PdfReader(templateFile);
                PRAcroForm s             = pdfReader.AcroForm;
                AcroFields pdfFormFields = pdfReader.AcroFields;

                foreach (var item in pdfFormFields.Fields)
                {
                    var    d    = item.Value.GetMerged(0);
                    int    type = pdfFormFields.GetFieldType(item.Key);
                    string aaac = pdfFormFields.GetField(item.Key);
                    PRAcroForm.FieldInformation aaad = s.GetField(item.Key);


                    PdfString aae = aaad.Info.GetAsString(PdfName.TU);

                    if (aae == null)
                    {
                        continue;
                    }
                    //     Console.WriteLine(item.Key+":"+type.ToString);
                    Console.WriteLine("===={0},{1},{2},{3}===", item.Key, type, aaac, aae.ToString());

                    if (type == 2)
                    {
                        string[] aaa = pdfFormFields.GetAppearanceStates(item.Key);
                        Console.WriteLine("{0}", string.Join(",", aaa));
                    }


                    //       PrintProperties(item);
                    var str = d.Get(PdfName.V);
                    if (!string.IsNullOrEmpty(str?.ToString()))
                    {
                        var    p = (str.GetBytes()[0] << 8) + str.GetBytes()[1];
                        string code;
                        switch (p)
                        {
                        case 0xefbb:
                            code = "UTF-8";
                            break;

                        case 0xfffe:
                            code = "Unicode";
                            break;

                        case 0xfeff:
                            code = "UTF-16BE";
                            break;

                        default:
                            code = "GBK";
                            break;
                        }
                        var value = Encoding.GetEncoding(code).GetString(str.GetBytes());
                        Console.WriteLine(item.Key + ":" + value);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("" + ex.Message);
            }
            finally
            {
                if (pdfStamper != null)
                {
                    pdfStamper.Close();
                }
                if (pdfReader != null)
                {
                    pdfReader.Close();
                }
            }



            return(DateTime.Now);
        }
Ejemplo n.º 2
0
    private void GetFormFields(Stream source)
    {
        PdfReader reader = null;

        try {
            reader = new PdfReader(source);
            PRAcroForm form = reader.AcroForm;
            if (form == null)
            {
                //ac.debugText("This document has no fields.");
                return;
            }
            //PdfLister list = new PdfLister(System.out);
            Hashtable refToField = new Hashtable();
            ArrayList fields     = form.Fields;
            foreach (PRAcroForm.FieldInformation field in fields)
            {
                refToField.Add(field.Ref.Number, field);
            }
            for (int page = 1; page <= reader.NumberOfPages; page++)
            {
                PdfDictionary dPage  = reader.GetPageN(page);
                PdfArray      annots = (PdfArray)PdfReader.GetPdfObject((PdfObject)dPage.Get(PdfName.ANNOTS));
                if (annots == null)
                {
                    break;
                }

                ArrayList           ali  = annots.ArrayList;
                PRIndirectReference iRef = null;
                foreach (PdfObject refObj in ali)
                {
                    PdfDictionary an   = (PdfDictionary)PdfReader.GetPdfObject(refObj);
                    PdfName       name = (PdfName)an.Get(PdfName.SUBTYPE);
                    if (name == null || !name.Equals(PdfName.WIDGET))
                    {
                        break;
                    }


                    PdfArray rect = (PdfArray)PdfReader.GetPdfObject(an.Get(PdfName.RECT));

                    string fName = "";

                    PRAcroForm.FieldInformation field = null;

                    while ((an != null))
                    {
                        PdfString tName = (PdfString)an.Get(PdfName.T);
                        if ((tName != null))
                        {
                            fName = tName.ToString() + "." + fName;
                        }
                        if (refObj.IsIndirect() && field == null)
                        {
                            iRef  = (PRIndirectReference)refObj;
                            field = (PRAcroForm.FieldInformation)refToField[iRef.Number];
                        }
                        //refObj = (PdfObject)an.Get(PdfName.PARENT);
                        an = (PdfDictionary)PdfReader.GetPdfObject((PdfObject)an.Get(PdfName.PARENT));
                    }
                    if (fName.EndsWith("."))
                    {
                        fName = fName.Substring(0, fName.Length - 1);
                    }

                    PDFFieldLocation tempLoc = new PDFFieldLocation();
                    ArrayList        arr     = rect.ArrayList;

                    tempLoc.fieldName = fName;
                    tempLoc.page      = page;
                    tempLoc.x1        = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)arr[0])).FloatValue;
                    tempLoc.y1        = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)arr[1])).FloatValue;
                    tempLoc.x2        = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)arr[2])).FloatValue;
                    tempLoc.y2        = ((PdfNumber)PdfReader.GetPdfObject((PdfObject)arr[3])).FloatValue;

                    this.PFDlocs.Add(tempLoc);
                }
            }
        }
        catch (Exception e) {
            throw new Exception("Critical Exception in GetFormFields", e);
        }
        finally {
            if ((reader != null))
            {
                reader.Close();
            }
        }
    }