Adding CSS Style Attribute to iText PdfFormField - c#

I'm trying to set a specific style on a iText.Forms.Field.PdfFormField to add margin-bottom or padding-bottom as the default input text being added to the PDF isn't aligning with the hard-coded text the PDF template already has.
Here's the code to add attributes only to one specific cell in the PDF template. The field object is a PdfFormField instance:
PdfString fieldName = field.GetFieldName();
if(fieldName.ToString() == "topmostSubform[0].CopyB[0].CopyHeader[0].CalendarYear[0].f2_1[0]"
|| fieldName.ToString() == "topmostSubform[0].CopyC[0].CopyHeader[0].CalendarYear[0].f2_1[0]")
{
field.SetFontSize(8f);
field.SetJustification(0);
field.SetDefaultStyle(new PdfString("padding-bottom: 4px")); --not being applied
}
I tried to go off of the solution on SO here: Using iText.Forms.Fields.PdfFormField.SetRichText () but I think I'm just missing something obvious in my approach.

Related

Retrieve hidden text blocks and replace all content between two of them

I want to use OpenXML to automate Word document creation by taking a given document as a template. This template contains text blocks with a special syntax (namely e.g. «tagname[»...«]tagname») in the form of hidden text (in the font dialog box, the 'Hidden' checkbox in the effects section is activated) so that they don't show up when the document is printed.
Depending on the tagname I want to replace any content which might be already there in the template file between the opening and closing tag with some other content (e.g. in «today[»01/01/2000«]today» the content 01/01/2000 would be substituted with 07/06/2018) preferably preserving the format (e.g. bold, italic or text color).
How can I retrieve the hidden text parts and is there an easy way to replace anything between corresponding tags using C#?
I am not sure what exactly are the text blocks in your template. Instead you can use Content Control from Developer options at the required place, then you can name them from the properties of content control.
today will be name of the Content Control
Once you have all the content control at your template, you can look for specific content control with name and add the new value you want to.
Here is the Snippet to give you an idea.
// Title is name of content control(today), value is what you want to add(1/01/2000)
private static void UpdateControl(WordprocessingDocument document, string title, string value)
{
MainDocumentPart mainPart = document.MainDocumentPart;
var sdtRuns = mainPart.Document.Descendants<SdtRun>()
.Where(run => run.SdtProperties.GetFirstChild<Tag>().Val.Value == title);
foreach (SdtRun sdtRun in sdtRuns)
{
sdtRun.Descendants<Text>().First().Text = value;
}
document.MainDocumentPart.Document.Save();
}

set attributes of custom tag in iText PdfHTML C#

I created custom tag using PdfHTML.
<condition align="right">Text</condition>
Created custom tag for "condition" to change the value of "Text". It worked. It appeared as "P tag" in the pdf. But it doesn't take attribute align="right". I is always left aligned. How can I set attributes. Created custom css class with below code segment to text alignment.
if (container != null && cssProps.containsKey(CssConstants.TEXT_ALIGN)) {
cssProps.put(CssConstants.TEXT_ALIGN, "right");
BackgroundApplierUtil.applyBackground(cssProps, context, container);
}
It doesn't work. Please help me to solve this.
Thanks in advance
Finally I found the answer. My mistake is using, "BackgroundApplierUtil.applyBackground(cssProps, context, container)" to apply font.
if (container != null && cssProps.containsKey(CssConstants.TEXT_ALIGN)) {
cssProps.put(CssConstants.TEXT_ALIGN, "right");
FontStyleApplierUtil.ApplyFontStyles(cssProps, context,stylecontainer ,container);
}
Thanks all.
https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-5-custom-tag-workers-and-css-appliers
This link details everything you need to know about custom tags within iText PDF.

iText 7.1.0.0 - How to replace Rich Form Text in PDF Form Field

I have a PDF template with form fields where I used iText 7.1.0 to replace some placeholder text inside different form fields which is working fine:
PdfReader reader = new PdfReader(pdf_template);
PdfDocument pdf = new PdfDocument(reader, new PdfWriter(pdf_output));
PdfAcroForm form = PdfAcroForm.GetAcroForm(pdf, true);
form.GetField("Vertragsnummer").SetValue("12345");
form.FlattenFields();
pdf.Close();
reader.Close();
Now I have a form field for which option is set to allow rich text format as there are some parts with bold font etc..
Inside the form field standard text there are again placeholders which I want to replace.
Currently I've got the following code:
PdfFormField tf_maintext = form.GetField("maintext");
tf_maintext.SetRichText(tf_maintext.GetRichText());
Now I need to replace the placeholders but I didn't find any working solution for C#.
For normal form fields I can use
PdfFormField kdnr = form.GetField("Kundennummer");
kdnr.SetValue(kdnr.GetValueAsString().Replace("7777777777", "1111111111"));
the normal Replace function to achieve this.
But I need a solution to replace text in the rich text format field to ensure that the formatting is still there afterwards.
How can I do this?

How to Append Clickable Hyperlink to Comment in Word

I have implemented a hyperlink interface; when the user selects text in the document and then clicks a button from my Word Add-in, there is a comment box that is pre-filled with text added to this selection.
However, I want to include a hyperlink that is clickable to bring the user to a Google Doc and this link should be at the end of the comment box. This is what I have tried so far:
Hyperlink link = new Link();
link.Address = (String) oAddress;
link.SubAddress = (String) oSubAddress;
link.TextToDisplay = "more";
selection.Comments.Add(rng2, comment + link)
It looks like you need to create a collection of Hyperlinks from your current document, then add your hyperlink to it.
Below code should highlight everything inside your comment.
// Select the element you want to apply the hyperlink to - you will have to use the Find method
Microsoft.Office.Interop.Word.Range currentRange = myDoc.Comments[0].Range
if (currentRange != null)
{
Microsoft.Office.Interop.Word.Hyperlink hp = (Microsoft.Office.Interop.Word.Hyperlink)
// This line is what you are missing
currentRange.Hyperlinks.Add(currentRange, "http://www.microsoft.com");
}
Credit to https://stackoverflow.com/a/8665770/5209435.
You can use the above as a base before using the Find method to narrow to find the text that you want to hyperlink and just apply it to the selected text. See this question.

OpenXML add custom style to Quick Style Gallery

I'm creating a Word Document using OpenXML. During the creation of the document, I need to create some custom styles.
Now I've one problem left: I want to put my custom styles inside the Quick Style Gallery, but I wasn't able to do that. The way that I'm following is explained in the following code:
var info = new LatentStyleExceptionInfo
{
Name = styleid,
PrimaryStyle = true,
UnhideWhenUsed = false,
SemiHidden = false,
UiPriority = 1
};
styleDefinitionsPart.Styles.OfType<LatentStyles>().First().Append(info);
Because I've found a link that tells that is the "PrimaryStyle" attribute responsible for put a Style inside the Quick Gallery.
Another thing: opening the styles.xml file, I've noticed that all the other styles has "1" or "0" for the OnOffValue, whereas the custom styles created with that piece of code has "true" or "false".
How can I solve it?
Thanks.
The XML tag that influences appearance of the style in the quick gallery is <w:qFormat/>. The API property that corresponds is Style.QuickStyle. The Interop name is also QuickStyle (boolean).
Btw, I don't think adding a latent style is going to help you. You'll want to add a real, actual, fully-fledged style if you want it to appear in the UI and be operative.

Categories

Resources