HTML JPG PDF XML DOCX
  Product Family
PDF

.NET SDK を使用した PDF 内のリンクの置換

サーバーサイド .NET API を使用して PDF ドキュメント内のリンク注釈を置換します。

Get Started

クラウド .NET SDK を使用したリンクの置換方法

クラウド .NET SDK を使用してリンク注釈を置換するには、 Aspose.PDF Cloud .NET SDK このクラウド SDK を使用すると、C#、ASP.NET、その他の .NET 言語でさまざまなクラウド プラットフォーム向けのクラウド ベースの PDF 作成、編集、変換アプリを簡単に構築できます。開く NuGet パッケージ マネージャーで検索し、 Aspose.PDF Cloud をインストールします。パッケージ マネージャー コンソールから次のコマンドを使用することもできます。

パッケージ マネージャー コンソール コマンド


     
    PM> Install-Package Aspose.Pdf-Cloud
     
     

.NET SDK を使用したリンクの置換手順

Aspose.PDF Cloud 開発者は、わずか数行のコードで PDF 内のリンク注釈を簡単に読み込み、変更できます。

  1. アプリケーションのシークレットとキーを使用して新しい Configuration オブジェクトを作成します
  2. クラウド API に接続するオブジェクトを作成します
  3. ドキュメントファイルをアップロードします
  4. PutLinkAnnotationAsync 関数を使用して希望のプロパティを設定します
  5. 応答をチェックして結果を記録します
  6. 更新されたファイルをダウンロードしてローカルで使用します
 

このサンプルコードは、PDF ドキュメント内のリンク注釈を置換する方法を示しています


    using Aspose.Pdf.Cloud.Sdk.Model;

    namespace Links
    {
        public class LinksReplace
        {
            public static async Task Modify(string documentName, string outputName, string LinkID, string LinkAction, string remoteFolder)
            {
		// Get your AppSid and AppSecret from https://dashboard.aspose.cloud (free registration required). 
		pdfApi = new PdfApi(AppSecret, AppSid);

                using (var file = File.OpenRead(Path.Combine(localFolder, documentName)))
		{ // Upload the local PDF to cloud storage folder name.
                    FilesUploadResult uploadResponse = await pdfApi.UploadFileAsync(Path.Combine(remoteFolder, documentName), documentName);
                    Console.WriteLine(uploadResponse.Uploaded[0]);
                }

                // Extract Link annotation by Id
                LinkAnnotationResponse getResponse = await pdfApi.GetLinkAnnotationAsync(documentName, LinkID, folder: remoteFolder);

                if (getResponse == null)
                    Console.WriteLine("LinksReplace(): Unexpected error in GetLink!");
                else if (getResponse.Code < 200 || getResponse.Code > 299)
                    Console.WriteLine("LinksReplace(): Failed to receive link from the document.");
                else if (getResponse.Link == null)
                     Console.WriteLine("LinksReplace(): link '{0}' not found in the document '{1]'.", LinkID, documentName);
                else {
                     Console.WriteLine("LinksReplace(): link '{0}' successfully received from the document '{1}.", LinkID, documentName);
                     Console.WriteLine(getResponse.Link.ToString());

                     Link link = new Link(LinkAction);

                     LinkAnnotation newLink = new LinkAnnotation(
                         new List<Link>() { link },
                         ActionType: getResponse.Link.ActionType,
                         Action: LinkAction,
                         Highlighting: getResponse.Link.Highlighting,
                         Color: new Color(A: 0xFF, R: 0xAA, G: 0x00, B: 0x00),
                         Rect: getResponse.Link.Rect
                     );

                     // Replace a link annotation with LinkId in the PDF on cloud storage.
                     AsposeResponse response = await pdfApi.PutLinkAnnotationAsync(documentName, LinkID, newLink, folder: remoteFolder);

                    if (response == null)
                        Console.WriteLine("LinksReplace(): Unexpected error in Modify!");
                    else if (response.Code < 200 || response.Code > 299)
                        Console.WriteLine("LinksReplace(): Failed to replaced link in the document.");
                    else { // Downloads the updated file for local use.
                        Console.WriteLine("LinksReplace(): link '{0}' successfully replaced in the document '{1}.", LinkID, documentName);
                        Stream stream = pdfApi.DownloadFile(Path.Combine(remoteFolder, documentName));
                        using var fileStream = File.Create(Path.Combine(localFolder, "replace_linkk_" + outputName));
                        stream.Position = 0;
                        await stream.CopyToAsync(fileStream);
                        Console.WriteLine("LinksReplace(): File '{0}' successfully downloaded.", "replace_link_" + outputName);
                    }
                }
            }
        }
    }