ITextsharp:读取Byte []内容中的pdf文件时出错(PdfReader)

时间:2018-09-21 14:25:58

标签: c# asp.net itext

我正在尝试通过一个列表将多个PDF合并到一个文件中,该列表的字节数为[]。使用PdfReader从Byte []列表中打开文档时,程序将启动以下异常:“文档无页面” 。当我查看Byte []列表的内容时,这些内容已经完成,但是总是会引发异常。

我尝试单独下载该单页的内容,并且打开该文件时生成的文档会启动错误。 pdf的划分效果很好,因为它可以物理生成每个文档,并且非常适合PDF的每一页。

在这种情况下,我感谢您的帮助或意见。

这是我用于拆分和合并文档的代码:

    public List<byte[]> SplitPDF(byte[] contentPdf)
    {
        try
        {
            var listBythe = new List<byte[]>();
            PdfImportedPage page = null;
            PdfCopy PdfCopy = null;
            PdfReader reader = new PdfReader(contentPdf);
            for (int numPage = 1; numPage <= reader.NumberOfPages; numPage++)
            {
                Document doc = new Document(PageSize.LETTER);
                var mStream = new MemoryStream();
                PdfCopy = new PdfCopy(doc, mStream);
                doc.Open();
                page = PdfCopy.GetImportedPage(reader, numPage);
                PdfCopy.AddPage(page);
                listBythe.Add(mStream.ToArray());
                doc.Close();
            }

            MergePdfToPage(listBythe);
            return listBythe;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    private byte[] MergePdfToPage(List<byte[]>contentPage)
    {
        byte[] docPdfByte = null;
        var ms = new MemoryStream();
        using (Document doc = new Document(PageSize.LETTER))
        {
            PdfCopy copy = new PdfCopy(doc, ms);
            doc.Open();
            var num = doc.PageNumber;
            foreach (var file in contentPage.ToArray())
            {
                using (var reader = new PdfReader(file))
                {
                    copy.AddDocument(reader);
                }
            }
            doc.Close();
            docPdfByte = ms.ToArray();
        }
        return docPdfByte;

2 个答案:

答案 0 :(得分:1)

我为您创建了一些东西,希望它能像对我一样好用。 类:

public class PDFFactory
{
    public PDFFactory()
    {
        PdfDocument = new Document(iTextSharp.text.PageSize.A4, 65, 65, 60, 60);
    }

    private Document _pdfDocument;
    public Document PdfDocument
    {
        get
        {
            return _pdfDocument;
        }
        set
        {
            _pdfDocument = value;
        }
    }

    private MemoryStream _pdfMemoryStream;
    public MemoryStream PDFMemoryStream
    {
        get
        {
            return _pdfMemoryStream;
        }
        set
        {
            _pdfMemoryStream = value;
        }
    }

    private string _pdfBase64;
    public string PDFBase64
    {
        get
        {
            if (this.DocumentClosed)
                return _pdfBase64;
            else
                return null;
        }
        set
        {
            _pdfBase64 = value;
        }
    }

    private byte[] _pdfBytes;
    public byte[] PDFBytes
    {
        get
        {
            if (this.DocumentClosed)
                return _pdfBytes;
            else
                return null;
        }
        set
        {
            _pdfBytes = value;
        }
    }

    public byte[] GetPDFBytes()
    {
        PDFDocument.Close();
        return PDFMemoryStream.GetBuffer();
    }

    public void closeDocument()
    {
        PDFDocument.Close();
        PDFBase64 = Convert.ToBase64String(this.PDFMemoryStream.GetBuffer());
        PDFBytes = this.PDFMemoryStream.GetBuffer();
    }
}

服务:

public byte[] ()
{
    PDFFactory pdf_1 = new PDFFactory();
    PDFFactory pdf_2 = new PDFFactory();
    List<byte[]> sourceFiles = new List<byte[]>();
    sourceFiles.Add(pdf_1.GetPDFBytes);
    sourceFiles.Add(pdf_2.GetPDFBytes);

    PDFFactory pdfFinal = new PDFFactory();
    for (int fileCounter = 0; fileCounter <= sourceFiles.Count - 1; fileCounter += 1)
    {
        PdfReader reader2 = new PdfReader(sourceFiles[fileCounter]);
        int numberOfPages = reader2.NumberOfPages;

        for (int currentPageIndex = 1; currentPageIndex <= numberOfPages; currentPageIndex++)
        {
            // Determine page size for the current page
            pdfFinal.PDFDocument.SetPageSize(reader2.GetPageSizeWithRotation(currentPageIndex));

            // Create page
            pdfFinal.PDFDocument.NewPage();
            PdfImportedPage importedPage = pdfFinal.PDFWriter.GetImportedPage(reader2, currentPageIndex);

            // Determine page orientation
            int pageOrientation = reader2.GetPageRotation(currentPageIndex);
            if ((pageOrientation == 90) || (pageOrientation == 270))
                pdfFinal.PDFWriter.DirectContent.AddTemplate(importedPage, 0, -1.0F, 1.0F, 0, 0, reader2.GetPageSizeWithRotation(currentPageIndex).Height);
            else
                pdfFinal.PDFWriter.DirectContent.AddTemplate(importedPage, 1.0F, 0, 0, 1.0F, 0, 0);
        }
    }

    pdfFinal.closeDocument();
    return pdfFinal.PDFBytes;
}

让我知道是否有帮助。

答案 1 :(得分:1)

在循环中,您这样做

            Document doc = new Document(PageSize.LETTER);
            var mStream = new MemoryStream();
            PdfCopy = new PdfCopy(doc, mStream);
            doc.Open();
            page = PdfCopy.GetImportedPage(reader, numPage);
            PdfCopy.AddPage(page);
            listBythe.Add(mStream.ToArray());
            doc.Close();

特别是,您在关闭mStream之前 检索了doc个字节。但是在doc关闭之前,mStream中的pdf不完整!

要从mStream获取完整的pdf,请更改操作说明的顺序

            Document doc = new Document(PageSize.LETTER);
            var mStream = new MemoryStream();
            PdfCopy = new PdfCopy(doc, mStream);
            doc.Open();
            page = PdfCopy.GetImportedPage(reader, numPage);
            PdfCopy.AddPage(page);
            doc.Close();
            listBythe.Add(mStream.ToArray());

相反。