You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用iText7设置PDF/A-2A合规性时遇Unexpected end of file异常

使用iText7转换PDF到PDF/A-2A时抛出"Unexpected end of file"异常

我有一个单页PDF文档,在尝试用iText7将其设置为PDF_A_2A合规级别时,出现如下异常:

iText.Kernel.Exceptions.PdfException: Unexpected end of file.
   at iText.Kernel.Pdf.Canvas.Parser.Util.PdfCanvasParser.ReadDictionary()
   at iText.Kernel.Pdf.Canvas.Parser.Util.PdfCanvasParser.Parse(IList`1 ls)
   at iText.Pdfa.Checker.PdfA1Checker.CheckContentStream(PdfStream contentStream)
   at iText.Pdfa.Checker.PdfAChecker.CheckPage(PdfPage page)
   at iText.Pdfa.Checker.PdfAChecker.CheckPages(PdfDocument document)
   at iText.Pdfa.Checker.PdfAChecker.CheckDocument(PdfCatalog catalog)
   at iText.Kernel.Utils.ValidationContainer.Validate(ValidationContext context)
   at iText.Kernel.Pdf.PdfDocument.Close()

我使用以下代码将原文档的页面逐个复制到新的PdfADocument中:

using (PdfADocument pdfADoc = new PdfADocument(writer, pdfAConformanceLevel, CreateOutputIntent(false)))
{
    pdfADoc.GetDocumentInfo().SetCreator("Scan2x");

    using (PdfDocument existingDoc = new PdfDocument(pdfReader))
    {
        int numberOfPages = existingDoc.GetNumberOfPages();

        for (int i = 1; i <= numberOfPages; i++)
        {
            PdfPage page = existingDoc.GetPage(i);

            //PdfDictionary pageDictionary = page.GetPdfObject();

            pdfADoc.AddPage(page.CopyTo(pdfADoc));
        }

        //existingDoc.CopyPagesTo(1, existingDoc.GetNumberOfPages(), pdfADoc);

        pdfADoc.SetTagged();
        pdfADoc.GetCatalog().SetLang(new PdfString("en-US"));
        pdfADoc.GetCatalog().SetViewerPreferences(new PdfViewerPreferences().SetDisplayDocTitle(true));
    }
}

 private static PdfOutputIntent CreateOutputIntent(bool hasCmykOutputIntent)
 {
     string exeDirectory = AppDomain.CurrentDomain.BaseDirectory;
     string colourProfilePath = "";
     if (hasCmykOutputIntent)
         colourProfilePath = Path.Combine(exeDirectory, "CMYK.icc");
     else
         colourProfilePath = Path.Combine(exeDirectory, "sRGBColor.icm");

     Stream iccProfileStream = File.OpenRead(colourProfilePath);
     return new PdfOutputIntent("Custom", "", "http://www.color.org", hasCmykOutputIntent ? "CMYK" : "sRGB IEC61966-2.1", iccProfileStream);
 }

该PDF的页面字典内容如下:

<< /Contents [114 0 R 115 0 R 116 0 R 117 0 R 118 0 R 119 0 R 120 0 R 121 0 R ] /CropBox [0 0 594.96 842.04 ] /Group << /CS /DeviceRGB /S /Transparency /Type /Group >> /MediaBox [0 0 594.96 842.04 ] /Parent 108 0 R /Resources << /ColorSpace << /CS0 132 0 R >> /Font << /C2_0 137 0 R /C2_1 143 0 R /TT0 146 0 R /TT1 149 0 R >> /ProcSet [/PDF /Text /ImageC /ImageI ] /XObject << /Im0 129 0 R >> >> /Rotate 0 /StructParents 0 /Tabs /S /Type /Page >> 

内容的提问来源于stack exchange,提问作者Gutto Freitas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 17:35:21