Underline and StrikeOut PDF watermark text

Hi,

I’m trying to set up Underline and StrikeOut props on watermark text, but it doesn’t seem to work as expected. Here is my code:

var wmText = “First Line\nSecond Line\nThird Line”;
var lines = wmText.Split(new string[] { “\r\n”, “\r”, “\r”, “\n”, “\n” }, StringSplitOptions.RemoveEmptyEntries);

var wmFontSize = 20;

var document = new Document(filename);
var artifact = new WatermarkArtifact();

artifact.SetLinesAndState(lines, new TextState()
{
FontSize = wmFontSize,
ForegroundColor = Color.Red,
Font = FontRepository.FindFont(“Courier”),
FontStyle = FontStyles.Italic | FontStyles.Bold,
Underline = true,
StrikeOut = true
});

artifact.ArtifactHorizontalAlignment = HorizontalAlignment.Center;
artifact.ArtifactVerticalAlignment = VerticalAlignment.Center;
artifact.Rotation = 0;
artifact.Opacity = 0.8;
artifact.IsBackground = true;

foreach (var page in document.Pages)
{
page.Artifacts.Add(artifact);
}

document.Save(resultFile);

Regards

@profiler

It seems that you are trying to apply underline and strikeout properties to watermark text in your PDF document using Aspose.PDF. However, the properties you are attempting to set may not be functioning as expected in the context of a watermark artifact.

Currently, the WatermarkArtifact class does not support text formatting options such as underline and strikeout directly. Instead, these properties are typically used with text annotations or free text annotations.

To achieve the desired effect of underlining and striking out text, you might consider using FreeTextAnnotation or StrikeOutAnnotation instead of WatermarkArtifact. Here’s an example of how you can create a free text annotation with underline and strikeout effects:

var document = new Document(filename);
var freeTextAnnotation = new FreeTextAnnotation(document.Pages[1], new Rectangle(100, 100, 300, 200))
{
    Contents = "First Line\nSecond Line\nThird Line",
    TextState = new TextState
    {
        FontSize = 20,
        ForegroundColor = Color.Red,
        Font = FontRepository.FindFont("Courier"),
        FontStyle = FontStyles.Italic | FontStyles.Bold,
        Underline = true,
        StrikeOut = true
    },
    HorizontalAlignment = HorizontalAlignment.Center,
    VerticalAlignment = VerticalAlignment.Center,
    Opacity = 0.8,
    IsBackground = true
};

document.Pages[1].Annotations.Add(freeTextAnnotation);
document.Save(resultFile);

In this example, a FreeTextAnnotation is created and added to the first page of the document. The text state includes the desired formatting options, including underline and strikeout.

If you specifically need to use a watermark, you may need to overlay multiple annotations or consider a different approach to achieve the visual effect you want.

Feel free to ask if you have any further questions or need additional assistance!

Sources:
[1]: Using Text Annotation for PDF|Aspose.PDF for .NET

We are trying to replace the current third party component with Aspose. Our product currently supports both watermark supports. Do you have a plan to implement the functionality?

@profiler

Please check the below article(s) in the API documentation where code samples are given to add text stamps (Watermark) in the PDF document while specifying the font styles.

In case it still does not help, please share a sample expected output PDF or image with us so that we can further proceed to assist you accordingly.

Thanks for the reply! I have successfully migrated most of the current functionality we already have, but what I am wondering is how to position the watermark text diagonally? Please see the attached document and the code I am using.

public static void ApplyWMToPDFStamp(string filename, WatermarkSettings ws, string resultFile)
{
    var document = new Document(filename);

    var wmFontSize = ws.FontSize;
    var wmText = ws.Text;
    var lines = wmText.Split(new string[] { "\r\n", "\r", "\\r", "\n", "\\n" }, StringSplitOptions.RemoveEmptyEntries);

    var text = new FormattedText();
    foreach (var l in lines)
    {
        text.AddNewLineText(l);
    }

    var stamp = new TextStamp(text);
    stamp.HorizontalAlignment = HorizontalAlignment.Center;

    if (ws.RotationAngle == RotationAngleConsts.HEADER)
        stamp.VerticalAlignment = VerticalAlignment.Top;
    else if (ws.RotationAngle == RotationAngleConsts.FOOTER)
        stamp.VerticalAlignment = VerticalAlignment.Bottom;
    else if (ws.RotationAngle == RotationAngleConsts.VERTICAL)
    {
        stamp.VerticalAlignment = VerticalAlignment.Center;
        stamp.Rotate = Rotation.on90;
    }
    else
        stamp.VerticalAlignment = VerticalAlignment.Center;

    if(ws.TextAlignment == "Right")
        stamp.TextAlignment = HorizontalAlignment.Right;
    else if (ws.TextAlignment == "Left")
        stamp.TextAlignment = HorizontalAlignment.Left;
    else if (ws.TextAlignment == "Justify")
        stamp.TextAlignment = HorizontalAlignment.Justify;
    else
        stamp.TextAlignment = HorizontalAlignment.Center;

    stamp.Background = true;

    // Set text properties
    stamp.TextState.Font = FontRepository.FindFont(ws.FontType);
    stamp.TextState.FontSize = wmFontSize;
    stamp.TextState.FontStyle = ws.WMFontStyles;
    stamp.TextState.Underline = ws.FontStyle.HasFlag(Spire.Pdf.Graphics.PdfFontStyle.Underline);
    stamp.TextState.StrikeOut = ws.FontStyle.HasFlag(Spire.Pdf.Graphics.PdfFontStyle.Strikeout);
    stamp.TextState.ForegroundColor = Color.FromRgb(System.Drawing.Color.Red);
    stamp.Opacity = ws.Transparency;
    stamp.TopMargin = 10;
    stamp.BottomMargin = 10;

    foreach (var page in document.Pages)
    {
        page.AddStamp(stamp);
    }

    document.Save(resultFile);
}

wm.pdf (150.2 KB)

@profiler

In order to add stamp diagonally, you can use TextStamp.RotateAngle property and specify an angle e.g. 45. Please try it and let us know in case you face any issues.

Works, thanks!