How can I increase the font size while generating a PDF file using this code?

I got a working answer to my question on printing four "quarter page" sections on one fullsize page with iTextSharp here.

The problem is that the print is sort of puny; here's an example of the top half of a page - you can see how small the print is as well as how much extra space is available:

enter image description here

This is the code that is used, based on the answer provided on the heretofore linked SO post:

public static void PrintSlips(List<AssignmentStudentMashup> asmList)
{
    PdfReader reader = new PdfReader(GetMasterDocument(asmList));
    Rectangle pageSize = reader.GetPageSize(1);
    string printedDate = DateTime.Now.ToShortDateString();
    printedDate = printedDate.Replace("/", "_");
    string printedSlipsFile = $"C:\\AYttFMApp\\AYttFMSlips_{printedDate}.pdf";

    // Delete the file if it exists.
    if (File.Exists(printedSlipsFile))
    {
        File.Delete(printedSlipsFile);
    }

    using (FileStream stream = new FileStream(
        printedSlipsFile,
        FileMode.Create,
        FileAccess.Write))
    {
        using (Document document = new Document(pageSize, 0, 0, 0, 0)) // pageSize could be replaced with "A4"
        {
            PdfWriter writer = PdfWriter.GetInstance(document, stream);
            document.Open();
            PdfPTable table = new PdfPTable(2)
            {
                TotalWidth = pageSize.Width,
                LockedWidth = true
            };
            table.DefaultCell.Border = Rectangle.NO_BORDER;
            table.DefaultCell.FixedHeight = pageSize.Height / 2;

            for (int i = 1; i <= reader.NumberOfPages; i++)
            {
                PdfImportedPage page = writer.GetImportedPage(reader, i);
                table.AddCell(Image.GetInstance(page));
            }
            document.Add(table);
        }
    }
}

internal static byte[] GetMasterDocument(List<AssignmentStudentMashup> asmList)
{
    int count = asmList.Count;
    using (var stream = new MemoryStream())
    {
        using (var document = new Document())
        {
            PdfWriter.GetInstance(document, stream);
            document.Open();
            for (int i = 1; i < count; ++i)
            {
                document.Add(new Paragraph(
                    $@"WeekOfAssignment: {asmList[i].WeekOfAssignment}
                      TalkTypeName: {asmList
                        [i].TalkTypeName}
                      StudentFullname: {asmList[i]
                            .StudentFullname}
                      AssistantFullname: {asmList[i]
                                .AssistantFullname}
                      CounselPointNum: {asmList[i]
                                    .CounselPointNum}
                      CounselPointStr: {asmList[i
                                        ].CounselPointStr}"));
                if (i < count) document.NewPage();
            }
        }
        return stream.ToArray();
    }
}

I assume the place to increase the font would be somewhere in GetMasterDocument(), but I don't know where/what to do. Also, I would like to bold the first portion of each line (the part that precedes the ":").

UPDATE

I tried Agus Rdz's suggestion from the link he provided:

BaseFont bfCourier = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false);
Font courier = new Font(bfCourier, 12, Font.BOLD, Color.Black);
. . .
CounselPointStr: {asmList[i
    //].CounselPointStr}"));
      ].CounselPointStr}", courier));

...but that won't compile; I get:

Error CS1503 Argument 1: cannot convert from 'iTextSharp.text.pdf.BaseFont' to 'iTextSharp.text.Font.FontFamily'

...and:

Error CS1503 Argument 4: cannot convert from 'System.Drawing.Color' to 'iTextSharp.text.BaseColor' AYttFMScheduler

So I then tried this, based on a comment added to that blog post:

Font fMSGothic = FontFactory.GetFont("MS-PGothic", BaseFont.IDENTITY_H, 16);
. . .
CounselPointStr: {asmList[i
    //].CounselPointStr}"));
      ].CounselPointStr}", courier));

...which compiles, but with that I get an I/O Exception, more specifically "Document has no pages"

Upvotes: 0

Views: 2131

Answers (1)

Bruno Lowagie
Bruno Lowagie

Reputation: 77528

This is wrong:

BaseFont bfCourier = BaseFont.CreateFont(BaseFont.COURIER, BaseFont.CP1252, false);
Font courier = new Font(bfCourier, 12, Font.BOLD, Color.Black);

In the first line, you create a BaseFont object. You can use that object to create a Font like this:

Font courier = new Font(bfCourier, 12);

If you want the font to be bold, you can create a BaseFont like this:

BaseFont bfCourier = BaseFont.CreateFont(BaseFont.COURIER_BOLD, BaseFont.CP1252, false);

The error you had, was caused by the fact that a Font can also be create in a different way:

Font courier = new Font(Font.FontFamily.COURIER, 12, Font.BOLD, BaseColor.BLACK);

You confused the concept of BaseFont and FontFamily. A font family (e.g. Courier) is a family of fonts consisting of different fonts (Courier regular, Courier bold, Courier italic, Courier bold-italic). A base font is a single font (e.g. Courier bold).

Note that you also mixed up the classes Color and BaseColor. Actually, the error messages you got were pretty self-explaining.

The "Document has no pages" error also explains what goes wrong: when you Close() the document, no content was added. Maybe you think you've added content, but you didn't. If you think you did, the most likely cause is another exception that got thrown. For instance: you've used a font that doesn't exist. An IOException occurs and you ignore it. As a result, no text is added and you get a "Document has no pages" error. Or, you try to add an Image but the path to that images is wrong. As a result, no content is added and you get a "Document has no pages" error.

In your case, you may get that error because asmList is empty. In that case, you are creating a document without any content because there are no students in asmList.

Upvotes: 1

Related Questions