Inheritance: PdfObject
 /**
 * Sets the value of the collection item.
 * @param value
 */
 public void AddItem(String key, PdfNumber n) {
     PdfName fieldname = new PdfName(key);
     PdfCollectionField field = (PdfCollectionField)schema.Get(fieldname);
     if (field.fieldType == PdfCollectionField.NUMBER) {
         Put(fieldname, n);
     }
 }
Example #2
0
        virtual public void CopyStructTreeForPage(PdfNumber sourceArrayNumber, int newArrayNumber) {
//            int documentHash = GetDocumentHash(reader);
//            if (!openedDocuments.ContainsKey(documentHash))
//            {
//                openedDocuments.Add(documentHash, null);
//                AddKid(structureTreeRoot, writer.CopyObject(structTreeRoot.Get(PdfName.K), true, true));
//            }
            if (CopyPageMarks(parentTree, sourceArrayNumber, newArrayNumber) == ReturnType.NOTFOUND) {
                throw new BadPdfFormatException(MessageLocalization.GetComposedMessage("structparent.not.found"));
            }
        }
Example #3
0
 /**
 * Creates a PdfPattern object.
 * @param   painter a pattern painter instance
 * @param   compressionLevel the compressionLevel for the stream
 * @since   2.1.3
 */
 internal PdfPattern(PdfPatternPainter painter, int compressionLevel) : base() {
     PdfNumber one = new PdfNumber(1);
     PdfArray matrix = painter.Matrix;
     if ( matrix != null ) {
         Put(PdfName.MATRIX, matrix);
     }
     Put(PdfName.TYPE, PdfName.PATTERN);
     Put(PdfName.BBOX, new PdfRectangle(painter.BoundingBox));
     Put(PdfName.RESOURCES, painter.Resources);
     Put(PdfName.TILINGTYPE, one);
     Put(PdfName.PATTERNTYPE, one);
     if (painter.IsStencil())
         Put(PdfName.PAINTTYPE, new PdfNumber(2));
     else
         Put(PdfName.PAINTTYPE, one);
     Put(PdfName.XSTEP, new PdfNumber(painter.XStep));
     Put(PdfName.YSTEP, new PdfNumber(painter.YStep));
     bytes = painter.ToPdf(null);
     Put(PdfName.LENGTH, new PdfNumber(bytes.Length));
     FlateCompress(compressionLevel);
 }
