This technical tip shows how developers can add text in headeror footer of a PDF file inside their Android applications. The TextStamp class
is used to add text in PDF document. We can also use the same class to add text
in Header/Footer area of PDF file. The TextStamp class provides necessary
methods to specify font size, font style, and font color etc for stamp object.
In order to add text in the Header area, first you need to create a Document
object and a TextStamp object using required methods. After that, you can call
addStamp(..) method of Page class to add text in the PDF file. Please note that when placing text in Header
section, you need to call setTopMargin(..) in such a way that it adjusts the
text in the header area of your PDF. Similarly when placing text in Footer
section, you need to call setBottomMargin(..) in such a way that it adjusts to
the text in Footer area.
·
When adding stamp object in Header section, you
also need to set VerticalAlignment to Top.
·
When adding stamp object in Footer section, you
also need to set VerticalAlignment to Bottom.
The following code
snippet shows you how to add text in the header of a PDF file.
Java
//open document
com.aspose.pdf.Document pdfDocument = new
com.aspose.pdf.Document("/mnt/sdcard/input.pdf");
//create text stamp
com.aspose.pdf.TextStamp textStamp = new
com.aspose.pdf.TextStamp("Sample Stamp");
//set properties of the stamp
textStamp.setTopMargin(10);
textStamp.setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);
textStamp.setVerticalAlignment(com.aspose.pdf.VerticalAlignment.Top);
//set text properties
textStamp.getTextState().setFont(new
com.aspose.pdf.FontRepository().findFont("Arial"));
textStamp.getTextState().setFontSize(14.0F);
textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Bold);
textStamp.getTextState().setFontStyle(com.aspose.pdf.FontStyles.Italic);
textStamp.getTextState().setForegroundColor(java.awt.Color.GREEN);
// iterate through all pages of PDF file
for (int Page_counter =1;
Page_counter<=pdfDocument.getPages().size();Page_counter++)
{
//add stamp to
all pages of PDF file
pdfDocument.getPages().get_Item(Page_counter).addStamp(textStamp);
}
//save output document
pdfDocument.save("/mnt/sdcard/TextStamp_output.pdf");
Overview: Aspose.Pdf for Android
Aspose.Pdf for Android is a PDF
document creation & manipulation component that enables Android
applications to read, write & manipulate PDF document without using any
other third party application. It allows PDF compression options, table
creation & manipulation, support for graph objects, extended security
controls, custom font handling, add or remove bookmarks, create table of
contents, add, update, delete attachments & annotations, import or export
PDF form data, print PDF docs & much
more.
- Homepage of Aspose.Pdffor Android
- Download Aspose.Pdf for Android
No comments:
Post a Comment