23

The PDF I can produce at the moment: screenshot
(source: yart.com.au)

I want the text to fill up the space in the lower left. How can I do that? Thanks!

This is my code:

private static void CreatePdf4(string pdfFilename, string heading, string text, string[] photos, string emoticon)
{
  Document document = new Document(PageSize.A4.Rotate(), 26, 36, 0, 0);
  PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(pdfFilename, FileMode.Create));
  document.Open();

  // Heading
  Paragraph pHeading = new Paragraph(new Chunk(heading, FontFactory.GetFont(FontFactory.HELVETICA, 54, Font.NORMAL)));
  document.Add(pHeading);

  // Photo 1
  Image img1 = Image.GetInstance(HttpContext.Current.Server.MapPath("/uploads/photos/" + photos[0]));
  img1.ScaleAbsolute(350, 261);
  img1.SetAbsolutePosition(46, 220);
  img1.Alignment = Image.TEXTWRAP;
  document.Add(img1);

  // Photo 2
  Image img2 = Image.GetInstance(HttpContext.Current.Server.MapPath("/uploads/photos/" + photos[1]));
  img2.ScaleAbsolute(350, 261);
  img2.SetAbsolutePosition(438, 220);
  img2.Alignment = Image.TEXTWRAP;
  document.Add(img2);

  // Text

  PdfContentByte cb = writer.DirectContent;
  cb.BeginText();
  cb.SetFontAndSize(BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1252, false), 18);
  cb.SetTextMatrix(46, 175);
  cb.ShowText(text);
  cb.EndText();

  // Photo 3
  Image img3 = Image.GetInstance(HttpContext.Current.Server.MapPath("/uploads/photos/" + photos[2]));
  img3.ScaleAbsolute(113, 153);
  img3.SetAbsolutePosition(556, 38);
  document.Add(img3);

  // Emoticon
  Image imgEmo = Image.GetInstance(HttpContext.Current.Server.MapPath("/Content/images/" + emoticon));
  imgEmo.ScaleToFit(80, 80);
  imgEmo.SetAbsolutePosition(692, 70);
  document.Add(imgEmo);

  document.Close();
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Aximili
  • 28,626
  • 56
  • 157
  • 216

2 Answers2

38

Solved

  PdfContentByte cb = writer.DirectContent;
  ColumnText ct = new ColumnText(cb);
  ct.SetSimpleColumn(new Phrase(new Chunk(text, FontFactory.GetFont(FontFactory.HELVETICA, 18, Font.NORMAL))),
                     46, 190, 530, 36, 25, Element.ALIGN_LEFT | Element.ALIGN_TOP);
  ct.Go(); 
Aximili
  • 28,626
  • 56
  • 157
  • 216
  • Works similar for PurePDF in AS3 as well. – Martin Jul 16 '12 at 02:58
  • 2
    how do you adjust the line spacing? – Cocoa Dev Apr 03 '13 at 17:16
  • Leading can be set as the first argument of the Phrase Object. new Phrase(float leading, Chunk chunk); – abbottmw Jan 17 '14 at 13:16
  • 3
    @CocoaDev @abbottmw The `leading` parameter of `Phrase` constructor doesn't work in this case because it will be overwritten by the `leading` parameter of the `SetSimpleColumn`, which in the example is the value 25. So you just have to change this value. – André Santaló Mar 07 '14 at 16:35
2

For Cocoa Dev, yeah we can set line spacing for the text using PdfPCell's SetLeading property as: PdfPCell.SetLeading(float fixedleading, float multiplied leading);