2012-10-28 14 views
5

Mam iTextSharp metoda szablon stopki tak:Jak używać znaczników HTML w iTextSharp ciąg

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() jest coraz ciąg tak:

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

i mam inne metoda tworzy łańcuchy znaków do HTML. Metoda jest:

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

Problem: Nie udało się użyć CreateSimpleHtmlParagraph metodę w powyższych kodów. footer.ShowTextAligned(al, footerOneLine[0], widthoftext, v, 0); Typy danych metod to footer.ShowTextAligned(int, string, float, float, float); Czy możesz mi pomóc, jak mogę zastosować metodę CreateSimpleHtmlParagraph w powyższych kodach? Pozdrawiam.

+0

Czy rzutowanie 'v' i' 0' na 'float' działa? (Tj. 'Footer.ShowTextAligned (al, footerOneLine [0], widthoftext, v jako float, 0 jako float);') – millimoose

+0

Nie widzę zawartości metody ShowTextAligned. Ponieważ jest w itextsharp.ddl. Jaka jest różnica? Nie ma problemu, gdzie mówisz. Metoda ShowTextAligned wymaga napisu w drugim elemencie. Ale metoda CreateSimpleHtmlParagraph zwraca akapit. Nie zarządzałem tą konwersją. –

+0

Przepraszam, chyba nie zrozumiałem wtedy twojego pytania poprawnie. – millimoose

Odpowiedz

0

ja nie wiem dokładnie jak używasz PdfTemplate ale jesteś mieszanie abstrakcje tekstowych iTextSharp takich jak Paragraph i Chunk z surowych poleceń PDF jak ShowText(). Abstrakcje ostatecznie wykorzystują surowe polecenia, ale wygodnie pomagają ci w myśleniu, takim jak podział linii i bieżące współrzędne, które normalnie musisz obliczyć ręcznie.

Dobrą wiadomością jest to, że istnieje jedna abstrakcja o nazwie ColumnText, która działa bezpośrednio z obiektem PdfWriter.PdfContentByte, o ile zgadzasz się na to, że masz stały prostokąt do narysowania tekstu.

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

Dziękuję bardzo za odpowiedź. Ale nauczyłem się C# przez sześć lub siedem miesięcy. Więc nie rozumiem twojego poniższego kodu. Przepraszam za moją złą studentkę. Jak mogę zintegrować poniższy kod z moją metodą PdfTemplate, gdzie znajdują się pytania. Z poważaniem. –

Powiązane problemy