AddDocListener() public method

Adds a IDocListener to the Document.
public AddDocListener ( IDocListener listener ) : void
listener IDocListener the new IDocListener
return void
コード例 #1
0
        public static List <byte[]> Split(PdfReader reader)
        {
            int p = 0;

            Document document;

            var data = new List <byte[]>();


            for (p = 1; p <= reader.NumberOfPages; p++)
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    document = new iTextSharp.text.Document();
                    PdfWriter writer = PdfWriter.GetInstance(document, memoryStream);
                    writer.SetPdfVersion(PdfWriter.PDF_VERSION_1_2);
                    writer.CompressionLevel = PdfStream.BEST_COMPRESSION;
                    writer.SetFullCompression();
                    document.SetPageSize(reader.GetPageSize(p));
                    document.NewPage();
                    document.Open();
                    document.AddDocListener(writer);
                    PdfContentByte  cb         = writer.DirectContent;
                    PdfImportedPage pageImport = writer.GetImportedPage(reader, p);
                    int             rot        = reader.GetPageRotation(p);
                    if (rot == 90 || rot == 270)
                    {
                        cb.AddTemplate(pageImport, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(p).Height);
                    }
                    else
                    {
                        cb.AddTemplate(pageImport, 1.0F, 0, 0, 1.0F, 0, 0);
                    }
                    document.Close();
                    document.Dispose();
                    //File.WriteAllBytes(DestinationFolder + "/" + p + ".pdf", memoryStream.ToArray());

                    data.Add(memoryStream.ToArray());

                    if (OnSplitProcess != null)
                    {
                        OnSplitProcess(p, null);
                    }
                }
            }
            reader.Close();
            reader.Dispose();

            return(data);
        }
コード例 #2
0
        public void ExtractPages(string inputFile, string outputFile,
            List<int> extractPages, System.Windows.Forms.ProgressBar progres)
        {
            if (inputFile == outputFile)
            {
                System.Windows.Forms.MessageBox.Show("Nie możesz użyć pliku wejściowego jako wyjściowego do zapisu.");
            }

            PdfReader inputPDF = new PdfReader(inputFile);

            Document doc = new Document();
            PdfReader reader = new PdfReader(inputFile);
            progres.Maximum = reader.NumberOfPages;

            using (MemoryStream memoryStream = new MemoryStream())
            {
                PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);
                doc.Open();
                doc.AddDocListener(writer);
                for (int i = 1; i <= reader.NumberOfPages; i++)
                {
                    progres.Value = i;
                    if (extractPages.FindIndex(s => s == i) == -1) continue;
                    doc.SetPageSize(reader.GetPageSize(i));
                    doc.NewPage();
                    PdfContentByte cb = writer.DirectContent;
                    PdfImportedPage pageImport = writer.GetImportedPage(reader, i);
                    int rot = reader.GetPageRotation(i);
                    if (rot == 90 || rot == 270)
                    {
                        cb.AddTemplate(pageImport, 0, -1.0F, 1.0F, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                    }
                    else
                    {
                        cb.AddTemplate(pageImport, 1.0F, 0, 0, 1.0F, 0, 0);
                    }
                }
                reader.Close();
                doc.Close();
                try
                {
                    File.WriteAllBytes(outputFile, memoryStream.ToArray());
                }
                catch
                {
                    throw new Exception("Błąd przy próbie zapisu do pliku. Upewnij się iż żaden inny proces obecnie go nie używa.");
                }
            }
        }
コード例 #3
0
 /**
 * Constructs a new RtfWriter that listens to the specified Document and
 * writes its output to the Stream.
 * 
 * @param doc The Document that this RtfWriter listens to
 * @param os The Stream to write to
 */
 protected RtfWriter2(Document doc, Stream os) : base(doc, os) {
     doc.AddDocListener(this);
     rtfDoc = new RtfDocument();
 }
コード例 #4
0
ファイル: PdfCopy.cs プロジェクト: boecko/iTextSharp
 /**
 * Constructor
 * @param document
 * @param os outputstream
 */
 public PdfCopy(Document document, Stream os)
     : base(new PdfDocument(), os)
 {
     document.AddDocListener(pdf);
     pdf.AddWriter(this);
     indirectMap = new Dictionary<PdfReader,Dictionary<RefKey,IndirectReferences>>();
 }
コード例 #5
0
ファイル: PdfWriter.cs プロジェクト: karino2/wikipediaconv
 /**
 * Use this method to get an instance of the <CODE>PdfWriter</CODE>.
 *
 * @return a new <CODE>PdfWriter</CODE>
 * @param document The <CODE>Document</CODE> that has to be written
 * @param os The <CODE>Stream</CODE> the writer has to write to.
 * @param listener A <CODE>DocListener</CODE> to pass to the PdfDocument.
 * @throws DocumentException on error
 */
 public static PdfWriter GetInstance(Document document, Stream os, IDocListener listener)
 {
     PdfDocument pdf = new PdfDocument();
     pdf.AddDocListener(listener);
     document.AddDocListener(pdf);
     PdfWriter writer = new PdfWriter(pdf, os);
     pdf.AddWriter(writer);
     return writer;
 }
コード例 #6
0
ファイル: PdfCopy.cs プロジェクト: pusp/o2platform
 /**
 * Constructor
 * @param document
 * @param os outputstream
 */
 public PdfCopy(Document document, Stream os) : base(new PdfDocument(), os) {
     document.AddDocListener(pdf);
     pdf.AddWriter(this);
     indirectMap = new Hashtable();
 }
コード例 #7
0
 /**
  * @Method open a existing pdf file
  * @Param path path of pdfFile
  * @Return returns true if the file was opened or false if file couldn't be opened
  */
 private bool open(DocumentData pdfDoc)
 {
     String path;
     pdfDoc.Docsignedpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)+ "\\"+ pdfDoc.Idoperation + "\\signed\\" + pdfDoc.Docname;
     if (pdfDoc.documentHasBeenSigned())
     {
         path = pdfDoc.Docpath + "-signed.pdf";
         //signedpath =
     }
     else
     {
         path = pdfDoc.Docpath;
     }
     if (File.Exists(pdfDoc.Docpath))
     {
         doc = new Document();
         reader = new PdfReader(path);
         fos = new FileStream(pdfDoc.Docsignedpath, FileMode.Create, FileAccess.Write);
         writer = PdfWriter.GetInstance(doc, fos);
         doc.Open();
         doc.AddDocListener(writer);
         return true;
     }
     return false;
 }