We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy.
Unfortunately, activation email could not send to your email. Please try again.

Change size of content on page

Thread ID:

Created:

Updated:

Platform:

Replies:

126239 Oct 3,2016 11:32 AM Oct 17,2016 06:14 AM Windows Forms 11
loading
Tags: PDF
Andy Dillbeck
Asked On October 3, 2016 11:32 AM

I have a program that is taking in PDFs that have a lot of pages and placing a watermark with page number and other information on it on the bottom of the page.
Every once in a while the watermark covers something important, and so we have to go back and find the original to see what the watermark is covering up.

I am curious if there is a way to shrink the page content, reduce it by half an inch, and then put the watermark in the area that is guaranteed to be empty.


Chinnu Muniyappan [Syncfusion]
Replied On October 4, 2016 06:45 AM

Hi Andy, 

Thank you for contacting Syncfusion support. 

Every once in a while the watermark covers something important, and so we have to go back and find the original to see what the watermark is covering up. 
You can also reduce the watermark transparency by using PdfGraphics.SetTransparency. please find the below code snippet and documentation link for more details. 
                //set transparancy 
                page.Graphics.SetTransparency(0.25f); 
               
                //Draw the water mark 
                page.Graphics.DrawString("Page: " + i.ToString(), font, PdfPens.Red, PdfBrushes.Red,new PointF(200,100)); 
 
 
I am curious if there is a way to shrink the page content, reduce it by half an inch, and then put the watermark in the area that is guaranteed to be empty. 
We have created workaround sample for shrink the existing page content by using PdfTemplate. Please refer the below sample for more details. 
 
Sample link: 
 
 
Please let us know if you have any concern. 

Regards, 
Chinnu 


Andy Dillbeck
Replied On October 4, 2016 02:41 PM

So I'm trying it out with the transparency set at 50% ( SetTransparency(0.5F) ) and I'm getting some weird results with font. 

When I run the program on my computer the inserted text is kind of fuzzy and thick.


Andy Dillbeck
Replied On October 4, 2016 02:52 PM

So I'm trying it out with the transparency set at 50% ( SetTransparency(0.5F) ) and I'm getting some weird results with font. 

When I run the program on my computer the inserted text is kind of fuzzy and thick.
development computer

Then on the production machine the font is very sharp, but the transparency doesn't work right
Production machine

When I print it to xps it looks fine, and then print it to paper from there it looks like I want it to
xps print on production machine

Any way to figure out what is going on with the fonts? I tried both the standard fonts
        Dim font As Syncfusion.Pdf.Graphics.PdfFont = New Syncfusion.Pdf.Graphics.PdfStandardFont(Syncfusion.Pdf.Graphics.PdfFontFamily.Helvetica, 12.0F)

and with TrueType fonts
        Dim font As Syncfusion.Pdf.Graphics.PdfFont = New Syncfusion.Pdf.Graphics.PdfTrueTypeFont(New Drawing.Font("Arial", 14))

with the same results.



Chinnu Muniyappan [Syncfusion]
Replied On October 5, 2016 04:50 AM

Hi Andy, 
 
Thank you for your update. 
 
We are not able to reproduce the mentioned transparency issue, however we have created a sample for using PdfBurshes to draw the watermark texts, please refer the below code snippet and sample for more details. 

'Draw the watermark 
lpage.Graphics.DrawString("Page : " + i.ToString(), font, PdfBrushes.Black, New PointF(200, lpage.Size.Height - 40)) 
 
Sample link: 
 
Please try this sample and let us know your result. 
 
If still you are facing the same issue, could you please provide us your output document and environment details it would be more helpful for us to investigate further on this. 
 
Regards, 
Chinnu 


Andy Dillbeck
Replied On October 5, 2016 11:54 AM

