c# iText 產生PDF 有文字,圖片,表格,文字樣式,對齊,頁首頁尾,等等等,

來源:互聯網
上載者:User

標籤:

#region 下載說明書PDF
protected void lbtnDownPDF_Click(object sender, EventArgs e)
{
int pid = ConvertHelper.GetInteger(Request["PID"]);
pds = p.GetModel(pid);
#region 第一種方法
DataTable datatable = new DataTable(dt.ToString());
try
{
string Url1 = "pdfBook\\" + GetNewName() + ".pdf";
//建立一個文檔對象,並設定他的初始化大小
Rectangle pSize = new Rectangle(144, 90);
//文檔的背景色
pSize.BackgroundColor = Color.WHITE;
//常用頁面大小 PageSize.A4;
Document document = new Document(PageSize.A4);
PdfWriter.GetInstance(document, new FileStream(Server.MapPath(Url1), FileMode.Create));
BaseFont bfChinese = BaseFont.CreateFont("C:\\WINDOWS\\Fonts\\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, new Color(0, 0, 0));
Font headerChinese = new Font(bfChinese, 15, Font.BOLD, new Color(0, 0, 0));
Font beizhufontChinese = new Font(bfChinese, 10, Font.ITALIC, new Color(255, 0, 0));
#region 頁首和頁尾
/**
* HeaderFooter的第2個參數為非false時代表列印頁碼
* 頁首頁尾中也可以加入圖片,並非只能是文字
*/
HeaderFooter header = new HeaderFooter(new Phrase("陝西龍鳳祥珠寶著作權", beizhufontChinese), false);

//設定是否有邊框等
// header.setBorder(Rectangle.NO_BORDER);
header.Border = Rectangle.BOTTOM_BORDER;
header.Alignment = 1;
header.BorderColor = Color.RED;
document.Header = header;

HeaderFooter footer = new HeaderFooter(new Phrase("-", beizhufontChinese), new Phrase("-", beizhufontChinese));
/**
* 0是靠左
* 1是置中
* 2是居右
*/
footer.Alignment = 1;
footer.BorderColor = Color.RED;
footer.Border = Rectangle.BOTTOM_BORDER;
document.Footer = footer;

/**
* 頁首頁尾的設定一定要在open前設定好
*/
#endregion
document.Open();

//按設定的字型輸出文本
string contents = replaceHtmlCode(pds.ProductName);
string details = replaceHtmlCode(pds.ProductDetail);
Paragraph heading = new Paragraph(contents, headerChinese);
heading.SpacingAfter = 18f;
heading.Alignment = 1;
//heading.IndentationLeft = 220f;
document.Add(heading);

MultiColumnText columns = new MultiColumnText();

columns.AddSimpleColumn(36f, 336f);

columns.AddSimpleColumn(360f, document.PageSize.Width - 36f);

Paragraph detail = new Paragraph(details, fontChinese);
detail.SpacingAfter = 9f;
detail.Alignment = Element.ALIGN_JUSTIFIED;
detail.IndentationLeft = 12f;//全文都縮排了
//document.Add(detail);
//輸出圖片到PDF檔案
string Url = "../uploadpic\\" + pds.Picture;//<img src="../uploadpic/20150213/wm201521317692.jpg" />
iTextSharp.text.Image jpeg01 = iTextSharp.text.Image.GetInstance(Server.MapPath(Url));
jpeg01.IndentationLeft = 150f;
//document.Add(jpeg01);
columns.AddElement(detail);
columns.AddElement(jpeg01);
columns.AddElement(detail);
columns.AddElement(detail);
columns.AddElement(detail);
//columns.AddElement(end);
document.Add(columns);
//Paragraph end = new Paragraph("陝西龍鳳祥珠寶著作權", beizhufontChinese);
//end.IndentationLeft = 200f;
//document.Add(end);
document.Close();

#region 自動下載
// 儲存檔案的實體路徑
string FullPath = HttpContext.Current.Server.MapPath(Url1);
// 初始化FileInfo類的執行個體,作為檔案路徑的封裝
FileInfo FI = new FileInfo(FullPath);
// 判斷檔案是否存在
if (FI.Exists)
{
// 將檔案儲存到本機
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(FI.Name));
Response.AddHeader("Content-Length", FI.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(FI.FullName);
Response.End();
}
#endregion
}
catch (DocumentException de)
{
Response.Write(de.ToString());
}
#endregion


}
#endregion

c# iText 產生PDF 有文字,圖片,表格,文字樣式,對齊,頁首頁尾,等等等,

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.