2014-09-10 43 views
0
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頁面的邊界..我試着另一種方式。如何將邊框應用於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"); 

每一行。如果我申請這個代碼,然後我沒有得到任何邊界.. 我怎樣才能添加頁面邊框的pdf在asp.net

+0

的可能重複(http://stackoverflow.com/ [如何使用iText庫5.5.2來繪製整個PDF頁面邊框]問題/ 25749828 /如何對平局邊界換全PDF,網頁,使用-的iText庫-5-5-2) – 2014-09-14 06:38:29

回答

1

使用Itextsharp.dll文件頁面試試這個,如果它可以幫助你

<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; 
}