ITextSharp裁剪PDF以删除白边距

时间:2015-08-24 21:08:46

标签: pdf itextsharp

我正在尝试使用iTextSharp删除白边。 我想从中得到: enter image description here

到此: enter image description here

我使用了Textmargin fider,但它只检测文本的边界。 继承我的代码

private Rectangle getOutputPageSize(PdfReader reader, int page)
{
    PdfReaderContentParser parser = new PdfReaderContentParser(reader);
    PTextMarginFinder finder = parser.ProcessContent<TextMarginFinder>(page, new TextMarginFinder());
    double lijevo = finder.GetLlx();
    double desno = finder.GetUrx();
    double gore = finder.GetUry();
    double dole = finder.GetLly();
    return new Rectangle(finder.GetLlx(), finder.GetLly(), finder.GetUrx(), finder.GetUry());
}
    private void button1_Click(object sender, EventArgs e)
    {
        string fileName = "e:\\test.pdf";
        if (File.Exists(fileName))
        {
            PdfReader pdfReader = new PdfReader(fileName);
            PdfStamper stamper = new PdfStamper(pdfReader, new System.IO.FileStream("E:\\gotovo\\test.pdf",FileMode.Append));
            for (int p = 1; p <= pdfReader.NumberOfPages; p++)
            {
                PdfDictionary page = pdfReader.GetPageN(1);
                PdfArray cropBox;
                PdfArray mediaBox;
                cropBox = page.GetAsArray(PdfName.CROPBOX);
                mediaBox = page.GetAsArray(PdfName.MEDIABOX);
                //ITextExtractionStrategy strategy = new SimpleTextExtractionStrategy();
                //string currentText = PdfTextExtractor.GetTextFromPage(pdfReader, page, strategy);
                Rectangle rect =getOutputPageSize(pdfReader, 1);
                float[] flo = { rect.Left, rect.Bottom, rect.Right, rect.Top };
                PdfArray pdfa = new PdfArray(flo);
                //PdfArray pdfa = new PdfArray(mediaBox);
                page.Put(PdfName.CROPBOX, pdfa); 
                stamper.MarkUsed(page); 
                //currentText = Encoding.UTF8.GetString(ASCIIEncoding.Convert(Encoding.Default, Encoding.UTF8, Encoding.Default.GetBytes(currentText)));
                //text.Append(currentText);
            }
            stamper.Close();
            pdfReader.Close();
        }
    }

我的代码只做这个: enter image description here 有没有其他方法可以正确划定界限?

0 个答案:

没有答案