The following code sample shows how developers can replace multiple text in a PDF file in C# and PHP using Aspose.Pdf for Cloud API in their applications. Developers can use Aspose REST API with any language: .NET, Java, PHP, Ruby, Rails, Python, jQuery and many more.
[C# Code Sample]
AsposeApp.AppSID = "77***********************************";
AsposeApp.AppKey = "9a*******************************";
//build URI
stringstrURI = "http://api.aspose.com/v1.1/pdf/test.pdf/replaceTextList";
//sign URI
stringsignedURI = Utils.Sign(strURI);
//build JSON to post
TextReplace textReplace1 = new TextReplace();
textReplace1.OldValue = "[!firm!]";
textReplace1.NewValue = "Aspose";
textReplace1.Regex = "false";
TextReplace textReplace2 = new TextReplace();
textReplace2.OldValue = "[!client!]";
textReplace2.NewValue = "Mark";
textReplace2.Regex = "false";
MultipleTextReplacestextReplaces = new MultipleTextReplaces();
textReplaces.TextReplaces = new TextReplace[] { textReplace1, textReplace2 };
stringstrJSON = JsonConvert.SerializeObject(textReplaces);
Console.Write(strJSON);
Utils.ProcessCommand(signedURI, "POST", strJSON);
strURI = "http://api.aspose.com/v1.1/storage/file/test.pdf";
signedURI = Utils.Sign(strURI);
Stream responseStream = Utils.ProcessCommand(signedURI, "GET");
using (Stream fileStream = System.IO.File.OpenWrite(@"test.pdf"))
{
Utils.CopyStream(responseStream, fileStream);
}
responseStream.Close();
//Following are required classes
public class MultipleTextReplaces
publicTextReplace[] TextReplaces { get; set; }
public class TextReplace
publicTextReplace() { }
public string OldValue { get; set; }
public string NewValue { get; set; }
public string Regex { get; set; }
[PHP Code Sample]
use Aspose\Cloud\Common\AsposeApp;
use Aspose\Cloud\Common\Utils;
use Aspose\Cloud\Common\Product;
AsposeApp::$appSID = "77******-1***-4***-a***-80**********";
AsposeApp::$appKey = "********************************";
$filePath = getcwd() . "/Input/test.pdf";
$fileName = basename($filePath);
$oldText1 = "[!firm!]";
$newText1 = "Aspose";
$oldText2 = "[!client!]";
$newText2 = "Mark";
$oldText3 = "[!transaction_date!]";
$newText3 = "01-01-2014";
echo "Uploading pdf file... <br/>";
$strURIRequest = "http://api.aspose.com/v1.1/storage/file/" . $fileName;
$signedURI = Utils::sign($strURIRequest);
echoUtils::uploadFileBinary($signedURI, $filePath);
echo "Pdf file has been uploaded successully<br/>";
echo "Replacing text...<br/>";
//Build JSON to post
$fieldsArray = array('TextReplaces'=>array(array('OldValue'=>$oldText1, 'NewValue'=>$newText1, 'Regex'=>'false'),
array('OldValue'=>$oldText2, 'NewValue'=>$newText2, 'Regex'=>'false'),
array('OldValue'=>$oldText3, 'NewValue'=>$newText3, 'Regex'=>'false')));
$json = json_encode($fieldsArray);
//Build URI to replace text
$strURI = "http://api.aspose.com/v1.1/pdf/" . $fileName . "/replaceTextList";
$signedURI = Utils::sign($strURI);
$responseStream = Utils::processCommand($signedURI, "POST", "json", $json);
//Save PDF file on server
$strURI = "http://api.aspose.com/v1.1/storage/file/" . $fileName;
$responseStream = Utils::processCommand($signedURI, "GET", "", "");
$outputPath = getcwd() . "/output/" . $fileName;
Utils::saveFile($responseStream, $outputPath);