private void WritePositioningOperator(PdfCanvas canvas) { if (firstPositioningOperands != null) { if ("T*".Equals(prevOperator)) { if (canvas.GetGraphicsState().GetLeading() != currLeading) { canvas.SetLeading((float)currLeading); } } PdfCleanUpProcessor.WriteOperands(canvas, firstPositioningOperands); } else { if (tdShift != null) { canvas.MoveText(tdShift[0], tdShift[1]); } else { if (tmShift != null) { canvas.SetTextMatrix(tmShift.Get(Matrix.I11), tmShift.Get(Matrix.I12), tmShift.Get(Matrix.I21), tmShift.Get (Matrix.I22), tmShift.Get(Matrix.I31), tmShift.Get(Matrix.I32)); } } } }
/// <summary>Cleans a page from the document by erasing all the areas which are provided or</summary> /// <param name="pageNumber">the page to be cleaned up</param> /// <param name="cleanUpLocations">the locations to be cleaned up</param> private void CleanUpPage(int pageNumber, IList <PdfCleanUpLocation> cleanUpLocations) { if (cleanUpLocations.Count == 0) { return; } IList <Rectangle> regions = new List <Rectangle>(); foreach (PdfCleanUpLocation cleanUpLocation in cleanUpLocations) { regions.Add(cleanUpLocation.GetRegion()); } PdfPage page = pdfDocument.GetPage(pageNumber); PdfCleanUpProcessor cleanUpProcessor = new PdfCleanUpProcessor(regions, pdfDocument); cleanUpProcessor.SetFilteredImagesCache(filteredImagesCache); cleanUpProcessor.ProcessPageContent(page); if (processAnnotations) { cleanUpProcessor.ProcessPageAnnotations(page, regions, redactAnnotations != null); } PdfCanvas pageCleanedContents = cleanUpProcessor.PopCleanedCanvas(); page.Put(PdfName.Contents, pageCleanedContents.GetContentStream()); page.SetResources(pageCleanedContents.GetResources()); ColorCleanedLocations(pageCleanedContents, cleanUpLocations); }
/// <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 static bool CreateAndCompareImages(PdfObject firstCs, PdfObject secondCs) { PdfImageXObject firstImage = CreateImageWithCs(firstCs); PdfImageXObject secondImage = CreateImageWithCs(secondCs); bool compareFirstToSecondResult = PdfCleanUpProcessor.AreColorSpacesDifferent(firstImage, secondImage); bool compareSecondToFirstResult = PdfCleanUpProcessor.AreColorSpacesDifferent(secondImage, firstImage); if (compareFirstToSecondResult != compareSecondToFirstResult) { throw new InvalidOperationException("The comparing of CS shall be a commutative operation."); } return(compareFirstToSecondResult); }
private void WriteText(String @operator, IList <PdfObject> operands, PdfArray cleanedText, PdfCanvas canvas ) { CanvasGraphicsState canvasGs = canvas.GetGraphicsState(); bool newLineShowText = "'".Equals(@operator) || "\"".Equals(@operator); if (newLineShowText) { if (canvasGs.GetLeading() != currLeading) { canvas.SetLeading((float)currLeading); } // after new line operator, removed text shift doesn't matter removedTextShift = null; } PdfTextArray tjShiftArray = null; if (removedTextShift != null) { float tjShift = (float)removedTextShift * 1000 / (canvasGs.GetFontSize() * canvasGs.GetHorizontalScaling() / 100); tjShiftArray = new PdfTextArray(); tjShiftArray.Add(new PdfNumber(tjShift)); } if (cleanedText != null) { if (newLineShowText) { // char spacing and word spacing are set via writeNotAppliedTextStateParams() method canvas.NewlineText(); } if (removedTextShift != null) { tjShiftArray.AddAll(cleanedText); cleanedText = tjShiftArray; } canvas.ShowText(cleanedText); } else { if (removedTextShift != null) { canvas.ShowText(tjShiftArray); } PdfCleanUpProcessor.WriteOperands(canvas, operands); } }
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(); }
// concatenation of two any TD, Td, T* result in Td private void StorePositioningInfoInShiftFields() { if ("Tm".Equals(prevOperator)) { tmShift = PdfCleanUpProcessor.OperandsToMatrix(firstPositioningOperands); } else { if ("T*".Equals(prevOperator)) { tdShift = new float[] { 0, -GetCurrLeading() }; } else { tdShift = new float[2]; tdShift[0] = ((PdfNumber)firstPositioningOperands[0]).FloatValue(); tdShift[1] = ((PdfNumber)firstPositioningOperands[1]).FloatValue(); } } firstPositioningOperands = null; }
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(); } } }