Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding absolute positioned text

Tags:

itextsharp

I'm trying to generate a letter, leaving an empty spot and then paste over the top of it the address, depending on where the envelope window is going to be.

So I start off doing this:

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

I was hoping to be able to add some text at those coordinates, but I couldn't figure out how... anybody know a way to do it?

like image 946
colinbashbash Avatar asked Aug 07 '12 22:08

colinbashbash


People also ask

How do you apply position absolute?

An element with position: absolute; is positioned relative to the nearest positioned ancestor (instead of positioned relative to the viewport, like fixed). However; if an absolute positioned element has no positioned ancestors, it uses the document body, and moves along with page scrolling.

What does an absolute position mean?

Absolute positioning defines the position of a given bounding box from the top and left side margins of the web page. This not only allows objects to be placed in an exact location, it also allows objects to be placed one on top of another.

What can I use instead of position absolute?

Fixed. The fixed value is similar to absolute as it can help you position an element anywhere relative to the document, however this value is unaffected by scrolling.


2 Answers

Found the answer "Here". (Below is quoted answer from Yannick Smits)

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

Try this:

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

parameters of SetSimpleColumn are:

  1. the phrase
  2. lower-left-x
  3. lower-left-y
  4. upper-right-x (llx + width)
  5. upper-right-y (lly + height)
  6. leading (The amount of blank space between lines of print)
  7. alignment.
like image 84
colinbashbash Avatar answered Nov 17 '22 03:11

colinbashbash


You can also use a ContentByte with a Text Matrix to draw text wherever you want.

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

The benefit is that if you don't have to draw the full size of the box the text will go into. With Simple Column, you are drawing a rectangle on the document, and positioning the text within it. With ContentByte, you dodge the rectangle, and position the text by itself.

like image 40
Mike Varosky Avatar answered Nov 17 '22 04:11

Mike Varosky