在IT技术中,如何添加绝对定位的文本?

20
我试图生成一封信,在其中留下一个空位,然后根据信封窗口的位置在其上方粘贴地址。因此,我开始执行以下操作:
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();
我希望能在那些坐标处添加一些文本,但我不知道怎么做...有人知道如何实现吗?
3个回答

26

找到了答案 "Here"。 (以下是引用自Yannick Smits的回答)

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

尝试这个:

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();

SetSimpleColumnж–№жі•зҡ„еҸӮж•°еҢ…жӢ¬:

  1. зҹӯиҜӯ
  2. е·ҰдёӢи§’xиҪҙеқҗж Ү
  3. е·ҰдёӢи§’yиҪҙеқҗж Ү
  4. еҸідёҠи§’xиҪҙеқҗж Ү (llx + е®ҪеәҰ)
  5. еҸідёҠи§’yиҪҙеқҗж Ү (lly + й«ҳеәҰ)
  6. иЎҢй—ҙи·қпјҲжү“еҚ°ж—¶жҜҸиЎҢд№Ӣй—ҙзҡ„з©әзҷҪй—ҙи·қпјү
  7. еҜ№йҪҗж–№ејҸгҖӮ

盒子高度和盒子宽度是什么意思?(第4项,第5项) - Cocoa Dev
我认为第4和第5个参数是urxury,而不是“盒子宽度”和“盒子高度”。第6个参数是“行间距”,我不确定它是什么,但是对于文本来说0f是有效的,因此它不是“行高”。 - Mark Lakata

20

您还可以使用ContentByte和Text Matrix来在任何地方绘制文本。

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();

好处是,如果您不必绘制文本将进入的完整框大小。使用Simple Column,您在文档上绘制矩形,并在其中定位文本。使用ContentByte,您可以避开矩形,仅单独定位文本。


很酷。不过我实际上想要限制文本的宽度。因此在我的情况下使用ColumnText更有用。谢谢! - colinbashbash
我在函数调用中使用了 setSimpleColumn,后续使用相同的测量值会导致相当不愉快的定位。改用这种方法很快就解决了这个问题!谢谢。 - Andrew Neely
但是使用ShowText方法,您无法为RTL语言设置RunDirection。 - Arman

1
感谢您提供的数据,我喜欢它,但我的英语很差,抱歉。
这是我使用iTextSharp标签的代码:
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();
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接