Dispose() public méthode

public Dispose ( ) : void
Résultat void
Exemple #1
0
        public HttpResponseMessage UpdatePdfDoc(UpdatePDF updatePDF)
        {
            HttpResponseMessage response = null;

            try
            {
                string ExistingVariableinPDF = updatePDF.ExistingVariableinPDF;
                string ReplacingVariable     = updatePDF.ReplacingVariable;
                string ReplacingDateVariable = updatePDF.ReplacingDateVariable;
                string sourceFile            = @updatePDF.sourceFile;
                Guid   guid = Guid.NewGuid();

                FileInfo fileInfo     = new FileInfo(sourceFile);
                string   tempfilename = Path.Combine(fileInfo.Directory.FullName, guid.ToString() + fileInfo.Extension);
                string   destFile     = @tempfilename;

                PdfReader  pReader       = new PdfReader(sourceFile);
                int        numberOfPages = pReader.NumberOfPages;
                FileStream fileStream    = new System.IO.FileStream(destFile, System.IO.FileMode.Create);
                stamper = new iTextSharp.text.pdf.PdfStamper(pReader, fileStream);
                PDFTextGetter(ExistingVariableinPDF, ReplacingVariable, StringComparison.CurrentCultureIgnoreCase, sourceFile, destFile);
                if (!String.IsNullOrEmpty(ReplacingDateVariable))
                {
                    PDFTextGetter(ReplacingDateVariable, DateTime.Now.DisplayWithSuffix(), StringComparison.CurrentCultureIgnoreCase, sourceFile, destFile);
                }

                stamper.Close();
                stamper.Dispose();
                pReader.Close();
                pReader.Dispose();
                fileStream.Close();
                fileInfo.Refresh();

                File.Delete(sourceFile);
                response = Request.CreateResponse(System.Net.HttpStatusCode.OK, guid.ToString());
            }
            catch (Exception ex)
            {
                response = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError);
                Logger.WriteLog("Exception while UpdatePdfDoc " + ex.Message + Environment.NewLine + ex.StackTrace);
            }

            // File.Move(tempfilename, sourceFile);
            return(response);
        }
Exemple #2
0
        void ExportToMultiplePDF(int index)
        {
            int finishedReports = 0;

            PdfStamper stamper = null;
            PdfReader reader = null;

                //Go through each student in the list

                foreach (var student in StudentList)
                {
                    string savePath = System.IO.Path.Combine(System.IO.Path.GetFullPath(textSavePath.Text),
                            string.Format("{0}.pdf",
                            index == 0
                            ? student.Name.Replace(' ', '_')
                            : student.NameReversed));

                    //Create FileStream for the template pdf
                    var templateStream = new FileStream(System.IO.Path.GetFullPath(textTempLocation.Text), FileMode.Open);

                    //Create FileStream for new student report pdf
                    var studentStream = new FileStream(savePath, FileMode.Create);

                    //Open existing pdf
                    reader = new PdfReader(templateStream);

                    //Create stamper to set the contents of the fields
                    stamper = new PdfStamper(reader, studentStream);

                    var form = stamper.AcroFields;

                    //Setting the text of the template to the stored info
                    form.SetField("Street", student.Address);
                    form.SetField("CityZipState", student.CityZipState);
                    form.SetField("ClassName", student.ClassName);
                    form.SetField("Comments", student.Comments);
                    form.SetField("Grade", student.Grade.ToString());
                    form.SetField("Critique", student.Critique);
                    form.SetField("HighSchool", student.HighSchool);
                    form.SetField("Session", student.Session);
                    form.SetField("Absences", student.SemesterTotalAbs.ToString());
                    form.SetField("Tardies", student.SemesterTotalTdy.ToString());
                    form.SetField("NetAbsences", student.SemesterNetAbs.ToString());
                    form.SetField("StuNameReversed", student.NameReversed);

                    form.SetField("Date", DateTime.Now.ToShortDateString());

                    //Flatten the fields that we do not want editable

                    stamper.FormFlattening = true;

                    stamper.PartialFormFlattening("StuNameReversed");
                    stamper.PartialFormFlattening("Street");
                    stamper.PartialFormFlattening("CityZipState");
                    stamper.PartialFormFlattening("HighSchool");
                    stamper.PartialFormFlattening("ClassName");
                    stamper.PartialFormFlattening("Session");
                    stamper.PartialFormFlattening("Absences");
                    stamper.PartialFormFlattening("Tardies");
                    stamper.PartialFormFlattening("NetAbsences");
                    stamper.PartialFormFlattening("Grade");
                    stamper.PartialFormFlattening("Critique");

                    stamper.Dispose();
                    reader.Dispose();

                    //reports progress to update status text on the bottom
                    bgProgressThread.ReportProgress(++finishedReports);

                }
        }