/// <summary> /// METODO 3: Sostituire un acrofield di tipo signature con un acrofield di tipo checkbox /// Locking for a checkbox and checking it /// </summary> /// <param name="fieldName">string Name of the signaturefield to substitute</param> public void SubstituteSignature(string fieldName) { //Checking if argument is null if (fieldName == null) { throw new ArgumentNullException(fieldName); } //Getting fields AcroFields form = reader.AcroFields; //Checking if document has no fields if (form.Fields.Count == 0) { throw new DocumentHasNoFieldsException(); } //Looking for a signatureBox with the given name var result = form.Fields .Where(kvp => form.GetTranslatedFieldName(kvp.Key).Equals(fieldName) && form.GetFieldType(kvp.Key) == AcroFields.FIELD_TYPE_SIGNATURE ) .Select(kvp => new { kvp.Key, Position = form.GetFieldPositions(kvp.Key) }) ?.FirstOrDefault(); //Checking if the query had results if (result == null) { throw new FieldNotFoundException(fieldName, AcroFields.FIELD_TYPE_SIGNATURE); } //Removing field form.RemoveField(result.Key); //Creating new checkbox with signaturefield's coordinates //Note: We're replacing the first occurrence RadioCheckField checkbox = new RadioCheckField(stamper.Writer, result.Position[0].position, "i_was_a_signature_field", "Yes") { //Setting look CheckType = RadioCheckField.TYPE_CHECK, Checked = true, BorderWidth = BaseField.BORDER_WIDTH_THIN, BorderColor = BaseColor.BLACK, BackgroundColor = BaseColor.WHITE }; //Adding checbox in signaturefield's page stamper.AddAnnotation(checkbox.CheckField, result.Position[0].page); }
// --------------------------------------------------------------------------- public byte[] ManipulatePdf(byte[] src, Dictionary <string, TextField> cache, string name, string login) { using (MemoryStream ms = new MemoryStream()) { PdfReader reader = new PdfReader(src); using (PdfStamper stamper = new PdfStamper(reader, ms)) { AcroFields form = stamper.AcroFields; form.FieldCache = cache; form.SetExtraMargin(2, 0); form.RemoveField("personal.password"); form.SetField("personal.name", name); form.SetField("personal.loginname", login); form.RenameField("personal.reason", "personal.motivation"); form.SetFieldProperty( "personal.loginname", "setfflags", TextField.READ_ONLY, null ); stamper.FormFlattening = true; stamper.PartialFormFlattening("personal.name"); } return(ms.ToArray()); } }
/// <summary> /// Remove a assinatura de um documento PDF /// </summary> internal static void remove(string filePath, string serialNumber = null) { try { // create a tmp file to remove string newFilePath = filePath.Substring(0, filePath.Length - 4) + "_tmp.pdf"; System.IO.File.Copy(filePath, newFilePath); // open the file PdfReader reader = new PdfReader(newFilePath); // get the fields inside the file AcroFields af = reader.AcroFields; // get the list of signatures List <string> names = af.GetSignatureNames(); if (names.Count == 0) { reader.Close(); throw new NoSignatureFoundException(); } // create the stream to file MemoryStream mStream = new MemoryStream(); // open the file to edit PdfStamper stamper = new PdfStamper(reader, mStream); AcroFields af2 = stamper.AcroFields; // close the reader file reader.Close(); if (serialNumber == null) { // remove all signatures for (int i = 0; i < names.Count; i++) { //af2.ClearSignatureField(names[i].ToString()); af2.RemoveField(names[i].ToString()); } } else { // find and remove the selected signature for (int i = 0; i < names.Count; i++) { PdfPKCS7 pk = af.VerifySignature(names[i]); if (pk.SigningCertificate.SerialNumber.ToString(16).ToUpper() == serialNumber) { //af2.ClearSignatureField(names[i].ToString()); af2.RemoveField(names[i].ToString()); } } } // clear the stream of obejct reader.RemoveUnusedObjects(); // close the stamper file stamper.Writer.CloseStream = false; stamper.Close(); // save file File.WriteAllBytes(newFilePath, mStream.ToArray()); // delete the tmp file e move the new to the right name System.IO.File.Delete(filePath); System.IO.File.Move(newFilePath, filePath); } catch (Exception ex) { throw ex; } }
public static void getRemision(string FilePath, string TemplatePath, Salida_remision oSR) { PdfReader reader = new PdfReader(TemplatePath); PdfStamper stamper = new PdfStamper(reader, new FileStream(FilePath, FileMode.Create)); try { AcroFields fields = stamper.AcroFields; // set form fields fields.SetField("folioRemision", oSR.Folio_remision); //fields.SetField("lblFolioRemision", "FOLIO REMISION"); fields.SetField("codigoCliente", oSR.Codigo_cliente); //fields.SetField("lblCodigoCliente", "REFERENCIA"); #region Trafico fields.SetField("lbllinea", "Línea: "); fields.SetField("linea", oSR.PTrafico.Transporte); fields.SetField("lbloperador", "Operador: "); fields.SetField("operador", oSR.PTrafico.Operador); fields.SetField("lblplaca", "Placa: "); fields.SetField("placa", oSR.PTrafico.Placa); fields.SetField("lblcita", "Folio - Fecha: "); string fechaCita = Convert.ToDateTime(oSR.PTrafico.Fecha_cita).ToString("dd/MM/yyyy"); fields.SetField("cita", oSR.PTrafico.Folio_cita + " - " + fechaCita + " " + oSR.PTrafico.Hora_cita); #endregion CultureInfo ci = new CultureInfo("es-MX"); fields.SetField("fecha_remision", oSR.Fecha_remision.ToString("dddd, dd \\de MMMM \\de yy", ci)); string fullPedimento = EntradaCtrl.PedimentoGetFullNumber(oSR.Referencia); oSR.Referencia = fullPedimento; fields.SetField("referencia", fullPedimento); Cliente oC = CatalogCtrl.Cliente_GetByIdEntrada(oSR.Id_entrada); fields.SetField("cliente", oC.Razon.ToUpper()); //fields.SetField("cliente" fields.SetField("proveedor", oSR.Proveedor); fields.SetField("proveedor_direccion", oSR.Proveedor_direccion); fields.SetField("codigo_k", oSR.Codigo); fields.SetField("codigo", oSR.Codigo); fields.SetField("orden_k", oSR.Orden); fields.SetField("orden", oSR.Orden); fields.SetField("vendor_k", oSR.Vendor); fields.SetField("vendor", oSR.Vendor); string lotePdf = string.Empty; string loteAct = string.Empty; string loteAnt = string.Empty; for (int indDet = 0; indDet < oSR.LstSRDetail.Count; indDet++) { Salida_remision_detail item = oSR.LstSRDetail[indDet]; loteAct = item.Lote + string.Empty; if (string.Compare(loteAct, loteAnt) != 0) { loteAnt = loteAct; lotePdf += loteAct + ", "; } switch (indDet) { case 0: fields.SetField("bulto", item.Bulto.ToString("N0")); fields.SetField("piezaxb", item.Piezaxbulto.ToString("N0")); fields.SetField("pieza", item.Piezas.ToString("N0")); break; case 1: fields.SetField("bulto_i", item.Bulto.ToString("N0")); fields.SetField("piezaxb_i", item.Piezaxbulto.ToString("N0")); fields.SetField("pieza_i", item.Piezas.ToString("N0")); break; default: break; } } if (lotePdf.Length > 0) { lotePdf = lotePdf.Substring(0, lotePdf.Length - 2); } fields.SetField("mercancia_k", oSR.Mercancia + (lotePdf.Length > 0 ? "\nLote: " + lotePdf : "")); fields.SetField("lblCodigo", "Código"); fields.SetField("lblOrden", "Orden de Compra"); fields.SetField("lblDescripcion", "Descripción"); fields.SetField("lblNoProveedor", "No. de Proveedor"); fields.SetField("lblTotalCtns1", "Total CTNS:"); fields.SetField("lblTotalCtns2", "Total CTNS:"); fields.SetField("lblCartones1", "Cartones"); fields.SetField("lblCartones2", "Cartones"); fields.SetField("lblPiezasCU1", "Piezas C/U"); fields.SetField("lblPiezasCU2", "Piezas C/U"); fields.SetField("lblTotalPzas", "Total Piezas"); addBarCodes(stamper, oSR); //fields.SetField("bulto", oSR.Bulto.ToString("N0")); //fields.SetField("bulto_i", oSR2.Bulto.ToString("N0")); //fields.SetField("piezaxb", oSR.Piezaxbulto.ToString("N0")); //fields.SetField("piezaxb_i", oSR2.Piezaxbulto.ToString("N0")); //fields.SetField("pieza", oSR.Pieza.ToString("N0")); //fields.SetField("pieza_i", oSR2.Pieza.ToString("N0")); //int total = oSR2.Pieza + oSR.Pieza; fields.SetField("piezatotal", oSR.PiezaTotal.ToString("N0")); fields.SetField("danada", "MERCANCÍA DAÑADA (" + oSR.Dano_especifico + ")"); if (oSR.Dano_especifico.Trim().Length == 0) { fields.RemoveField("danada"); } fields.SetField("elaboro", oSR.Elaboro); fields.SetField("autorizo", oSR.Autorizo); stamper.FormFlattening = true; //addBarCodes(FilePath, oSR1); } catch (Exception) { throw; } finally { stamper.Close(); reader.Close(); } }