PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

excerpt information from PDF pages
http://forum.pdfsharp.com/viewtopic.php?f=2&t=104
Page 1 of 1

Author:  peteratoce [ Sat Mar 24, 2007 4:04 pm ]
Post subject:  excerpt information from PDF pages

I want to excerpt information from PDF pages and use it in the pages of a new document, without the need to drag any information about the original page and the offsets of the excerpt along. This is what I came up with so far:

' load XPdfForm from file
Dim filename As String = "testpage.pdf"
Dim form As XPdfForm = XPdfForm.FromFile(filename)
form.PageNumber = 1
Dim OrigWidth As Double = form.Width
Dim OrigHeight As Double = form.Height

' set parameters for excerpt
Dim OffsetX As Double = 100
Dim OffsetY As Double = 600
Dim ExcerptWidth As Double = 400
Dim ExcerptHeight As Double = 600

' create new PdfDocument that has page dimensions
' just equal to the dimensions of the excerpt
Dim tmpPDFdoc As New PdfDocument
tmpPDFdoc.PageLayout = PdfPageLayout.SinglePage
Dim tmppage As PdfPage = tmpPDFdoc.AddPage
tmppage.Orientation = PageOrientation.Portrait
tmppage.Width = XUnit.FromPoint(ExcerptWidth)
tmppage.Height = XUnit.FromPoint(ExcerptHeight)

Dim box As XRect
box = New XRect(-OffsetX, OffsetY - OrigHeight, OrigWidth, OrigHeight)

' paint the excerpt into the new page
Dim gfx As XGraphics
gfx = XGraphics.FromPdfPage(tmppage)
gfx.DrawImage(form, box)

tmpPDFdoc.Save("tmp.pdf")
tmpPDFdoc.Close()

' now the excerpted content can be used in another
' document...
Dim myPDFdoc As New PdfDocument
myPDFdoc.PageLayout = PdfPageLayout.SinglePage
Dim page As PdfPage = myPDFdoc.AddPage
page.Orientation = PageOrientation.Portrait
page.Width = XUnit.FromPoint(1000)
page.Height = XUnit.FromPoint(2000)
gfx = XGraphics.FromPdfPage(page)
form = XPdfForm.FromFile("tmp.pdf")
form.PageNumber = 1
box = New XRect(100, 100, ExcerptWidth, ExcerptHeight)
gfx.DrawRectangle(New XPen(XColor.FromKnownColor(XKnownColor.Black)), box)
gfx.DrawImage(form, box)
myPDFdoc.Save("result.pdf")
myPDFdoc.Close()

What I am looking for is a more intelligent method that can avoid temporary storage of the excerpt in a file!

I have unfortunately no deep understanding of the PDF innards, so any help is highly welcome.

Peter

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/