When the total number of PDF pages is uncertain, the page numbers (I of n) and PDF pages are added to the exported pdf.

Source: Internet
Author: User

When the total number of PDF pages is uncertain, the page numbers (I of n) and PDF pages are added to the exported pdf.

To export a PDF file, you must first export the PDF code.

The Code is as follows:

1 Document document1 = new Document (); // create a Document instance 2. Writer. getInstance (document1, new FileStream (@ "C: \ 001.mode", FileMode. create); // Create a PDF 3 document1.Open (); // open page 4 Random ran = new Random (); // define a random number to simulate uncertain pages 5 for (int I = 0; I <ran. next (30,100); I ++) 6 {7 document1.SetPageSize (PageSize. a4); // set the page size 8 document1.NewPage (); // open a new page 9 document1.Add (new Paragraph ("PDF1, PDF1, PDF1, PDF1, PDF1 ")); // Add something 10} 11 document1.Close ();

You have created a pdf file, and then add the page number to the pdf file.
The Code is as follows:

1 // create a PdfReader object 2 PdfReader reader = new PdfReader (@ "C: \ 001.20."); 3 // obtain the document page 4 int n = reader. numberOfPages; 5 // create a Document variable 6 document Document = new document (); 7 // create this Document 8 bytes writer = others writer. getInstance (document, new FileStream (@ "C: \ 002.133", FileMode. create); 9 // open document 10 document. open (); 11 // Add content 12 bytes contentbyte cb = writer. directContent; 13 for (int I = 0; I <reader. numberOfPages; I ++) 14 {15 // obtain the size 16 Rectangle psize = reader. getPageSize (I + 1); 17 float width = psize. width; 18 float height = psize. height; 19 // set the page size 20 document. setPageSize (psize); 21 // create a new page 22 document. newPage (); 23 // Add content 24 PdfImportedPage page1 = writer. getImportedPage (reader, I + 1); 25 cb. addTemplate (page1, 0, 0); 26 cb. moveTo (0, height); 27 cb. stroke (); 28 cb. moveTo (width, height); 29 cb. stroke (); 30 // write the page 31 BaseFont bf = BaseFont. createFont (BaseFont. HELVETICA, BaseFont. CP1252, BaseFont. NOT_EMBEDDED); 32 cb. beginText (); 33 cb. setFontAndSize (bf, 14); 34 cb. showTextAligned (Response contentbyte. ALIGN_CENTER, string. format ("{0} of {1}", (I + 1 ). toString (), n. toString (), width/2, 18, 0); 35 cb. endText (); 36} 37 // close document 38 document. close (); 39 reader. close ();

The author has created a new pdf file with the same content, but only the additional page number (for example, 1 of 351_numbers can only be removed from the original million, and bytes can be modified to PDF. What's wrong with the younger brother? Let's look at this article.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.