CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2014

    C# and PHP Code Samples for Replacing Multiple Texts inside PDF File in Cloud

    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";
    //build JSON to post
    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);
    Utils.ProcessCommand(signedURI, "POST", strJSON);
    //build URI
    strURI = "http://api.aspose.com/v1.1/storage/file/test.pdf";
    //sign URI
    signedURI = Utils.Sign(strURI);
    Stream responseStream = Utils.ProcessCommand(signedURI, "GET");
    using (Stream fileStream = System.IO.File.OpenWrite(@"test.pdf"))
    Utils.CopyStream(responseStream, fileStream);
    //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";
    //build URI
    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
    //build URI
    $strURI = "http://api.aspose.com/v1.1/storage/file/" . $fileName;
    //sign URI
    $signedURI = Utils::sign($strURI);
    $responseStream = Utils::processCommand($signedURI, "GET", "", "");
    $outputPath = getcwd() . "/output/" . $fileName;
    Utils::saveFile($responseStream, $outputPath);
    echo "The text has been replaced and Pdf file has saved at: " . $outputPath;

  2. #2
    Join Date
    Apr 2014

    Re: C# and PHP Code Samples for Replacing Multiple Texts inside PDF File in Cloud


    I'm programming with C# ,and I got a problem with below codes.Can someone help me out?
    //open document
    Document pdfDocument = new Document(“input.pdf”);
    //create TextAbsorber object to find all instances of the input search phrase
    TextFragmentAbsorbertextFragmentAbsorber = new TextFragmentAbsorber(“Figure”);
    //accept the absorber for all the pages
    //get the extracted text fragments
    TextFragmentCollectiontextFragmentCollection = textFragmentAbsorber.TextFragments;
    //loop through the fragments
    foreach (TextFragmenttextFragment in textFragmentCollection)
    //update text and other properties
    textFragment.Text = “New Phrase”;
    textFragment.TextState.Font = FontRepository.FindFont(“Verdana”);
    textFragment.TextState.FontSize = 22;
    textFragment.TextState.ForegroundColor =Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Blue);
    textFragment.TextState.BackgroundColor =Aspose.Pdf.Color.FromRgb(System.Drawing.Color.Green);
    Another question oes Aspose or some other free DPF component will simplify this procedure?

    all the best.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.