Inheritance: PdfArray
Example #1
0
        virtual internal protected void FlatFields() {
            if (append)
                throw new ArgumentException(MessageLocalization.GetComposedMessage("field.flattening.is.not.supported.in.append.mode"));
            GetAcroFields();
            IDictionary<String, AcroFields.Item> fields = acroFields.Fields;
            if (fieldsAdded && partialFlattening.IsEmpty()) {
                foreach (String obf in fields.Keys) {
                    partialFlattening.Add(obf);
                }
            }
            PdfDictionary acroForm = reader.Catalog.GetAsDict(PdfName.ACROFORM);
            PdfArray acroFds = null;
            if (acroForm != null) {
                acroFds = (PdfArray)PdfReader.GetPdfObject(acroForm.Get(PdfName.FIELDS), acroForm);
            }
            foreach (KeyValuePair<String, AcroFields.Item> entry in fields) {
                String name = entry.Key;
                if (!partialFlattening.IsEmpty() && !partialFlattening.Contains(name))
                    continue;
                AcroFields.Item item = entry.Value;
                for (int k = 0; k < item.Size; ++k) {
                    PdfDictionary merged = item.GetMerged(k);
                    PdfNumber ff = merged.GetAsNumber(PdfName.F);
                    int flags = 0;
                    if (ff != null)
                        flags = ff.IntValue;
                    int page = item.GetPage(k);
                    if (page < 1)
                	    continue;
                    PdfDictionary appDic = merged.GetAsDict(PdfName.AP);
                    PdfObject as_n = null;
                    if (appDic != null) {
                        as_n = appDic.GetAsStream(PdfName.N);
                        if (as_n == null)
                            as_n = appDic.GetAsDict(PdfName.N);
                    }
                    if (acroFields.GenerateAppearances) {
                        if (appDic == null || as_n == null) {
                            try {
                                acroFields.RegenerateField(name);
                                appDic = acroFields.GetFieldItem(name).GetMerged(k).GetAsDict(PdfName.AP);
                            }
                            // if we can't create appearances for some reason, we'll just continue
                            catch (DocumentException) {
                            }
                        } else if (as_n.IsStream()) {
                            PdfStream stream = (PdfStream) as_n;
                            PdfArray bbox = stream.GetAsArray(PdfName.BBOX);
                            PdfArray rect = merged.GetAsArray(PdfName.RECT);
                            if (bbox != null && rect != null) {
                                float rectWidth = rect.GetAsNumber(2).FloatValue - rect.GetAsNumber(0).FloatValue;
                                float bboxWidth = bbox.GetAsNumber(2).FloatValue - bbox.GetAsNumber(0).FloatValue;
                                float rectHeight = rect.GetAsNumber(3).FloatValue - rect.GetAsNumber(1).FloatValue;
                                float bboxHeight = bbox.GetAsNumber(3).FloatValue - bbox.GetAsNumber(1).FloatValue;
                                float widthCoef = Math.Abs(bboxWidth != 0 ? rectWidth / bboxWidth : float.MaxValue);
                                float heightCoef = Math.Abs(bboxHeight != 0 ? rectHeight / bboxHeight : float.MaxValue);

                                if (widthCoef != 1 || heightCoef != 1)
                                {
                                    NumberArray array = new NumberArray(widthCoef, 0, 0, heightCoef, 0, 0);
                                    stream.Put(PdfName.MATRIX, array);
                                    MarkUsed(stream);
                                }
                            }
                        }
                    } else if (appDic != null && as_n != null) {
                        PdfArray bbox = ((PdfDictionary) as_n).GetAsArray(PdfName.BBOX);
                        PdfArray rect = merged.GetAsArray(PdfName.RECT);
                        if (bbox != null && rect != null) {
                            float widthDiff = (bbox.GetAsNumber(2).FloatValue - bbox.GetAsNumber(0).FloatValue) -
                                              (rect.GetAsNumber(2).FloatValue - rect.GetAsNumber(0).FloatValue);
                            float heightDiff = (bbox.GetAsNumber(3).FloatValue - bbox.GetAsNumber(1).FloatValue) -
                                               (rect.GetAsNumber(3).FloatValue - rect.GetAsNumber(1).FloatValue);
                            if (Math.Abs(widthDiff) > 1 || Math.Abs(heightDiff) > 1) {
                                try {
                                    //simulate Adobe behavior.
                                    acroFields.GenerateAppearances = true;
                                    acroFields.RegenerateField(name);
                                    acroFields.GenerateAppearances = false;
                                    appDic = acroFields.GetFieldItem(name).GetMerged(k).GetAsDict(PdfName.AP);
                                }
                                // if we can't create appearances for some reason, we'll just continue
                                catch (DocumentException) { }
                            }
                        }
                    }
                    if (appDic != null && (flags & PdfFormField.FLAGS_PRINT) != 0 && (flags & PdfFormField.FLAGS_HIDDEN) == 0) {
                        PdfObject obj = appDic.Get(PdfName.N);
                        PdfAppearance app = null;
                        if (obj != null) {
                            PdfObject objReal = PdfReader.GetPdfObject(obj);
                            if (obj is PdfIndirectReference && !obj.IsIndirect())
                                app = new PdfAppearance((PdfIndirectReference)obj);
                            else if (objReal is PdfStream) {
                                ((PdfDictionary)objReal).Put(PdfName.SUBTYPE, PdfName.FORM);
                                app = new PdfAppearance((PdfIndirectReference)obj);
                            }
                            else {
                                if (objReal != null && objReal.IsDictionary()) {
                                    PdfName as_p = merged.GetAsName(PdfName.AS);
                                    if (as_p != null) {
                                        PdfIndirectReference iref = (PdfIndirectReference)((PdfDictionary)objReal).Get(as_p);
                                        if (iref != null) {
                                            app = new PdfAppearance(iref);
                                            if (iref.IsIndirect()) {
                                                objReal = PdfReader.GetPdfObject(iref);
                                                ((PdfDictionary)objReal).Put(PdfName.SUBTYPE, PdfName.FORM);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        if (app != null) {
                            Rectangle box = PdfReader.GetNormalizedRectangle(merged.GetAsArray(PdfName.RECT));
                            PdfContentByte cb = GetOverContent(page);
                            cb.SetLiteral("Q ");
                            cb.AddTemplate(app, box.Left, box.Bottom);
                            cb.SetLiteral("q ");
                        }
                    }
                    if (partialFlattening.IsEmpty())
                        continue;
                    PdfDictionary pageDic = reader.GetPageN(page);
                    PdfArray annots = pageDic.GetAsArray(PdfName.ANNOTS);
                    if (annots == null)
                        continue;
                    for (int idx = 0; idx < annots.Size; ++idx) {
                        PdfObject ran = annots.GetPdfObject(idx);
                        if (!ran.IsIndirect())
                            continue;
                        PdfObject ran2 = item.GetWidgetRef(k);
                        if (!ran2.IsIndirect())
                            continue;
                        if (((PRIndirectReference)ran).Number == ((PRIndirectReference)ran2).Number) {
                            annots.Remove(idx--);
                            PRIndirectReference wdref = (PRIndirectReference)ran2;
                            while (true) {
                                PdfDictionary wd = (PdfDictionary)PdfReader.GetPdfObject(wdref);
                                PRIndirectReference parentRef = (PRIndirectReference)wd.Get(PdfName.PARENT);
                                PdfReader.KillIndirect(wdref);
                                if (parentRef == null) { // reached AcroForm
                                    for (int fr = 0; fr < acroFds.Size; ++fr) {
                                        PdfObject h = acroFds.GetPdfObject(fr);
                                        if (h.IsIndirect() && ((PRIndirectReference)h).Number == wdref.Number) {
                                            acroFds.Remove(fr);
                                            --fr;
                                        }
                                    }
                                    break;
                                }
                                PdfDictionary parent = (PdfDictionary)PdfReader.GetPdfObject(parentRef);
                                PdfArray kids = parent.GetAsArray(PdfName.KIDS);
                                for (int fr = 0; fr < kids.Size; ++fr) {
                                    PdfObject h = kids.GetPdfObject(fr);
                                    if (h.IsIndirect() && ((PRIndirectReference)h).Number == wdref.Number) {
                                        kids.Remove(fr);
                                        --fr;
                                    }
                                }
                                if (!kids.IsEmpty())
                                    break;
                                wdref = parentRef;
                            }
                        }
                    }
                    if (annots.IsEmpty()) {
                        PdfReader.KillIndirect(pageDic.Get(PdfName.ANNOTS));
                        pageDic.Remove(PdfName.ANNOTS);
                    }
                }
            }
            if (!fieldsAdded && partialFlattening.IsEmpty()) {
                for (int page = 1; page <= reader.NumberOfPages; ++page) {
                    PdfDictionary pageDic = reader.GetPageN(page);
                    PdfArray annots = pageDic.GetAsArray(PdfName.ANNOTS);
                    if (annots == null)
                        continue;
                    for (int idx = 0; idx < annots.Size; ++idx) {
                        PdfObject annoto = annots.GetDirectObject(idx);
                        if ((annoto is PdfIndirectReference) && !annoto.IsIndirect())
                            continue;
                        if (!annoto.IsDictionary() || PdfName.WIDGET.Equals(((PdfDictionary)annoto).Get(PdfName.SUBTYPE))) {
                            annots.Remove(idx);
                            --idx;
                        }
                    }
                    if (annots.IsEmpty()) {
                        PdfReader.KillIndirect(pageDic.Get(PdfName.ANNOTS));
                        pageDic.Remove(PdfName.ANNOTS);
                    }
                }
                EliminateAcroformObjects();
            }
        }
 /**
  * An array of numbers that shall be taken pairwise to define points in a 2D
  * unit square. The unit square is mapped to the rectangular bounds of the
  * {@link Viewport}, image XObject, or forms XObject that contains the
  * measure dictionary. This array shall contain the same number of number
  * pairs as the GPTS array; each number pair is the unit square object
  * position corresponding to the geospatial position in the GPTS array.
  *
  * @param pairedpoints
  */
 virtual public void SetLPTS(NumberArray pairedpoints) {
     Put(PdfName.LPTS, pairedpoints);
 }
 /**
  * An array of numbers that shall be taken pairwise to define a series of
  * points that describes the bounds of an area for which geospatial
  * transformations are valid.
  *
  * @param bounds
  */
 virtual public void SetBounds(NumberArray bounds) {
     base.Put(PdfName.BOUNDS, bounds);
 }
Example #4
0
 /**
  * An array of numbers that shall be taken pairwise, defining points in
  * geographic space as degrees of latitude and longitude. These values shall
  * be based on the geographic coordinate system described in the GCS
  * dictionary.
  *
  * @param pairedpoints
  */
 public void SetGPTS(NumberArray pairedpoints) {
     Put(PdfName.GPTS, pairedpoints);
 }