2012-08-07 55 views
16

我試圖生成一個字母,留下一個空白點,然後在其頂部粘貼地址,具體取決於信封窗口的位置。添加絕對定位的文本

於是我開始這樣做:

Document doc = new Document(PageSize.LETTER, 72, 72, 72, 72); 
var w = PdfWriter.GetInstance(doc, output); 
Font font = FontFactory.GetFont("arial", 10); 
doc.Open(); 
doc.Add(new Paragraph("date", font) { SpacingAfter = 5 }); 
doc.Add(new Paragraph("\n\n\n\n\n\n", font));//empty spot 
doc.Add(new Paragraph("long\n paragraph\ns panning\ multiple\n lines\n", font) { SpacingAfter = 5 }); 
doc.Add(new Paragraph("long\n paragraph\ns panning\ multiple\n lines\n", font) { SpacingAfter = 5 }); 
doc.Add(new Paragraph("long\n paragraph\ns panning\ multiple\n lines\n", font) { SpacingAfter = 5 }); 
doc.Add(new Paragraph("long\n paragraph\ns panning\ multiple\n lines\n", font) { SpacingAfter = 5 }); 
doc.Add(new Paragraph("long\n paragraph\ns panning\ multiple\n lines\n", font) { SpacingAfter = 5 }); 

float llx = 63f, lly = 450f, urx = 387f, ury = 531f; 
?? Somehow add "name\n address line 1\n address line2\n city state zip" 

doc.Close(); 

我希望能夠在這些座標添加一些文字,但我無法弄清楚如何人......知道一個辦法做到這一點?

回答

21

找到答案「Here」。 (從Yannick Smits下面是引用答案)

===============

試試這個:SetSimpleColumn的

ColumnText ct = new ColumnText(cb); 
Phrase myText = new Phrase("TEST paragraph\nNewline"); 
ct.SetSimpleColumn(myText, 34, 750, 580, 317, 15, Element.ALIGN_LEFT); 
ct.Go(); 

參數爲:

  1. 短語
  2. 左下-X
  3. 左下-Y
  4. 右上-X(LLX +寬度)
  5. 右上-γ(LLY +高度)
  6. 領先(的空白空間的量的​​打印行之間)
  7. 對準。
+0

是什麼長方體高度和框寬度是什麼意思? (項目4,5) – 2013-04-03 19:41:55

+0

我認爲參數4和5是'urx'和'ury',而不是'box width'和'box height'。第6個參數是「前導」,我不確定它是什麼,但是它適用於文本,因此它不是「行高」。 – 2013-04-17 23:17:39

+0

謝謝馬克。我糾正了它。 – colinbashbash 2013-04-18 13:51:15

16

您還可以使用帶文本矩陣的ContentByte在任何地方繪製文本。

PdfContentByte cb = writer.DirectContent; 
cb.BeginText(); 
BaseFont f_cn = BaseFont.CreateFont("c:\\windows\\fonts\\calibri.ttf", BaseFont.CP1252, BaseFont.NOT_EMBEDDED); 
cb.SetFontAndSize(f_cn, 6); 
cb.SetTextMatrix(475, 15); //(xPos, yPos) 
cb.ShowText("Some text here and the Date: " + DateTime.Now.ToShortDateString()); 
cb.EndText(); 

好處是,如果您不必繪製文本的全部大小,文本將進入。使用簡單列,您正在文檔上繪製一個矩形,並將文本放置在其中。使用ContentByte,您避開矩形,並自行定位文本。

+0

很酷。但我實際上想限制文本的寬度。所以使用ColumnText實際上在我的實例中更有用。謝謝! – colinbashbash 2012-08-08 18:03:39

+0

我在函數調用中使用了setSimpleColumn,隨後調用相同的測量值導致了相當不愉快的定位。更改爲這種方法很快修復!謝謝。 – 2015-10-14 18:43:02

+0

但是使用ShowText方法不能爲RTL語言設置RunDirection。 – Arman 2017-04-24 11:59:06

0

非常感謝您的數據,我喜歡它,但我的英語非常差,抱歉。

這是我iTextSharp etiquetas代碼:

