0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iTextSharpでPDFの綴じ方向を取得する方法

Last updated at Posted at 2020-02-13

iTextSharpでPDFの綴じ方向を取得するには、ViewerPreferencesを取得します。
Java版のiTextも似たようなものだと思います。

using iTextSharp.text.pdf;

var pr = new PdfReader(stream);
var vp = iTextSharp.text.pdf.intern.PdfViewerPreferencesImp.GetViewerPreferences(pr.Catalog).GetViewerPreferences();
this.Direction = Directions.Default;
if (vp.Contains(PdfName.DIRECTION))
{
    var name = vp.GetAsName(PdfName.DIRECTION);
    if (name == PdfName.R2L)
    {
        this.Direction = Directions.R2L;
    }
    else if (name == PdfName.L2R)
    {
        this.Direction = Directions.L2R;
    }
}
pr.Close();

(参考元:http://itext.2136553.n4.nabble.com/Using-getSimpleViewerPreferences-td2167775.html)

Viewer Preferenceを取得する部分は、iTextSharp.text.pdf.intern.PdfViewerPreferencesImp.GetViewerPreferences(pr.Catalog).GetViewerPreferences();
なんだこれ…。

綴じ方向とは、Adobe ReaderのCtrl+Dで確認できる設定です。
image.png
注意点として、

  • ここで「綴じ方: 左」と表示されていても実際にはDirectionが設定されていない場合がある
  • 実際には右綴じなのに「綴じ方: 左」と表示されるファイルが結構ある。

一方で取得ではなく設定したいならこんな感じです。

using iTextSharp.text.pdf;

var org = new PdfReader(file);
using (var outfile = new System.IO.FileStream(TemporaryFile, System.IO.FileMode.CreateNew))
{
    var st = new PdfStamper(org, outfile);
    st.AddViewerPreference(PdfName.DIRECTION, R2L ? PdfName.R2L : PdfName.L2R);
    st.Close();
}
org.Close();

(参考元:https://kiwanami.hatenadiary.org/entry/20101215/1292400269)

Viewer Preferencesに関してはこの辺りが参考になりそうです。
とりあえず綴じ方向以外はあまり有用そうには見えませんね。

ちなみに使っているのはLGPL版の4.1.6です。
AGPLに移行後の最新版は知りませんが、非ジェネリック版のCollectionを使っていたりAPIが直感的じゃなかったりで使いづらいですね。
上のとか、PdfReader.ViewerPreferencesにgetアクセサ追加とかが順当ですよね。
実際のPdfReader.ViewerPreferencesはsetアクセサしかない上、int型です。
まぁJava由来ですからね。PDFの仕様自体が難解ですし。

ソースコードは開発中のBookViewer後継アプリBookViewerApp3のものです。

0
3
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?