Replace text in text objects

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
Pegasus
Posts: 3
Joined: Mon Aug 27 2007

Replace text in text objects

Post by Pegasus »

Hello,

Two questions here:

1) We want to translate an English PDF file to a foreign language. What we want specifically is to replace the English text (word) in each text object to the foreign text (word).

We have purchased licenses for "acPDFCreator.Net.dll" and "acPDFCreatorLib.Net.dll" (version 2.0). Will these two .dlls be sufficient for this purpose?

2) I've got sample code segment about the syntax of replacing text as below:

----------------------------------------------------------------------------
Object[] myObjArray = (Object []) axPDF.get_ObjectAttribute ("Pages[1]", "Objects");

foreach (ACPDFCREACTIVEX.acObject obj in myObjArray)
{

//Get the type of Attribute
ACPDFCREACTIVEX.IacAttribute attribute = (ACPDFCREACTIVEX.IacAttribute ) obj.get_Evaluate ("ObjectType");

//Check if it is a text object
if (Int32.Parse(attribute.Value.ToString()) == 6)
{
MessageBox.Show ("acObjectTypeText found","Amyuni PDF Creator");

//Retrieves value of object found
attribute = (ACPDFCREACTIVEX.IacAttribute) obj.get_Evaluate ("Text");

//text property is not empty and exist
if(attribute.Value != null)
{

//Retrieve text value
MessageBox.Show (attribute.Value.ToString());

//change the text value to the new text
attribute.value = "New text";

axPDF.DoCommandTool(ACPDFCREACTIVEX.CommandToolConstants.acCommandToolRunDocument);

}
}
}

----------------------------------------------------------------------------

Anyone can tell me the object type of "axPDF" on the first line? How to derive this object from IacDocument object?

Thanks!
Jose
Amyuni Team
Posts: 553
Joined: Tue Oct 01 2002
Contact:

Post by Jose »

Hello,

In the code snippet above the axPDF object is the name of the PDF Creator which is being hosted on a form.

Hope that helps?
Pegasus
Posts: 3
Joined: Mon Aug 27 2007

Post by Pegasus »

Thanks Jose!


This is Emily. After I received your sample code, I learned how to do this, so I wanted to remove my question from the forum. However, I wasn't able to delete my question.

-Emily
Joan
Amyuni Team
Posts: 2799
Joined: Wed Sep 11 2002
Contact:

Post by Joan »

Hello Emily,

Threads can't be deleted from the forum. Please post the code you received from Jose so other users having the same requirement will be ableto use your code.

Thank you.
Pegasus
Posts: 3
Joined: Mon Aug 27 2007

Re: Replace text in text objects

Post by Pegasus »

Here's the sample code for replacing text in text objects in .NET (that Jose sent to me):

private void btnLoopObjLib_Click(object sender, EventArgs e)

{

//initailize library
//This should be done once
acPDFCreatorLib.Initialize();

//set license key
acPDFCreatorLib.SetLicenseKey ( "Amyuni .Net Eval Version","07EFC....AC86803E0" );

// Open the PDF document file.
string strFilePathAndName = Application.StartupPath + "\\Source_Files\\fivepage.pdf";

//need to open a file stream
System.IO.FileStream fs = new System.IO.FileStream(strFilePathAndName, FileMode.Open, FileAccess.Read, FileShare.Read);

//Create Document Object
Amyuni.PDFCreator.IacDocument PdfDoc = new Amyuni.PDFCreator.IacDocument(null);

PdfDoc.Open(fs, "");

//Get page count
int intPageCount = PdfDoc.PageCount;

//Loop through all the Pages
for (int counter = 1; counter <= intPageCount; counter++)
{
try
{

//Declare ArrayList and fill it with all of the objects in the
//current PDF document
ArrayList arList = (ArrayList)PdfDoc.GetPage(counter).Attribute("Objects").Value;

foreach (Amyuni.PDFCreator.IacObject obj in arList)
{

//Check if it an text object
if (obj.Attribute("ObjectType").Value.ToString() == "5")
{
obj.Attribute("Text").Value = "New Value";

//or
//MessageBox.Show(obj.Attribute("Text").Value);
}
}
}
catch (Exception ev)
{
MessageBox.Show(ev.Message);
}
}

/*********************************************************************/

// Open the PDF document file.
string strFilePathAndNameSave = Application.StartupPath + "\\Resulting_Files\\Save_fivepage_add_text.pdf";

//save doc to new file
System.IO.FileStream fs2 = new System.IO.FileStream(strFilePathAndNameSave,
FileMode.Create,
FileAccess.Write,
FileShare.Read);

PdfDoc.Save(fs2, Amyuni.PDFCreator.IacFileSaveOption.acFileSaveAll);

//end and cleanup
//this should only be done once
acPDFCreatorLib.Terminate();
}
}
Post Reply