228 lines
7.9 KiB
C#
228 lines
7.9 KiB
C#
using iTextSharp.text;
|
|
using iTextSharp.text.pdf;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using WebApplication5.Models;
|
|
|
|
namespace WebApplication5.Writer
|
|
{
|
|
public class WriterPDF
|
|
{
|
|
private static string[] listOfPodpunkty = new string[] { "a", "b", "c", "d", "e", "f", "g", "h" };
|
|
|
|
private Font catFont;
|
|
private Font titleFont;
|
|
private Font baseFont;
|
|
|
|
Document document;
|
|
PdfContentByte cb;
|
|
PdfWriter writer;
|
|
FileStream fs;
|
|
public void WriteQuestions(List<Question> questions, int questionNumber)
|
|
{
|
|
var localQuestionCount = 0;
|
|
foreach (var question in questions)
|
|
{
|
|
WriteQuestion(question.QuestionText, questionNumber + localQuestionCount);
|
|
WriteAnswers(question.Answers.ToList());
|
|
localQuestionCount++;
|
|
}
|
|
}
|
|
|
|
public void CreateFile(string fileName)
|
|
{
|
|
FontFactory.Register(ConfigurationManager.AppSettings["Fonts"]);
|
|
|
|
titleFont = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED, 18, Font.BOLD);
|
|
catFont = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED, 14, Font.BOLD);
|
|
baseFont = FontFactory.GetFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED, 12);
|
|
var path = ConfigurationManager.AppSettings["TestsFolder"];
|
|
|
|
document = new Document();
|
|
BaseFont helvetica = BaseFont.CreateFont(BaseFont.HELVETICA, BaseFont.CP1250, BaseFont.EMBEDDED);
|
|
Font helvetica16 = new Font(helvetica, 16);
|
|
// open the writer
|
|
fs = new FileStream(path + fileName + ".pdf", FileMode.Create, FileAccess.Write);
|
|
writer = PdfWriter.GetInstance(document, fs);
|
|
document.Open();
|
|
// the pdf content
|
|
cb = writer.DirectContent;
|
|
}
|
|
private void WriteQuestion(string question, int questionNumber)
|
|
{
|
|
document.Add(new Paragraph(string.Format("{0}. {1}", questionNumber, question), baseFont));
|
|
}
|
|
private string FormatAnswer(int letter, string answer)
|
|
{
|
|
return string.Format("{0}) {1}", listOfPodpunkty[letter], answer);
|
|
}
|
|
private void WriteAnswers(List<Answer> answers)
|
|
{
|
|
PdfPTable table = new PdfPTable(2);
|
|
table.SpacingAfter = 10;
|
|
table.SpacingBefore = 5;
|
|
table.WidthPercentage = 90;
|
|
table.DefaultCell.Border = 0;
|
|
for (int i = 0; i < answers.Count; i++)
|
|
{
|
|
table.AddCell(new Phrase(FormatAnswer(i, answers.ElementAt(i).AnswerText), baseFont));
|
|
}
|
|
|
|
if (answers.Count % 2 == 1)
|
|
{
|
|
var cell = new PdfPCell(new Phrase(" "));
|
|
cell.Border = 0;
|
|
table.AddCell(cell);
|
|
}
|
|
|
|
document.Add(table);
|
|
}
|
|
|
|
|
|
public void WriteTitle(string title)
|
|
{
|
|
document.AddTitle(title);
|
|
var phar = new Paragraph();
|
|
phar.Add(new Paragraph(title, titleFont));
|
|
document.Add(phar);
|
|
}
|
|
public void WriteCategory(string cat)
|
|
{
|
|
var phar = new Paragraph();
|
|
AddEmptyLine(phar, 1);
|
|
phar.Add(new Paragraph(cat, catFont));
|
|
AddEmptyLine(phar, 1);
|
|
document.Add(phar);
|
|
}
|
|
public void EndPage()
|
|
{
|
|
document.NewPage();
|
|
}
|
|
private static void AddEmptyLine(Paragraph paragraph, int number)
|
|
{
|
|
for (int i = 0; i < number; i++)
|
|
{
|
|
paragraph.Add(new Paragraph(" "));
|
|
}
|
|
}
|
|
public void CloseFile()
|
|
{
|
|
document.Close();
|
|
fs.Close();
|
|
writer.Close();
|
|
}
|
|
|
|
public void WriteAnswerTemplate(List<Question> questions, string qrCode)
|
|
{
|
|
AddUserId();
|
|
|
|
WriteTitle("Odpowiedzi");
|
|
|
|
PdfPTable outer = new PdfPTable(2);
|
|
outer.WidthPercentage = 95;
|
|
outer.HorizontalAlignment = Element.ALIGN_LEFT;
|
|
var size = questions.Count - (questions.Count / 2);
|
|
var isEven = questions.Count % 2 == 0;
|
|
outer.DefaultCell.Border = 0;
|
|
outer.AddCell(CreateAnswersTable(questions.Take(size).ToList(), 0));
|
|
outer.AddCell(CreateAnswersTable(questions.Skip(size).ToList(), size, isEven));
|
|
|
|
document.Add(outer);
|
|
AddQRCode(qrCode);
|
|
EndPage();
|
|
}
|
|
|
|
private PdfPTable CreateAnswersTable(List<Question> questions, int moved, bool isEven = true)
|
|
{
|
|
var maxWidth = questions.Max(t => t.Answers.Count) + 1;
|
|
var table = new PdfPTable(maxWidth);
|
|
table.DefaultCell.Border = 0;
|
|
table.HorizontalAlignment = Element.ALIGN_LEFT;
|
|
|
|
var cornerCell = new PdfPCell(new Phrase(" "));
|
|
cornerCell.Border = 0;
|
|
table.AddCell(cornerCell);
|
|
|
|
var widths = new List<float>();
|
|
var ratio = (5 * maxWidth - 2);
|
|
var percentage = 100 / ratio;
|
|
var rest = 100 % ratio;
|
|
|
|
widths.Add(2 * percentage + rest);
|
|
for (int i = 1; i < maxWidth; i++)
|
|
{
|
|
widths.Add(percentage * 5);
|
|
}
|
|
table.SetWidthPercentage(widths.ToArray(), document.PageSize);
|
|
for (int k = 0; k < maxWidth - 1; k++)
|
|
{
|
|
var lettersCell = new PdfPCell(new Phrase(listOfPodpunkty[k]));
|
|
lettersCell.Border = 0;
|
|
lettersCell.HorizontalAlignment = Element.ALIGN_CENTER;
|
|
table.AddCell(lettersCell);
|
|
}
|
|
|
|
for (int i = 0; i < questions.Count; i++)
|
|
{
|
|
var ansCount = 1;
|
|
var numberCell = new PdfPCell(new Phrase(string.Format("{0}.", i + 1 + moved), baseFont));
|
|
numberCell.Border = 0;
|
|
numberCell.HorizontalAlignment = Element.ALIGN_RIGHT;
|
|
table.AddCell(numberCell);
|
|
|
|
foreach (var a in questions.ElementAt(i).Answers)
|
|
{
|
|
var answerCell = new PdfPCell(new Phrase(" ", baseFont));
|
|
answerCell.Border = 15;
|
|
table.AddCell(answerCell);
|
|
ansCount++;
|
|
}
|
|
|
|
for (int j = ansCount; j < maxWidth; j++)
|
|
{
|
|
var emptyAnswerCell = new PdfPCell(new Phrase(" "));
|
|
emptyAnswerCell.Border = 0;
|
|
table.AddCell(emptyAnswerCell);
|
|
}
|
|
}
|
|
if (!isEven)
|
|
{
|
|
for (int j = 0; j < maxWidth; j++)
|
|
{
|
|
var emptyAnswerCell = new PdfPCell(new Phrase(" ", baseFont));
|
|
emptyAnswerCell.Border = 0;
|
|
table.AddCell(emptyAnswerCell);
|
|
}
|
|
}
|
|
return table;
|
|
}
|
|
|
|
private void AddQRCode(string salt)
|
|
{
|
|
var qrcode = new BarcodeQRCode(salt, 100, 100, null);
|
|
var image = qrcode.GetImage();
|
|
var mask = qrcode.GetImage();
|
|
mask.MakeMask();
|
|
image.ImageMask = mask; // making the background color transparent
|
|
image.SetAbsolutePosition(document.PageSize.Width - document.RightMargin - 80, document.PageSize.Height - document.TopMargin - 80);
|
|
document.Add(image);
|
|
}
|
|
|
|
private void AddUserId()
|
|
{
|
|
WriteTitle("PESEL");
|
|
PdfPTable table = new PdfPTable(11);
|
|
table.SpacingBefore = 10;
|
|
table.WidthPercentage = 50;
|
|
table.HorizontalAlignment = Element.ALIGN_LEFT;
|
|
for (int i = 0; i < 11; i++)
|
|
table.AddCell(" ");
|
|
|
|
document.Add(table);
|
|
}
|
|
}
|
|
} |