C # generate a PDF file and read the TXT file content

Source: Internet
Author: User

Using system. IO;
Using itextsharp. text;
Using itextsharp.text.pdf;
// Icsharpcode. sharpziplib. dll and itextsharp. dll must be referenced in the project.
Public String txtfilepath;
Public String savepdfpath; // path for saving the PDF file

# Region read TXT content
Private string readxieyi (string filepath)
{
String xieyi = "";
Fileinfo Fi = new fileinfo (filepath );
Streamreader sr = Fi. opentext ();
Xieyi = Sr. readtoend ();
Sr. Close ();
Return xieyi;
}
# Endregion
# Region PDF document creation method
Private void zscsc ()
{
Document document = new document (pagesize. A4 );

Try
{
// <Deleetask>
// <Add key = "txtfilepath" value = "config/pubcode/"/>
// <! -- The Node (value: config/pubcode/In webui) file added in webcofig is saved under the specified file. -->
// <Deleetask>
String name = online. webui. accountcontroller. loginname + "_" + _ pic_id + ". pdf ";
Savepdfpath = appdomain. currentdomain. basedirectory. tostring () + configurationsettings. receivettings ["savepdfpath"] + name;
// Object filename = "C: //" + name; // filename, file storage path
Using writer = Using writer. getinstance (document, new filestream (savepdfpath. tostring (), filemode. Create ));

Document. addtitle ("image authorization Protocol ");
Document. addauthor ("quanjing. com ");
Document. addheader ("expires", "0 ");
Document. open ();

Basefont bfsun = basefont. createfont (@ "C: \ WINDOWS \ fonts \ simkai. TTF", basefont. identity_h, false); // The called font
Font font = new font (bfsun, 15 );

String text = "image authorization protocol \ n ";
Document. Add (new paragraph (text, font ));

// <Deleetask>
// <Add key = "txtfilepath" value = "config/pubcode/xieyi.txt"/>
// <! -- The advantage of adding a node in webcofig (the value is config/pubcode/xieyi.txt in webui) is that if the TXT name is changed, you can directly change webconfig. -->
// <Deleetask>
Txtfilepath = appdomain. currentdomain. basedirectory. tostring () + configurationsettings. deleettings ["txtfilepath"];

Font = new font (bfsun, 10 );
TEXT = readxieyi (txtfilepath); // (@ "C: \ xieyi.txt"); read the path of the TXT file "image authorized TXT file protocol content ";
Document. Add (new paragraph (text, font ));

// Insert an image
String strcatalogid;
Datatable dt = online. Business. Production. bizdownpro. getcatalogid (_ pic_id );
Strcatalogid = DT. Rows [0] [0]. tostring ();
String filenameurl = "http://images.com/" + strcatalogid. tolower () + "/Thu/" + _ pic_id.tolower () + ". jpg "; //" http://images.com/west004/thu/babf00254.jpg "; // path of the image
Itextsharp. Text. Image JPEG = itextsharp. Text. image. getinstance (New uri (filenameurl ));
Document. Add (new paragraph ("\ n authorized image:", font ));
Document. Add (JPEG );

// Insert the image number
Document. Add (new paragraph ("\ n image No.:" + _ pic_id, font ));
String activatecode;
Activatecode = _ pic_id + online. webui. accountcontroller. loginname; // use the image number and the login name to generate MD5 encoding. Only the first 25 digits are used.
String md5code;
Md5code = system. web. security. formsauthentication. hashpasswordforstoringinconfigfile (activatecode, "MD5 "). tolower (). substring (0, 20); // encrypt MD5, take only the first 20 digits // "QJ-QQQQW-EEEER-TTTTY-UUUUN ";
String code;
Code = md5code. substring (0, 5); // separate each five digits "-"
Code = code + "-" + md5code. substring (5, 5 );
Code = code + "-" + md5code. substring (10, 5 );
Code = code + "-" + md5code. substring (15, 5 );
Code = "Qj-" + code. tostring (). toupper ();
Document. Add (new paragraph ("\ n authorization code:" + code, font); // code is the authorization code
}

Catch (incluentexception de)
{
Response. Write (De. Message );
}
Catch (ioexception IOE)
{
Response. Write (IOE. Message );
}
Document. Close ();
}
# Endregion
// Button event
Protected void button2_click (Object sender, eventargs E)
{
String name = online. webui. accountcontroller. loginname + "_" + _ pic_id + ". pdf ";
Savepdfpath = appdomain. currentdomain. basedirectory. tostring () + configurationsettings. receivettings ["savepdfpath"] + name;
If (! File. exists (savepdfpath. tostring ()))
{
Zscsc (); // create a PDF document
Readdowndata (); // download the TXT file
}
Else
{
Readdowndata (); // download the PDF file
}
}

// Read the TXT file and download the PDF file
Public void readdowndata ()
{
String name = online. webui. accountcontroller. loginname + "_" + _ pic_id + ". pdf ";
Savepdfpath = appdomain. currentdomain. basedirectory. tostring () + configurationsettings. receivettings ["savepdfpath"] + name;
// Read the file and write it to the client for response
Filestream FS = new filestream (savepdfpath. tostring (), filemode. Open, fileaccess. Read );

Byte [] B = new byte [fs. Length];
FS. Read (B, 0, B. Length );
FS. Flush ();
FS. Close ();
// File. Delete (filename. tostring (); no deletion is performed after the download.
Response. Clear ();
Response. clearheaders ();
Response. Buffer = false;
Response. contenttype = "application/octet-stream"; // contenttype;
Response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (name, system. Text. encoding. utf8 ));
Response. appendheader ("Content-Length", B. length. tostring ());
Response. outputstream. Write (B, 0, B. Length );
Response. Flush ();
Response. End ();
}

Reprinted http://www.idai.com.cn/blog/article.asp? Id = 489

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.