PDFsharp & MigraDoc Foundation
https://forum.pdfsharp.net/

Bug with Adding text to split pages
https://forum.pdfsharp.net/viewtopic.php?f=3&t=199
Page 1 of 1

Author:  mdreyer [ Tue Jul 24, 2007 3:09 pm ]
Post subject:  Bug with Adding text to split pages

Hello,

I have a multi-page PDF document that I need to split, add some text and then save the pdf page to a BLOB SQL Server field. I created a simple Proof of concept to test if this tool will work for me.

I open the source document in import mode, split into an individual page in a new document, close the new document, reopen the new document in Modify mode, draw some text, save the new document and close it.

I get no errors, however no text is added to the new document page.

What am I doing wrong?

Here is the source code:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim inputdoc As PdfDocument = PdfReader.Open(txtPDFFile.Text, PdfDocumentOpenMode.Import)

For index As Integer = 0 To inputdoc.PageCount - 1
Dim page As PdfPage = inputdoc.Pages(index)
''inputdoc.Save(txtPDFFile.Text)

Dim filename As String = inputdoc.FullPath.Substring(0, inputdoc.FullPath.LastIndexOf(".pdf")) + _
"(Page " + String.Format("{0:000}", index + 1) + ").pdf"
Dim outputdoc As New PdfDocument(filename)
outputdoc.Pages.Add(page)
outputdoc.Close()

' reopen the new doc to modify and add the unique number
Dim newdoc As PdfDocument = PdfReader.Open(filename, PdfDocumentOpenMode.Modify)
For i As Integer = 0 To newdoc.PageCount - 1
Dim NewPage As PdfPage = newdoc.Pages(i)

Dim gfx As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage(NewPage)
Dim font As New PdfSharp.Drawing.XFont("Verdana", 10, XFontStyle.Regular)
gfx.DrawString(TextBox1.Text.ToString(), font, XBrushes.Black, 20, 20, XStringFormat.Default)
newdoc.Save(filename)

newdoc.Close()
Next
Next

inputdoc.Close()
MessageBox.Show("Done...")
Me.Close()
End Sub

Author:  mdreyer [ Tue Jul 24, 2007 4:08 pm ]
Post subject:  This does not work either...

Does not give me an error...just does not write anything to the pdf file

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim inputdoc As PdfDocument = PdfReader.Open(txtPDFFile.Text, PdfDocumentOpenMode.Import)

For index As Integer = 0 To inputdoc.PageCount - 1

Dim page As PdfPage = inputdoc.Pages(index)

Dim filename As String = inputdoc.FullPath.Substring(0, inputdoc.FullPath.LastIndexOf(".pdf")) + _
"(Page " + String.Format("{0:000}", index + 1) + ").pdf"

Dim outputdoc As New PdfDocument()
Dim pagetmp As PdfPage = outputdoc.Pages.Add(page)
Dim gfx As PdfSharp.Drawing.XGraphics = _
PdfSharp.Drawing.XGraphics.FromPdfPage(pagetmp, XGraphicsPdfPageOptions.Append)
Dim font As New PdfSharp.Drawing.XFont("Verdana", 18, XFontStyle.Regular)

gfx.DrawString(TextBox1.Text.ToString(), font, XBrushes.Black, 30, 50, XStringFormat.Default)
outputdoc.Save(filename)
outputdoc.Close()

' reopen the new doc to modify and add the unique number
'Dim newdoc As PdfDocument = PdfReader.Open(filename, PdfDocumentOpenMode.Modify)
'For i As Integer = 0 To newdoc.PageCount - 1
' Dim NewPage As PdfPage = newdoc.Pages(i)

' Dim gfx As PdfSharp.Drawing.XGraphics = PdfSharp.Drawing.XGraphics.FromPdfPage(NewPage)
' Dim font As New PdfSharp.Drawing.XFont("Verdana", 10, XFontStyle.Regular)
' gfx.DrawString(TextBox1.Text.ToString(), font, XBrushes.Black, 20, 20, XStringFormat.Default)
' newdoc.Save(filename)

' newdoc.Close()
'Next
Next

inputdoc.Close()
MessageBox.Show("Done...")
Me.Close()
End Sub

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