如何使用iTextSharp创建包含纵向和横向页面的单个PDF文档

Pau*_*ett 2 itextsharp

我已经能够在单独的文档中创建纵向页面和横向页面,但现在需要在一个文档中执行此操作.我正在使用ITextSharp库,document.setpagesize似乎适用于所有页面.它是否正确?

我正在使用PDFLib并且更改页面方向在该库中不是问题.

有什么建议?保罗.

Mar*_*rer 5

它应该只适用于该调用后呈现的页面.

Document doc = new Document(PageSize.WHAT_EVER);
PdfWriter writer = new PdfWriter( doc, outputStream );
doc.open();
// so long as you set the page size before add()ing anything, it should ignore the
// page sized used in the constructor.
doc.setPageSize(PageSize.LETTER); // 8.5" x 11"
// actually, I think you need to call newPage or actually add enough stuff to start a new page
// for setPageSize to take effect.  setPageSize by itself won't do the trick.
doc.add(stuffToFillALetterPageButNotStartANewOne); 

doc.setPageSize(new Rectangle(792f, 612f)); // 11" x 8.5"
doc.add(moreStuffToFillALandscapePageThusStartingANewPage);
doc.close();
Run Code Online (Sandbox Code Playgroud)

生成的PDF应该有两页.将是8.5x11,其他11x8.5.请注意,iText [sharp]不会生成旋转页面(8.5"x11"@ 90度(或270 ... 颤抖)).它比......更清晰.

处理旋转页面并不好玩.至少我在TOO_MANY_YEARS的PDF体验中从未遇到过180转的8.5x11.那时我只需要陷入杀气腾腾的愤怒之中.或者也许我应该生成一些PDF,只是为了看看我能抓住他们的裤子.

[在这里插入恶魔咯咯]