C# code for setting up line spacing in pdf form

PDF-XChange Editor SDK for Developers

Moderators: TrackerSupp-Daniel, Tracker Support, Paul - Tracker Supp, Vasyl-Tracker Dev Team, Chris - Tracker Supp, Sean - Tracker, Ivan - Tracker Software, Tracker Supp-Stefan

Forum rules
DO NOT post your license/serial key, or your activation code - these forums, and all posts within, are public and we will be forced to immediately deactivate your license.

When experiencing some errors, use the IAUX_Inst::FormatHRESULT method to see their description and include it in your post along with the error code.
Post Reply
troutinger
User
Posts: 8
Joined: Tue Apr 18, 2023 9:18 am

C# code for setting up line spacing in pdf form

Post 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
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2352
Joined: Thu Jun 30, 2005 4:11 pm
Location: Canada

Re: C# code for setting up line spacing in pdf form

Post by Vasyl-Tracker Dev Team »

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.
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
troutinger
User
Posts: 8
Joined: Tue Apr 18, 2023 9:18 am

Re: C# code for setting up line spacing in pdf form

Post 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
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2352
Joined: Thu Jun 30, 2005 4:11 pm
Location: Canada

Re: C# code for setting up line spacing in pdf form

Post by Vasyl-Tracker Dev Team »

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.
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2352
Joined: Thu Jun 30, 2005 4:11 pm
Location: Canada

Re: C# code for setting up line spacing in pdf form

Post by Vasyl-Tracker Dev Team »

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);
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
troutinger
User
Posts: 8
Joined: Tue Apr 18, 2023 9:18 am

Re: C# code for setting up line spacing in pdf form

Post 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
(292.71 KiB) Downloaded 56 times
User avatar
Vasyl-Tracker Dev Team
Site Admin
Posts: 2352
Joined: Thu Jun 30, 2005 4:11 pm
Location: Canada

Re: C# code for setting up line spacing in pdf form

Post by Vasyl-Tracker Dev Team »

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
Vasyl Yaremyn
Tracker Software Products
Project Developer

Please archive any files posted to a ZIP, 7z or RAR file or they will be removed and not posted.
troutinger
User
Posts: 8
Joined: Tue Apr 18, 2023 9:18 am

Re: C# code for setting up line spacing in pdf form

Post 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
User avatar
TrackerSupp-Daniel
Site Admin
Posts: 8439
Joined: Wed Jan 03, 2018 6:52 pm

Re: C# code for setting up line spacing in pdf form

Post by TrackerSupp-Daniel »

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,
Dan McIntyre - Support Technician
Tracker Software Products (Canada) LTD

+++++++++++++++++++++++++++++++++++
Our Web site domain and email address has changed as of 26/10/2023.
https://www.pdf-xchange.com
Support@pdf-xchange.com
troutinger
User
Posts: 8
Joined: Tue Apr 18, 2023 9:18 am

Re: C# code for setting up line spacing in pdf form

Post by troutinger »

Hey,

has there already been an update which concerns the problem shown above?


Regards,
Michael
Post Reply