itextsharp trimming pdf document's pages

前端 未结 3 1168
梦如初夏
梦如初夏 2020-12-17 22:05

I have a pdf document that has form fields that I\'m filling out programatically with c#. Depending on three conditions, I need to trim (delete) some of the pages from that

3条回答
  •  自闭症患者
    2020-12-17 22:51

    Here is the code I use to copy all but the last page of an existing PDF. Everything is in memory streams. The variable pdfByteArray is a byte[] of the original pdf obtained using ms.ToArray(). pdfByteArray is overwritten with the new PDF.

            PdfReader originalPDFReader = new PdfReader(pdfByteArray);
    
            using (MemoryStream msCopy = new MemoryStream())
            {
               using (Document docCopy = new Document())
               {
                  using (PdfCopy copy = new PdfCopy(docCopy, msCopy))
                  {
                     docCopy.Open();
                     for (int pageNum = 1; pageNum <= originalPDFReader.NumberOfPages - 1; pageNum ++)
                     {
                        copy.AddPage(copy.GetImportedPage(originalPDFReader, pageNum ));
                     }
                     docCopy.Close();
                  }
               }
    
               pdfByteArray = msCopy.ToArray();
    

提交回复
热议问题