Here's the code I was using, which included PDFPens and PDFBrushes:

            Dim g As Syncfusion.Pdf.Graphics.PdfGraphics = lpage.Graphics
            Dim state As Syncfusion.Pdf.Graphics.PdfGraphicsState = g.Save
            g.SetTransparency(0.5F)
            g.DrawString(i & " " & companycity & " - " & Now.ToString("M-d-yy HH:mm:ss tt"), font, Syncfusion.Pdf.Graphics.PdfPens.Black, Syncfusion.Pdf.Graphics.PdfBrushes.Black, New System.Drawing.PointF(30, lpage.Graphics.ClientSize.Height - 35))

I switched it to 

            g.DrawString(i & " " & companycity & " - " & Now.ToString("M-d-yy HH:mm:ss tt"), font, Syncfusion.Pdf.Graphics.PdfBrushes.Black, New System.Drawing.PointF(30, lpage.Graphics.ClientSize.Height - 35))

and now it looks like this:


Attachment: 10516112222andyd273new_2dd7b62b.zip

Andy Dillbeck
Replied On October 5, 2016 02:53 PM

We have created workaround sample for shrink the existing page content by using PdfTemplate. Please refer the below sample for more details. 

So I got this working, and it looks like it might work better than the transparency thing, but I'm curious if there is a way to save over the source PDF with the resized PDF. 

I've tried: 

        Dim newDoc As New Syncfusion.Pdf.PdfDocument
        Dim newPage As Syncfusion.Pdf.PdfPage
        Dim template As Syncfusion.Pdf.Graphics.PdfTemplate
        Dim lDoc As New Syncfusion.Pdf.Parsing.PdfLoadedDocument(path)

        For Each lpage As Syncfusion.Pdf.PdfPageBase In lDoc.Pages
            newDoc.PageSettings.Size = lpage.Size
            newDoc.PageSettings.Margins.All = 0

            newPage = newDoc.Pages.Add
            template = lpage.CreateTemplate

            newPage.Graphics.DrawPdfTemplate(template, System.Drawing.PointF.Empty, New System.Drawing.SizeF(newPage.Size.Width, newPage.Size.Height - 36))

            Dim g As Syncfusion.Pdf.Graphics.PdfGraphics = newPage.Graphics
            Dim state As Syncfusion.Pdf.Graphics.PdfGraphicsState = g.Save
            g.DrawString(i & " " & companycity & " - " & Now.ToString("M-d-yy HH:mm:ss tt"), font, Syncfusion.Pdf.Graphics.PdfBrushes.Black, New System.Drawing.PointF(30, lpage.Graphics.ClientSize.Height - 35))

            newPage.Graphics.Restore()
            i += 1
        Next
        lDoc.Close()
        lDoc = Nothing

        newDoc.Save(path)
        newDoc.Close()

and it works if I write to a new path, but I really want to write over the same file. However If I try to save to the same path I get an error: 'Unexpected token Eof before 925'

So far I've tried deleting the original file first, saving to a new path and then overwriting the original, and using file streams instead of paths, but none of them seem to work. 
What's the right way to do it?

Chinnu Muniyappan [Syncfusion]
Replied On October 6, 2016 09:11 AM

Hi Andy, 
 
Thank you for your update. 

Here's the code I was using, which included PDFPens and PDFBrushes: 
 
            Dim g As Syncfusion.Pdf.Graphics.PdfGraphics = lpage.Graphics 
            Dim state As Syncfusion.Pdf.Graphics.PdfGraphicsState = g.Save 
            g.SetTransparency(0.5F) 
            g.DrawString(i & " " & companycity & " - " & Now.ToString("M-d-yy HH:mm:ss tt"), font, Syncfusion.Pdf.Graphics.PdfPens.Black, Syncfusion.Pdf.Graphics.PdfBrushes.Black, New System.Drawing.PointF(30, lpage.Graphics.ClientSize.Height - 35)) 
 
