チュートリアル
簡単にライブラリーを使用するためのチュートリアルコレクション
PDF にファイルや文書のハイパーリンクを組み込むことは、デジタル文書のインタラクティビティと実用性を高める強力な方法であり、静的なファイル以上の価値を持たせることができます。このようなリンクを埋め込むことで、関連ファイル、追加文書、または同じ文書内の特定のセクションに PDF コンテンツをシームレスに接続し、読者により充実した使いやすい体験を提供します。
本記事では、Spire.PDF for .NET を使用して、C# コードで PDF 文書にファイルおよび文書リンクを追加する方法を示します。
まず、Spire.PDF for .NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.PDF
PDF 文書におけるファイルリンク注釈は、ユーザーが PDF から外部ファイルに直接移動できるインタラクティブなリンクを指します。Spire.PDF では、ファイルリンク注釈を操作するための PdfFileLinkAnnotation クラスが提供されています。
以下の手順で PDF 文書にファイルリンク注釈を追加します。
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using System.Drawing;
namespace AddFileLinkAnnotation
{
class Program
{
static void Main(string[] args)
{
// PdfDocumentオブジェクトを作成
PdfDocument doc = new PdfDocument();
// PDFファイルを読み込む
doc.LoadFromFile("Sample.pdf");
// 特定のページを取得
PdfPageBase page = doc.Pages[0];
// ページに基づいてPdfTextFinderオブジェクトを作成
PdfTextFinder finder = new PdfTextFinder(page);
// 検索オプションを設定
finder.Options.Parameter = TextFindParameter.WholeWord;
finder.Options.Parameter = TextFindParameter.IgnoreCase;
// 指定されたテキストのインスタンスを検索
List fragments = finder.Find("貴金属");
// 最初のインスタンスを取得
PdfTextFragment textFragment = fragments[0];
// テキスト境界を取得
RectangleF textBound = textFragment.Bounds[0];
// ファイルリンク注釈を作成
PdfFileLinkAnnotation fileLinkAnnotation = new PdfFileLinkAnnotation(textBound, "G:/Documents/PreciousMetal.docx");
fileLinkAnnotation.Color = Color.Red;
fileLinkAnnotation.Border.Width = 2f;
// 注釈のコレクションに注釈を追加
page.Annotations.Add(fileLinkAnnotation);
// 結果をファイルに保存
doc.SaveToFile("output/PDFにファイルリンクを追加.pdf");
// リソースを解放
doc.Dispose();
}
}
}
PDF ファイルにおける文書リンク注釈は、ユーザーが同じ PDF 文書内の異なるページやセクションに移動できるハイパーリンクを指します。Spire.PDF では、文書リンク注釈を操作するための PdfDocumentLinkAnnotation クラスが提供されています。
以下の手順で PDF 文書に文書リンク注釈を追加します。
using Spire.Pdf.Annotations;
using Spire.Pdf.Texts;
using Spire.Pdf;
using Spire.Pdf.General;
using System.Drawing;
namespace AddDocumentLinkAnnotation
{
class Program
{
static void Main(string[] args)
{
// PdfDocumentオブジェクトを作成
PdfDocument doc = new PdfDocument();
// PDFファイルを読み込む
doc.LoadFromFile("Sample.pdf");
// 特定のページを取得
PdfPageBase page = doc.Pages[0];
// ページに基づいてPdfTextFinderオブジェクトを作成
PdfTextFinder finder = new PdfTextFinder(page);
// 検索オプションを設定
finder.Options.Parameter = TextFindParameter.WholeWord;
finder.Options.Parameter = TextFindParameter.IgnoreCase;
// 指定されたテキストのインスタンスを検索
List fragments = finder.Find("貴金属");
// 最初のインスタンスを取得
PdfTextFragment textFragment = fragments[0];
// テキスト境界を取得
RectangleF textBound = textFragment.Bounds[0];
// ドキュメントリンク注釈を作成
PdfDocumentLinkAnnotation documentLinkAnnotation = new PdfDocumentLinkAnnotation(textBound);
documentLinkAnnotation.Color = Color.Red;
documentLinkAnnotation.Border.Width = 2f;
// 注釈の目的地を設定
documentLinkAnnotation.Destination = new PdfDestination(doc.Pages[1]);
// 注釈のコレクションに注釈を追加
page.Annotations.Add(documentLinkAnnotation);
// 結果をファイルに保存
doc.SaveToFile("output/PDFにドキュメントリンクを追加.pdf");
// リソースを解放
doc.Dispose();
}
}
}
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
PDF のハイパーリンクは、読者が特定のページに簡単にアクセスできるようにする重要な機能です。 PDF ドキュメントにハイパーリンクを追加することで、ドキュメントに関する追加情報をより簡単に読者に提供したり、関連リソースに移動させたりすることができます。 読者がハイパーリンクをクリックすると、対応するページがブラウザで開かれ、読書体験を大きく向上させることができます。 この記事では、Spire.PDF for .NET を使用して、.NET を使用して PDF 内の既存のテキストにハイパーリンクを追加する方法について説明します。
まず、Spire.PDF for.NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.PDF
PDF ドキュメント内のハイパーリンクは、注釈要素としてページに挿入されます。PDF ドキュメント内の指定されたテキストにハイパーリンクを追加するには、まずテキストの位置を特定する必要があります。 テキストの位置を取得すると、指定されたテキストへのハイパーリンクの追加を実現するように、オブジェクトへの指定されたリンクを持つ PdfUriAnnotation を作成し、取得の位置に挿入することができます。 詳しい手順は以下のとおりです。
using Spire.Pdf;
using Spire.Pdf.Annotations;
using Spire.Pdf.Exporting.XPS.Schema;
using Spire.Pdf.General.Find;
using Spire.Pdf.Texts;
using System;
using System.Collections.Generic;
using System.Drawing;
using TextFindParameter = Spire.Pdf.Texts.TextFindParameter;
namespace ChangeHyperlink
{
internal class Program
{
static void Main(string[] args)
{
//PdfDocumentのオブジェクトを作成する
PdfDocument pdf = new PdfDocument();
//PDFファイルを読み込む
pdf.LoadFromFile("サンプル.pdf");
//ドキュメントの最初のページを取得する
PdfPageBase page = pdf.Pages[0];
//PdfTextFinderオブジェクトを作成し、ファインダーのオプションを設定する
PdfTextFinder finder = new PdfTextFinder(page);
finder.Options.Parameter = TextFindParameter.WholeWord;
//ページ上の指定されたテキストを検索し、2番目の結果を取得する
List collection = finder.Find("気候変動");
PdfTextFragment fragment = collection[1];
//2番目の検索結果のテキスト範囲をループする
foreach (RectangleF bounds in fragment.Bounds)
{
//PdfUriAnnotationのオブジェクトを作成する
PdfUriAnnotation url = new PdfUriAnnotation(bounds);
//リンクを設定する
url.Uri = "https://www.jma.go.jp/jma/kishou/know/whitep/3-1.html";
//枠線を設定する
url.Border = new PdfAnnotationBorder(1f);
//枠線の色を設定する
url.Color = Color.Blue;
//ページにハイパーリンクを挿入する
page.AnnotationsWidget.Add(url);
}
//PDFファイルを保存する
pdf.SaveToFile("ハイパーリンクの追加.pdf");
pdf.Dispose();
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Annotations
Imports Spire.Pdf.Exporting.XPS.Schema
Imports Spire.Pdf.General.Find
Imports Spire.Pdf.Texts
Imports System
Imports System.Collections.Generic
Imports System.Drawing
Imports TextFindParameter = Spire.Pdf.Texts.TextFindParameter
Namespace ChangeHyperlink
Friend Class Program
Shared Sub Main(ByVal args() As String)
'PdfDocumentのオブジェクトを作成する
Dim pdf As PdfDocument = New PdfDocument()
'PDFファイルを読み込む
pdf.LoadFromFile("サンプル.pdf")
'ドキュメントの最初のページを取得する
Dim page As PdfPageBase = pdf.Pages(0)
'PdfTextFinderオブジェクトを作成し、ファインダーのオプションを設定する
Dim finder As PdfTextFinder = New PdfTextFinder(page)
finder.Options.Parameter = TextFindParameter.WholeWord
'ページ上の指定されたテキストを検索し、2番目の結果を取得する
Dim collection As List= finder.Find("気候変動")
Dim fragment As PdfTextFragment = collection(1)
'2番目の検索結果のテキスト範囲をループする
Dim bounds As RectangleF
For Each bounds In fragment.Bounds
'PdfUriAnnotationのオブジェクトを作成する
Dim url As PdfUriAnnotation = New PdfUriAnnotation(bounds)
'リンクを設定する
url.Uri = "https://www.jma.go.jp/jma/kishou/know/whitep/3-1.html"
'枠線を設定する
url.Border = New PdfAnnotationBorder(1.0F)
'枠線の色を設定する
url.Color = Color.Blue
'ページにハイパーリンクを挿入する
page.AnnotationsWidget.Add(url)
Next
'PDFファイルを保存する
pdf.SaveToFile("ハイパーリンクの追加.pdf")
pdf.Dispose()
End Sub
End Class
End Namespace
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。
PDF ドキュメント内のハイパーリンクを使用することで、ユーザーはページにジャンプしたりドキュメントを開いたりすることができ、PDF ファイルはよりインタラクティブで使いやすくなります。しかし、リンクの対象サイトが変更された場合や、リンクが誤ったページを指している場合は、ドキュメントのユーザーにトラブルや誤解を招く可能性があります。そのため、PDF ドキュメント内の誤ったまたは無効なハイパーリンクを変更または削除することは、ハイパーリンクの正確性と使いやすさを確保するために非常に重要であり、ユーザーにとってより良い読書体験を提供することができます。この記事では、Spire.PDF for .NET を使用して、.NET プログラムを介して PDF ドキュメント内のハイパーリンクを変更または削除する方法を紹介します。
まず、Spire.PDF for.NET パッケージに含まれている DLL ファイルを .NET プロジェクトの参照として追加する必要があります。DLL ファイルは、このリンクからダウンロードするか、NuGet を介してインストールできます。
PM> Install-Package Spire.PDF
PDF ページ上のハイパーリンクの URL を変更するには、ハイパーリンク注釈ウィジェットを取得し、PdfUriAnnotationWidget.Uri プロパティを使用して URL を再設定する必要があります。詳しい手順は以下の通りです。
using Spire.Pdf;
using Spire.Pdf.Annotations;
using System;
namespace ChangeHyperlink
{
internal class Program
{
static void Main(string[] args)
{
//PdfDocumentのオブジェクトを作成する
PdfDocument pdf = new PdfDocument();
//PDFファイルを読み込む
pdf.LoadFromFile("サンプル.pdf");
//最初のページを取得する
PdfPageBase page = pdf.Pages[0];
//最初のハイパーリンクを取得する
PdfUriAnnotationWidget url = (PdfUriAnnotationWidget)page.AnnotationsWidget[0];
//このハイパーリンクのURLを再設定する
url.Uri = "https://www.jma.go.jp/jma/kishou/know/whitep/3-1.html";
//PDFファイルを保存する
pdf.SaveToFile("ハイパーリンクの変更.pdf");
pdf.Dispose();
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Annotations
Imports System
Namespace ChangeHyperlink
Friend Class Program
Shared Sub Main(ByVal args() As String)
'PdfDocumentのオブジェクトを作成する
Dim pdf As PdfDocument = New PdfDocument()
'PDFファイルを読み込む
pdf.LoadFromFile("サンプル.pdf")
'最初のページを取得する
Dim page As PdfPageBase = pdf.Pages(0)
'最初のハイパーリンクを取得する
Dim url As PdfUriAnnotationWidget = CType(page.AnnotationsWidget(0), PdfUriAnnotationWidget)
'このハイパーリンクのURLを再設定する
url.Uri = "https://www.jma.go.jp/jma/kishou/know/whitep/3-1.html"
'PDFファイルを保存する
pdf.SaveToFile("ハイパーリンクの変更.pdf")
pdf.Dispose()
End Sub
End Class
End Namespace
Spire.PDF for .NET は、PdfPageBase.AnnotationsWidget.RemoveAt() メソッドを提供し、インデックスによって PDF ページ上のハイパーリンクを削除します。PDF ドキュメントからすべてのハイパーリンクを削除するには、ページを繰り返し、各ページの注釈ウィジェットを取得し、注釈が PdfUriAnnotationWidget クラスのインスタンスであるかどうかを確認し、もしそうであれば注釈を削除する必要があります。以下に詳細な手順を示します。
using Spire.Pdf;
using Spire.Pdf.Annotations;
using System;
using System.Dynamic;
namespace DeleteHyperlink
{
internal class Program
{
static void Main(string[] args)
{
//PdfDocumentのオブジェクトを作成する
PdfDocument pdf = new PdfDocument();
//PDFファイルを読み込む
pdf.LoadFromFile("サンプル.pdf");
//最初のページの2番目のハイパーリンクを削除する
//PdfPageBase page = pdf.Pages[0];
//page.AnnotationsWidget.RemoveAt(1);
//ドキュメント内のすべてのハイパーリンクを削除する
//ドキュメント内のページをループする
foreach (PdfPageBase page in pdf.Pages)
{
//ページ内の注釈のコレクションを取得する
PdfAnnotationCollection collection = page.AnnotationsWidget;
for (int i = collection.Count - 1; i >= 0; i--)
{
PdfAnnotation annotation = collection[i];
//注釈がPdfUriAnnotationWidgetのインスタンスであるかどうかを判定する
if (annotation is PdfUriAnnotationWidget)
{
PdfUriAnnotationWidget url = (PdfUriAnnotationWidget)annotation;
//ハイパーリンクを削除する
collection.Remove(url);
}
}
}
//ドキュメントを保存する
pdf.SaveToFile("ハイパーリンクの削除.pdf");
pdf.Dispose();
}
}
}
Imports Spire.Pdf
Imports Spire.Pdf.Annotations
Imports System
Imports System.Dynamic
Namespace DeleteHyperlink
Friend Class Program
Shared Sub Main(ByVal args() As String)
'PdfDocumentのオブジェクトを作成する
Dim pdf As PdfDocument = New PdfDocument()
'PDFファイルを読み込む
pdf.LoadFromFile("サンプル.pdf")
'最初のページの2番目のハイパーリンクを削除する
'PdfPageBase page = pdf.Pages[0];
'page.AnnotationsWidget.RemoveAt(1);
'ドキュメント内のすべてのハイパーリンクを削除する
'ドキュメント内のページをループする
Dim page As PdfPageBase
For Each page In pdf.Pages
'ページ内の注釈のコレクションを取得する
Dim collection As PdfAnnotationCollection = page.AnnotationsWidget
Dim i As Integer
For i = collection.Count - 1 To 0 Step i - 1
Dim annotation As PdfAnnotation = collection(i)
'注釈がPdfUriAnnotationWidgetのインスタンスであるかどうかを判定する
If TypeOf annotation Is PdfUriAnnotationWidget Then
Dim url As PdfUriAnnotationWidget = CType(annotation, PdfUriAnnotationWidget)
'ハイパーリンクを削除する
collection.Remove(url)
End If
Next
Next
'ドキュメントを保存する
pdf.SaveToFile("ハイパーリンクの削除.pdf")
pdf.Dispose()
End Sub
End Class
End Namespace
結果ドキュメントから評価メッセージを削除したい場合、または機能制限を取り除く場合は、についてこのメールアドレスはスパムボットから保護されています。閲覧するにはJavaScriptを有効にする必要があります。にお問い合わせ、30 日間有効な一時ライセンスを取得してください。