Replacing String within a PDF file

The PDF Creator .NET Library enables you to create secure PDF documents on the fly and to view, print and process existing PDF documents. If you have any questions about PDF Creator .Net, please post them here.
Post Reply
perkinelmer
Posts: 1
Joined: Wed Mar 09 2011

Replacing String within a PDF file

Post by perkinelmer » Thu Mar 10 2011

I have generated a PDF file using the Amyuni Evaluation Convertor. Now I want to replace certain strings within this generated PDf file.
But when i use the ObjectByName method to find the text to be replace it returns me a null value.

I cannot find the text which i need to replace within the document.

Can someone help me out regarding this??

Code:
acPDFCreatorLib.Initialize();
acPDFCreatorLib.SetLicenseKey("", "R34A6B.....");

FileStream file1 = new FileStream(@"C:\TestPDF.pdf", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite);

IacDocument doc = new IacDocument(null);

int intPageCount = doc.PageCount;

for (int counter = 1; counter <= intPageCount; counter++)
{
IacObject frame = doc.GetPage(counter).ObjectByName("{Company Name}"); ==> returns me a null value.

if (frame != null)
{
frame.Attribute("Text").Value = "Test";
frame.Refresh();
}
}

acPDFCreatorLib.Terminate();


Contents of PDF file:

Company Name: {Company Name}
Address: {Company Address}
Location, Room: {Location}
Serial Number: {Serial Number}
System Name: {System name}
Date of IQ/OQ: {Date}
PerkinElmer Operative: {CSE Name}
Job Number: {Job Number}
Recertification Period 12 months
ReQualification Due Date: {REDate}

Devteam
Posts: 115
Joined: Fri Oct 14 2005
Location: Montreal
Contact:

Re: Replacing String within a PDF file

Post by Devteam » Thu Mar 24 2011

GetObjectByName will not return an object based on the text that it contains but based on its name. Most objects do not have names, only form fields or annotations usually have names, or objects that have been created using PDF Creator can have names associated to them.

In order to implement what you are doing, you can loop through all the objects on the page and search/replace the strings you are looking for:

Code: Select all

IacPage page1 = document.GetPage (1);
Amyuni.PDFCreator.IacAttribute attribute = page1.AttributeByName ("Objects");
// listobj is an array list of objects
System.Collections.ArrayList listobj = (System.Collections.ArrayList) attribute.Value;

foreach ( object o in listobj )
{
    Amyuni.PDFCreator.IacObject iacObj=(Amyuni.PDFCreator.IacObject)o;
    MessageBox.Show ("Name: " + iacObj.Name + "  "+ "Type: "+iacObj. AttributeByName ("ObjectType").Value.ToString ());
}
Amyuni Development Team

Efficient and accurate conversion to PDF, XPS, PDF/A and more. Free trials at - http://www.amyuni.com

Post Reply