Skip to content

Instantly share code, notes, and snippets.

@justingarrick
Created July 2, 2013 15:54
Show Gist options
  • Save justingarrick/5910539 to your computer and use it in GitHub Desktop.
Save justingarrick/5910539 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using MyProject.Domain;
using EO.Pdf;
using EO.Pdf.Acm;
namespace MyProject.Services
{
public class EoAssessmentPrinter
{
private bool isTwoColumn;
private string title;
private const string Space = " ";
public Stream CreateAssessmentPdf(AssessmentTestVersion assessment, bool isTwoColumn)
{
this.isTwoColumn = isTwoColumn;
PdfDocument pdf = new PdfDocument();
AcmPageLayout layout = GetLayout(isTwoColumn);
AcmRender renderer = new AcmRender(pdf, layout);
renderer.BeforeRenderPage += new AcmPageEventHandler(BeforeRenderPage);
SetTitle(assessment.AssessmentName);
foreach (AssessmentTestQuestion question in assessment.Questions.OrderBy(t => t.AssessmentSectionItem.OrderNumber))
{
AcmParagraph paragraph = AddQuestion(question);
renderer.Render(paragraph);
}
pdf.Save("C:\\dev\\testcols" + (isTwoColumn ? 2 : 1) + ".pdf"); // just save the PDF for now
return null;
}
private void SetTitle(string title)
{
this.title = title;
}
private AcmParagraph AddQuestion(Question question)
{
AcmParagraph paragraph = new AcmParagraph();
int? questionNumber = ((AssessmentTestQuestion)question).AssessmentSectionItem.OrderNumber;
AcmText questionText = new AcmText(questionNumber + "." + Space + question.ItemBody);
paragraph.Children.Add(questionText);
paragraph.Children.Add(new AcmLineBreak());
for (int i = 0; i < question.QuestionResponses.Count; i++)
{
AssessmentItemResponse answer = question.QuestionResponses[i];
AcmText answerText = AddAnswer(answer);
answerText.Style.Margin.Left = 0.5f;
paragraph.Children.Add(answerText);
if (i < question.QuestionResponses.Count - 1)
paragraph.Children.Add(new AcmLineBreak());
}
return paragraph;
}
private AcmText AddAnswer(AssessmentItemResponse answer)
{
AcmText answerText = new AcmText(answer.Label + "." + Space + answer.Answer);
return answerText;
}
private void BeforeRenderPage(object sender, AcmPageEventArgs e)
{
PdfPage page = e.Page;
AcmRender renderer = new AcmRender(page, 0, new AcmPageLayout(new AcmPadding(1, 0, 1, 0)));
if (null != title)
{
AcmBlock header = new AcmBlock(new AcmText(title));
header.Style.Border.Bottom = new AcmLineInfo(AcmLineStyle.Solid, Color.Black, 0.01f);
header.Style.Top = 0.1f;
header.Style.HorizontalAlign = AcmHorizontalAlign.Center;
renderer.Render(header);
}
AcmBlock footer = new AcmBlock(new AcmText(string.Format("{0}", page.Index)));
footer.Style.Border.Top = new AcmLineInfo(AcmLineStyle.Solid, Color.Black, 0.01f);
footer.Style.Top = 10.6f;
footer.Style.HorizontalAlign = AcmHorizontalAlign.Right;
renderer.Render(footer);
}
private static AcmPageLayout GetLayout(bool isTwoColumn)
{
return isTwoColumn
? new AcmPageLayout(new AcmPadding(0.5f), new AcmColumn(0.5f, 3f), new AcmColumn(4.5f, 3f))
: new AcmPageLayout(new AcmPadding(0.5f));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment