0

I want to insert a paragraph (which will word-wrap over several lines) into a PDF document using iTextSharp, but I want to restrict the width of the paragraph to the left half of the page. I see no "width" property for the Paragraph class, but surely there is a way to do this, stimmt?

UPDATE

The supposed answer doesn't work for me because it uses iText (Java) stuff apparently unavailable in iTextSharp (C#). Specifically (to begin with, there might be more):

ct.setSimpleColumn(myText, 60, 750, document.getPageSize().getWidth()

Although there is a "SetSimpleColumn" for *Sharp (uppercased initial 's'), there is no "GetPageSize".

UPDATE 2

I'm beginning to think that what I really need to do may be to create a 'borderless table' as suggested, and as articulated in "BestiTextQuestionsOnStackOverflowFull.pdf"

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 2
    The simplest way might be to use a table without borders. You'll need to adjust cell padding and a couple of other things but this will allow you to "Document.Add()" it. If that's not working for you then as @OneFineDay said you'll want to create your own rectangle to control it and use a [`ColumnText`](http://stackoverflow.com/a/15166587/231316) – Chris Haas Apr 08 '15 at 22:38
  • 1
    Please read [the documentation](http://itextpdf.com/learn), for instance [The Best iText Questions on StackOverflow](http://pages.itextpdf.com/ebook-stackoverflow-questions.html) and you'll discover that many of the questions you've posted recently are duplicates. For instance: this question is a possible duplicate of [How to fit a String inside a rectangle?](http://stackoverflow.com/questions/13526043/how-to-fit-a-string-inside-a-rectangle) It could also be as simple as defining a custom left margin. – Bruno Lowagie Apr 09 '15 at 07:34
  • Interesting; I downloaded the sample of the "Best iText Questions on StackOverflow"; how much larger is the full edition? – B. Clay Shannon-B. Crow Raven Apr 09 '15 at 14:44
  • 1
    Regarding your update to your question: getters/setters in iText are mostly properties in iTextSharp. `document.getPageSize().getWidth()` becomes `document.PageSize.Width`. – rhens Apr 10 '15 at 12:30

1 Answers1

0

This is one way to do it - a borderless, single-row table with WidthPercentage set to 50 and Horizontal Alignment sent to Left:

using (var ms = new MemoryStream())
{
    using (var doc = new Document(PageSize.A4, 50, 50, 25, 25))                     {
        //Create a writer that's bound to our PDF abstraction and our stream
        using (var writer = PdfWriter.GetInstance(doc, ms))
        {

            //Open the document for writing
            doc.Open();

            var courier9RedFont = FontFactory.GetFont("Courier", 9, BaseColor.RED);
            var importantNotice = new Paragraph("Sit on a potato pan Otis - if you don't agree that that's the best palindrome ever, I will sic Paladin on you, or at least say, 'All down but nine - set 'em up on the other alley, pard'", courier9RedFont);
            importantNotice.Leading = 0;
            importantNotice.MultipliedLeading = 0.9F; // reduce the width between lines in the paragraph with these two settings

            // Add a single-cell, borderless, left-aligned, half-page, table
            PdfPTable table = new PdfPTable(1);
            PdfPCell cellImportantNote = new PdfPCell(importantNotice);
            cellImportantNote.BorderWidth = PdfPCell.NO_BORDER;
            table.WidthPercentage = 50;
            table.HorizontalAlignment = Element.ALIGN_LEFT;
            table.AddCell(cellImportantNote);
            doc.Add(table);

            doc.Close();
        }
        var bytes = ms.ToArray();
        String PDFTestOutputFileName = String.Format("iTextSharp_{0}.pdf", DateTime.Now.ToShortTimeString());
        PDFTestOutputFileName = PDFTestOutputFileName.Replace(":", "_");
        var testFile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), PDFTestOutputFileName);
        File.WriteAllBytes(testFile, bytes);
        MessageBox.Show(String.Format("{0} written", PDFTestOutputFileName));
    }
}
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862