I´m trying to add a text at a specifik position on top of each page in a PDF, without manipulating the actual page.
Adding a new layer seems to be the solution and this method does the job as expected, with the exception that swedish characters is not displayd correctly. I am using version 22.10 (licensed) and have tried 24.10 too.
public string Sign(string path, string filename, string text, string name)
{
var doc = new Document(Path.Combine(path, filename));
for (int i = 1; i <= doc.Pages.Count; i++)
{
var newLayer = new Layer("TopLayer", "Top Layer");
newLayer.Contents.Add(new GSave());
newLayer.Contents.Add(new SetColorSpace(ColorSpace.DeviceRGB.ToString()));
newLayer.Contents.Add(new SetCMYKColorStroke(1, 0, 0, 0));
newLayer.Contents.Add(new BT());
newLayer.Contents.Add(new SelectFont("Times New Roman", 12));
newLayer.Contents.Add(new MoveTextPosition(50, 20));
newLayer.Contents.Add(new ShowText("ÅÄÖåäö"));
newLayer.Contents.Add(new ShowText(text));
newLayer.Contents.Add(new ShowText($" {name}."));
newLayer.Contents.Add(new ShowText($" Page {i}/{doc.Pages.Count}"));
newLayer.Contents.Add(new ET());
newLayer.Contents.Add(new GRestore());
var layers = doc.Pages[i].Layers = new List<Layer>();
layers.Add(newLayer);
}
string output = Path.Combine(path, "signed_" + filename);
doc.Save(output);
return output;
}
2024-11-06 13-19.png (1.6 KB)
How do I fix this or is there a better way to solve the task?
@MathiasOlsson
Looks like a font related issue. Please make sure that the fonts that support Swedish language characters are present and installed in your system. In case issue still persists, please let us know.
Thank you for your response. Sorry for my late reply.
Yes, I noticed it was a font problem. It seams the “SelectFont” does not hav any affect - no matter what font I choose. I even tried the font the already exists in the dokument. But It will still use some kind of default font.
I ended up using the AddStamp function wich pretty much does wat I wanted it to do. Exept that I wanted a part of the text to be italic.
private static void AddStampText(string text, string name, int i, Aspose.Pdf.Document pdfDocument, double xIndent,
double sealWidth, Page page, double yIndent, double opacy, out bool doubleLineText)
{
FormattedText formattedText = new FormattedText($"{text} {name}. Sida {i}/{pdfDocument.Pages.Count}",
new FontColor(1, 0, 0), Aspose.Pdf.Facades.FontStyle.TimesRoman, EncodingType.Winansi, true,
12);
doubleLineText = false;
// Om texten blir så lång så sidan inte räcker till så delar vi upp texten på två rader.
if (formattedText.TextWidth + xIndent + sealWidth > page.PageInfo.Width)
{
doubleLineText = true;
formattedText = new FormattedText($"{text}", new FontColor(1, 0, 0),
Aspose.Pdf.Facades.FontStyle.TimesRoman, EncodingType.Winansi, true, 12);
formattedText.AddNewLineText($"{name}. Sida {i}/{pdfDocument.Pages.Count}\"");
}
var ftStamp = new TextStamp(formattedText);
ftStamp.XIndent = xIndent;
ftStamp.YIndent = yIndent;
ftStamp.Opacity = opacy;
page.AddStamp(ftStamp);
}
@MathiasOlsson
If possible, can you please provide below:
- Sample expected output PDF with desired text formatting
- Output PDF that you are currently able to produce
Thank you.
This assignment was a POC and it was cancelled or at least put on hold for the moment. So I can´t spend creating example files.
If you still want to investigate it further:
Expected output is TEXT + NAME (in italic) + PAGE at bottom left of the page like this:
2024-11-13 stamp.png (7.2 KB)
The font is “Book Antiqua”. The text can not intefear with other content on the page. Thats is why I was using layers.
The TextStamp works great, but it seams it can only use one FormattedText object with one font style (no italic in the middle).
Update:
This is the result of adding TextStamp with swedish characters:
2024-11-13 AddStamp.png (7.8 KB)
@MathiasOlsson
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.
Issue ID(s): PDFNET-58635
You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.
1 Like