iText
を使用してズームレベルを変更するコードは、で与えられます。iText home pageC#
に変換したいと思います。数え切れないほどの時間を経て、私は最終的にコードを書き換えて、リンクを変更していないことを知りました。それは私が間違いをしたに違いないことを意味する。PDF内のすべてのリンク先のズームレベルを変更する
EDIT:
要求としては、simple PDF exampleを見てください。次のように
私のコードは次のとおりです。
using (var reader = new PdfReader(input))
{
using (var stamper = new PdfStamper(reader, ms))
{
for (int i = 1; i <= reader.NumberOfPages; i++)
{
// Get a page of a PDF page
PdfDictionary page = reader.GetPageN(i);
// Get all the annotations of page i
PdfArray annotsArray = page.GetAsArray(PdfName.ANNOTS);
// If page does not have annotations
if (annotsArray == null)
{
continue;
}
// For each annotation
for (int j = 0; j < annotsArray.Size; j++)
{
// for current annotation
PdfDictionary annotation = annotsArray.GetAsDict(j);
// test if it is LINK
PdfDictionary annotationAction = annotation.GetAsDict(PdfName.A);
if (annotationAction == null || PdfName.LINK.Equals(annotationAction.Get(PdfName.S)))
{
PdfArray d = annotation.GetAsArray(PdfName.DEST);
if (d != null && d.Length == 5 && PdfName.XYZ.Equals(d.GetAsName(1)))
{
d[4] = new PdfNumber(150);
}
}
}
}
}
}
Javaで
オリジナルのコードは、はるかに短いです:私が思いついたきた@mklする
public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
PdfReader reader = new PdfReader(src);
PdfDictionary page = reader.getPageN(11);
PdfArray annots = page.getAsArray(PdfName.ANNOTS);
for (int i = 0; i < annots.size(); i++) {
PdfDictionary annotation = annots.getAsDict(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)))
d.set(4, new PdfNumber(0));
}
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
}
UPDATED EDIT 2
感謝ソリューションで。あなたが入力として異なるPDFを使用していますが、問題のにリンクされている例えばthe source PDF from the iText web siteを使用している場合
for (int i = 1; i <= reader.NumberOfPages; i++)
{
PdfDictionary page = reader.GetPageN(i);
PdfArray annotsArray = page.GetAsArray(PdfName.ANNOTS);
if (annotsArray == null)
{
continue;
}
for (int j = 0; j < annotsArray.Size; j++)
{
PdfDictionary annotation = annotsArray.GetAsDict(j);
PdfDictionary annotationAction = annotation.GetAsDict(PdfName.A);
if (PdfName.GOTO.Equals(annotationAction.Get(PdfName.S)))
{
PdfArray d = annotationAction.GetAsArray(PdfName.D);
if (d != null)
{
Console.WriteLine(d[4]);
d[4] = new PdfNumber(1.20);
}
}
}
}
コードでは、** XYZ **タイプの目的地のみが処理されます。おそらく、あなたのサンプルpdfは、他のタイプの宛先を使用します。したがって、あなたのテストPDFを共有してください。 – mkl
@mklサンプルファイルを追加しました。 – menteith
* 'PdfArray d = annotation.GetAsArray(PdfName.GOTO)' * - これは2つの点で間違っています:** D **という名前の配列を必要とし、** GoTo **という名前の配列ではなく、この配列が含まれています'annotationAction'では、' annotation'には直接ではありません。したがって: 'PdfArray d = annotationAction.GetAsArray(PdfName.D) ' – mkl