Example #4
0
 private ReturnType CopyPageMarks(PdfDictionary parentTree, PdfNumber arrayNumber, int newArrayNumber) {
     PdfArray pages = (PdfArray)GetDirectObject(parentTree.Get(PdfName.NUMS));
     if (pages == null) {
         PdfArray kids = (PdfArray)GetDirectObject(parentTree.Get(PdfName.KIDS));
         if (kids == null)
             return ReturnType.NOTFOUND;
         int cur = kids.Size/2;
         int begin = 0;
         while (true) {
             PdfDictionary kidTree = (PdfDictionary)GetDirectObject(kids[cur + begin]);
             switch (CopyPageMarks(kidTree,arrayNumber,newArrayNumber)) {
                 case ReturnType.FOUND:
                     return ReturnType.FOUND;
                 case ReturnType.ABOVE:
                     begin += cur;
                     cur /= 2;
                     if (cur == 0)
                         cur = 1;
                     if (cur + begin == kids.Size)
                         return ReturnType.ABOVE;
                     break;
                 case ReturnType.BELOW:
                     if (cur + begin == 0)
                         return ReturnType.BELOW;
                     if (cur == 0)
                         return ReturnType.NOTFOUND;
                     cur /= 2;
                     break;
                 default:
                     return ReturnType.NOTFOUND;
             }
         }
     } else {
         if (pages.Size == 0)
             return ReturnType.NOTFOUND;
         return FindAndCopyMarks(pages, arrayNumber.IntValue, newArrayNumber);
     }
 }
 /**
  * The conversion factor used to multiply a value in partial units of the
  * previous number format array element to obtain a value in the units of
  * this dictionary. When this entry is in the first number format in the
  * array, its meaning (that is, what it shall be multiplied by) depends on
  * which entry in the RectilinearMeasure references the NumberFormat
  * array.
  * 
  * @param n
  */
 virtual public void SetConversionFactor(PdfNumber n) {
     base.Put(PdfName.C, n);
 }
 /**
  * A positive integer that shall specify the precision or denominator of a
  * fractional amount:
  * <ul>
  * <li>
  * When the Fractional Value is {@link Fraction#DECIMAL}, this entry shall
  * be the precision of a decimal display; it shall be a multiple of 10.
  * Low-order zeros may be truncated unless FixedDenominator is true. Default
  * value: 100 (hundredths, corresponding to two decimal digits).</li>
  * <li>When the value of F is {@link Fraction#FRACTION}, this entry shall be
  * the denominator of a fractional display. The fraction may be reduced
  * unless the value of FD is true. Default value: 16.</li>
  * </ul>
  * 
  * @param precision
  */
 virtual public void SetPrecision(PdfNumber precision) {
     base.Put(PdfName.D, precision);
 }
 /**
 * Sets a field property. Valid property names are:
 * <p>
 * <ul>
 * <li>flags - a set of flags specifying various characteristics of the field�s widget annotation.
 * The value of this entry replaces that of the F entry in the form�s corresponding annotation dictionary.<br>
 * <li>setflags - a set of flags to be set (turned on) in the F entry of the form�s corresponding
 * widget annotation dictionary. Bits equal to 1 cause the corresponding bits in F to be set to 1.<br>
 * <li>clrflags - a set of flags to be cleared (turned off) in the F entry of the form�s corresponding
 * widget annotation dictionary. Bits equal to 1 cause the corresponding
 * bits in F to be set to 0.<br>
 * <li>fflags - a set of flags specifying various characteristics of the field. The value
 * of this entry replaces that of the Ff entry in the form�s corresponding field dictionary.<br>
 * <li>setfflags - a set of flags to be set (turned on) in the Ff entry of the form�s corresponding
 * field dictionary. Bits equal to 1 cause the corresponding bits in Ff to be set to 1.<br>
 * <li>clrfflags - a set of flags to be cleared (turned off) in the Ff entry of the form�s corresponding
 * field dictionary. Bits equal to 1 cause the corresponding bits in Ff
 * to be set to 0.<br>
 * </ul>
 * @param field the field name
 * @param name the property name
 * @param value the property value
 * @param inst an array of <CODE>int</CODE> indexing into <CODE>AcroField.Item.merged</CODE> elements to process.
 * Set to <CODE>null</CODE> to process all
 * @return <CODE>true</CODE> if the property exists, <CODE>false</CODE> otherwise
 */
 public bool SetFieldProperty(String field, String name, int value, int[] inst)
 {
     if (writer == null)
         throw new Exception("This AcroFields instance is read-only.");
     Item item = (Item)fields[field];
     if (item == null)
         return false;
     InstHit hit = new InstHit(inst);
     if (Util.EqualsIgnoreCase(name, "flags")) {
         PdfNumber num = new PdfNumber(value);
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 item.GetMerged(k).Put(PdfName.F, num);
                 item.GetWidget(k).Put(PdfName.F, num);
                 MarkUsed(item.GetWidget(k));
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "setflags")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 PdfNumber num = item.GetWidget(k).GetAsNumber(PdfName.F);
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val | value);
                 item.GetMerged(k).Put(PdfName.F, num);
                 item.GetWidget(k).Put(PdfName.F, num);
                 MarkUsed(item.GetWidget(k));
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "clrflags")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 PdfDictionary widget = item.GetWidget( k );
                 PdfNumber num = widget.GetAsNumber(PdfName.F);
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val & (~value));
                 item.GetMerged(k).Put(PdfName.F, num);
                 widget.Put(PdfName.F, num);
                 MarkUsed(widget);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "fflags")) {
         PdfNumber num = new PdfNumber(value);
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 item.GetMerged(k).Put(PdfName.FF, num);
                 item.GetValue(k).Put(PdfName.FF, num);
                 MarkUsed(item.GetValue(k));
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "setfflags")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 PdfDictionary valDict = item.GetValue( k );
                 PdfNumber num = valDict.GetAsNumber( PdfName.FF );
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val | value);
                 item.GetMerged(k).Put(PdfName.FF, num);
                 valDict.Put(PdfName.FF, num);
                 MarkUsed(valDict);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "clrfflags")) {
         for (int k = 0; k < item.Size; ++k) {
             if (hit.IsHit(k)) {
                 PdfDictionary valDict = item.GetValue( k );
                 PdfNumber num = valDict.GetAsNumber(PdfName.FF);
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val & (~value));
                 item.GetMerged(k).Put(PdfName.FF, num);
                 valDict.Put(PdfName.FF, num);
                 MarkUsed(valDict);
             }
         }
     }
     else
         return false;
     return true;
 }
 /** Sets the field value and the display string. The display string
 * is used to build the appearance in the cases where the value
 * is modified by Acrobat with JavaScript and the algorithm is
 * known.
 * @param name the fully qualified field name or the partial name in the case of XFA forms
 * @param value the field value
 * @param display the string that is used for the appearance. If <CODE>null</CODE>
 * the <CODE>value</CODE> parameter will be used
 * @return <CODE>true</CODE> if the field was found and changed,
 * <CODE>false</CODE> otherwise
 * @throws IOException on error
 * @throws DocumentException on error
 */
 public bool SetField(String name, String value, String display)
 {
     if (writer == null)
         throw new DocumentException("This AcroFields instance is read-only.");
     if (xfa.XfaPresent) {
         name = xfa.FindFieldName(name, this);
         if (name == null)
             return false;
         String shortName = XfaForm.Xml2Som.GetShortName(name);
         XmlNode xn = xfa.FindDatasetsNode(shortName);
         if (xn == null) {
             xn = xfa.DatasetsSom.InsertNode(xfa.DatasetsNode, shortName);
         }
         xfa.SetNodeText(xn, value);
     }
     Item item = (Item)fields[name];
     if (item == null)
         return false;
     PdfDictionary merged = item.GetMerged( 0 );
     PdfName type = merged.GetAsName(PdfName.FT);
     if (PdfName.TX.Equals(type)) {
         PdfNumber maxLen = merged.GetAsNumber(PdfName.MAXLEN);
         int len = 0;
         if (maxLen != null)
             len = maxLen.IntValue;
         if (len > 0)
             value = value.Substring(0, Math.Min(len, value.Length));
     }
     if (display == null)
         display = value;
     if (PdfName.TX.Equals(type) || PdfName.CH.Equals(type)) {
         PdfString v = new PdfString(value, PdfObject.TEXT_UNICODE);
         for (int idx = 0; idx < item.Size; ++idx) {
             PdfDictionary valueDic = item.GetValue(idx);
             valueDic.Put(PdfName.V, v);
             valueDic.Remove(PdfName.I);
             MarkUsed(valueDic);
             merged = item.GetMerged(idx);
             merged.Remove(PdfName.I);
             merged.Put(PdfName.V, v);
             PdfDictionary widget = item.GetWidget(idx);
             if (generateAppearances) {
                 PdfAppearance app = GetAppearance(merged, display, name);
                 if (PdfName.CH.Equals(type)) {
                     PdfNumber n = new PdfNumber(topFirst);
                     widget.Put(PdfName.TI, n);
                     merged.Put(PdfName.TI, n);
                 }
                 PdfDictionary appDic = widget.GetAsDict(PdfName.AP);
                 if (appDic == null) {
                     appDic = new PdfDictionary();
                     widget.Put(PdfName.AP, appDic);
                     merged.Put(PdfName.AP, appDic);
                 }
                 appDic.Put(PdfName.N, app.IndirectReference);
                 writer.ReleaseTemplate(app);
             }
             else {
                 widget.Remove(PdfName.AP);
                 merged.Remove(PdfName.AP);
             }
             MarkUsed(widget);
         }
         return true;
     }
     else if (PdfName.BTN.Equals(type)) {
         PdfNumber ff = item.GetMerged(0).GetAsNumber(PdfName.FF);
         int flags = 0;
         if (ff != null)
             flags = ff.IntValue;
         if ((flags & PdfFormField.FF_PUSHBUTTON) != 0) {
             //we'll assume that the value is an image in base64
             Image img;
             try {
                 img = Image.GetInstance(Convert.FromBase64String(value));
             }
             catch {
                 return false;
             }
             PushbuttonField pb = GetNewPushbuttonFromField(name);
             pb.Image = img;
             ReplacePushbuttonField(name, pb.Field);
             return true;
         }
         PdfName v = new PdfName(value);
         ArrayList lopt = new ArrayList();
         PdfArray opts = item.GetValue(0).GetAsArray(PdfName.OPT);
         if (opts != null) {
             for (int k = 0; k < opts.Size; ++k) {
                 PdfString valStr = opts.GetAsString(k);
                 if (valStr != null)
                     lopt.Add(valStr.ToUnicodeString());
                 else
                     lopt.Add(null);
             }
         }
         int vidx = lopt.IndexOf(value);
         PdfName valt = null;
         PdfName vt;
         if (vidx >= 0) {
             vt = valt = new PdfName(vidx.ToString());
         }
         else
             vt = v;
         for (int idx = 0; idx < item.Size; ++idx) {
             merged = item.GetMerged(idx);
             PdfDictionary widget = item.GetWidget(idx);
             PdfDictionary valDict = item.GetValue(idx);
             MarkUsed(item.GetValue(idx));
             if (valt != null) {
                 PdfString ps = new PdfString(value, PdfObject.TEXT_UNICODE);
                 valDict.Put(PdfName.V, ps);
                 merged.Put(PdfName.V, ps);
             }
             else {
                 valDict.Put(PdfName.V, v);
                 merged.Put(PdfName.V, v);
             }
             MarkUsed(widget);
             if (IsInAP(widget,  vt)) {
                 merged.Put(PdfName.AS, vt);
                 widget.Put(PdfName.AS, vt);
             }
             else {
                 merged.Put(PdfName.AS, PdfName.Off_);
                 widget.Put(PdfName.AS, PdfName.Off_);
             }
         }
         return true;
     }
     return false;
 }
