2012-10-28 24 views
5

Tôi có một iTextSharp chân mẫu phương pháp như thế này:Làm thế nào để tôi sử dụng các thẻ HTML trong iTextSharp chuỗi

public PdfTemplate footerTemplate(){ 
    PdfTemplate footer = cb.CreateTemplate(500, 50); 
    footer.BeginText(); 
    BaseFont bf2 = BaseFont.CreateFont(BaseFont.TIMES_ITALIC, "windows-1254", BaseFont.NOT_EMBEDDED); 
    footer.SetFontAndSize(bf2, 11); 
    footer.SetColorStroke(BaseColor.DARK_GRAY); 
    footer.SetColorFill(BaseColor.GRAY); 
    int al = -200; 
    int v = 45 - 15; 
     float widthoftext = 500.0f - bf2.GetWidthPoint(footerOneLine[0], 11); 
     footer.ShowTextAligned(al, footerOneLine[0], widthoftext, v, 0); 
    footer.EndText(); 
    return footer; 
} 

footerTemplate() là nhận được chuỗi như thế này:

footerOneLine.Add("<b>All this line is bold, and <u>this is bold and underlined</u></b>"); 

Và tôi có một khác phương thức tạo chuỗi thành HTML. Phương thức là:

private Paragraph CreateSimpleHtmlParagraph(String text) { 
    //Our return object 
    Paragraph p = new Paragraph(); 

    //ParseToList requires a StreamReader instead of just text 
    using (StringReader sr = new StringReader(text)) { 
     //Parse and get a collection of elements 
     List<IElement> elements = iTextSharp.text.html.simpleparser.HTMLWorker.ParseToList(sr, null); 
     foreach (IElement e in elements) { 
      //Add those elements to the paragraph 
      p.Add(e); 
     } 
    } 
    //Return the paragraph 
    return p; 
} 

Vấn đề: Tôi không quản lý sử dụng phương thức CreateSimpleHtmlParagraph trong các mã trên. Các loại dữ liệu của phương thức footer.ShowTextAligned(al, footerOneLine[0], widthoftext, v, 0);footer.ShowTextAligned(int, string, float, float, float); Bạn có thể giúp tôi làm cách nào tôi có thể sử dụng phương pháp CreateSimpleHtmlParagraph trong các mã ở trên không? Trân trọng.

+0

Việc truyền 'v' và' 0' đến 'float' có hoạt động không? (Tức là 'footer.ShowTextAligned (al, footerOneLine [0], widthoftext, v như float, 0 như float);') – millimoose

+0

Tôi không thể thấy nội dung của phương thức ShowTextAligned. Bởi vì nó nằm trong itextsharp.ddl. Sự khác biệt là gì? Không có vấn đề gì bạn nói. Phương thức ShowTextAligned yêu cầu chuỗi trong phần tử thứ hai. Nhưng phương thức CreateSimpleHtmlParagraph trả về đoạn văn. Tôi không quản lý chuyển đổi này. –

+0

Xin lỗi, tôi đoán tôi đã không hiểu chính xác câu hỏi của bạn. – millimoose

Trả lời

0

Tôi không biết chính xác cách bạn đang sử dụng PdfTemplate nhưng bạn đang trộn các văn bản trừu tượng của iTextSharp chẳng hạn như ParagraphChunk với các lệnh PDF thô như ShowText(). Tóm tắt cuối cùng sử dụng các lệnh thô nhưng thuận tiện giúp bạn với suy nghĩ như ngắt dòng và tọa độ hiện tại mà bạn thường phải tính toán theo cách thủ công.

Tin tốt là có một trừu tượng được gọi là ColumnText hoạt động trực tiếp với đối tượng PdfWriter.PdfContentByte miễn là bạn sẵn sàng chấp nhận rằng bạn có hình chữ nhật cố định để vẽ văn bản.

//Create a ColumnText from the current writer 
var ct = new ColumnText(writer.DirectContent); 
//Set the dimensions of the ColumnText 
ct.SetSimpleColumn(0, 0, 500, 0 + 20); 
//Create two fonts 
var helv_n = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); 
var helv_b = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, BaseFont.WINANSI, BaseFont.NOT_EMBEDDED); 
//Create a paragraph 
var p = new Paragraph(); 
//Add two chunks to the paragraph with different fonts 
p.Add(new Chunk("Hello ", new iTextSharp.text.Font(helv_n, 12))); 
p.Add(new Chunk("World", new iTextSharp.text.Font(helv_b, 12))); 
//Add the paragraph to the ColumnText 
ct.AddElement(p); 
//Tell the ColumnText to draw itself 
ct.Go(); 
+0

Cảm ơn bạn rất nhiều vì đã trả lời. Nhưng tôi đã học được C# trong sáu hoặc bảy tháng. Vì vậy, tôi không hiểu mã dưới đây của bạn. Xin lỗi vì học sinh kém của tôi. Làm cách nào để tích hợp mã được cung cấp bên dưới vào phương thức PdfTemplate của tôi nơi các địa điểm trong câu hỏi. Trân trọng. –

Các vấn đề liên quan