How to truly crop a PDF file?

早过忘川 提交于 2019-12-11 18:05:25

问题


I am trying to extract and save a certain portion of a PDF file using specific coordinates (x,y,w,h). I am using the following code, which seems to be working okay:

Function CroppedPdf(Source As Byte(), PageNumber As Integer,
        Rect As System.Drawing.Rectangle) As MemoryStream
    Dim reader As New PdfReader(Source)
    Dim h = reader.GetPageSize(1).Height
    Dim document = New iTextSharp.text.Document(New iTextSharp.text.Rectangle(
            Rect.Right / 300 * 72, h - (Rect.Top / 300 * 72), Rect.Left / 300 * 72,
            h - (Rect.Bottom / 300 * 72)))
    document.SetMargins(0, 0, 0, 0)
    Dim destination = New MemoryStream
    Dim writer = PdfWriter.GetInstance(document, destination)
    document.Open()
    Dim cb = writer.DirectContent
    document.NewPage()
    Dim page = writer.GetImportedPage(reader, 1)
    cb.AddTemplate(page, 0, 0)
    document.Close()
    Return destination
End Function

The problem is that the resulting pdf is only seemingly cropped. When I try to run text extraction on it, I get back the text of the entire original source document. Furthermore, when splitting up a page in 10 pieces, the same document is actually stored 10 times with differences only in the viewport. How can I truly crop the PDF file, storing only the exact portion of the file I am interested in?

来源:https://stackoverflow.com/questions/18460427/how-to-truly-crop-a-pdf-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!