public static TextProperties GetTextProperties(this AcroFields pdfForm, string acroFieldName) { AcroFields.Item acroField = pdfForm.GetFieldItem(acroFieldName); PdfDictionary merged = acroField.GetMerged(0); TextField textField = new TextField(null, null, null); pdfForm.DecodeGenericDictionary(merged, textField); return(new TextProperties { FontName = textField.Font.With(x => x.FullFontName[0][3]), FontSize = textField.FontSize, Alignment = textField.Alignment }); }
Queue <Tuple <string, string, string> > Fill109Rows(IEnumerable <MissionLog> logs, AcroFields fields, string fieldName) { AcroFields.Item item = fields.GetFieldItem(fieldName); PdfDictionary merged = item.GetMerged(0); TextField textField = new TextField(null, null, null); fields.DecodeGenericDictionary(merged, textField); var collection = new Queue <Tuple <string, string, string> >(); float fieldWidth = fields.GetFieldPositions(fieldName)[0].position.Width; float padRight = textField.Font.GetWidthPoint("m", textField.FontSize); foreach (var log in logs) { if (log.Data == null) { continue; } string formTime = log.Time.ToString("HHmm"); string formOperator = (log.Person ?? new Member()).LastName; foreach (var logMsg in log.Data.Replace("\r", "").Split(new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)) { int left = 0; int right = logMsg.Length - 1; while (left < right) { string part = logMsg.Substring(left, right - left + 1); while (left < right && (textField.Font.GetWidthPoint(part, textField.FontSize) + padRight) > fieldWidth) { right = left + part.LastIndexOf(' '); part = logMsg.Substring(left, right - left); } collection.Enqueue(new Tuple <string, string, string>(formTime, part, formOperator)); formTime = ""; left = right; right = logMsg.Length - 1; } } } return(collection); }
/// <summary> /// Get a Standard Type 1 (14) font from a text field /// </summary> /// <param name="acroFields">AcroFields</param> /// <param name="fieldName">Text field name</param> /// <returns> /// Standard Type 1 (14) font if possible, or BaseFont.HELVETICA /// </returns> public static BaseFont GetStandardFont(AcroFields acroFields, string fieldName) { var baseFont = BaseFont.CreateFont(); try { var item = acroFields.GetFieldItem(fieldName); var pdfDictionary = item.GetMerged(0); var textField = new TextField(null, null, null); acroFields.DecodeGenericDictionary(pdfDictionary, textField); baseFont = BaseFont.CreateFont( // keep next line for reference: Google and StackOverflow aren't always right... // textField.Font.FullFontName[0][3], textField.Font.PostscriptFontName, BaseFont.WINANSI, false ); } catch { // iTextSharp.text.DocumentException, but we don't care } return(baseFont); }
/// <summary> /// Handles the Click event of the BtnGenerateReport control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> private void BtnGenerateReport_Click(object sender, EventArgs e) { if (!string.IsNullOrWhiteSpace(txtPDFFile.Text)) { _formFields.Clear(); try { using (MemoryStream ms = new MemoryStream()) { using (FileStream fs = File.OpenRead(txtPDFFile.Text)) { fs.CopyTo(ms); } PdfReader.unethicalreading = true; PdfReader pdfReader = new PdfReader(ms.ToArray()); AcroFields pdfFormFields = pdfReader.AcroFields; foreach (KeyValuePair <string, AcroFields.Item> kvp in pdfFormFields.Fields) { FormField formField = new FormField { FieldName = pdfFormFields.GetTranslatedFieldName(kvp.Key), FieldValue = pdfFormFields.GetField(kvp.Key), FieldType = GetFormFieldType(pdfFormFields.GetFieldType(kvp.Key)) }; AcroFields.Item field = pdfFormFields.GetFieldItem(kvp.Key); PdfDictionary merged = field.GetMerged(0); TextField fld = new TextField(null, null, null); pdfFormFields.DecodeGenericDictionary(merged, fld); formField.FontName = GetFontName(fld.Font); string fontSize = (fld.FontSize == 0.0f) ? "Auto" : fld.FontSize.ToString(); formField.FontSize = fontSize; List <AcroFields.FieldPosition> fieldPositions = pdfFormFields.GetFieldPositions(kvp.Key).ToList(); float pageHeight = pdfReader.GetPageSizeWithRotation(fieldPositions[0].page).Height; formField.FieldPositionTop = (pageHeight - fieldPositions[0].position.Top); formField.FieldPositionBottom = (pageHeight - fieldPositions[0].position.Bottom); formField.FieldPositionLeft = fieldPositions[0].position.Left; formField.FieldPositionRight = fieldPositions[0].position.Right; formField.FieldHeight = fieldPositions[0].position.Height; formField.FieldWidth = fieldPositions[0].position.Width; formField.PageNumber = fieldPositions[0].page; _formFields.Add(formField); } GenerateTreeViewDisplay(); pnlSaveOptions.Enabled = true; } } catch (Exception ex) { MessageBox.Show("There was an issue with processing the request. Message: \n\n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); pnlSaveOptions.Enabled = pnlSaveOptions.Enabled ? true : false; } } }