如何使用iTextSharp将缩放级别设置为pdf?

时间:2014-06-06 17:45:47

标签: c# pdf itextsharp

我需要使用iTextSharp将缩放级别设置为75%到pdf文件。我正在使用以下代码来设置缩放级别。

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();

但是我收到错误"第1页是请求,但文档只有0页"在doc.Close();

1 个答案:

答案 0 :(得分:1)

你需要使用PdfStamper(由mkl表示)而不是PdfWriter(由Chris Haas明确表示)。请查看AddOpenAction示例:

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();
}

结果是a PDF that opens with a zoom factor of 75%