Changing the zoom factor in a link destination
We create a file with specific /XYZ
destinations and we change the zoom factor of these destinations. These examples were written in the context of questions such as:
changezoomxyzdestination
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.annotations;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.PdfDictionary;
import com.itextpdf.kernel.pdf.PdfName;
import com.itextpdf.kernel.pdf.PdfArray;
import com.itextpdf.kernel.pdf.PdfNumber;
import java.io.File;
public class ChangeZoomXYZDestination {
public static final String DEST = "./target/sandbox/annotations/change_zoom_xyz_destination.pdf";
public static final String SRC = "./src/main/resources/pdfs/xyz_destination.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new ChangeZoomXYZDestination().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
PdfDictionary pageDict = pdfDoc.getPage(11).getPdfObject();
PdfArray annots = pageDict.getAsArray(PdfName.Annots);
// Loop over the annotations
for (int i = 0; i < annots.size(); i++) {
PdfDictionary annotation = annots.getAsDictionary(i);
if (PdfName.Link.equals(annotation.getAsName(PdfName.Subtype))) {
PdfArray d = annotation.getAsArray(PdfName.Dest);
if (d != null && d.size() == 5 && PdfName.XYZ.equals(d.getAsName(1))) {
// Change the zoom factor of the current link to 0
d.set(4, new PdfNumber(0));
}
}
}
pdfDoc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
namespace iText.Samples.Sandbox.Annotations
{
public class ChangeZoomXYZDestination
{
public static readonly String DEST = "results/sandbox/annotations/change_zoom_xyz_destination.pdf";
public static readonly String SRC = "../../../resources/pdfs/xyz_destination.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new ChangeZoomXYZDestination().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
PdfDictionary pageDict = pdfDoc.GetPage(11).GetPdfObject();
PdfArray annots = pageDict.GetAsArray(PdfName.Annots);
// Loop over the annotations
for (int i = 0; i < annots.Size(); i++)
{
PdfDictionary annotation = annots.GetAsDictionary(i);
if (PdfName.Link.Equals(annotation.GetAsName(PdfName.Subtype)))
{
PdfArray d = annotation.GetAsArray(PdfName.Dest);
if (d != null && d.Size() == 5 && PdfName.XYZ.Equals(d.GetAsName(1)))
{
// Change the zoom factor of the current link to 0
d.Set(4, new PdfNumber(0));
}
}
}
pdfDoc.Close();
}
}
}
xyzdestination
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.annotations;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.navigation.PdfDestination;
import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
import com.itextpdf.layout.Document;
import com.itextpdf.layout.element.AreaBreak;
import com.itextpdf.layout.element.Link;
import com.itextpdf.layout.element.Paragraph;
import java.io.File;
public class XYZDestination {
public static final String DEST = "./target/sandbox/annotations/xyz_destination.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new XYZDestination().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
for (int i = 0; i < 10; i++) {
doc.add(new Paragraph("Test"));
doc.add(new AreaBreak());
}
for (int i = 1; i < 11; i++) {
// Create a link destination to the page, specified in the 1st argument.
PdfDestination d = PdfExplicitDestination.createXYZ(pdfDoc.getPage(i), 36, 806, 0);
Paragraph c = new Paragraph(new Link("Goto page " + i, d));
doc.add(c);
}
doc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Navigation;
using iText.Layout;
using iText.Layout.Element;
namespace iText.Samples.Sandbox.Annotations
{
public class XYZDestination
{
public static readonly String DEST = "results/sandbox/annotations/xyz_destination.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new XYZDestination().ManipulatePdf(DEST);
}
protected void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfWriter(dest));
Document doc = new Document(pdfDoc);
for (int i = 0; i < 10; i++)
{
doc.Add(new Paragraph("Test"));
doc.Add(new AreaBreak());
}
for (int i = 1; i < 11; i++)
{
// Create a link destination to the page, specified in the 1st argument.
PdfDestination d = PdfExplicitDestination.CreateXYZ(pdfDoc.GetPage(i), 36, 806, 0);
Paragraph c = new Paragraph(new Link("Goto page " + i, d));
doc.Add(c);
}
doc.Close();
}
}
}
addopenaction
JAVA
JAVA
/*
This file is part of the iText (R) project.
Copyright (c) 1998-2023 Apryse Group NV
Authors: Apryse Software.
For more information, please contact iText Software at this address:
sales@itextpdf.com
*/
package com.itextpdf.samples.sandbox.stamper;
import com.itextpdf.kernel.pdf.PdfDocument;
import com.itextpdf.kernel.pdf.PdfPage;
import com.itextpdf.kernel.pdf.PdfReader;
import com.itextpdf.kernel.pdf.PdfWriter;
import com.itextpdf.kernel.pdf.navigation.PdfDestination;
import com.itextpdf.kernel.pdf.navigation.PdfExplicitDestination;
import java.io.File;
public class AddOpenAction {
public static final String SRC = "./src/main/resources/pdfs/hello.pdf";
public static final String DEST = "./target/sandbox/stamper/add_open_action.pdf";
public static void main(String[] args) throws Exception {
File file = new File(DEST);
file.getParentFile().mkdirs();
new AddOpenAction().manipulatePdf(DEST);
}
protected void manipulatePdf(String dest) throws Exception {
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
PdfPage page1 = pdfDoc.getPage(1);
float page1Height = page1.getPageSize().getHeight();
PdfDestination pdfDestination = PdfExplicitDestination.createXYZ(page1, 0, page1Height, 0.75f);
pdfDoc.getCatalog().setOpenAction(pdfDestination);
pdfDoc.close();
}
}
C#
C#
using System;
using System.IO;
using iText.Kernel.Pdf;
using iText.Kernel.Pdf.Navigation;
namespace iText.Samples.Sandbox.Stamper
{
public class AddOpenAction {
public static readonly String SRC = "../../../resources/pdfs/hello.pdf";
public static readonly String DEST = "results/sandbox/stamper/add_open_action.pdf";
public static void Main(String[] args)
{
FileInfo file = new FileInfo(DEST);
file.Directory.Create();
new AddOpenAction().ManipulatePdf(DEST);
}
protected internal virtual void ManipulatePdf(String dest)
{
PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(dest));
PdfPage page1 = pdfDoc.GetPage(1);
float page1Height = page1.GetPageSize().GetHeight();
PdfDestination pdfDestination = PdfExplicitDestination.CreateXYZ(page1, 0, page1Height, 0.75f);
pdfDoc.GetCatalog().SetOpenAction(pdfDestination);
pdfDoc.Close();
}
}
}