FormattedText Right Alignment

I have some code that merges 3 PDFs together and then attempts to write out a Page Header, Page Footer and Page Number in the lower right. I'm able to do that using a Stamp and a FormattedText object and I'm able to get the text on the page. However, I need to put a title and date on the right side of the report. But, those 2 values need to be on separate lines and right aligned.

How can I right align formatted text with a stamp OR, is there just another way to do it? Perhaps using different objects?

Hi Dmccormack,

Thanks for using our products.

You can use table in page header and footer section to manage multiline stamps easily. Kindly check the following documentation link for details and code snippets as per your requirement.

Table in Header/Footer section

Please do let us know if you need any further assistance.

Thanks & Regards,

Hi Dmccormack,
I think your requirement is to add multiline watermark over merged PDF file, so my understanding is that in order to use multiline watermark, you can use FormattedText.AddNewLineText method and in order to right align the stamp, specify its origin withSetOrigin method. I hope following code can fulfill your requirement.

//open document
PdfFileStamp fileStamp = new PdfFileStamp("Test.pdf", "output.pdf");
//Instantiate a stamp object
Aspose.Pdf.Facades.Stamp logoStamp = new Aspose.Pdf.Facades.Stamp();
//Instantiate an object of FormattedText class
FormattedText formatText = new FormattedText("Document Title",System.Drawing.Color.FromArgb(180, 0, 0), Aspose.Pdf.Facades.FontStyle.TimesItalic, EncodingType.Winansi, false, 20);
logoStamp.SetOrigin(450, 20);
//Add another line for Stamp
formatText.AddNewLineText(""+DateTime.Now.Date.ToShortDateString());
//BindLogo to PDF
logoStamp.BindLogo(formatText);
//add stamp to PDF file
fileStamp.AddStamp(logoStamp);
//save updated PDF file
fileStamp.Close();

http://www.aspose.com/documentation/.net-components/aspose.pdf-for-.net/adding-multiline-watermark-to-existing-pdf.html

I think the Table in Header/Footer is exactly what I need. Thank you.

The multiline stamp would be more complicated because the text I'm adding are going to be variable in length depending on circumstances. I'd much rather have something else align for me then try to get the origin exactly right.

Actually, i'm trying to use the example but I can't seem to find the object:

Aspose.Pdf.Generator

and

Aspose.Pdf.Generator.Pdf

which namespace is that in?

Hi David,

Aspose.Pdf.Generator is the namespace which is a part of our Aspose.Pdf for .NET (v6.x) Component. Which version of Aspose.Pdf for .NET are you using? If you are using some old version of Aspose.Pdf for .NET then I would suggest you to upgrade to the latest version of Aspose.Pdf for .NET and try it.

In case of any confusion, please feel free to contact support.

Thank You & Best Regards,

I was on an older version. I'm now running the latest from Downloads.I followed the example from above and it did create a header as the link showed. However, it also erased everything else I already had in the .pdf. I'm guessing maybe I didn't open the pdf correctly?

Your code had:

Aspose.Pdf.Generator.Pdf pdfConv = new Aspose.Pdf.Generator.Pdf();

pdfConv.Save(@"d:\pdftest\Table_in_Header.pdf");

So, I changed that too:

System.IO.Stream inPDFStream = File.Open(inFile, FileMode.Append);
Aspose.Pdf.Generator.Pdf pdfConv = new Aspose.Pdf.Generator.Pdf(inPDFStream);

........
pdfConv.Close(); <-- at the very end.

Seems like it should work, but doesn't.

A little more help please?

Edit: P.S. The hard part of my problem is getting text to automatically right align, that's where I'm really having the most issues.

Success. The key was 'TextWidth'. Once I created the formatted text I was able to determine the exact width of the text that was to be created.

So, what I did was create a function:

private static float GetRightStartingPoint(PdfFileInfo fileInfo, int pageNumber, float textWidth)
{
return fileInfo.GetPageWidth(pageNumber) - textWidth - 40;
}

GetPageWidth tells me how wide the page is (I have portrait and landscaped mixed)

textWidth is the width of the text I'm adding in.

-40 is for the margin.

So I'm basically starting from the right side and subtracting widths from it, which creates a right aligned block of text.