PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri May 29, 2020 12:57 pm

All times are UTC

Forum rules

Please read this before posting on this forum: Forum Rules

Post new topic Reply to topic  [ 2 posts ] 
Author Message
PostPosted: Thu Mar 09, 2017 9:02 pm 

Joined: Thu Mar 09, 2017 7:16 pm
Posts: 1
Update: I did the code modification discussed in the topic '
PdfSharp wrongly interprets orientation (viewtopic.php?f=2&t=610&hilit=rotate+270+landscape)' and modified my code to set the width and height after the rotation (it was getting set to 612x612 if not) and this seemed to have resolved this issue.

The code change I did was to PdfSharp-gdi/PdfPage.cs->internal override void WriteObject(PdfWriter writer) {} (line 579) and I commented out the 'hack' of creating the MediaBox and also the line where it reassigned the media box towards the bottom of the procedure.

--- End Update ---

I have an application that is used to do some conversions of documents from various systems. I have recently (last 2 years) implemented the PdfSharp library to handle some of the PDF operations within this product. Recently, two clients have reported images that get processed with my product and are getting corrupted. The pages appear as if the background/frame of the pages is set to landscape, but the text is drawn on it in portrait. It does not happen to all of the pages of the document, but it does happen pretty consistently with some of their files.

I have narrowed the file down to being a PDF file that is created by a multi-function copier/scanner devices (Konika Minolta BizHubs and Xerox Workcenters so far). The page appears just fine in other programs before being processed, but once it is ran though my process, it becomes corrupted. Inspecting the files show that the pages are set to "Landscape" with a rotate set to 270 degrees. I have tried to add in the following code block to resolve this, and sometimes it works, sometimes it leaves the page sideways (270 degrees) and sometimes leaves the page messed up with the original cropping issue I am trying to fix:

   outPDF = PdfSharp.Pdf.IO.PdfReader.Open(NewName, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Modify)
   inPDF = PdfSharp.Pdf.IO.PdfReader.Open(_DocFilename, PdfSharp.Pdf.IO.PdfDocumentOpenMode.Import)

   For intPage As Int16 = 0 To inPDF.PageCount - 1
      If inPDF.Pages.Item(intPage).Elements.ContainsKey("/Rotate") Then
         Dim val As Integer = inPDF.Pages.Item(intPage).Elements.GetInteger("/Rotate")
         Debug.WriteLine("       Rotation = " & val.ToString & ", Orientation = " & inPDF.Pages.Item(intPage).Orientation.ToString)
         Debug.WriteLine("       Size = " & inPDF.Pages.Item(intPage).Width.Point.ToString & " x " & inPDF.Pages.Item(intPage).Height.Point)
         If inPDF.Pages.Item(intPage).Orientation = PdfSharp.PageOrientation.Landscape And val = 270 Then
            Dim OldWidth As Integer = inPDF.Pages.Item(intPage).Width.Point
            Dim OldHeight As Integer = inPDF.Pages.Item(intPage).Height.Point
            If OldWidth < OldHeight Then

               ' We need to fix this page??
               Debug.WriteLine("                     --- Fixing this page")

               inPDF.Pages.Item(intPage).Width = OldHeight
               inPDF.Pages.Item(intPage).Height = OldWidth

               inPDF.Pages.Item(intPage).Rotate = -90
               inPDF.Pages.Item(intPage).Orientation = PdfSharp.PageOrientation.Portrait

            End If
         End If
      End If

Catch ex As Exception
   LogException("Saving File", "Can not append non-image pdf to existing non-pdf file.")
End Try

My process basically takes each page of the file, copies it out to a temporary location (using another 3rd party system). Does some processing (if user defines any) and them uses the PdfSharp library to multipage the PDF files back together in the destination location. I have inspected the temporary files coming from the source location, and do not see the same issue. Also, this has just started occurring once I started including the PdfSharp calls to do the multipage (before it would just export a bunch of single page files, but they where all great).

I have tried searching this site, the other PdfSharp sites, and Google about this issue the last couple of days, but have not found anything that really seemed to lead anywhere. Personally, I do not really want to modify the PdfSharp code to try to resolve this, but would be happy to if lead in the right direction.

I started with PdfSharp version 1.32 GDI+
I just downloaded and tested with version 1.50-beta3b GDI+

I use the GDI+ in order to convert other file types to PDF if necessary by using DrawBitmap calls of rendered versions of the other file formats (which all seems to work great).

I do have one file that I am allowed to distribute on a limited basis, so please send me a message with your email if you would like a sample file. This file, when ran through the above code, will write to debug log the following for each and every page of the file:

Rotation = 270, Orientation = Landscape
Size = 612 x 792
--- Fixing this page

Much thanks,

Reply with quote  
PostPosted: Fri Mar 10, 2017 5:59 pm 
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 539
Location: CCAA

Have you tried the fix from this post (and only this fix):

Best regards
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)

Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC

Who is online

Users browsing this forum: Google [Bot] and 30 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group