Determine properties such as if PDF is Simplex or Duplex in iTextSharp

不羁岁月 提交于 2019-12-01 12:36:00

问题


I am using iTextSharp for reading and managing PDF documents. Things such as stamping overlays for the background or logos and backers. The PDF's are statement files, so I cannot give an example. I am wondering how to view the settings of the PDF to see if the PDF file is Simplex or Duplex, and that sort of information. Any help or suggestions would be appreciated. At the moment I test for certain criteria of second page, and this is a poor and bad way to do this. Thanks in advance, and happy coding!


回答1:


The duplex mode is stored in the document's /ViewerPreferences dictionary under the /Duplex key. It supports three values, /DuplexFlipLongEdge, /DuplexFlipShortEdge, and /Simplex. You can use the code below to inspect this:

//Assume false by default since this was introduced in PDF 1.7
Boolean isDuplex = false;

//Bind a reader to our file
using (var r = new PdfReader(testFile)) {
    //Get the view preferences
    var prefs = r.Catalog.GetAsDict(PdfName.VIEWERPREFERENCES);

    //Make sure we found something
    if (prefs != null) {
        //Get the duplex key
        var duplex = prefs.Get(PdfName.DUPLEX);

        //Make sure we got something and it is one of the duplex modes
        isDuplex = (duplex != null && (duplex.Equals(PdfName.DUPLEXFLIPLONGEDGE) || duplex.Equals(PdfName.DUPLEXFLIPSHORTEDGE)));
    }
}



回答2:


I know its 2 years later but I just spent hours searching, found this... but eventually found...

I create a button that runs this script (that pops up the printer dialogue with duplex pre selected if available... note that selecting another printer erases this pre selection.. also change "Long" for "Short" if you flip that way... q8)

var pp = this.getPrintParams(); pp.DuplexType = pp.constants.duplexTypes.DuplexFlipLongEdge; this.print(pp);



来源:https://stackoverflow.com/questions/23200641/determine-properties-such-as-if-pdf-is-simplex-or-duplex-in-itextsharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!