empira / PDFsharp-1.5

A .NET library for processing PDF
MIT License
1.28k stars 588 forks source link

Put Text in bottom Landscape page wrong #98

Open JorgelGtzGtz opened 5 years ago

JorgelGtzGtz commented 5 years ago

Hello I try to put text in Landscape page in the bottom, but not work. I attach example and I mark with yellow without text where is the correct position to put the text.

POSITION

This is my code : public static bool PdfSharpStampPdf(string[] files, string textStamp, out string message) { bool result = false; message = string.Empty; try { foreach (var file in files) { // Open the document using (PdfDocument inputDocument = PdfReader.Open(file, PdfDocumentOpenMode.Import)) { PdfDocument PDFNewDoc = new PdfDocument(); // Iterate pages foreach (PdfPage page in inputDocument.Pages) { PdfPage pp = PDFNewDoc.AddPage(page); string stampText = Path.GetFileNameWithoutExtension(file) + " " + textStamp; XGraphics gfx = XGraphics.FromPdfPage(pp); XRect rect = new XRect(); if (page.Orientation == PdfSharp.PageOrientation.Landscape) { rect.X = 0; rect.Y = 0; rect.Width = pp.Width; rect.Height = pp.Height; } else { rect.X = 0; rect.Y = 0; rect.Width = pp.Width; rect.Height = pp.Height; }

                        XFont font = new XFont("Arial", 8, XFontStyle.Regular);
                        if (page.Orientation == PdfSharp.PageOrientation.Landscape)
                        {
                            gfx.DrawString(stampText, font, XBrushes.Black, rect, XStringFormats.BottomCenter);
                        }
                        else {
                            gfx.DrawString(stampText, font, XBrushes.Black, rect, XStringFormats.BottomCenter);
                        }

                    }
                    PDFNewDoc.Save(file);
                }
            }
            result = true;
        }
        catch (Exception ex)
        {
            result = false;
            message = String.Format("Error PdfSharp occured for stamp Documents Exception: {0}", ex.Message);
        }
        return result;
    }
Spiker985 commented 4 years ago

@JorgelGtzGtz I know this is old. But why even check for the orientation change on the page if you're just going to execute the exact same code, with the exact same values in the variables?

@ThomasHoevel You may want to mark this issue as closed.