如何从 iText 7 中的 pdf outlines/bookmark 访问矩形、适合类型、缩放、页码
How to access rectangles, fit type, zoom, page number from outlines/bookmark of pdf in iText 7
我正在使用 iText 7。
我正在尝试使用 iText 访问 PDF 书签的属性,例如(矩形、适合类型、缩放、页码)。
我遵循这个层次结构 -> PdfDocument
-> GetOutlines(true)
-> 这给出了每个 bookmark/outline。
我遍历了每个书签,但无法访问应包含矩形、适合类型、缩放和页码的视图目标属性。
我知道在创建新书签时我使用了 PdfExplicitDestination.CreateXYZ
但我无法从 pdf 文档访问查看目标。
您可以从 PdfDestination
中检索的数据大多是非常原始的 PDF。因此,目标不是您想要的格式,而是名称、字节字符串或数组。前两个选项被命名为目的地,即您必须在 Dests 名称树中查找目的地数组。
IDictionary<String, PdfObject> names = document.GetCatalog().GetNameTree(PdfName.Dests).GetNames();
[...]
if ([... some PdfOutline instance ...].GetDestination() is PdfDestination destination)
{
PdfObject destObject = destination.GetPdfObject();
if (destObject is PdfString str)
{
destObject = names[str.ToUnicodeString()];
}
else if (destObject is PdfName nam)
{
destObject = names[nam.GetValue()];
}
现在您应该有一个 PdfArray
并且可以检查它的值。页面字典是其中的第一个对象,所以
if (destObject is PdfArray arr)
{
if (arr.Get(0) is PdfDictionary pageDict)
{
int pageNumber = document.GetPageNumber(pageDict);
[...]
}
[... inspect remaining array entries ...]
}
}
在检查剩余的数组条目时,实际上有许多可能的数组形式需要考虑:
[page /XYZ left top zoom]
Display the page designated by page, with the coordinates (left, top) positioned at the upper-left corner of the window and the contents of the page magnified by the factor zoom. A null value for any of the parameters left, top, or zoom specifies that the current value of that parameter shall be retained unchanged. A zoom value of 0 has the same meaning as a null value.
[page /Fit]
Display the page designated by page, with its contents magnified just enough to fit the entire page within the window both horizontally and vertically. If the required horizontal and vertical magnification factors are different, use the smaller of the two, centring the page within the window in the other dimension.
[page /FitH top]
Display the page designated by page, with the vertical coordinate top positioned at the top edge of the window and the contents of the page magnified just enough to fit the entire width of the page within the window. A null value for top specifies that the current value of that parameter shall be retained unchanged.
[page /FitV left]
Display the page designated by page, with the horizontal coordinate left positioned at the left edge of the window and the contents of the page magnified just enough to fit the entire height of the page within the window. A null value for left specifies that the current value of that parameter shall be retained unchanged.
[page /FitR left bottom right top]
Display the page designated by page, with its contents magnified just enough to fit the rectangle specified by the coordinates left, bottom, right, and top entirely within the window both horizontally and vertically. If the required horizontal and vertical magnification factors are different, use the smaller of the two, centring the rectangle within the window in the other dimension.
[page /FitB]
(PDF 1.1) Display the page designated by page, with its contents magnified just enough to fit its bounding box entirely within the window both horizontally and vertically. If the required horizontal and vertical magnification factors are different, use the smaller of the two, centring the bounding box within the window in the other dimension.
[page /FitBH top]
(PDF 1.1) Display the page designated by page, with the vertical coordinate top positioned at the top edge of the window and the contents of the page magnified just enough to fit the entire width of its bounding box within the window. A null value for top specifies that the current value of that parameter shall be retained unchanged.
[page /FitBV left]
(PDF 1.1) Display the page designated by page, with the horizontal coordinate left positioned at the left edge of the window and the contents of the page magnified just enough to fit the entire height of its bounding box within the window. A null value for left specifies that the current value of that parameter shall be retained unchanged.
(ISO 32000-2,Table 149 — 目标语法)
我正在使用 iText 7。
我正在尝试使用 iText 访问 PDF 书签的属性,例如(矩形、适合类型、缩放、页码)。
我遵循这个层次结构 -> PdfDocument
-> GetOutlines(true)
-> 这给出了每个 bookmark/outline。
我遍历了每个书签,但无法访问应包含矩形、适合类型、缩放和页码的视图目标属性。
我知道在创建新书签时我使用了 PdfExplicitDestination.CreateXYZ
但我无法从 pdf 文档访问查看目标。
您可以从 PdfDestination
中检索的数据大多是非常原始的 PDF。因此,目标不是您想要的格式,而是名称、字节字符串或数组。前两个选项被命名为目的地,即您必须在 Dests 名称树中查找目的地数组。
IDictionary<String, PdfObject> names = document.GetCatalog().GetNameTree(PdfName.Dests).GetNames();
[...]
if ([... some PdfOutline instance ...].GetDestination() is PdfDestination destination)
{
PdfObject destObject = destination.GetPdfObject();
if (destObject is PdfString str)
{
destObject = names[str.ToUnicodeString()];
}
else if (destObject is PdfName nam)
{
destObject = names[nam.GetValue()];
}
现在您应该有一个 PdfArray
并且可以检查它的值。页面字典是其中的第一个对象,所以
if (destObject is PdfArray arr)
{
if (arr.Get(0) is PdfDictionary pageDict)
{
int pageNumber = document.GetPageNumber(pageDict);
[...]
}
[... inspect remaining array entries ...]
}
}
在检查剩余的数组条目时,实际上有许多可能的数组形式需要考虑:
[page /XYZ left top zoom] Display the page designated by page, with the coordinates (left, top) positioned at the upper-left corner of the window and the contents of the page magnified by the factor zoom. A null value for any of the parameters left, top, or zoom specifies that the current value of that parameter shall be retained unchanged. A zoom value of 0 has the same meaning as a null value.
[page /Fit] Display the page designated by page, with its contents magnified just enough to fit the entire page within the window both horizontally and vertically. If the required horizontal and vertical magnification factors are different, use the smaller of the two, centring the page within the window in the other dimension.
[page /FitH top] Display the page designated by page, with the vertical coordinate top positioned at the top edge of the window and the contents of the page magnified just enough to fit the entire width of the page within the window. A null value for top specifies that the current value of that parameter shall be retained unchanged.
[page /FitV left] Display the page designated by page, with the horizontal coordinate left positioned at the left edge of the window and the contents of the page magnified just enough to fit the entire height of the page within the window. A null value for left specifies that the current value of that parameter shall be retained unchanged.
[page /FitR left bottom right top] Display the page designated by page, with its contents magnified just enough to fit the rectangle specified by the coordinates left, bottom, right, and top entirely within the window both horizontally and vertically. If the required horizontal and vertical magnification factors are different, use the smaller of the two, centring the rectangle within the window in the other dimension.
[page /FitB] (PDF 1.1) Display the page designated by page, with its contents magnified just enough to fit its bounding box entirely within the window both horizontally and vertically. If the required horizontal and vertical magnification factors are different, use the smaller of the two, centring the bounding box within the window in the other dimension.
[page /FitBH top] (PDF 1.1) Display the page designated by page, with the vertical coordinate top positioned at the top edge of the window and the contents of the page magnified just enough to fit the entire width of its bounding box within the window. A null value for top specifies that the current value of that parameter shall be retained unchanged.
[page /FitBV left] (PDF 1.1) Display the page designated by page, with the horizontal coordinate left positioned at the left edge of the window and the contents of the page magnified just enough to fit the entire height of its bounding box within the window. A null value for left specifies that the current value of that parameter shall be retained unchanged.
(ISO 32000-2,Table 149 — 目标语法)