IronPDF FormField value with new line - ironpdf

Using IronPDF, I need to fill in a PDF Form Text Field with a multiline string. A single string works fine, but using Environnment.NewLine adds a symbol; \n and \r\n are displayed as text.
I need to avoid modifying the PDF in any way.
'Example that isn't working
Dim pdf = PdfDocument.FromFile("mypdf.pdf")
pdf.Form.Fields(0).Value = "A string with" & Environment.NewLine & "multiple lines"
Thanks in advance!

The solution I found was applying the new line in the HTML:
<div>
#{
var text = #Model.Value.Split("\n");
foreach (string line in text)
{
#line.ToString();
<br />
}
}
</div>

Related

iTextSharp does not read fields in pdf correctly

I have issue with iTextSharp. Let's assume I have two rows of fields in PDF file (the file is given and I don't know how was created)
Row 1:
data[0].#subform[0].Tabella1[0].Riga2[0].DATA[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAINIPM[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAINILM[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAENDLM[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAENDAM[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAINIPP[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAINILP[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAENDLP[0]
data[0].#subform[0].Tabella1[0].Riga2[0].ORAENDAP[0]
Row 2:
data[0].#subform[0].Tabella1[0].Riga3[0].DATA[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAINIPM[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAINILM[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAENDLM[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAENDAM[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAINIPP[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAINILP[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAENDLP[0]
data[0].#subform[0].Tabella1[0].Riga3[0].ORAENDAP[0]
I read this fields using below code:
String newFile = source.Insert(source.Length - 4, "newModyfiy");
using (FileStream outFile = new FileStream(newFile, FileMode.Create))
{
PdfReader pdfReader = new PdfReader(source);
foreach (KeyValuePair<String, AcroFields.Item> kvp in pdfReader.AcroFields.Fields)
{
int fileType = pdfReader.AcroFields.GetFieldType(kvp.Key);
string filedValue = pdfReader.AcroFields.GetField(kvp.Key);
string transFileName = pdfReader.AcroFields.GetTranslatedFieldName(kvp.Key);
textBox1.Text = textBox1.Text + fileType.ToString() + " " + filedValue + " " + transFileName + Environment.NewLine;
}
pdfReader.Close();
}
I am getting for both rows values of the first row only. My target is to write values to those fields and save new file. When I use:
PdfStamper pdfStamper = new PdfStamper(pdfReader, new FileStream(newFile, FileMode.Create), '\0', true);
I always overwrite values of first row (when I try to set value in second row it appears in first). If I change the last parameter PdfStamper to false it writes fileds correctlly but file is not editable manually.
Is it a matter of pdf file? Is there a way to read and then write values to proper fileds?
I have spent on this few days and could not find reason of this strange behaviour.
Any small help or even clue will be appereciated.
Edit:
I add mentioned PDF file.
https://ufile.io/mwni5
I have deleted some object but general structure is kept.

How to loop TextFragment in Aspose.PDF

In a process of writing text to PDF, I'm using TextFragment for setting properties of various fields. Instead of setting for each field separately, how do make use of a loop?
My present code:
TextFragment a = new TextFragment("Hi!");
tf.setPosition(dropDown);
tf.getTextState().setFont(new FontRepository().findFont("Arial"));
tf.getTextState().setFontSize(10.0F);
.
.
.
TextFragment n = new TextFragment("n");
tf.setPosition(dropDown);
tf.getTextState().setFont(new FontRepository().findFont("Arial"));
tf.getTextState().setFontSize(10.0F);
I need something like this:
some loop {
.
.
TextFragment txtFrag = new TextFragment(A);
tf.setPosition(dropDown);
tf.getTextState().setFont(new FontRepository().findFont("Arial"));
tf.getTextState().setFontSize(10.0F);
} //This should set properties for all fields
The string in TextFragment("String") is not same for all the fields. It's different for various form fields.
You may simply add text fragments in your PDF file and once you finish adding text, you may get or set different properties for all the text fragments in a PDF file by using the code below:
// Load document
Document document = new Document( dataDir + "input.pdf");
// Create TextAbsorber object to extract all textFragments
TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber();
// Accept the absorber for first page of document
document.getPages().accept(textFragmentAbsorber);
// Get the extracted text fragments into collection
TextFragmentCollection textFragmentCollection = textFragmentAbsorber.getTextFragments();
// Loop through the Text fragments
for (TextFragment textFragment : (Iterable<TextFragment>) textFragmentCollection) {
// Iterate through text fragments
System.out.println("Text :- " + textFragment.getText());
textFragment.getTextState().setFont(new FontRepository().findFont("Arial"));
textFragment.getTextState().setFontSize(10.0F);
System.out.println("Position :- " + textFragment.getPosition());
System.out.println("XIndent :- " + textFragment.getPosition().getXIndent());
System.out.println("YIndent :- " + textFragment.getPosition().getYIndent());
System.out.println("Font - Name :- " + textFragment.getTextState().getFont().getFontName());
}
// Save generated document
document.save(dataDir + "input_17.12.pdf");
You may visit Working with Text for more information on this. I hope this will be helpful. Please let us know if you need any further assistance.
I work with Aspose as Developer Evangelist.

Processing text and convert to string without removing the text format in php

