Asp.net uses itextsharp to generate PDF documents

Source: Internet
Author: User

1 Recently, when I was working on a system, I needed to generate a PDF document. I searched for the open-source itextsharp file to generate a PDF document. I learned how to use Asp.net to generate PDF documents.CodeAs follows:
2 Private   Void Creatpdf ()
3 {
4 Document Doc =   New Document ();
5 Memorystream MS =   New Memorystream ();
6 Try
7 {
8 Pdfwriter writer = Using writer. getinstance (Doc, MS );
9 Writer. closestream =   False ;
10 Doc. open ();
11 Doc. Add ( New Paragraph ( " Hello word! " ));
12 }
13 Catch (Exception ERR)
14 {
15Throw NewException (ERR. Message );
16}
17 Finally
18 {
19Doc. Close ();
20Viewpdf (MS );
21}
22 }
23
24 Private   Void Viewpdf (Stream FS)
25 {
26 Response. Clear ();
27 // Chinese name
28 // Response. appendheader ("content-disposition", "attachment; filename =" +
29 // Httputility. urlencode (filename, system. Text. encoding. utf8) + "; charset = gb2312 ");
30 Response. addheader ( " Content-Disposition " , " Attachment?filename=out= " );
31 Response. addheader ( " Content-Length " , FS. length. tostring ());
32 Response. contenttype =   " Application/PDF " ;
33 Long Filelength = FS. length;
34 Int Size =   10240 ; // 10 K one-multipart download, 10 K is 1
35 Byte [] Readdata =   New   Byte [Size];
36 If (Size > Filelength)
37 Size = Convert. toint32 (filelength );
38 Long FPOs =   0 ;
39 Bool Isend =   False ;
40 While ( ! Isend)
41 {
42 If (FPOs + Size) > = Filelength)
43 {< br> 44 size = convert. toint32 (filelength - FPOs );
45 isend = true ;
46 }
47 Readdata =   New   Byte [Size];
48 FS. Position = FPOs;
49 FS. Read (readdata, 0 , Size );
50 Response. binarywrite (readdata );
51 Response. outputstream. Flush ();
52 FPOs + = Size;
53 }
54 FS. Close ();
55 Response. outputstream. Close ();
56 Response. End (); // Very important. Without this sentence, the HTML code of the page will be saved to the file.
57 Response. Close ();
58 }
59
60 The above Code simply saves a PDF document. The specific settings of the PDF document format need to be further studied. Handle exceptions in actual use.
61

 

 

 

 

 

This blog details the use of itextsharp: http://www.cnblogs.com/hardrock
Use itextsharp to enter the Chinese (China, Japan, and South Korea) PDF form (complete solution)

 

 

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.