Align the text to left

HI,

We are using addpageNumber and would like to display the text to the bottom left. Can you please let me know if this is possible. We tried various coordinates and the text is always alligned center.

The mehod we are using is

public void addPageNumber(java.lang.StringformatString,
                          intposition,
                          floatleftMargin,
                          floatrightMargin,
                          floattopMargin,
                          floatbottomMargin)

Thanks In Advance

B

Hi Mayur,

Thanks for contacting support.

I have tested the scenario and have observed that the “Position” parameter of the addPageNumber(..) method does not support the value to set the page number stamp on the Bottom-Left position. For the sake of implementation, I have logged this requirement in our issue tracking system under the New Features list as PDFNEWJAVA-33617. We will further investigate this requirement in detail and will keep you updated on the status of a correction.

However, as a workaround, you may specify 0 as the position parameter and set the left margin value to negative so that the page stamp is displayed on the Bottom-Left. Please take a look at the following code snippet. For your reference, I have also attached the resultant PDF generated on my end.

We are sorry for this inconvenience.

Java

    String inFile = "c:/pdftest/DemoWord.pdf";
    String outFile = "c:/pdftest/DemoWord_PageNumber.pdf";

    com.aspose.pdf.facades.PdfFileStamp fileStamp = new com.aspose.pdf.facades.PdfFileStamp(inFile,
     outFile);

    // add page number
    fileStamp.addPageNumber(new
     com.aspose.pdf.facades.FormattedText("Page # .",
      new Color(0, 0, 100), com.aspose.pdf.facades.FontStyle.CourierBold,
      com.aspose.pdf.facades.EncodingType.Winansi, false, 10),
     0, -550, 0, 0, 0);

     fileStamp.close();

Hi,

With the coordinates provided it is alligning text to the complete left and if the text is too long, the starting characters are trimmed off. We need to position the text in allignment with the left margin of the pdf and should be able to control the position of the text in the botton left area of the page.

The left margin of the text should be fixed and with the coordinates provided, I think the right margin is fixed and when new data is added it is moving left

The text is displayed at bottom left even with the followig coordinates but the same trimming issue

fileStamp.addPageNumber(new FormattedText(stripText, new Color(0,10,100),
FontStyle.CourierBold, EncodingType.Winansi, false, 10), 0, 0, 0, 0, 0)

~B

Hi Mayur,

In order to display a stamp in the bottom-left position, I would suggest using the PageNumberStamp class of the Aspose.Pdf namespace. Please try using the following code snippet. For further information, please visit Add Page Number Stamp in a PDF File.

[C#]

//open document
Document pdfDocument = new Document("c:/pdftest/DemoWord.pdf");

//create page number stamp
PageNumberStamp pageNumberStamp = new PageNumberStamp();

//whether the stamp is background
pageNumberStamp.Background = false;
pageNumberStamp.Format = "Page # of " + pdfDocument.Pages.Count;
pageNumberStamp.BottomMargin = 10;

// set horizontal alignment for stamp object
pageNumberStamp.HorizontalAlignment = Aspose.Pdf.HorizontalAlignment.Left;

// set vertical alignment for stamp object
pageNumberStamp.VerticalAlignment = Aspose.Pdf.VerticalAlignment.Bottom;
pageNumberStamp.StartingNumber = 1;

//set text properties
pageNumberStamp.TextState.Font = FontRepository.FindFont("Arial");
pageNumberStamp.TextState.FontSize = 14.0F;

//add stamp to particular page
pdfDocument.Pages[1].AddStamp(pageNumberStamp);

//save output document
pdfDocument.Save("c:/pdftest/Bottom_Left_AlignedStamp.pdf");

Hi Nayyer,

We need to implement the mentioned feature in Java and I do not see PageNumberStamp in the java api reference. Please let me know if there is a way we can achieve this in Java using PdfFileStamp or any other class.

For your reference, I’m mentioning the requirement once again: We should be able to add a dynamic text at the bottom of the page, and it should be in alignment with the left margin of the page and the left margin position should be fixed.

Hi Mayur,

First of all, please accept my apologies as I shared the information related to Aspose.Pdf for .NET. Please try using the following code snippet for Aspose.Pdf for Java. Ensure that you are using the latest release of Aspose.Pdf for Java 4.1.2.

// open document
com.aspose.pdf.Document pdfDocument = new com.aspose.pdf.Document("input.pdf");

// create page number stamp
com.aspose.pdf.PageNumberStamp pageNumberStamp = new com.aspose.pdf.PageNumberStamp();
// whether the stamp is background
pageNumberStamp.setBackground(false);
pageNumberStamp.setFormat("Page # of " + pdfDocument.getPages().size());
pageNumberStamp.setBottomMargin(10);

// set horizontal alignment for stamp object
pageNumberStamp.setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Left);

// set vertical alignment for stamp object
pageNumberStamp.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Bottom);
pageNumberStamp.setStartingNumber(1);

// set text properties
pageNumberStamp.getTextState().setFont(com.aspose.pdf.FontRepository.findFont("Arial"));
pageNumberStamp.getTextState().setFontSize(14.0F);
pageNumberStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);
pageNumberStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Italic);
pageNumberStamp.getTextState().setForegroundColor(java.awt.Color.BLUE);

// add stamp to particular page
pdfDocument.getPages().get_Item(1).addStamp(pageNumberStamp);

// save output document
pdfDocument.save("PageNumberStamp_output.pdf");

Hi Mayur,


Thanks for your patience. In reference to above reported issue (PDFNEWJAVA-33617), please note we have already implemented additional positions in Aspose.Pdf for java as following. Moreover, you can also use the predefined constants:


5 - bottom left,
6 - sides left,
7 - upper left.


PdfFileStamp.POS_BOTTOM_MIDDLE
PdfFileStamp.POS_BOTTOM_RIGHT
PdfFileStamp.POS_UPPER_RIGHT
PdfFileStamp.POS_SIDES_RIGHT
PdfFileStamp.POS_UPPER_MIDDLE
PdfFileStamp.POS_BOTTOM_LEFT
PdfFileStamp.POS_SIDES_LEFT
PdfFileStamp.POS_UPPER_LEFT


Please download and try latest version of Aspose.Pdf for Java, it will help you to accomplish the task.

Please feel free to contact us for any further assistance.

Best Regards,