I switched it to  
 
            g.DrawString(i & " " & companycity & " - " & Now.ToString("M-d-yy HH:mm:ss tt"), font, Syncfusion.Pdf.Graphics.PdfBrushes.Black, New System.Drawing.PointF(30, lpage.Graphics.ClientSize.Height - 35)) 
 
We are not able to reproduce the issue and the attached output document was preserved as excepted in Adobe reader please find the below screenshot for more details. 
 
 
 
Could you please provide us the below details, it would more helpful for us to investigate further on this. 
1.PDF reader which is using for viewing the PDF document 
2.System environment details 
3.Cultrue setting details 
 
and it works if I write to a new path, but I really want to write over the same file. However If I try to save to the same path I get an error: 'Unexpected token Eof before 925' 
 
So far I've tried deleting the original file first, saving to a new path and then overwriting the original, and using file streams instead of paths, but none of them seem to work.  
What's the right way to do it? 
 
We have created a workaround sample for overwriting the existing PDF document to new PDF document, please refer the below code snippet and sample for more details. 
 
       'save the new PDF document 
 
        Dim ms As MemoryStream = New MemoryStream() 
 
        newDoc.Save(ms) 
 
        'Close the document 
 
        ldoc.Close(True) 
 
        newDoc.Close(True) 
 
        'Save the PDF to the same path 
 
        System.IO.File.WriteAllBytes(path, ms.ToArray()) 
 
        ms.Dispose() 
 
Sample link : 
 
 
 
Please let us know if you have any concern. 
 
Regards, 
Chinnu 


Andy Dillbeck
Replied On October 10, 2016 04:09 PM

So I have this mostly working, except that on some PDF's stuff on the edges gets cut off, especially along the top. Is there a way to reliably get it to fit the page?
The attached document has abnormally sized pages, and I think that may be part of the reason things are getting trimmed.

Here's my code for putting the watermark on the bottom:

        Dim lDoc As New Syncfusion.Pdf.Parsing.PdfLoadedDocument(path)
        Dim newDoc As New Syncfusion.Pdf.PdfDocument
        Dim newPage As Syncfusion.Pdf.PdfPage
        Dim template As Syncfusion.Pdf.Graphics.PdfTemplate
        Dim font As Syncfusion.Pdf.Graphics.PdfFont = New Syncfusion.Pdf.Graphics.PdfStandardFont(Syncfusion.Pdf.Graphics.PdfFontFamily.Helvetica, 12.0F)
        Dim i As Integer = 1

        For Each lpage As Syncfusion.Pdf.PdfPageBase In lDoc.Pages
            newDoc.PageSettings.Size = lpage.Size
            newDoc.PageSettings.Margins.All = 0

            newPage = newDoc.Pages.Add
            template = lpage.CreateTemplate

            newPage.Graphics.DrawPdfTemplate(template, System.Drawing.PointF.Empty, New System.Drawing.SizeF(lpage.Size.Width - 10, lpage.Size.Height - 35))

            Dim g As Syncfusion.Pdf.Graphics.PdfGraphics = newPage.Graphics
            Dim state As Syncfusion.Pdf.Graphics.PdfGraphicsState = g.Save
            g.DrawString(i & " " & companycity & " - " & Now.ToString("M-d-yy HH:mm:ss tt"), font, Syncfusion.Pdf.Graphics.PdfBrushes.Black, New System.Drawing.PointF(30, lpage.Graphics.ClientSize.Height - 35))

            newPage.Graphics.Restore()
            i += 1
        Next

        Dim ms As New System.IO.MemoryStream
        newDoc.Save(ms)

        lDoc.Close()
        newDoc.Close()

        System.IO.File.WriteAllBytes(path, ms.ToArray)
        ms.Dispose()

