Adhere Text

Skip to end of metadata
Go to start of metadata
Adhere Text

Developers can adhere formatted text to any page of the PDF document using Aspose.Pdf.Kit and AddText method of PdfFileMend class serves the purpose.
Before calling AddText method, we have to find the Height, Width and Rotation of the page (on which text is to be adhered) of input PDF document. To get this information about the page, we create an object of PdfFileInfo class and call GetPageHeight GetPageWidth and GetPageRotation methods. These methods take the page number of the page (whose height, width and rotation information is required) as the argument.

(Note: Rotation of a page in a PDF document can have 0, 90, 180 or 270 value.)

The example below demonstrates to adhere formatted text on a single page. But other overloaded methods of AddText also allow to adhere formatted text on multiple pages too.

In .NET, follow these steps to adhere the text on a page in PDF document:

  • Create variables to hold the paths of input (inputFile) and output (outputFile) PDF files.
  • Instantiate PdfFileInfo object (fileInfo) and pass the variable (inputFile) holding the path of input PDF file to its constructor.
  • Call the GetPageHeight GetPageWidth and GetPageRotation methods of PdfFileInfo object to get the height, width and rotation of the page where a developer needs to adhere text.
  • Instantiate PdfFileMend object (mendor) and pass the variables (inputFile and outputFile) holding the paths of input and output PDF files.
  • Call AddText method to adhere formatted text FormattedText o n the page according to page's rotation value.
[Java]
String inputFile = "rotation270.pdf";
String outputFile = "kitOut.pdf";
int[] pages = new int[] {1,2};
 PdfFileInfo fileInfo = new PdfFileInfo(inputFile);
float height = fileInfo.getPageHeight(1);
float width = fileInfo.getPageWidth(1);
int rotation = fileInfo.getPageRotation(1);
PdfFileMend mendor = new PdfFileMend(inputFile, outputFile);
switch (rotation) {
        case 0:
        //lowerleftX < upperrightX,lowerleftY < upperrightY
        mendor.addText(new FormattedText("PdfFileMend testing! 0 rotation."),
               pages, 10, height - 25, width, height - 10);
        break;

        case 90:
       //lowerleftX > upperrightX,lowerleftY > upperrightY
        mendor.addText(new FormattedText("PdfFileMend testing! 90 rotation."),
              pages, width - 25, height - 10, width - 45,height - 150);
       break;

        case 180:
       //lowerleftX < upperrightX,lowerleftY < upperrightY
        mendor.addText(new FormattedText("PdfFileMend testing! 180 rotation."),
               pages, 10, 10, width, 25);
        break;

        case 270:
       //lowerleftX < upperrightX,lowerleftY < upperrightY
        mendor.addText(new FormattedText("PdfFileMend testing! 270 rotation."),
              pages, 10, 10, 25, height - 10);
       break;

        default:
        break;
}

mendor.close();
}
 
Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.