Example #9
0
 virtual protected internal void ReadObjStm(PRStream stream, IntHashtable map) {
     if (stream == null) return;
     int first = stream.GetAsNumber(PdfName.FIRST).IntValue;
     int n = stream.GetAsNumber(PdfName.N).IntValue;
     byte[] b = GetStreamBytes(stream, tokens.File);
     PRTokeniser saveTokens = tokens;
     tokens = new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateSource(b)));
     try {
         int[] address = new int[n];
         int[] objNumber = new int[n];
         bool ok = true;
         for (int k = 0; k < n; ++k) {
             ok = tokens.NextToken();
             if (!ok)
                 break;
             if (tokens.TokenType != PRTokeniser.TokType.NUMBER) {
                 ok = false;
                 break;
             }
             objNumber[k] = tokens.IntValue;
             ok = tokens.NextToken();
             if (!ok)
                 break;
             if (tokens.TokenType != PRTokeniser.TokType.NUMBER) {
                 ok = false;
                 break;
             }
             address[k] = tokens.IntValue + first;
         }
         if (!ok)
             throw new InvalidPdfException(MessageLocalization.GetComposedMessage("error.reading.objstm"));
         for (int k = 0; k < n; ++k) {
             if (map.ContainsKey(k)) {
                 tokens.Seek(address[k]);
                 tokens.NextToken();
                 PdfObject obj;
                 if (tokens.TokenType == PRTokeniser.TokType.NUMBER) {
             	    obj = new PdfNumber(tokens.StringValue);
                 }
                 else {
             	    tokens.Seek(address[k]);
             	    obj = ReadPRObject();
                 }
                 xrefObj[objNumber[k]] = obj;
             }
         }            
     }
     finally {
         tokens = saveTokens;
     }
 }
