how to re-sizing pdf content with iTextsharp

天大地大妈咪最大 提交于 2020-01-07 02:33:05

问题


I just need to resize page content in a pdf file, it's not related to Reducing file size I just need to reduce each page content which is one image to be able to print it as a booklet in landscape mode. So I need to reduce the height and the width of each page content.

I'm using iTextSharp with c#


回答1:


The easiest way might be to change the size of default user space units for the pages in question.

The default user space units can be configured on a per-page basisusing the page dictionary entry /UserUnit, cf. table 30 in the PDF specification ISO 32000-1:2008:

UserUnit number (Optional; PDF 1.6) A positive number that shall give the size of default user space units, in multiples of 1⁄72 inch. The range of supported values shall be implementation-dependent.

Default value: 1.0 (user space unit is 1⁄72 inch).

Thus, all you have to do is create a PdfStamper for a PdfReader for your PDF file, iterate over the page dictionaries, read their current /UserUnit value (default: 1.0), reduce it as desired, write it back, mark the page dictionary as changed, and write the result of the PdfStamper.




回答2:


Try adding your content to the document and set the documents pages to landscape using the SetPageSize property.
Here is the code to set the pages to Landscape:
Document document = new Document();
document.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());
//Add some content
PdfPTable table = new PdfPTable(1);
PdfPCell cell = new PdfPCell(new Phrase("Some test text"));
cell.HorizontalAlignment = 1; //0=Left, 1=Centre, 2=Right
table.AddCell(cell);



来源:https://stackoverflow.com/questions/13210402/how-to-re-sizing-pdf-content-with-itextsharp

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