提问者:小点点

"pdf间接对象属于其他PDF文档."多pdf创建


工作正常

    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文件上添加分页符时会引发错误

文档?.添加(new Area aBreak(Area aBreak.NEXT_PAGE));

我见过很多关于它是字体问题的帖子,但是我的资源使用字体程序工厂来生成局部变量字体,这似乎并没有解决这个问题。


共1个答案

匿名用户

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

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