and here's my code for printing:

        Dim viewer As New Syncfusion.Windows.Forms.PdfViewer.PdfDocumentView
        viewer.Load(InputfilePath)

        Dim File As String = Split(InputfilePath, "\")(4)

        Dim print As New System.Windows.Forms.PrintDialog()
        print.Document = viewer.PrintDocument
        print.Document.DocumentName = File
        print.Document.Print()
        viewer.Unload()
        viewer.Dispose()


Attachment: 101016132006andyd273new_fd1b1908.zip

Chinnu Muniyappan [Syncfusion]
Replied On October 11, 2016 07:25 AM

Hi Andy, 
 
Thank you for your update. 
 
We have created a sample for achieving your requirement, please refer the below code snippet and sample for more details. 
 
        'Create a new PDF section 
        Dim section As PdfSection 
 
        For Each lpage As Syncfusion.Pdf.PdfPageBase In lDoc.Pages 
 
            'Add the section 
            section = newDoc.Sections.Add() 
 
            'Set the page size  
            section.PageSettings.Size = lpage.Size 
 
            'Set margins 
            section.PageSettings.Margins.All = 0 
 
            'Add a new page  
            newPage = section.Pages.Add() 
 
            'Create template. 
            template = lpage.CreateTemplate 
 
Sample link: 
 
Please let us know if you have any concern. 
 
Regards, 
Chinnu 


Andy Dillbeck
Replied On October 14, 2016 03:53 PM

This appears to all be resolved.
I didn't use the section thing, I just specified that the everything should use Syncfusion.Pdf.PdfPageSize.Letter, which makes sure that everything fits on a page even if it has to be squished slightly.

        Dim lDoc As New Syncfusion.Pdf.Parsing.PdfLoadedDocument(path)
        Dim newDoc As New Syncfusion.Pdf.PdfDocument

        newDoc.PageSettings.Size = Syncfusion.Pdf.PdfPageSize.Letter
        newDoc.PageSettings.Margins.All = 0

        Dim newPage As Syncfusion.Pdf.PdfPage
        Dim template As Syncfusion.Pdf.Graphics.PdfTemplate
        Dim font As Syncfusion.Pdf.Graphics.PdfFont = New Syncfusion.Pdf.Graphics.PdfStandardFont(Syncfusion.Pdf.Graphics.PdfFontFamily.Helvetica, 12.0F)
        Dim i As Integer = 1

        For Each lpage As Syncfusion.Pdf.PdfPageBase In lDoc.Pages
            newPage = newDoc.Pages.Add
            template = lpage.CreateTemplate

            newPage.Graphics.DrawPdfTemplate(template, New System.Drawing.PointF(5, 5), New System.Drawing.SizeF(Syncfusion.Pdf.PdfPageSize.Letter.Width - 10, Syncfusion.Pdf.PdfPageSize.Letter.Height - 35))

            Dim g As Syncfusion.Pdf.Graphics.PdfGraphics = newPage.Graphics
            Dim state As Syncfusion.Pdf.Graphics.PdfGraphicsState = g.Save
            g.DrawString(i & " " & companycity & " - " & Now.ToString("M-d-yy HH:mm:ss tt"), font, Syncfusion.Pdf.Graphics.PdfBrushes.Black, New System.Drawing.PointF(30, Syncfusion.Pdf.PdfPageSize.Letter.Height - 35))

            newPage.Graphics.Restore()
            i += 1
        Next

        Dim ms As New System.IO.MemoryStream
        newDoc.Save(ms)

        lDoc.Close()
        newDoc.Close()

        System.IO.File.WriteAllBytes(path, ms.ToArray)
        ms.Dispose()


Chinnu Muniyappan [Syncfusion]
Replied On October 17, 2016 06:14 AM

Hi Andy, 
 
While using PdfPageSize.Letter was working fine because the letter size large than the default page size but the input document contains various page sizes, please refer the below screenshot, while using sections the output document also preserves the same size as input PDF page size, so we are suggesting you to use PdfSection to create a document with various page size. 
 
 
 
Regards, 
Chinnu 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;