public void impriItex() 
{ 
    iTextSharp.text.Font fontH1 = new iTextSharp.text.Font(FUENTE_BASE, 14, iTextSharp.text.Font.ITALIC); 
    iTextSharp.text.Font fuente = new iTextSharp.text.Font(FUENTE_BASE, 12, iTextSharp.text.Font.NORMAL); 
    iTextSharp.text.Font FUENTE_CABECERA_TABLA = new iTextSharp.text.Font(FUENTE_BASE, 10, iTextSharp.text.Font.BOLD); 
    dataGridViewOculta.DataSource = fun.CargaDato(cargaVelores); 
    dataGridViewTotales.DataSource = fun.CargaDato(cargaVelores); 
    dataGridViewVentas.DataSource = fun.CargaDato(cargaVelores); 
    //Letras 
    //Tabla 
    PdfPTable pdfTable = new PdfPTable(6); 
    pdfTable.DefaultCell.Padding = 2; 
    pdfTable.WidthPercentage = 40; 
    pdfTable.HorizontalAlignment = Element.ALIGN_CENTER; 
    pdfTable.DefaultCell.BorderWidth = 1; 
    //cabeceras 
    foreach (DataGridViewColumn column in dataGridViewOculta.Columns) 
    { 

     PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, FUENTE_CABECERA_TABLA)); 
     cell.HorizontalAlignment = Element.ALIGN_CENTER; 
     cell.BackgroundColor = new iTextSharp.text.BaseColor(210, 240, 240); 
     pdfTable.AddCell(cell); 
    } 

    //datos 
    foreach (DataGridViewRow row in dataGridViewOculta.Rows) 
    { 
     try 
     { 
      foreach (DataGridViewCell cell in row.Cells) 
      { 
       String texto = cell.Value.ToString(); 
       String textovacio = texto.Replace("00:00:00", " "); 
       PdfPCell cosa = new PdfPCell(new Phrase(textovacio, fuente)); 
       cosa.HorizontalAlignment = Element.ALIGN_CENTER; 
       pdfTable.AddCell(cosa); 
       pdfTable.HorizontalAlignment = Element.ALIGN_CENTER; 
      } 
     } 
     catch { return; } 
    } 

    //Exporting to PDF 
    string folderPath = "C:\\PDFs\\"; 
    if (!Directory.Exists(folderPath)) 
    { 
     Directory.CreateDirectory(folderPath); 
    } 
    using (FileStream stream = new FileStream(folderPath + "Etiquetas.pdf", FileMode.Create)) 
    { 
     Document pdfDoc = new Document(PageSize.A4, 0f, 0f, 0f, 0f); 
     PdfWriter writer = PdfWriter.GetInstance(pdfDoc, stream); 
     PdfPTable tabla = new PdfPTable(1); 
     pdfDoc.Open(); 
     PdfContentByte cb = writer.DirectContent; 
     PdfContentByte lineas = writer.DirectContent; 

     for (int fila = 0; fila < dataGridViewVentas.Rows.Count - 1; fila++) 
     { 
      for (int col = 0; col < dataGridViewVentas.Rows[fila].Cells.Count; col++) 
       labelTitulo.Text = "Etiquetas"; 
      Cantidad = dataGridViewVentas.Rows[fila].Cells[0].Value.ToString(); 
      IdColor = dataGridViewVentas.Rows[fila].Cells[1].Value.ToString(); 
      IdCategoria = dataGridViewVentas.Rows[fila].Cells[2].Value.ToString(); 
      NomCategoria = dataGridViewVentas.Rows[fila].Cells[3].Value.ToString(); 
      Colores = dataGridViewVentas.Rows[fila].Cells[4].Value.ToString(); 
      CodigoDeGrupo = dataGridViewVentas.Rows[fila].Cells[5].Value.ToString(); 
      lblNombreSocio.Text = IdColor + ":" + Cantidad; 
      String espacio = "   "; 
      Paragraph linea = new Paragraph("________________________________________________________", fuente); 
      Paragraph lineaDoble = new Paragraph(Cantidad, fuente); 
      Paragraph SocioPar = new Paragraph("Color:" + lblNombreSocio.Text, fuente); 
      Paragraph SociedadPar = new Paragraph("Categoria: " + NomCategoria, fuente); 
      Paragraph Titulo = new Paragraph(labelTitulo.Text, fontH1); 
      Paragraph parrafoEspacio = new Paragraph(" ", fuente); 
      linea.Alignment = Element.ALIGN_CENTER; 
      lineaDoble.Alignment = Element.ALIGN_CENTER; 
      Titulo.Alignment = Element.ALIGN_CENTER; 

      SocioPar.Alignment = Element.ALIGN_CENTER; 
      SociedadPar.Alignment = Element.ALIGN_CENTER; 
      pdfTable.HorizontalAlignment = Element.ALIGN_CENTER; 
      pdfDoc.Add(linea); 
      pdfDoc.Add(Titulo); 
      pdfDoc.Add(lineaDoble); 
      pdfDoc.Add(parrafoEspacio); 
      pdfDoc.Add(SocioPar); 
      pdfDoc.Add(SociedadPar); 

      // pdfDoc.Add(pdfTable);} 
      for (int x = 0; x < Convert.ToInt32(Cantidad); x++) 
      { 
       cb.Rectangle(posx, posy, 40f, 25f); 
       lineas.SetLineWidth(1); 
       lineas.MoveTo(posx+20, posy+15); 
       lineas.LineTo(posx+20,posy); 
       lineas.Stroke(); 
      // lineas. 
       lineas.MoveTo(posx + 20, posy+10); 
       lineas.LineTo(posx, posy + 10); 
       lineas.MoveTo(posx + 40, posy + 10); 
       lineas.LineTo(posx, posy + 10); 
       lineas.Stroke(); 
       //texto !!!!!!!!!!! 
       lineas.BeginText(); 
       lineas.SetFontAndSize(FUENTE_BASE, 6); 
       lineas.SetTextMatrix(posx, posy); 
       lineas.ShowText(IdColor); 
       lineas.EndText(); 
       //tabla.AddCell("prueba"); 
       // pdfDoc.Add(tabla); 
       Chunk c = new Chunk(x.ToString()); 
       // iTextSharp.text.Rectangle rect = new iTextSharp.text.Rectangle(posx, 650f, 25f, 10f); 
       // cb.Rectangle(rect); 
       cb.Stroke(); 
       float nuevo = posx + 42f; 
       posx = nuevo; 
       if (posx > 500f) 
        { posx = 20; posy = posy-35f; } 
      } 
      pdfDoc.NewPage();    
      posx = 10f; 
      posy = 700; 
     } 
     pdfDoc.Close(); 
     stream.Close(); 
    } 
}