PDF에서 링크 업데이트
다음 코드 스니펫은 Aspose.PDF.Drawing 라이브러리와 함께 작동합니다.
PDF 파일에서 링크 업데이트
PDF 파일에 하이퍼링크를 추가하는 방법에 대해 논의한 바와 같이, LinkAnnotation 클래스는 PDF 파일에 링크를 추가할 수 있게 해줍니다. PDF 파일 내에서 기존 링크를 가져오는 데 사용되는 유사한 클래스도 있습니다. 기존 링크를 업데이트해야 하는 경우 이 클래스를 사용하십시오. 기존 링크를 업데이트하려면:
PDF 파일을 로드합니다.
PDF 파일의 특정 페이지로 이동합니다.
GoToAction 객체의 Destination 속성을 사용하여 링크 대상을 지정합니다.
대상 페이지는 XYZExplicitDestination 생성자를 사용하여 지정됩니다.
동일한 문서의 페이지로 링크 대상 설정
다음 코드 스니펫은 PDF 파일에서 링크를 업데이트하고 그 대상을 문서의 두 번째 페이지로 설정하는 방법을 보여줍니다.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void UpdateLinkAnnotation ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Open PDF document
using ( var document = new Aspose . Pdf . Document ( dataDir + "UpdateLinks.pdf" ))
{
// Get the first link annotation from first page of document
var linkAnnot = ( Aspose . Pdf . Annotations . LinkAnnotation ) document . Pages [ 1 ]. Annotations [ 1 ];
// Modification link: change link destination
var goToAction = ( Aspose . Pdf . Annotations . GoToAction ) linkAnnot . Action ;
// Specify the destination for link object
// The first parameter is document object, second is destination page number.
// The 5ht argument is zoom factor when displaying the respective page. When using 2, the page will be displayed in 200% zoom
goToAction . Destination = new Aspose . Pdf . Annotations . XYZExplicitDestination ( 1 , 1 , 2 , 2 );
// Save PDF document
document . Save ( dataDir + "UpdateLinks_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void UpdateLinkAnnotation ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Open PDF document
using var document = new Aspose . Pdf . Document ( dataDir + "UpdateLinks.pdf" );
// Get the first link annotation from first page of document
var linkAnnot = ( Aspose . Pdf . Annotations . LinkAnnotation ) document . Pages [ 1 ]. Annotations [ 1 ];
// Modification link: change link destination
var goToAction = ( Aspose . Pdf . Annotations . GoToAction ) linkAnnot . Action ;
// Specify the destination for link object
// The first parameter is document object, second is destination page number.
// The 5ht argument is zoom factor when displaying the respective page. When using 2, the page will be displayed in 200% zoom
goToAction . Destination = new Aspose . Pdf . Annotations . XYZExplicitDestination ( 1 , 1 , 2 , 2 );
// Save PDF document
document . Save ( dataDir + "UpdateLinks_out.pdf" );
}
웹 주소로 링크 대상 설정
하이퍼링크를 웹 주소를 가리키도록 업데이트하려면 GoToURIAction 객체를 인스턴스화하고 이를 LinkAnnotation의 Action 속성에 전달합니다. 다음 코드 스니펫은 PDF 파일에서 링크를 업데이트하고 그 대상을 웹 주소로 설정하는 방법을 보여줍니다.
다른 PDF 파일로 링크 대상 설정
다음 코드 스니펫은 PDF 파일에서 링크를 업데이트하고 그 대상을 다른 PDF 파일로 설정하는 방법을 보여줍니다.
.NET Core 3.1
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void UpdateLinkAnnotation ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Open PDF document
using ( var document = new Aspose . Pdf . Document ( dataDir + "UpdateLinks.pdf" ))
{
var linkAnnot = ( Aspose . Pdf . Annotations . LinkAnnotation ) document . Pages [ 1 ]. Annotations [ 1 ];
var goToR = ( Aspose . Pdf . Annotations . GoToRemoteAction ) linkAnnot . Action ;
// Next line update destination, do not update file
goToR . Destination = new Aspose . Pdf . Annotations . XYZExplicitDestination ( 2 , 0 , 0 , 1.5 );
// Next line update file
goToR . File = new Aspose . Pdf . FileSpecification ( dataDir + "input.pdf" );
// Save PDF document
document . Save ( dataDir + "SetTargetLink_out.pdf" );
}
}
.NET 8
// For complete examples and data files, visit https://github.com/aspose-pdf/Aspose.PDF-for-.NET
private static void UpdateLinkAnnotation ()
{
// The path to the documents directory
var dataDir = RunExamples . GetDataDir_AsposePdf_LinksActions ();
// Open PDF document
using var document = new Aspose . Pdf . Document ( dataDir + "UpdateLinks.pdf" );
var linkAnnot = ( Aspose . Pdf . Annotations . LinkAnnotation ) document . Pages [ 1 ]. Annotations [ 1 ];
var goToR = ( Aspose . Pdf . Annotations . GoToRemoteAction ) linkAnnot . Action ;
// Next line update destination, do not update file
goToR . Destination = new Aspose . Pdf . Annotations . XYZExplicitDestination ( 2 , 0 , 0 , 1.5 );
// Next line update file
goToR . File = new Aspose . Pdf . FileSpecification ( dataDir + "input.pdf" );
// Save PDF document
document . Save ( dataDir + "SetTargetLink_out.pdf" );
}
LinkAnnotation 텍스트 색상 업데이트
링크 주석에는 텍스트가 포함되어 있지 않습니다. 대신, 텍스트는 주석 아래 페이지의 내용에 배치됩니다. 따라서 텍스트의 색상을 변경하려면 주석의 색상을 변경하려고 하지 말고 페이지 텍스트의 색상을 교체하십시오. 다음 코드 스니펫은 PDF 파일에서 링크 주석의 색상을 업데이트하는 방법을 보여줍니다.