I have the following text as inputText:
HEADER:This is title 1
HEADER:This is title 2
HEADER: This is title 3
I want to remove "HEADER:" and convert the text into long string and keep the text format(without remove the spaces) as sample below:
This is title 1 This is title 2 This is title 3
Assuming the length for each line is fix to 30 character (include spaces).
The following is my code.
$string = $this->input->post(inputText);
//move the string to array
$Text = array($string);
$lines = count($Text);
$x=0;
while($x<=$lines)
{
//Remove "HEADER:" Length is = 7
$newText[$x] = array(substr($Text[$x],7));
$x++;
}
$stringText = implode("\n",$newText);
echo $stringText;
But it seems this code is not working..
Hey Julie the problem was coming that the explode function was removing the white spaces in the string so
$string = str_replace(" ", "-", $string);
Now you will get your sub string with all white spaces would be replaced by -
$Texts = explode('HEADER:',$string);
foreach($Texts as $key=>$data)
{
$Texts[$key] = str_replace('-',"&nbsp",$data);
}
By this you will have a array with sub strings you required in a array
UPDATE
$stringPhp = str_replace(" ", "-", $string);
$Texts = explode('HEADER:',$string);
$TextPHP = explode('HEADER:',$string);
foreach($TextPHP as $key=>$data)
{
$TextsPHP[$key] = str_replace('-',"&nbsp",$data);
}
//U can use $TextsPHP for you php I am sure it will not give you any troubles.
for html suppose you want to print $Texts[1] element.
<input type="text" value="<?php echo $Texts[1];" />
Now you will see that in html output, those blank spaces would be present

How to get text of element but excluding the sub-elements text

I want to get the text of the element without including the text of its elements. I have tried getText(), but it returns text that includes all the child elements text.
In the following example: When I retrieved text from the first div, it returns text that includes all its subelements.
<div class="row”>
<div class="col-lg-4 section”>
<div class="col-md-12”>
inseam 28 30 32
</div>
</div>
<div class="col-lg-5 section”>
<div class="col-md-13”>
inseam 28 34 36
</div>
</div>
</div>
Please let me know how to do this using webdriver in java.
Thanks
sean
I've been searching for the same thing for a while, here's my solution for those who can specify a WebElement or a list of WebElements:
def remove_child_text_from_webelement(webelement):
# Declaring the current text for this webelement
current_text = webelement.text
# Getting its childs elements in a list
childs_list = webelement.find_elements_by_xpath('./*')
# Manipulating text to remove child text from parents
childrens_text_list = [child.text for child in childs_list]
#return (childrens_text_list,type(childrens_text_list))
for children_text in childrens_text_list:
match_index = current_text.find(children_text)
if match_index != -1:
match_length = len(children_text)
current_text = current_text[0:match_index] + current_text[match_index+match_length:]
return current_text
Now you can do something like:
[remove_child_text_from_webelement(e) for e in browser.find_elements_by_xpath('//div[contains(#class,"person")]')]
When I retrieved text from the first div with class 'row', it returns text that includes all its subelements.
This happened because you retrieved text from the parent div and hence all the innerHTML/text of the child divs were retrieved along with them.
Below is the way to retrieve the necessary innerHTML/text only:
1- for 'inseam 28 30 32':
String text = driver.findElement(By.xpath("//div[#class='col-md-12']")).getText();
OR
String text = driver.findElement(By.className("col-md-12")).getText();
2- for 'inseam 28 34 36':
String text = driver.findElement(By.xpath("//div[#class='col-md-13']")).getText();
OR
String text = driver.findElement(By.className("col-md-13")).getText();
Not tried it specifically with Selenium, but with jQuery you can use contents() to get all elements including raw text nodes, filter by nodeType 3 (text nodes) and then take the first, in your example:
JSFiddle: http://jsfiddle.net/TrueBlueAussie/p33gcfk2/1/
var text = $('.row').contents().filter(function () {
return this.nodeType == 3;
}).first();
alert(text.text());
This is happening because you are trying to get the text of parent tag. If you want to get tag of particular child, you have to reach all the way there. You can take use of "nth-child" or "nth-of-type". For e.g in this case, if you want to have return this text "inseam 28 34 36".
The CSS selector will be "div.row div:nth-of-type(3)" or you can directly specify the div class "div.col-md-13"
You can refer to this article on more on selectors https://saucelabs.com/resources/selenium/css-selectors

Parse this string to an array of Guids or strings

I'm working with Sitefinity and when you add a custom Tags attribute to a Page it results in the following string value:
"[\"1f3560ca-84b9-6a87-9ce5-ff00009465c7\",\"893460ca-84b9-6a87-9ce5-ff00009465c7\"]"
Does anyone have a clever conversion method that can convert this string into an array of guids or strings?
I would write something that splits by , and removes the brackets... I just feel there must be a better way though but it doesn't come to mind.
You can use Microsoft JavaScriptSerializer class, which can help you turn a JSON string into objects.
var serializer = new JavaScriptSerializer();
var deserializedResult = serializer.Deserialize<List<string>>(tags);
This is my current solution...
string tags = "[\"1f3560ca-84b9-6a87-9ce5-ff00009465c7\",\"893460ca-84b9-6a87-9ce5-ff00009465c7\"]";
return tags
.Replace("[", "")
.Replace("]", "")
.Replace(" ", "")
.Replace("\"", "")
.Split(',')
.Where(t =>
{
Guid g;
return Guid.TryParse(t, out g);
}).Select(t => new Guid(t)))
Have you tried casting it to TrackedList?

Resources