首页 > 解决方案 > C# iTextSharp PdfCopy to MemoryStream 复制整个文档和单页

问题描述

我正在尝试使用内存流从 3 页 PDF 文档制作一页 PDF。但是当下面的代码执行时,所有 3 页都会被添加,而不仅仅是第一页。

我在这里想念什么?请帮忙。注意:我使用的是 iTextSharp v5.5.13

using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.parser;
MemoryStream ms = new MemoryStream();
byte[] fileToBeEncrypted = null;
string sSourcePDF = "C:\\my3pageFile.pdf;
PdfReader pdfReader = new PdfReader(sSourcePDF);

    Document document = new Document();
    PdfCopy copy = new PdfCopy(document, ms) {CloseStream = false};
    document.Open();
    copy.AddPage(copy.GetImportedPage(pdfReader, iPage));
    document.Close();
    fileToBeEncrypted = ms.ToArray();  //returns the ENTIRE DOCUMENT AND NOT JUST PAGE 1

任何人都可以帮忙吗?

谢谢

汤姆

标签: asp.netitext

解决方案


上面的代码 pdfCopy.GetImportedPages 确实有效,我在自己的代码中发现了一个错误。


推荐阅读