2012-10-28 28 views
5

Böyle bir İTextSharp altbilgi şablon yöntemi vardır:Nasıl İTextSharp dizede HTML etiketlerini kullanırız

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

Ve bir başka var:

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() böyle dize oluyor yöntem dizeleri HTML’ye dönüştürür. yöntemdir:

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

sorun: Yukarıdaki kodları CreateSimpleHtmlParagraph yöntemi kullanmak başaramadı. footer.ShowTextAligned(al, footerOneLine[0], widthoftext, v, 0); yönteminin veri türleri footer.ShowTextAligned(int, string, float, float, float); Yukarıdaki kodlarda CreateSimpleHtmlParagraph yöntemini nasıl kullanabiliyor musunuz? Saygılarımızla.

+0

"v" ve "0", "float" işlevini çalıştırıyor mu? (I.e footer.ShowTextAligned (al, footerOneLine [0], widthoftext, float olarak, float 0);) – millimoose

+0

ShowTextAligned yönteminin içeriğini göremiyorum. Çünkü itextsharp.ddl içinde. Fark ne? Söylediğiniz yerde sorun yok. ShowTextAligned yöntemi, ikinci öğede dizge gerektirir. Ancak CreateSimpleHtmlParagraph yöntemi paragrafı döndürür. Bu dönüşümü yönetmedim. –

+0

Maalesef, sorunuzu doğru bir şekilde anlamadım. – millimoose

cevap

0

Eğer PdfTemplate kullanıyor ancak böyle ShowText() gibi ham PDF komutlarıyla Paragraph ve Chunk bir şekilde itextsharp metin soyutlamalar karıştırma nasıl olduğunu tam olarak bilmiyorum. Soyutlamalar en sonunda ham komutları kullanırlar, ancak normalde manuel olarak hesaplamanız gereken satır kesmeleri ve geçerli koordinatlar gibi düşünmenize yardımcı olurlar.

İyi haber şu ki, ColumnText adlı bir soyutlama olup, PdfWriter.PdfContentByte nesnesini kullanarak, metin çizmek için sabit bir dikdörtgenin olduğunu kabul ettiğiniz sürece doğrudan çalışır.

//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

Cevabınız için çok teşekkür ederim. Ama C'yi altı ya da yedi ay boyunca öğrendim. Bu yüzden aşağıdaki kodunuzu anlamıyorum. Kötü öğrenmem için üzgünüm. Verilen kodun, PdfTemplate yöntemine, sorudaki yerlere nasıl entegre edilir. Saygılarımla. –

İlgili konular