Page 1 of 1
C# code for setting up line spacing in pdf form
Posted: Tue Apr 18, 2023 9:30 am
by troutinger
Hey,
in my project I have to fill in forms with text in a pdf file. The text is so long, that it needs more than one line in the form field.
This works very well:
PDFXEdit.IPXC_FormField ff_zusammenfassung = Doc.AcroForm.GetFieldByName("Zusammenfassung");
ff_zusammenfassung.SetValueText(_proto.Abstract);
But the problem is that everytime I write text in the form, the preset line spacing is set from 2 to 1. How can I ajust / set the line spacing in the form by C# code? I haven't found any example for this issue.
Regards,
Michael
Re: C# code for setting up line spacing in pdf form
Posted: Mon Apr 24, 2023 10:15 pm
by Vasyl - PDF-XChange
Hi Michael.
Unfortunately, there isn't a simple way to do that. But if you know the font size of text - you may use the following workaround(C#):
Code: Select all
PDFXEdit.IPXC_FormField field = coreDoc.AcroForm.Field[k];
double fontSize = 16;
double percents = 200;
for (uint i = 0; i < field.WidgetsCount; i++)
{
PDFXEdit.IPXC_Annotation annot = field.Widget[i];
PDFXEdit.IPXS_PDFVariant pdfVar = annot.PDFObject;
PDFXEdit.IPXS_Document cosDoc = coreDoc.CosDocument;
cosDoc.LockDocumentExclusive();
string sDS = pdfVar.Dict_GetString("DS", "");
{
if (sDS != "")
sDS += ";";
sDS += string.Format("line-height:{0}pt;line-spacing:{1}%;", 1.2 * fontSize * percents / 100.0, percents);
pdfVar.Dict_SetString("DS", sDS);
}
cosDoc.UnlockDocumentExclusive();
}
field.SetValueText("sample text line 1\rsample text line 2\rsample text line 3");
Note: "DS" parameter contains the default(initial) style for the rich-formatted text in terms of XFA-format (XFA-Reference:
https://www.pdfa.org/norm-refs/XFA-3_3.pdf, page 1191). But unfortunately the XFA's "line-height" attribute doesn't support the percentage value to use it for line spacing. It uses the following formula instead:
line-height = 1.2*fontSize*percents/100 pt
It works well until the user decides to change the font size for text inside form-field. Because there we have just fixed line-height, specified in points and changing the font size will not affect the specified fixed line-heigh value. To solve this problem I also used in code above the "line-spacing" parameter as a tip for the text-editor that there is not just fixed value in points but also is proportional value in percentages. Problem is that only PDFXEditor app supports that additional "line-spacing" attribute. Other pdf-apps use fixed values only for the line spacing.
HTH.
Re: C# code for setting up line spacing in pdf form
Posted: Wed Apr 26, 2023 12:48 pm
by troutinger
Hi,
thank you for your answer. I tried it as follows:
Code: Select all
PDFXEdit.IPXC_FormField _zusammenfassung = Doc.AcroForm.GetFieldByName("Zusammenfassung");
double fontSize = 9;
double percents = 200;
for (uint c = 0; c < _zusammenfassung.WidgetsCount; c++)
{
PDFXEdit.IPXC_Annotation annot = _zusammenfassung.Widget[c];
PDFXEdit.IPXS_PDFVariant pdfVar = annot.PDFObject;
PDFXEdit.IPXS_Document cosDoc = Doc.CosDocument;
cosDoc.LockDocumentExclusive();
string sDS = pdfVar.Dict_GetString("DS", "");
{
if (sDS != "")
sDS += ";";
sDS += string.Format("line-height:{0}pt;line-spacing:{1}%;", 1.2 * fontSize * percents / 100.0, percents);
pdfVar.Dict_SetString("DS", sDS);
}
cosDoc.UnlockDocumentExclusive();
}
_zusammenfassung.SetValueText(_proto.Abstract);
The code runs without any errors. The loop is done once. But when I write Doc to file (Doc.WriteToFile(path)) the pdf does not show any difference in line spaccing in the _zusammenfassung formfield.
I tried several percent and fontsize numbers, but this does not affect the linespacing in the formfield at all. I can even place some stupid text in DS, and even this does not show any effect.
Do I have to configure the form in a special way? I set it up with multi row and enable text formating.
Thanks for your answer,
Michael
Re: C# code for setting up line spacing in pdf form
Posted: Thu Apr 27, 2023 6:35 pm
by Vasyl - PDF-XChange
Hi Michael.
I set it up with multi row and enable text formating.
Yes, I forgot to mention that important requirements, glad you did it. Anyway, I can't reproduce on my test forms the issue you reported, even with your code. Maybe there is something document-specific. Can you provide the minimal test doc where the problem can be reproduced?
Cheers.
Re: C# code for setting up line spacing in pdf form
Posted: Thu Apr 27, 2023 6:57 pm
by Vasyl - PDF-XChange
Possible reason of trouble - you need to use CultureInfo.InvariantCulture for the Format because on some systems it uses the ',' instead of '.' for decimal numbers. So correct is:
string.Format(CultureInfo.InvariantCulture, "line-height:{0}pt;line-spacing:{1}%;", 1.2 * fontSize * percents / 100.0, percents);
Re: C# code for setting up line spacing in pdf form
Posted: Fri Apr 28, 2023 6:52 am
by troutinger
Hello,
I attached you my sample program (without the key section).
There you can set up different fontsizes and percents. But it has no effect on the resulting pdf.
I hope you can reproduce the problem.
Best regards,
Michael
FieldsModifySampleOrg.zip
Re: C# code for setting up line spacing in pdf form
Posted: Thu May 04, 2023 11:23 pm
by Vasyl - PDF-XChange
Sorry for the delay with that... I checked it and seems that our Editor works perfectly with it, while Acrobat - does not. In the ResDoc.pdf the Acrobat just ignores the valid "DS" (default style) parameter because you have not specified for him the "RV" (rich value). But the Editor handles the DS even if the RV is absent or empty, which is more usable. So unfortunately, but there doesn't seem to be a suitable workaround for your case. You have only one way - to build and set the rich value too. And with the current API you cannot build that value easily. It is too complex task to do it on your side.
BUT soon we will add couple simple methods to our API to get something like:
Code: Select all
string sDS = field.DS;
if (sDS != "") sDS += ";";
sDS += string.Format("line-height:{0}pt;line-spacing:{1}%;", 1.2 * fontSize * percents / 100.0, percents);
field.DS = sDS;
field.SetValueText("aaa\r\bbb\rccc");
field.GenerateRichValue(); // will generate the rich-text value from the field's plain-text value
Re: C# code for setting up line spacing in pdf form
Posted: Mon May 08, 2023 2:26 pm
by troutinger
Hello,
thank you for your answer.
The only thing i cannot understand is that you say your editor shows different spacing with my program.
My pdfxchange editor does not show any difference whatever I enter in the two fields.
Best regards,
Michael
Re: C# code for setting up line spacing in pdf form
Posted: Tue May 23, 2023 3:52 pm
by Daniel - PDF-XChange
Hello, troutinger
Are you speaking of this comment at the beginning of the thread?
Vasyl-Tracker Dev Team wrote: ↑Mon Apr 24, 2023 10:15 pm
Problem is that only PDFXEditor app supports that additional "line-spacing" attribute. Other pdf-apps use fixed values only for the line spacing.
If so, I don't believe that he meant to say the editor will always render differently, just that there are some cases where it may occur.
Kind regards,
Re: C# code for setting up line spacing in pdf form
Posted: Mon Jan 08, 2024 7:42 am
by troutinger
Hey,
has there already been an update which concerns the problem shown above?
Regards,
Michael