We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Replacing text in a PDF using Aspose.Pdf.Kit?

Hi,


My requirement is to replace the password in a pdf with “". What I want is a way to get this done using Pdf.Kit only because I have only the Kit license :slight_smile:

I’m sharing the codes for other’s reference. Please help me on getting this done. Thanks a lot.

Sajith

I tried the following methods with Aspose.Pdf (Demo) and Aspose.Pdf.Kit
  1. Just replaced the direct password with "” using Pdf.Kit - Replacing works fine for simpler PDFs using Pdf.Kit but it fails for my pdf which contains tables/images/text splits/paragraphs etc… (bit complex one)
  2. Then I tried Aspose.Pdf (full version) and it works fine for the direct word replacement for my complex pdf (But this is still not applicable for me since this doesn’t work when the password is different)
  3. Then I tried to find the X,Y coordinates of the word “Password” and inserted a text on top of the existing text. It worked fine and that’s what i wanted to get done exactly when looking at the final pdf. But this method is still not viable since it was done with the full demo version. I saw a thread saying that the X,Y coordinates support is not there yet in Pdf.Kit :frowning: Is there any other way to do somewhat same in Kit?
  4. Is there any other free library to get this done? please show a location to download the dll.

Code for Option 1
public void ReplaceKit()
{
// create an object of PdfContentEditor
PdfContentEditor editor = new PdfContentEditor();
// bind the source PDF document
editor.BindPdf(@“D:\Projects\existing.pdf”);
// specify the source string that needs to be replaced with target string
editor.ReplaceText(“demo123”, “");
// save the updated PDF document
editor.Save(@“D:\Projects\new.pdf”);
}

Code for Option 2
public void ReplaceFullVersion()
{
//open input PDF
Aspose.Pdf.Facades.PdfContentEditor pdfContentEditor = new Aspose.Pdf.Facades.PdfContentEditor();
pdfContentEditor.BindPdf(@“D:\Projects\existing.pdf”);
//replace text on all pages
pdfContentEditor.ReplaceText(“demo123:”, "”);
//save output PDF
pdfContentEditor.Save(@“D:\Projects\new.pdf”);
}

Code for Option 3
//-----------------------------------------------------------------------------------------------------------
// This method finds the phrase “Password” from the document and replaces with “Password: **********”
// The function works as follows
// # Finds the location of the phrases “Password” from the whole document and locates the X and Y cordinates
// # Font settings are setup such as size=10, font=“CenturyGothic”, background=“White” and Foreground=“Black”
// # This is NOT REPLACING but HIDING the actual text
//------------------------------------------------------------------------------------------------------------
public void HideUsingFullVersion()
{
//open document
Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(@“D:\Projects\existing.pdf”);
//create TextAbsorber object to find all instances of the input search phrase
Aspose.Pdf.Text.TextFragmentAbsorber textFragmentAbsorber = new Aspose.Pdf.Text.TextFragmentAbsorber(“Password”);
//accept the absorber for all the pages
pdfDocument.Pages.Accept(textFragmentAbsorber);
//get the extracted text fragments
Aspose.Pdf.Text.TextFragmentCollection textFragmentCollection = textFragmentAbsorber.TextFragments;

//get the password page
Aspose.Pdf.Page pdfPage = (Aspose.Pdf.Page)pdfDocument.Pages[1];
//Create text fragment
Aspose.Pdf.Text.TextFragment password = new Aspose.Pdf.Text.TextFragment(“Password: *******”);

//Set text properties
password.TextState.FontSize = 10;
password.TextState.Font = Aspose.Pdf.Text.FontRepository.FindFont(“CenturyGothic”);
password.TextState.BackgroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.White);
password.TextState.ForegroundColor = Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Black);

//loop through the fragments
foreach (Aspose.Pdf.Text.TextFragment textFragment in textFragmentCollection)
{
foreach (Aspose.Pdf.Text.TextSegment textSegment in textFragment.Segments)
{
//Determine the position of the phrase
password.Position = new Aspose.Pdf.Text.Position(textSegment.Position.XIndent, textSegment.Position.YIndent);
//Create TextBuilder object
Aspose.Pdf.Text.TextBuilder textBuilder = new Aspose.Pdf.Text.TextBuilder(pdfPage);

//Append the text fragment to the PDF page
textBuilder.AppendText(password);
}
}

pdfDocument.Save(@“D:\Projects\new.pdf”);
}

Hi Sajith,


Thanks for contacting support and sorry for the delayed response.

Aspose.Pdf.Kit for .NET has been discontinued as a separate product since July-2011 and all its classes and enumerations are now present under Aspose.Pdf.Facades namespace. Please note that we might not be able to fix the issues or introduce new features/enhancements in legacy Aspose.Pdf.Kit for .NET. As per your requirement or getting XY coordinates of particular text string, please continue using TextFragmentAbsorber class as specified in Code for option 3 above. In case you encounter any problem, please share the source PDF file so that we can test the scenario at our end.

In order to test the product without any limitations, you may consider requesting a 30 days temporary license. For further information, please visit Get a temporary license

Thanks a lot for the information :slight_smile: