使用iTextSharp增加现有pdf的左边距

时间:2015-11-05 21:27:12

标签: pdf itextsharp itext margin

我现有的Pdf文件左边距为1/4英寸。我需要将左边距增加到1/2英寸。我尝试使用以下代码移动文本以裁剪每个页面的内容。内容移到左侧,这与我的意图相反。任何关于如何实现这一点的建议将不胜感激。

public void ResetMargin(string inputFilePath, string outputFilePath, int count)
{
    using (PdfReader pdf = new PdfReader(inputFilePath))
    {
      PdfDictionary pageDict;
      PdfArray cropBox;
      PdfArray mediaBox;
      float letterWidth = PageSize.LETTER.Width;
      float letterHeight = PageSize.LETTER.Height;
      int pageCount = pdf.NumberOfPages;

      for (int i = 1; i <= pageCount; i++)
      {
        pageDict = pdf.GetPageN(i);
        cropBox = pageDict.GetAsArray(PdfName.CROPBOX);
        mediaBox = pageDict.GetAsArray(PdfName.MEDIABOX);

        cropBox[0] = new PdfNumber(36);
        cropBox[1] = new PdfNumber(0);
        cropBox[2] = new PdfNumber(letterWidth);
        cropBox[3] = new PdfNumber(letterHeight);

        mediaBox[0] = new PdfNumber(36);
        mediaBox[1] = new PdfNumber(0);
        mediaBox[2] = new PdfNumber(letterWidth);
        mediaBox[3] = new PdfNumber(letterHeight);

        pageDict.Put(PdfName.CROPBOX, cropBox);
        pageDict.Put(PdfName.MEDIABOX, mediaBox);
      }

      PdfStamper stamper = new PdfStamper(pdf, new FileStream(outputFilePath, FileMode.Create));
      stamper.Close();
    }
}

0 个答案:

没有答案