public void pdf()
{
iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
PdfPTable table = new PdfPTable(3);
table.TotalWidth = 400f;
Document document = new Document(PageSize.A2, 88f, 88f, 10f, 10f);
MemoryStream memoryStream = new MemoryStream();
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
document.Open();
PdfContentByte content = writer.DirectContent;
iTextSharp.text.Rectangle rectangle = new iTextSharp.text.Rectangle(document.PageSize);
rectangle.Left += document.LeftMargin;
rectangle.Right -= document.RightMargin;
rectangle.Top -= document.TopMargin;
rectangle.Bottom += document.BottomMargin;
content.SetColorStroke(iTextSharp.text.BaseColor.BLUE);
content.Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, rectangle.Height);
content.Stroke();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Invoice.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter stringWriter = new StringWriter();
HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
divM.RenderControl(htmlTextWriter);
StringReader sr = new StringReader(stringWriter.ToString());
// Document doc = new Document(PageSize.A2, 7f, 7f, 7f, 0f);
HTMLWorker htmlparser = new HTMLWorker(document);
htmlparser.SetStyleSheet(styles);
PdfWriter.GetInstance(document, Response.OutputStream);
document.Open();
htmlparser.Parse(sr);
document.Close();
Response.Write(document);
Response.End();
}
在这里,我没有在pdf页面中获得任何边界......我尝试了另一种方式。
styles.LoadStyle("pdf", "border", "1");
如果我应用了这一行,我的每一行都有边框
styles.LoadStyle("pdf", "border-top", "1");
styles.LoadStyle("pdf", "border-bottom", "1");
styles.LoadStyle("pdf", "border-left", "1");
styles.LoadStyle("pdf", "border-right", "1");
styles.LoadStyle("pdf", "size", "4");
如果我应用此代码,那么我没有任何边框.. 如何使用asp.net中的Itextsharp.dll文件在pdf页面中添加页面边框
答案 0 :(得分:1)
试试这个,如果它可以帮助你
<form id="form2" runat="server">
<asp:Button ID="btnReportForPDF" runat="server" Text="" OnClick="GenerateReportForPDF" />
</form>
命名空间: -
using iTextSharp.text;
using iTextSharp.text.pdf;
代码供您参考:
protected void GenerateReportForPDF(object sender, EventArgs e)
{
Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK);
using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
{
PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
Phrase phrase = null;
PdfPCell cell = null;
PdfPTable table = null;
document.Open();
//Header Table
table = new PdfPTable(1);
table.TotalWidth = 400f;
table.LockedWidth = true;
table.SetWidths(new float[] { 1f });
//Company Name and Address
phrase = new Phrase();
phrase.Add(new Chunk("Microsoft Northwind Traders Company\n\n", FontFactory.GetFont("Arial", 16, Font.BOLD, Color.RED)));
phrase.Add(new Chunk("107, Park site,\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
phrase.Add(new Chunk("Salt Lake Road,\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
phrase.Add(new Chunk("Seattle, USA", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
table.AddCell(cell);
document.Add(table);
table = new PdfPTable(2);
table.HorizontalAlignment = Element.ALIGN_LEFT;
table.SetWidths(new float[] { 0.3f, 1f });
table.SpacingBefore = 20f;
//Employee Details
cell = PhraseCell(new Phrase("Employee Record", FontFactory.GetFont("Arial", 12, Font.UNDERLINE, Color.BLACK)), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
table.AddCell(cell);
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 30f;
table.AddCell(cell);//cell will get added in a table.
table = new PdfPTable(2);
table.SetWidths(new float[] { 0.5f, 2f });
table.TotalWidth = 340f;
table.LockedWidth = true;
table.SpacingBefore = 20f;
table.HorizontalAlignment = Element.ALIGN_RIGHT;
phrase = new Phrase();
phrase.Add(new Chunk("Title\n", FontFactory.GetFont("Arial", 10, Font.BOLD, Color.BLACK)));
phrase.Add(new Chunk("(Moderator)", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)));
cell = PhraseCell(phrase, PdfPCell.ALIGN_LEFT);
cell.VerticalAlignment = PdfPCell.ALIGN_MIDDLE;
cell.Colspan = 2;
table.AddCell(cell);
cell = PhraseCell(new Phrase(" "), PdfPCell.ALIGN_LEFT);
cell.Colspan = 2;
table.AddCell(cell);
//Employee Id
table.AddCell(PhraseCell(new Phrase("Employee code:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase("0001", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Address
table.AddCell(PhraseCell(new Phrase("Address:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
phrase = new Phrase(new Chunk("507 - 20th Ave. E.\nApt. 2A\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
phrase.Add(new Chunk("Seattle\n", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
phrase.Add(new Chunk("WA USA 98122", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)));
table.AddCell(PhraseCell(phrase, PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
//Date of Birth
table.AddCell(PhraseCell(new Phrase("Date of Birth:", FontFactory.GetFont("Arial", 8, Font.BOLD, Color.BLACK)), PdfPCell.ALIGN_LEFT));
table.AddCell(PhraseCell(new Phrase("17 MARCH", FontFactory.GetFont("Arial", 8, Font.NORMAL, Color.BLACK)), PdfPCell.ALIGN_LEFT));
cell = PhraseCell(new Phrase(), PdfPCell.ALIGN_CENTER);
cell.Colspan = 2;
cell.PaddingBottom = 10f;
table.AddCell(cell);
document.Add(table);
//Add border to page
PdfContentByte content = writer.DirectContent;
Rectangle rectangle = new Rectangle(document.PageSize);
rectangle.Left += document.LeftMargin;
rectangle.Right -= document.RightMargin;
rectangle.Top -= document.TopMargin;
rectangle.Bottom += document.BottomMargin;
content.SetColorStroke(Color.BLACK);
content.Rectangle(rectangle.Left, rectangle.Bottom, rectangle.Width, rectangle.Height);
content.Stroke();
document.Close();
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename=Employee.pdf");
Response.ContentType = "application/pdf";
Response.Buffer = true;
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(bytes);
Response.End();
Response.Close();
}
}
private static PdfPCell PhraseCell(Phrase phrase, int align)
{
PdfPCell cell = new PdfPCell(phrase);
cell.BorderColor = Color.WHITE;
cell.VerticalAlignment = PdfPCell.ALIGN_TOP;
cell.HorizontalAlignment = align;
cell.PaddingBottom = 2f;
cell.PaddingTop = 0f;
return cell;
}