16
2015
10

Crop Pdf from each edge

 

public void TrimPdf(string sourceFilePath, string outputFilePath)
  {
        PdfReader pdfReader = new PdfReader(sourceFilePath);
        float widthTo_Trim =         iTextSharp.text.Utilities.MillimetersToPoints(5);

        using (FileStream output = new FileStream(outputFilePath, FileMode.Create, FileAccess.Write))
        using (PdfStamper pdfStamper = new PdfStamper(pdfReader, output))
        {

            for (int page = 1; page <= pdfReader.NumberOfPages; page++)
            {
                Rectangle cropBox = pdfReader.GetCropBox(page);

                cropBox.Left += widthTo_Trim;
                cropBox.Right += widthTo_Trim;
               cropBox.Top += widthTo_Trim;
                cropBox.Bottom += widthTo_Trim;

                pdfReader.GetPageN(page).Put(PdfName.CROPBOX, new PdfRectangle(cropBox));


            }


        }

        //
    }




public void TrimLeftandRightFoall(string sourceFilePath, string outputFilePath, float cropwidth)
    {

        PdfReader pdfReader = new PdfReader(sourceFilePath);
        float width = (float)GetPDFwidth(sourceFilePath);
        float height = (float)GetPDFHeight(sourceFilePath);
        float widthTo_Trim = iTextSharp.text.Utilities.MillimetersToPoints(cropwidth);


        PdfRectangle rectLeftside = new PdfRectangle(widthTo_Trim, widthTo_Trim, width-widthTo_Trim , height-widthTo_Trim);



        using (var output = new FileStream(outputFilePath, FileMode.CreateNew, FileAccess.Write))
        {
            // Create a new document
            Document doc = new Document();

            // Make a copy of the document
            PdfSmartCopy smartCopy = new PdfSmartCopy(doc, output);

            // Open the newly created document
            doc.Open();

            // Loop through all pages of the source document
            for (int i = 1; i <= pdfReader.NumberOfPages; i++)
            {
                // Get a page
                var page = pdfReader.GetPageN(i);
                page.Put(PdfName.MEDIABOX, rectLeftside);


           var copiedPage = smartCopy.GetImportedPage(pdfReader, i);
                smartCopy.AddPage(copiedPage);
            }

            doc.Close();

        }

    }


 

« 上一篇下一篇 »

相关文章:

Crop that pdf page horizontaly into 4 pieces and create a new pdf for each piece  (2015-10-16 9:1:54)

C# Crop PDF File   (2015-10-16 8:57:13)

万兴PDF编辑器 Wondershare PDF Editor  (2014-6-12 12:30:44)

PDF转word  (2014-2-25 16:47:38)

Aspose.PDF功能介绍  (2014-2-20 16:47:23)

Excel转PDF的方法  (2014-2-19 20:20:48)

Aspose.Pdf for .NET实现PDF转Excel、EPS、SVG等  (2014-2-16 16:26:25)

itextsharp去除PDF内的水印  (2014-2-14 14:3:43)

iText去除PDF水印  (2014-2-14 13:29:35)

PDF解析  (2014-2-13 8:46:18)

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。