How to set zoom level to pdf using iTextSharp?

一个人想着一个人 提交于 2019-11-27 08:38:57

问题


I need to set the zoom level 75% to pdf file using iTextSharp. I am using following code to set the zoom level.

PdfReader reader = new PdfReader("input.pdf".ToString());
iTextSharp.text.Document doc = new iTextSharp.text.Document(reader.GetPageSize(1));
doc.OpenDocument();
PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream("Zoom.pdf", FileMode.Create));
PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, doc.PageSize.Height, 0.75f);
doc.Open();
PdfAction action = PdfAction.GotoLocalPage(1, pdfDest, writer);
writer.SetOpenAction(action);
doc.Close();

But I am getting the error "the page 1 was request but the document has only 0 pages" in the doc.Close();


回答1:


You need to use PdfStamper (as indicated by mkl) instead of PdfWriter (as made clear by Chris Haas). Please take a look at the AddOpenAction example:

public void manipulatePdf(String src, String dest) throws IOException, DocumentException {
    PdfReader reader = new PdfReader(src);
    PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
    PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, reader.getPageSize(1).getHeight(), 0.75f);
    PdfAction action = PdfAction.gotoLocalPage(1, pdfDest, stamper.getWriter());
    stamper.getWriter().setOpenAction(action);
    stamper.close();
    reader.close();
}

The result is a PDF that opens with a zoom factor of 75%.



来源:https://stackoverflow.com/questions/24087786/how-to-set-zoom-level-to-pdf-using-itextsharp

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