2016-04-06 5 views
0

Entschuldigung für mein Englisch, Ich habe Code, der ein Dokument erstellt. Im ersten Drittel befindet sich das Rechteck mit dem Text, der den Header darstellt. Mein Code:Itextpdf Dokument mit Text, Rechtecke und Tabellen

private void createGPDFbutton_Click(object sender, EventArgs e) 
    { 
     string PDFcesta = AppDomain.CurrentDomain.BaseDirectory; 
     Document gdoc = new Document(iTextSharp.text.PageSize.A4, 42, 10, 42, 35); 
     //***** definice fontů ***** 
     //Nastavení fontu, aby to tisklo řádně česky 
     string ARIALUNI_TFF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "ARIALUNI.TTF"); 
     string CONSOLA_TTF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "consola.ttf"); 
     string COUR_TTF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "cour.ttf"); 
     string COURBD_TTF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "courbd.ttf"); // courier tučné 
     string TIMES_TTF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "times.ttf"); 
     string TIMESBD_TTF = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Fonts), "timesbd.ttf"); // courier tučné 
     //Create a base font object making sure to specify IDENTITY-H 
     BaseFont BF_ARIAL = BaseFont.CreateFont(ARIALUNI_TFF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
     BaseFont BF_CONSOLAS = BaseFont.CreateFont(CONSOLA_TTF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
     BaseFont BF_COURIER = BaseFont.CreateFont(COUR_TTF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
     BaseFont BF_COURIER_BOLD = BaseFont.CreateFont(COURBD_TTF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
     BaseFont BF_TIMES = BaseFont.CreateFont(TIMES_TTF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
     BaseFont BF_TIMES_BOLD = BaseFont.CreateFont(TIMESBD_TTF, BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); 
     //Create a specific font object 
     iTextSharp.text.Font f = new iTextSharp.text.Font(BF_ARIAL, 12, iTextSharp.text.Font.NORMAL); 
     iTextSharp.text.Font f1 = new iTextSharp.text.Font(BF_CONSOLAS, 10, iTextSharp.text.Font.NORMAL); 
     //***** konec fontů ***** 
     try 
     { 
      PdfWriter gwriter = PdfWriter.GetInstance(gdoc, new FileStream(PDFcesta + "graphicsPDF.pdf", FileMode.Create)); 
      gdoc.Open(); 
      PdfContentByte cb = gwriter.DirectContent; 
      cb.SaveState(); // uložení aktuálního nastavení grafiky 
      cb.SetColorStroke(new BaseColor(0,0,0)); 
      cb.SetColorFill(new BaseColor(255,192,203)); 
      cb.SetLineWidth(1.5f); 
      // Bottom left coordinates x & y, followed by width, height and radius of corners. 
      cb.RoundRectangle(48f, 600f, 495.833f, 192.4f, 5f); 
      cb.FillStroke(); 
      cb.SetLineWidth(0.5f); 
      cb.Rectangle((gdoc.PageSize.Width/2) - 35f, 630f,70f,10f); 
      cb.Stroke(); 
      cb.Rectangle((gdoc.PageSize.Width/2) - 35f, 640f, 70f, 10f); 
      cb.Stroke(); 
      //zápis textu 
      cb.BeginText(); 
      cb.SetColorFill(BaseColor.BLACK); 
      cb.SetFontAndSize(BF_TIMES_BOLD, 15f); 
      cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "VÝKAZ", gdoc.PageSize.Width/2, 753f, 0); 
      cb.SetFontAndSize(BF_TIMES_BOLD, 15f); 
      cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "ZISKU A ZTRÁTY", gdoc.PageSize.Width/2, 733f, 0); 
      cb.SetFontAndSize(BF_TIMES_BOLD, 5f); 
      cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "v plném rozsahu", gdoc.PageSize.Width/2, 713f, 0); 
      cb.SetFontAndSize(BF_TIMES_BOLD, 12f); 
      cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "31.12.2015", gdoc.PageSize.Width/2, 683f, 0); 
      cb.SetFontAndSize(BF_TIMES_BOLD, 7f); 
      cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "v tisících Kč", gdoc.PageSize.Width/2, 663f, 0); 
      cb.SetFontAndSize(BF_TIMES, 5f); 
      cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "IČO", gdoc.PageSize.Width/2, 643f, 0); 
      cb.SetFontAndSize(BF_TIMES_BOLD, 5f); 
      cb.ShowTextAligned(PdfContentByte.ALIGN_CENTER, "00020711", gdoc.PageSize.Width/2, 633f, 0); 
      cb.EndText(); 
      //konec textu 
      cb.RestoreState(); // obnovení původního nastavení grafiky 
      // tabulka 


      // konec tabulky 
      gdoc.Close(); 
      System.Diagnostics.Process.Start(PDFcesta + "graphicsPDF.pdf"); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 


    } 

Ich brauche die Tabelle nach diesem Rechteck einzufügen. Zwischen den Kommentaren

// tabulka 

// konec tabulky 

Wie löst man das?

+0

Gibt es einen Grund, warum Sie den gesamten Text an absoluten Positionen hinzufügen anstatt 'document.add()' oder 'ColumnText' zu verwenden? Ihr Code sieht komplexer aus als benötigt. –

Antwort

0

Sie fügen viele Textzeilen an absoluten Positionen hinzu. Ihre letzte Textzeile wurde an der Position Y = 633 hinzugefügt. Sie möchten jetzt eine Tabelle an einer absoluten Position unterhalb der letzten Zeilenposition hinzufügen, sagen wir an der Position X = 36, Y = 620. Sie möchten, dass die Tabelle zentriert wird. Die Breite des Tisches beträgt also 523; das ist gdoc.PageSize.Width - 36 (linker Rand) - 36 (rechter Rand).

Wie dies zu tun ist, wird in der offiziellen Dokumentation erläutert. Siehe zum Beispiel die Antwort auf die Frage How to add a table to the bottom of the last page? (Bitte durchsuchen Sie die Dokumentation und führen Sie einige Suchen durch, zum Beispiel für die WriteSelectedRows() method).

Wenn Sie eine PdfPTable erstellt haben namens table, würden Sie so etwas wie dieses benötigen:

table.TotalWidth = 523; 
float y = table.writeSelectedRows(0, -1, 36, 620, writer.DirectContent); 

Der Wert y wird die Position des unteren Teil der Tabelle sein.

Verwandte Themen