"Pdf indirect object belongs to other PDF document." 多pdf创建

"Pdf indirect object belongs to other PDF document." multi-pdf creation

工作正常

    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();
        }
    }

无效

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();
        }
    }
}

当它尝试在新的 pdf 文件上添加分页符时抛出错误

Document?.Add(new AreaBreak(AreaBreakType.NEXT_PAGE));

看到很多帖子说是字体的问题,但是我的资源是用字体程序工厂生成局部变量字体,好像没有解决问题。

所以,它最终成为我访问图像的方式。

    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;
        }
    }

是我现在用的,好像还行。

以前是:

        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;
        }