Einzelnen Beitrag anzeigen

Benutzerbild von stahli
stahli

Registriert seit: 26. Nov 2003
Ort: Halle/Saale
4.336 Beiträge
 
Delphi 11 Alexandria
 
#2

Re: iTextSharp - Fontwechsel innerhalb Zelle

  Alt 8. Mai 2009, 11:30
Ich habe eine Lösung gefunden

Das funktioniert über Phrasen, denen mehrere Chunks zugewiesen werden...
Das Ergebnis seht Ihr in der Anlage.

iTextSharp ist etwas gewöhnungsbedürftig aber durchaus sehr leistungsfähig
Eine deutsche Hilfe ist allerdigs kaum zu finden.

Falls es jemand mal braucht, anbei mein Quelltext dazu.


Stahli


Delphi-Quellcode:
method _Default.ButtonPdf_Click(sender: System.Object; e: System.EventArgs);
const
  Days: array [1..7] of String = ['Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So'];
var
  PdfDoc: Document;
  PdfDocFileName: String;
  PdfTable{, PdfTableNested}: PdfPTable;
  NumColumns, HeaderWidth: Integer;
  X, Y, WeekNo: Integer;
  Font3: Font;
  Font6: Font;
  Font7: Font;
  Font8: Font;
  Font9: Font;
  Font10: Font;
  TC, TC1: Chunk;
  TP: Phrase;
  FirstDate: DateTime;
  ArrayDays: array [1..7, 0..53] of DateTime;
  cell: PdfPCell;
  IsOddMonth: Boolean;

  procedure PaintTable(XO: Integer);
  var
    S1, S2: String;
    gf: Boolean;
  begin
     NumColumns := 28;
     HeaderWidth := 4;
    PdfTable := new PdfPTable(NumColumns);
            
     PdfTable.WidthPercentage := 100; // percentage
            
     PdfTable.DefaultCell.HorizontalAlignment := Element.ALIGN_CENTER;

    gf := False;
    for Y := 0 to 7 do
    begin
      for X := 0 to 27 do
      begin
        if X = 0 then
        begin
          if Y = 0 then
                TC := new Chunk("Woche", Font7)
          else
                TC := new Chunk(Days[Y], Font9);
             cell := new PdfPCell(new Phrase(TC));
             cell.BackgroundColor := new Color(227, 227, 255);
          cell.VerticalAlignment := Element.ALIGN_MIDDLE;
               cell.HorizontalAlignment := Element.ALIGN_CENTER;
              PdfTable.addCell(cell);
        end
        else
        if Y = 0 then
        begin
          //Size := 8;
          if WeekNo = 0 then
                TC := new Chunk("<<<", Font8)
          else
          if WeekNo = 53 then
                TC := new Chunk(">>>", Font8)
          else
             TC := new Chunk(WeekNo.ToString + ".", Font8);
             cell := new PdfPCell(new Phrase(TC));
             cell.BackgroundColor := new Color(227, 227, 255);
          cell.VerticalAlignment := Element.ALIGN_BOTTOM;
               cell.HorizontalAlignment := Element.ALIGN_CENTER;
              PdfTable.addCell(cell);
          Inc(WeekNo);
        end
        else
        begin
          TP := new Phrase;
             cell := new PdfPCell(TP);
          S1 := ArrayDays[Y, XO + X - 1].Day.ToString;
          if Length(S1) = 1 then
            S1 := '0' + S1;
          S2 := ArrayDays[Y, XO + X - 1].Month.ToString;
          if Length(S2) = 1 then
            S2 := '0' + S2;
...
          begin
...
            begin
                  TC := new Chunk("X", Font3);
              TP.Add(TC.NewLine);
              if ((S1 <> '10') or (S2 <> '02')) then
              begin
                TC := new Chunk("gr", Font10);
                TC.setBackground(new Color(160, 160, 160));
                TP.Add(TC);
              end;
...
                   begin
                    TC := new Chunk(" ", Font7);
                TP.Add(TC);
                TC := new Chunk("ge", Font10);
                TC.setBackground(new Color(255, 255, 0));
                TP.Add(TC);
              end;
                  TC := new Chunk("X", Font3);
              TP.Add(TC.NewLine);
              gf := not gf;
            end
            else
            begin
                  TC := new Chunk(" ", Font7);
              TP.Add(TC.NewLine);
              TC := new Chunk(" ", Font10);
              TP.Add(TC.NewLine);
            end;
               if not IsOddMonth then
              cell.BackgroundColor := new Color(230, 215, 215)
            else
              cell.BackgroundColor := new Color(255, 255, 255);
          end;
          cell.VerticalAlignment := Element.ALIGN_TOP;
               cell.HorizontalAlignment := Element.ALIGN_CENTER;
              PdfTable.addCell(cell);
        end;
      end;
    end;

     PdfDoc.Add(PdfTable);

  end;

begin
  PdfDocFileName := 'E:\PdfDoc.pdf';
  PdfDoc := new Document(PageSize.A4.Rotate);

   Font3 := FontFactory.getFont(FontFactory.HELVETICA, 3, Font.NORMAL);
   Font6 := FontFactory.getFont(FontFactory.HELVETICA, 6, Font.NORMAL);
   Font7 := FontFactory.getFont(FontFactory.HELVETICA, 7, Font.NORMAL);
   Font8 := FontFactory.getFont(FontFactory.HELVETICA, 8, Font.NORMAL);
   Font9 := FontFactory.getFont(FontFactory.HELVETICA, 9, Font.NORMAL);
   Font10 := FontFactory.getFont(FontFactory.HELVETICA, 10, Font.NORMAL);

...

  PdfWriter.getInstance(PdfDoc, new FileStream(PdfDocFileName, FileMode.Create));
  PdfDoc.Open;
  PdfDoc.Add(new Paragraph("Pdf:"));
  PdfDoc.Add(new Paragraph(" "));
 
  WeekNo := 0;
  PaintTable(0);

  PdfDoc.Add(new Paragraph(" "));

  PaintTable(27);

  PdfDoc.Close();
end;
Miniaturansicht angehängter Grafiken
pdf_737.jpg  
  Mit Zitat antworten Zitat