引用

itextsharp 5.5.13.2

itextsharp.xtra 5.5.13.2

方法

/// <summary>
/// 截取pdf文件,例如把A4截出指定的A6区域
/// </summary>
/// <param name="file"></param>
/// <param name="oldchar"></param>
/// <param name="repChar"></param>
/// <param name="size"></param>
public void CropDocument(string file, string oldchar, string repChar, iTextSharp.text.Rectangle size)
{
    int pageNumber = 1;//第一页
    PdfReader reader = new PdfReader(file);
    Document document = new Document(size);
    PdfWriter writer = PdfWriter.GetInstance(document,
    new FileStream(file.Replace(oldchar, repChar),
    FileMode.Create, FileAccess.Write));
    document.Open();
    PdfContentByte cb = writer.DirectContent;
    document.NewPage();
    PdfImportedPage page = writer.GetImportedPage(reader,
    pageNumber);
    cb.AddTemplate(page, 0, 0);
    document.Close();
}

用法

CropDocument("E:\\Desktop\\test.pdf","test.pdf","new.pdf",new iTextSharp.text.Rectangle(0,0,100,100));
//Rectangle参数说明
//圆点为页面左下角
//前两个参数x1,y1为区域的开始点
//后两个参数x2,y2为区域的结束点