Velvasi L

Velvasi L

  • NA
  • 1
  • 4.4k

ITextsharp, pdfstamper

Jan 8 2014 4:29 AM
I have an application where I categorize patient records from different pdfs and remove unwanted billing ones and merge all the wanted ones together and write the page# from which each page of the combined pdf came from on top of it. For eg, if the first page of combined pdf comes from records9999 page 110 I try to write on the top left hand corner (records9999-page 110) so that I can easily track it. I am able to combine the wanted pages, but the stamping is not working for some reason, but it is working in another part. Your help would be greatly appreciated. Please find the below code. I have commented out as much as possible.


private void createReviewFiles() { try { objarraylist = new ArrayList(); if (dg_codedetails.Rows.Count > 0) { int pageOffset = 0; ArrayList master = new ArrayList(); int f = 0; Document document = null; PdfCopy writer = null; String pfilename = String.Empty; String pdfilename = String.Empty; String outputPdfPath = String.Empty; DirectoryInfo dirReview = new DirectoryInfo(dir.FullName + "\\" + "ReviewFolder");//creating new directory if (!dirReview.Exists) dirReview.Create(); outputPdfPath = dirReview + "\\" + "ReviewFiles.pdf"; foreach (DataGridViewRow dgr in dg_codedetails.Rows) { pfilename = dgr.Cells[0].Value.ToString();//pdf file name cells[0] pdfilename = dgr.Cells[7].Value.ToString();//pdf file name to be written in specific format cells[7] Int16 startvalue = Convert.ToInt16(dgr.Cells[2].Value);//cells 2 comes from grid, starting page Int16 endvalue = Convert.ToInt16(dgr.Cells[3].Value);//cells 2 comes from grid, ending page PdfReader reader = new PdfReader(dir.FullName + "\\" + pfilename); reader.ConsolidateNamedDestinations(); // we retrieve the total number of pages int n = reader.NumberOfPages; pageOffset += n; if (f == 0) { // step 1: creation of a document-object document = new Document(reader.GetPageSizeWithRotation(1)); // step 2: we create a writer that listens to the document writer = new PdfCopy(document, new FileStream(outputPdfPath, FileMode.Create)); // step 3: we open the document document.Open(); f += 1; } for (int i = startvalue; i <= endvalue; i++) { if (writer != null) { string text = "(" + pfilename.Replace(".pdf", "").Replace(".PDF", "") + "-page " + i + ")";//converting pdf name to specific format if (!objarraylist.Contains(text)) objarraylist.Add(text); PdfImportedPage page = writer.GetImportedPage(reader, i); writer.AddPage(page); } } PRAcroForm form = reader.AcroForm; if (form != null && writer != null) { writer.CopyAcroForm(reader); } } if (document != null) { document.Close(); } watermarkText(outputPdfPath, dirReview + "\\" + "ReviewPage.pdf"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } public void watermarkText(String inputpath,String outputpath) { try { PdfReader reader = new PdfReader(inputpath); Int64 pagecount = reader.NumberOfPages; int j = 0; string stringToWriteToPdf = String.Empty; using (MemoryStream memoryStream = new MemoryStream()) { String pfilename = String.Empty; PdfStamper pdfStamper = new PdfStamper(reader, memoryStream); int textcount = 0; for (int i = 1; i <= pagecount; i++) { j = i - 1; stringToWriteToPdf = objarraylist[j].ToString(); textcount = stringToWriteToPdf.Length; if (stringToWriteToPdf.Length > 30) { textcount += textcount + 5; } iTextSharp.text.Rectangle pageSize = reader.GetPageSizeWithRotation(i); PdfContentByte pdfPageContents = pdfStamper.GetOverContent(i); pdfPageContents.BeginText(); BaseFont baseFont = BaseFont.CreateFont(BaseFont.HELVETICA_BOLD, Encoding.ASCII.EncodingName, BaseFont.EMBEDDED); pdfPageContents.SetFontAndSize(baseFont, 8); pdfPageContents.SetRGBColorFill(255, 0, 0); pdfPageContents.MoveText(pageSize.Width - (pageSize.Width - 10), pageSize.Height - 20); pdfPageContents.ShowText(stringToWriteToPdf); pdfPageContents.EndText(); } pdfStamper.FormFlattening = true; pdfStamper.Close(); System.IO.File.WriteAllBytes(outputpath, memoryStream.ToArray()); } objarraylist.Clear(); Process pdfProcess = new Process(); pdfProcess.StartInfo.FileName = acro; pdfProcess.StartInfo.Arguments = " /A page=" + 1 + " " + " \"" + outputpath + " \""; pdfProcess.Start(); int iHandle1 = keyboard.FindWindow(null, "Mysoftware"); keyboard.SetForegroundWindow(iHandle1); } catch (Exception ex) { } } The stamper is correctly doing the stamping when I combine the unwanted pages together using the below code.
private void createNonreviewFiles() { try { if (dgvNonereviewed.Rows.Count > 0) { objarraylist = new ArrayList(); int pageOffset = 0; ArrayList master = new ArrayList(); int f = 0; Document document = null; PdfCopy writer = null; String pfilename = String.Empty; String pdfilename = String.Empty; String outputPdfPath = String.Empty; DirectoryInfo dirnonreview = new DirectoryInfo(dir.FullName + "\\" + "NonreviewFolder"); if (!dirnonreview.Exists) dirnonreview.Create(); outputPdfPath = dirnonreview + "\\" + "NonreviewFiles.pdf"; foreach (DataGridViewRow dgr in dgvNonereviewed.Rows) { pfilename = dgr.Cells[0].Value.ToString(); PdfReader reader = new PdfReader(dir1.FullName + "\\" + pfilename); reader.ConsolidateNamedDestinations(); // we retrieve the total number of pages int n = reader.NumberOfPages; pageOffset += n; if (f == 0) { document = new Document(reader.GetPageSizeWithRotation(1)); writer = new PdfCopy(document, new FileStream(outputPdfPath, FileMode.Create)); document.Open(); } int nonreviewpage = 0; for (int i = 1; i <= n; i++) { if (dictionary[f].ToString().Equals("N")) { nonreviewpage = i; } else { String[] str = dictionary[f].ToString().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); nonreviewpage = Convert.ToInt16(str[i - 1].ToString()); } if (writer != null) { string text = "(" + pfilename.Replace("_none_review.pdf", "").Replace("_none_review.PDF", "") + "-page " + nonreviewpage + ")"; if (!objarraylist.Contains(text)) objarraylist.Add(text); PdfImportedPage page = writer.GetImportedPage(reader, i); writer.AddPage(page); } } PRAcroForm form = reader.AcroForm; if (form != null && writer != null) { writer.CopyAcroForm(reader); } f += 1; } if (document != null) { document.Close(); } watermarkText(outputPdfPath, dirnonreview + "\\" + "NonreviewPages.pdf"); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }