Quantcast
Viewing all articles
Browse latest Browse all 3131

Page number stamp not working

Hi ,

Please find the code to stamp page numbers.The code is throwing exception. Please help.

import java.io.FileInputStream;

import java.io.FileNotFoundException;

 

import com.aspose.pdf.Document;

import com.aspose.pdf.License;

import com.aspose.pdf.MarginInfo;

import com.aspose.pdf.Page;

import com.aspose.pdf.PageCollection;

import com.aspose.pdf.PageInfo;

import com.aspose.pdf.PageNumberStamp;

 

publicclass TestPageNumberStamp {

 

                        /**

                         * @param args

                         * @throws FileNotFoundException

                         */

                        publicstaticvoid main(String[] args) throws FileNotFoundException {

 

                                                java.util.Locale locale1 = new java.util.Locale("en");

                                                java.util.Locale.setDefault(locale1);

 

                                                License lic = new License();

                                                lic.setLicense(new FileInputStream(

                                                                                                "D:\\Workspaces\\Examples\\Aspose\\src\\conf\\Aspose.Total.Java.lic"));

 

                                                // Added document

                                                Document doc = new com.aspose.pdf.Document();

 

                                                PageInfo pageInfo = doc.getPageInfo();

                                                MarginInfo marginInfo = pageInfo.getMargin();

 

                                                marginInfo.setLeft(37);

                                                marginInfo.setRight(37);

                                                marginInfo.setTop(37);

                                                marginInfo.setBottom(37);

 

                                                pageInfo.isLandscape(true);

 

                                                com.aspose.pdf.Table table = new com.aspose.pdf.Table();

                                                table.setColumnWidths("50 100");

                                                // added page.

                                                Page curPage = doc.getPages().add();

 

                                                for (int i = 1; i <= 3; i++) {

                                                                        com.aspose.pdf.Row row = table.getRows().add();

                                                                        com.aspose.pdf.Cell cell1 = row.getCells().add();

                                                                        cell1.getParagraphs().add(

                                                                                                                        new com.aspose.pdf.TextFragment("Content 1"));

 

                                                                        com.aspose.pdf.Cell cell2 = row.getCells().add();

                                                                        cell2.getParagraphs().add(

                                                                                                                        new com.aspose.pdf.TextFragment("Content 2"));

                                                }

                                               

                                                curPage.getParagraphs().add(table);

 

                                                final PageNumberStamp pageNumberStamp = new PageNumberStamp();

                                                // whether the stamp is background

                                                pageNumberStamp.setBackground(false);

                                                pageNumberStamp.setFormat("Page # of " + 1);

                                                pageNumberStamp.setBottomMargin(10);

                                                pageNumberStamp

                                                                                                .setHorizontalAlignment(com.aspose.pdf.HorizontalAlignment.Center);

                                                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);

 

                                                final PageCollection pages = doc.getPages();

                                                finalint pageSize = pages.size();

                                                for (int i = 1; i <= pageSize; i++) {

                                                                        final Page page = pages.get_Item(i);

                                                                        page.addStamp(pageNumberStamp);

                                                }

                                               

                                                doc.save("pageStamp.pdf");

 

                        }

 

}

 


Viewing all articles
Browse latest Browse all 3131

Trending Articles