/// <summary> /// Clears the contents of a given page leaving header and footer. /// </summary> /// <param name="inputPdf">The pdf to modify.</param> /// <param name="outputPdf">The pdf created after modification.</param> /// <param name="pageNum">The page number to be cleared.</param> public static void ClearContents(string inputPdf, string outputPdf, int pageNum) { using (FileStream fs = new FileStream(outputPdf, FileMode.Create, FileAccess.Write)) { PdfReader reader = new PdfReader(inputPdf); PdfStamper stamper = new PdfStamper(reader, fs); //Get the reactangle leaving header and footer Rectangle mediabox = reader.GetPageSize(pageNum); float llx = mediabox.GetLeft(10f); float urx = mediabox.GetRight(10f); float lly = mediabox.GetBottom(10f) + 100f; // Leave footer float ury = mediabox.GetTop(10f) - 120f; //Leave header List <PdfCleanUpLocation> cleanUpLocations = new List <PdfCleanUpLocation> { new PdfCleanUpLocation(pageNum, new iTextSharp.text.Rectangle(llx, lly, urx, ury), BaseColor.WHITE) }; //Use the PdfCleanUpProcessor to clean the page PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper); cleaner.CleanUp(); stamper.Close(); reader.Close(); } }
/// <summary> /// Removes the pagination from the footer. /// </summary> /// <param name="inputPdf">The pdf to modify.</param> /// <param name="outputPdf">The pdf created with removed pagination.</param> public static void RemoveFooterPagination(string inputPdf, string outputPdf) { using (FileStream fs = new FileStream(outputPdf, FileMode.Create, FileAccess.Write)) { //TaggedPdfReaderTool reader = new TaggedPdfReaderTool(); //using (MemoryStream ms = new MemoryStream()) //{ // reader.ConvertToXml(new PdfReader(inputPdf), ms); //} // Not a tagged pdf PdfReader reader = new PdfReader(inputPdf); PdfStamper stamper = new PdfStamper(reader, fs); List <PdfCleanUpLocation> cleanUpLocations = new List <PdfCleanUpLocation>(); int n = reader.NumberOfPages; for (int i = 6; i <= n; i++) { cleanUpLocations.Add(new PdfCleanUpLocation(i, new iTextSharp.text.Rectangle(400, 0, 440, 40), BaseColor.WHITE)); } PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper); cleaner.CleanUp(); //If clean up does not work add a white image iTextSharp.text.Image image = iTextSharp.text.Image.GetInstance(new Bitmap(120, 20), BaseColor.WHITE); image.SetAbsolutePosition(400, 40); //Adds the image to the output pdf stamper.GetOverContent(1).AddImage(image, true); stamper.Close(); reader.Close(); } }
private void CleanUp(String input, String output, IList<PdfCleanUpLocation> cleanUpLocations) { DirectoryInfo outDir = new DirectoryInfo(OUTPUT_PATH); if (!outDir.Exists) { outDir.Create(); } PdfReader reader = new PdfReader(input); Stream fos = new FileStream(output, FileMode.Create); PdfStamper stamper = new PdfStamper(reader, fos); PdfCleanUpProcessor cleaner = (cleanUpLocations == null)? new PdfCleanUpProcessor(stamper) : new PdfCleanUpProcessor(cleanUpLocations, stamper); cleaner.CleanUp(); stamper.Close(); fos.Close(); reader.Close(); }
private void Redact(string inFile, string outFile, List <RectAndText> RATs) { using (Stream stream = new FileStream(inFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { PdfReader pdfReader = new PdfReader(stream); using (PdfStamper stamper = new PdfStamper(pdfReader, new FileStream(outFile, FileMode.OpenOrCreate))) { List <PdfCleanUpLocation> cleanUpLocations = new List <PdfCleanUpLocation>(); foreach (RectAndText RAT in RATs) { cleanUpLocations.Add(new PdfCleanUpLocation(RAT.Page, RAT.Rect, iTextSharp.text.BaseColor.WHITE)); } PdfCleanUpProcessor cleaner = new PdfCleanUpProcessor(cleanUpLocations, stamper); cleaner.CleanUp(); } } }