Works fine
using (PdfWriter pdfWriter = new(Path.Combine(settings.OutputFolder, $"checks.pdf")))
using (PdfDocument pdfDocument = new(pdfWriter))
using (Document document = new(pdfDocument))
{
pdfDocument.SetDefaultPageSize(iText.Kernel.Geom.PageSize.LEGAL);
foreach (var l in group)
{
l.SetPDF(pdfDocument, document);
l.PrintPages();
}
}
Does not work
for(int i = 0; i < letters.Count/200; i )
{
var group = letters.Skip(i * 200).Take(200);
using (PdfWriter pdfWriter = new(Path.Combine(settings.OutputFolder, $"checks_{i 1}.pdf")))
using (PdfDocument pdfDocument = new(pdfWriter))
using (Document document = new(pdfDocument))
{
pdfDocument.SetDefaultPageSize(iText.Kernel.Geom.PageSize.LEGAL);
foreach (var l in group)
{
l.SetPDF(pdfDocument, document);
l.PrintPages();
}
}
}
it throws an error when it tries to add a page break on the new pdf file
Document?.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));
I've seen many posts about it being a problem with fonts, but my resources use a font program factory to generate local variable fonts, which doesn't seem to have solved the problem.
CodePudding user response:
So, it ended up being the way I was accessing images.
public class Resource
{
public Dictionary<string, PdfDocument> Templates { get; set; } = new Dictionary<string, PdfDocument>();
public Dictionary<string, ImageData> Images { get; set; } = new Dictionary<string, ImageData>();
public FontProgram Times_Roman = FontProgramFactory.CreateFont(StandardFonts.TIMES_ROMAN);
public FontProgram Times_Bold = FontProgramFactory.CreateFont(StandardFonts.TIMES_BOLD);
public FontProgram Times_Italic = FontProgramFactory.CreateFont(StandardFonts.TIMES_ITALIC);
public FontProgram Arial = FontProgramFactory.CreateFont(Path.Combine(SettingsFactory.GetSettings().AdminFolder, "_Font", "Arial.ttf"));
public FontProgram Arial_Bold = FontProgramFactory.CreateFont(Path.Combine(SettingsFactory.GetSettings().AdminFolder, "_Font", "Arial Bold.ttf"));
public FontProgram MICR = FontProgramFactory.CreateFont(Path.Combine(SettingsFactory.GetSettings().AdminFolder, "_Font", "MICRE13Bol.ttf"));
//public PdfFont Times_Roman = PdfFontFactory.CreateFont(StandardFonts.TIMES_ROMAN);
//public PdfFont Times_Bold = PdfFontFactory.CreateFont(StandardFonts.TIMES_BOLD);
//public PdfFont Times_Italic = PdfFontFactory.CreateFont(StandardFonts.TIMES_ITALIC);
//public PdfFont Arial = PdfFontFactory.CreateFont(Path.Combine(SettingsFactory.GetSettings().AdminFolder, "_Font", "Arial.ttf"));
//public PdfFont Arial_Bold = PdfFontFactory.CreateFont(Path.Combine(SettingsFactory.GetSettings().AdminFolder, "_Font", "Arial Bold.ttf"));
//public PdfFont MICR = PdfFontFactory.CreateFont(Path.Combine(SettingsFactory.GetSettings().AdminFolder, "_Font", "MICRE13Bol.ttf"));
public PdfDocument GetTemplate(string Name)
{
if (Templates.ContainsKey(Name))
return Templates[Name];
string filePath = Path.Combine(SettingsFactory.GetSettings().AdminFolder, "_Templates", Name);
if (File.Exists(filePath))
{
Templates.Add(Name, new PdfDocument(new PdfReader(filePath)));
return Templates[Name];
}
throw new FileNotFoundException("Template not found", filePath);
return null;
}
public ImageData GetImage(string Name)
{
if (Images.ContainsKey(Name))
return Images[Name];
string filePath = Path.Combine(SettingsFactory.GetSettings().AdminFolder, "_Images", Name);
if (File.Exists(filePath))
{
//Images.Add(Name, new Image(ImageDataFactory.Create(filePath)));
Images.Add(Name, ImageDataFactory.Create(filePath));
return Images[Name];
}
throw new FileNotFoundException("Image not found", filePath);
return null;
}
}
Is what I'm using now, and it seems to work.
It used to be:
public Image GetImage(string Name)
{
if (Images.ContainsKey(Name))
return Images[Name];
string filePath = Path.Combine(SettingsFactory.GetSettings().AdminFolder, "_Images", Name);
if (File.Exists(filePath))
{
Images.Add(Name, new Image(ImageDataFactory.Create(filePath)));
//Images.Add(Name, ImageDataFactory.Create(filePath));
return Images[Name];
}
throw new FileNotFoundException("Image not found", filePath);
return null;
}