Example #10
0
 /**
 * Sets a field property. Valid property names are:
 * <p>
 * <ul>
 * <li>flags - a set of flags specifying various characteristics of the field’s widget annotation.
 * The value of this entry replaces that of the F entry in the form’s corresponding annotation dictionary.<br>
 * <li>setflags - a set of flags to be set (turned on) in the F entry of the form’s corresponding
 * widget annotation dictionary. Bits equal to 1 cause the corresponding bits in F to be set to 1.<br>
 * <li>clrflags - a set of flags to be cleared (turned off) in the F entry of the form’s corresponding
 * widget annotation dictionary. Bits equal to 1 cause the corresponding
 * bits in F to be set to 0.<br>
 * <li>fflags - a set of flags specifying various characteristics of the field. The value
 * of this entry replaces that of the Ff entry in the form’s corresponding field dictionary.<br>
 * <li>setfflags - a set of flags to be set (turned on) in the Ff entry of the form’s corresponding
 * field dictionary. Bits equal to 1 cause the corresponding bits in Ff to be set to 1.<br>
 * <li>clrfflags - a set of flags to be cleared (turned off) in the Ff entry of the form’s corresponding
 * field dictionary. Bits equal to 1 cause the corresponding bits in Ff
 * to be set to 0.<br>
 * </ul>
 * @param field the field name
 * @param name the property name
 * @param value the property value
 * @param inst an array of <CODE>int</CODE> indexing into <CODE>AcroField.Item.merged</CODE> elements to process.
 * Set to <CODE>null</CODE> to process all
 * @return <CODE>true</CODE> if the property exists, <CODE>false</CODE> otherwise
 */
 public bool SetFieldProperty(String field, String name, int value, int[] inst)
 {
     if (writer == null)
         throw new Exception("This AcroFields instance is read-only.");
     Item item = (Item)fields[field];
     if (item == null)
         return false;
     InstHit hit = new InstHit(inst);
     if (Util.EqualsIgnoreCase(name, "flags")) {
         PdfNumber num = new PdfNumber(value);
         for (int k = 0; k < item.merged.Count; ++k) {
             if (hit.IsHit(k)) {
                 ((PdfDictionary)item.merged[k]).Put(PdfName.F, num);
                 ((PdfDictionary)item.widgets[k]).Put(PdfName.F, num);
                 MarkUsed((PdfDictionary)item.widgets[k]);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "setflags")) {
         for (int k = 0; k < item.merged.Count; ++k) {
             if (hit.IsHit(k)) {
                 PdfNumber num = (PdfNumber)PdfReader.GetPdfObject(((PdfDictionary)item.widgets[k]).Get(PdfName.F));
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val | value);
                 ((PdfDictionary)item.merged[k]).Put(PdfName.F, num);
                 ((PdfDictionary)item.widgets[k]).Put(PdfName.F, num);
                 MarkUsed((PdfDictionary)item.widgets[k]);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "clrflags")) {
         for (int k = 0; k < item.merged.Count; ++k) {
             if (hit.IsHit(k)) {
                 PdfNumber num = (PdfNumber)PdfReader.GetPdfObject(((PdfDictionary)item.widgets[k]).Get(PdfName.F));
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val & (~value));
                 ((PdfDictionary)item.merged[k]).Put(PdfName.F, num);
                 ((PdfDictionary)item.widgets[k]).Put(PdfName.F, num);
                 MarkUsed((PdfDictionary)item.widgets[k]);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "fflags")) {
         PdfNumber num = new PdfNumber(value);
         for (int k = 0; k < item.merged.Count; ++k) {
             if (hit.IsHit(k)) {
                 ((PdfDictionary)item.merged[k]).Put(PdfName.FF, num);
                 ((PdfDictionary)item.values[k]).Put(PdfName.FF, num);
                 MarkUsed((PdfDictionary)item.values[k]);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "setfflags")) {
         for (int k = 0; k < item.merged.Count; ++k) {
             if (hit.IsHit(k)) {
                 PdfNumber num = (PdfNumber)PdfReader.GetPdfObject(((PdfDictionary)item.values[k]).Get(PdfName.FF));
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val | value);
                 ((PdfDictionary)item.merged[k]).Put(PdfName.FF, num);
                 ((PdfDictionary)item.values[k]).Put(PdfName.FF, num);
                 MarkUsed((PdfDictionary)item.values[k]);
             }
         }
     }
     else if (Util.EqualsIgnoreCase(name, "clrfflags")) {
         for (int k = 0; k < item.merged.Count; ++k) {
             if (hit.IsHit(k)) {
                 PdfNumber num = (PdfNumber)PdfReader.GetPdfObject(((PdfDictionary)item.values[k]).Get(PdfName.FF));
                 int val = 0;
                 if (num != null)
                     val = num.IntValue;
                 num = new PdfNumber(val & (~value));
                 ((PdfDictionary)item.merged[k]).Put(PdfName.FF, num);
                 ((PdfDictionary)item.values[k]).Put(PdfName.FF, num);
                 MarkUsed((PdfDictionary)item.values[k]);
             }
         }
     }
     else
         return false;
     return true;
 }
Example #11
0
 private void AdjustTabOrder(PdfArray annots, PdfIndirectReference ind, PdfNumber nn)
 {
     int v = nn.IntValue;
     ArrayList t = (ArrayList)tabOrder[annots] ;
     if (t == null) {
         t = new ArrayList();
         int size = annots.Size - 1;
         for (int k = 0; k < size; ++k) {
             t.Add(zero);
         }
         t.Add(v);
         tabOrder[annots] =  t;
         annots.Add(ind);
     }
     else {
         int size = t.Count - 1;
         for (int k = size; k >= 0; --k) {
             if ((int)t[k] <= v) {
                 t.Insert(k + 1, v);
                 annots.ArrayList.Insert(k + 1, ind);
                 size = -2;
                 break;
             }
         }
         if (size != -2) {
             t.Insert(0, v);
             annots.ArrayList.Insert(0, ind);
         }
     }
 }
Example #12
0
 PdfObject GetColorspace()
 {
     if (icc_profile != null) {
         if ((colorType & 2) == 0)
             return PdfName.DEVICEGRAY;
         else
             return PdfName.DEVICERGB;
     }
     if (gamma == 1f && !hasCHRM) {
         if ((colorType & 2) == 0)
             return PdfName.DEVICEGRAY;
         else
             return PdfName.DEVICERGB;
     }
     else {
         PdfArray array = new PdfArray();
         PdfDictionary dic = new PdfDictionary();
         if ((colorType & 2) == 0) {
             if (gamma == 1f)
                 return PdfName.DEVICEGRAY;
             array.Add(PdfName.CALGRAY);
             dic.Put(PdfName.GAMMA, new PdfNumber(gamma));
             dic.Put(PdfName.WHITEPOINT, new PdfLiteral("[1 1 1]"));
             array.Add(dic);
         }
         else {
             PdfObject wp = new PdfLiteral("[1 1 1]");
             array.Add(PdfName.CALRGB);
             if (gamma != 1f) {
                 PdfArray gm = new PdfArray();
                 PdfNumber n = new PdfNumber(gamma);
                 gm.Add(n);
                 gm.Add(n);
                 gm.Add(n);
                 dic.Put(PdfName.GAMMA, gm);
             }
             if (hasCHRM) {
                 float z = yW*((xG-xB)*yR-(xR-xB)*yG+(xR-xG)*yB);
                 float YA = yR*((xG-xB)*yW-(xW-xB)*yG+(xW-xG)*yB)/z;
                 float XA = YA*xR/yR;
                 float ZA = YA*((1-xR)/yR-1);
                 float YB = -yG*((xR-xB)*yW-(xW-xB)*yR+(xW-xR)*yB)/z;
                 float XB = YB*xG/yG;
                 float ZB = YB*((1-xG)/yG-1);
                 float YC = yB*((xR-xG)*yW-(xW-xG)*yW+(xW-xR)*yG)/z;
                 float XC = YC*xB/yB;
                 float ZC = YC*((1-xB)/yB-1);
                 float XW = XA+XB+XC;
                 float YW = 1;//YA+YB+YC;
                 float ZW = ZA+ZB+ZC;
                 PdfArray wpa = new PdfArray();
                 wpa.Add(new PdfNumber(XW));
                 wpa.Add(new PdfNumber(YW));
                 wpa.Add(new PdfNumber(ZW));
                 wp = wpa;
                 PdfArray matrix = new PdfArray();
                 matrix.Add(new PdfNumber(XA));
                 matrix.Add(new PdfNumber(YA));
                 matrix.Add(new PdfNumber(ZA));
                 matrix.Add(new PdfNumber(XB));
                 matrix.Add(new PdfNumber(YB));
                 matrix.Add(new PdfNumber(ZB));
                 matrix.Add(new PdfNumber(XC));
                 matrix.Add(new PdfNumber(YC));
                 matrix.Add(new PdfNumber(ZC));
                 dic.Put(PdfName.MATRIX, matrix);
             }
             dic.Put(PdfName.WHITEPOINT, wp);
             array.Add(dic);
         }
         return array;
     }
 }
 public void CopyStructTreeForPage(PdfNumber sourceArrayNumber, int newArrayNumber)
 {
     if (!openedDocuments[fileName])
     {
     PdfObject res = writer.CopyObject(writer.CopyObject(structTreeRoot.Get(PdfName.K), true, true));
     if (!(res is PdfIndirectReference))
         res = writer.AddToBody(res).IndirectReference;
     structureTreeRoot.AddPageMark(newArrayNumber, (PdfIndirectReference)res);
     AddKid(structureTreeRoot, res);
     openedDocuments.Add(fileName, true);
     }
     if (CopyPageMarks(parentTree, sourceArrayNumber, newArrayNumber) == returnType.NOTFOUND) {
     throw new BadPdfFormatException(MessageLocalization.GetComposedMessage("structparent.not.found"));
     }
 }
        private Font RetrieveFontFromAcroForm(PdfName fontName, PdfNumber size) {
            PdfIndirectReference fontIndirReference = pdfStamper.Reader.AcroForm.GetAsDict(PdfName.DR).GetAsDict(PdfName.FONT).GetAsIndirectObject(fontName);
            BaseFont bfont = BaseFont.CreateFont((PRIndirectReference) fontIndirReference);

            return new Font(bfont, size.FloatValue);
        }
        private void DrawOverlayText(PdfContentByte canvas, IList<Rectangle> textRectangles, PdfString overlayText, 
                                     PdfString otDA, PdfNumber otQ, PdfBoolean otRepeat) {
            ColumnText ct = new ColumnText(canvas);
            ct.SetLeading(0, 1.2F);
            ct.UseAscender = true;

            String otStr = overlayText.ToUnicodeString();

            canvas.SaveState();
            IDictionary<string, IList<object>> parsedDA = ParseDAParam(otDA);

            Font font = null;

            if (parsedDA.ContainsKey(STROKE_COLOR)) {
                IList<object> strokeColorArgs = parsedDA[STROKE_COLOR];
                SetStrokeColor(canvas, strokeColorArgs);
            }

            if (parsedDA.ContainsKey(FILL_COLOR)) {
                IList<object> fillColorArgs = parsedDA[FILL_COLOR];
                SetFillColor(canvas, fillColorArgs);
            }

            if (parsedDA.ContainsKey("Tf")) {
                IList<object> tfArgs = parsedDA["Tf"];
                font = RetrieveFontFromAcroForm((PdfName) tfArgs[0], (PdfNumber) tfArgs[1]);
            }

            foreach (Rectangle textRect in textRectangles) {
                ct.SetSimpleColumn(textRect);

                if (otQ != null) {
                    ct.Alignment = otQ.IntValue;
                }

                Phrase otPhrase;

                if (font != null) {
                    otPhrase = new Phrase(otStr, font);
                } else {
                    otPhrase = new Phrase(otStr);
                }

                float y = ct.YLine;

                if (otRepeat != null && otRepeat.BooleanValue) {
                    int status = ct.Go(true);

                    while (!ColumnText.HasMoreText(status)) {
                        otPhrase.Add(otStr);
                        ct.SetText(otPhrase);
                        ct.YLine = y;
                        status = ct.Go(true);
                    }
                }

                ct.SetText(otPhrase);
                ct.YLine = y;
                ct.Go();
            }

            canvas.RestoreState();
        }
Example #16
0
 /**
 * Adds a <CODE>PdfNumber</CODE> to the <CODE>PdfArray</CODE>.
 *
 * @param  number   displacement of the string
 */
 public void Add(PdfNumber number) {
     Add((float)number.DoubleValue);
 }
 private LockPermissions(int p) {
     number = new PdfNumber(p);
 }
Example #18
0
 /**
  * A factor that shall be used to convert the largest units along the y axis
  * to the largest units along the x axis. It shall be used for calculations
  * (distance, area, and angle) where the units are be equivalent; if not
  * specified, these calculations may not be performed (which would be the
  * case in situations such as x representing time and y representing
  * temperature). Other calculations (change in x, change in y, and slope)
  * shall not require this value.
  *
  * @param cyx
  */
 public void SetCYX(PdfNumber cyx)
 {
     Put(PdfName.CYX, cyx);
 }
Example #19
0
        virtual public void PdfObjectCheckTest1()
        {

            string filename = OUT + "PdfObjectCheckTest1.pdf";
            FileStream fos = new FileStream(filename, FileMode.Create);

            Document document = new Document();

            PdfAWriter writer = PdfAWriter.GetInstance(document, fos, PdfAConformanceLevel.PDF_A_1B);
            writer.CreateXmpMetadata();

            document.Open();

            Font font = FontFactory.GetFont(RESOURCES + "FreeMonoBold.ttf", BaseFont.WINANSI, BaseFont.EMBEDDED, 12);
            document.Add(new Paragraph("Hello World", font));

            FileStream iccProfileFileStream = File.Open(RESOURCES + "sRGB Color Space Profile.icm", FileMode.Open, FileAccess.Read, FileShare.Read);
            ICC_Profile icc = ICC_Profile.GetInstance(iccProfileFileStream);
            iccProfileFileStream.Close();

            writer.SetOutputIntents("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", icc);

            ByteBuffer.HIGH_PRECISION = true;
            PdfNumber num = new PdfNumber(65535.12);
            writer.ExtraCatalog.Put(new PdfName("TestNumber"), num);

            bool exceptionThrown = false;
            try
            {
                document.Close();
            }
            catch (PdfAConformanceException e)
            {
                if (e.GetObject().Equals(num))
                    exceptionThrown = true;
            }
            if (!exceptionThrown)
                Assert.Fail("PdfAConformanceException should be thrown.");
        }
Example #20
0
 /** Sets the field value and the display string. The display string
 * is used to build the appearance in the cases where the value
 * is modified by Acrobat with JavaScript and the algorithm is
 * known.
 * @param name the fully qualified field name or the partial name in the case of XFA forms
 * @param value the field value
 * @param display the string that is used for the appearance. If <CODE>null</CODE>
 * the <CODE>value</CODE> parameter will be used
 * @return <CODE>true</CODE> if the field was found and changed,
 * <CODE>false</CODE> otherwise
 * @throws IOException on error
 * @throws DocumentException on error
 */
 public bool SetField(String name, String value, String display)
 {
     if (writer == null)
         throw new DocumentException("This AcroFields instance is read-only.");
     if (xfa.XfaPresent) {
         name = xfa.FindFieldName(name, this);
         if (name == null)
             return false;
         String shortName = XfaForm.Xml2Som.GetShortName(name);
         xfa.SetNodeText(xfa.FindDatasetsNode(shortName), value);
     }
     Item item = (Item)fields[name];
     if (item == null)
         return false;
     PdfName type = (PdfName)PdfReader.GetPdfObject(((PdfDictionary)item.merged[0]).Get(PdfName.FT));
     if (PdfName.TX.Equals(type)) {
         PdfNumber maxLen = (PdfNumber)PdfReader.GetPdfObject(((PdfDictionary)item.merged[0]).Get(PdfName.MAXLEN));
         int len = 0;
         if (maxLen != null)
             len = maxLen.IntValue;
         if (len > 0)
             value = value.Substring(0, Math.Min(len, value.Length));
     }
     if (display == null)
         display = value;
     if (PdfName.TX.Equals(type) || PdfName.CH.Equals(type)) {
         PdfString v = new PdfString(value, PdfObject.TEXT_UNICODE);
         for (int idx = 0; idx < item.values.Count; ++idx) {
             PdfDictionary valueDic = (PdfDictionary)item.values[idx];
             valueDic.Put(PdfName.V, v);
             valueDic.Remove(PdfName.I);
             MarkUsed(valueDic);
             PdfDictionary merged = (PdfDictionary)item.merged[idx];
             merged.Remove(PdfName.I);
             merged.Put(PdfName.V, v);
             PdfDictionary widget = (PdfDictionary)item.widgets[idx];
             if (generateAppearances) {
                 PdfAppearance app = GetAppearance(merged, display, name);
                 if (PdfName.CH.Equals(type)) {
                     PdfNumber n = new PdfNumber(topFirst);
                     widget.Put(PdfName.TI, n);
                     merged.Put(PdfName.TI, n);
                 }
                 PdfDictionary appDic = (PdfDictionary)PdfReader.GetPdfObject(widget.Get(PdfName.AP));
                 if (appDic == null) {
                     appDic = new PdfDictionary();
                     widget.Put(PdfName.AP, appDic);
                     merged.Put(PdfName.AP, appDic);
                 }
                 appDic.Put(PdfName.N, app.IndirectReference);
                 writer.ReleaseTemplate(app);
             }
             else {
                 widget.Remove(PdfName.AP);
                 merged.Remove(PdfName.AP);
             }
             MarkUsed(widget);
         }
         return true;
     }
     else if (PdfName.BTN.Equals(type)) {
         PdfNumber ff = (PdfNumber)PdfReader.GetPdfObject(((PdfDictionary)item.merged[0]).Get(PdfName.FF));
         int flags = 0;
         if (ff != null)
             flags = ff.IntValue;
         if ((flags & PdfFormField.FF_PUSHBUTTON) != 0)
             return true;
         PdfName v = new PdfName(value);
         if ((flags & PdfFormField.FF_RADIO) == 0) {
             for (int idx = 0; idx < item.values.Count; ++idx) {
                 ((PdfDictionary)item.values[idx]).Put(PdfName.V, v);
                 MarkUsed((PdfDictionary)item.values[idx]);
                 PdfDictionary merged = (PdfDictionary)item.merged[idx];
                 merged.Put(PdfName.V, v);
                 merged.Put(PdfName.AS, v);
                 PdfDictionary widget = (PdfDictionary)item.widgets[idx];
                 if (IsInAP(widget,  v))
                     widget.Put(PdfName.AS, v);
                 else
                     widget.Put(PdfName.AS, PdfName.Off_);
                 MarkUsed(widget);
             }
         }
         else {
             ArrayList lopt = new ArrayList();
             PdfObject opts = PdfReader.GetPdfObject(((PdfDictionary)item.values[0]).Get(PdfName.OPT));
             if (opts != null && opts.IsArray()) {
                 ArrayList list = ((PdfArray)opts).ArrayList;
                 for (int k = 0; k < list.Count; ++k) {
                     PdfObject vv = PdfReader.GetPdfObject((PdfObject)list[k]);
                     if (vv != null && vv.IsString())
                         lopt.Add(((PdfString)vv).ToUnicodeString());
                     else
                         lopt.Add(null);
                 }
             }
             int vidx = lopt.IndexOf(value);
             PdfName valt = null;
             PdfName vt;
             if (vidx >= 0) {
                 vt = valt = new PdfName(vidx.ToString());
             }
             else
                 vt = v;
             for (int idx = 0; idx < item.values.Count; ++idx) {
                 PdfDictionary merged = (PdfDictionary)item.merged[idx];
                 PdfDictionary widget = (PdfDictionary)item.widgets[idx];
                 MarkUsed((PdfDictionary)item.values[idx]);
                 if (valt != null) {
                     PdfString ps = new PdfString(value, PdfObject.TEXT_UNICODE);
                     ((PdfDictionary)item.values[idx]).Put(PdfName.V, ps);
                     merged.Put(PdfName.V, ps);
                 }
                 else {
                     ((PdfDictionary)item.values[idx]).Put(PdfName.V, v);
                     merged.Put(PdfName.V, v);
                 }
                 MarkUsed(widget);
                 if (IsInAP(widget,  vt)) {
                     merged.Put(PdfName.AS, vt);
                     widget.Put(PdfName.AS, vt);
                 }
                 else {
                     merged.Put(PdfName.AS, PdfName.Off_);
                     widget.Put(PdfName.AS, PdfName.Off_);
                 }
             }
         }
         return true;
     }
     return false;
 }
Example #21
0
 /**
  * Adds a point to the Point Data dictionary.
  * @param value an XPTS value
  * @param identifier
  */
 virtual public void AddXPTSValue(PdfNumber value, PtIdentifier identifier) {
     xpts.Add(value);
     names.Add(DecodeUnits.Decode(identifier));
 }
Example #22
0
		virtual protected internal PdfObject ReadOneObjStm (PRStream stream, int idx) {
            int first = stream.GetAsNumber(PdfName.FIRST).IntValue;
            byte[] b = GetStreamBytes(stream, tokens.File);
            PRTokeniser saveTokens = tokens;
            tokens = new PRTokeniser(new RandomAccessFileOrArray(new RandomAccessSourceFactory().CreateSource(b)));
            try {
				int address = 0;
                bool ok = true;
                ++idx;
                for (int k = 0; k < idx; ++k) {
                    ok = tokens.NextToken();
                    if (!ok)
                        break;
                    if (tokens.TokenType != PRTokeniser.TokType.NUMBER) {
                        ok = false;
                        break;
                    }
                    ok = tokens.NextToken();
                    if (!ok)
                        break;
                    if (tokens.TokenType != PRTokeniser.TokType.NUMBER) {
                        ok = false;
                        break;
                    }
                    address = tokens.IntValue + first;
                }
                if (!ok)
                    throw new InvalidPdfException(MessageLocalization.GetComposedMessage("error.reading.objstm"));
                tokens.Seek(address);
                tokens.NextToken();
                PdfObject obj;
                if (tokens.TokenType == PRTokeniser.TokType.NUMBER) {
                    obj = new PdfNumber(tokens.StringValue);
                }
                else {
                    tokens.Seek(address);
                    obj = ReadPRObject();
                }
                return obj;
            }
            finally {
                tokens = saveTokens;
            }
        }
Example #23
0
 private void AdjustTabOrder(PdfArray annots, PdfIndirectReference ind, PdfNumber nn) {
     int v = nn.IntValue;
     List<int> t;
     if (!tabOrder.TryGetValue(annots, out t)) {
         t = new List<int>();
         int size = annots.Size - 1;
         for (int k = 0; k < size; ++k) {
             t.Add(zero);
         }
         t.Add(v);
         tabOrder[annots] =  t;
         annots.Add(ind);
     }
     else {
         int size = t.Count - 1;
         for (int k = size; k >= 0; --k) {
             if (t[k] <= v) {
                 t.Insert(k + 1, v);
                 annots.Add(k + 1, ind);
                 size = -2;
                 break;
             }
         }
         if (size != -2) {
             t.Insert(0, v);
             annots.Add(0, ind);
         }
     }
 }
Example #24
0
 /**
  * Adds a <CODE>PdfNumber</CODE> to the <CODE>PdfArray</CODE>.
  *
  * @param       number          displacement of the string
  */
 public void Add(PdfNumber number)
 {
     arrayList.Add(number.DoubleValue);
 }