PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Nov 10, 2024 5:51 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 Jun 10, 2021 9:12 am 
Offline

Joined: Thu Jun 10, 2021 9:05 am
Posts: 1
Page is blackened after adding watermark in case of some pdf files . Please see attached image.
What could be the reason , and possible fix.

Here is code

Page is blackened after adding watermark in case of some pdf files . Please see attached image.
What could be the reason , and possible fix.

[see the blacked out page image][1]


[1]: https://i.stack.imgur.com/vPw4u.png


It does not happen for all the files but for some files only.
Code:
    var _pdfInBytes = File.ReadAllBytes("c:\\test\\test123.pdf");
                    string watermarkText = "This watermark text on left side";
                    var coordinates = new Point(25, 200);
                    using (var pdfNewDoc = new PdfDocument())
                    {
                        using (var pdfImport = PdfReader.Open(new MemoryStream(_pdfInBytes, true), PdfDocumentOpenMode.Import))
                        {
                            if (pdfImport.PageCount == 0)
                            {
                                return;
                            }
                            foreach (var pg in pdfImport.Pages)
                            {
                                pdfNewDoc.AddPage(pg);
                            }
                            var page = pdfNewDoc.Pages[0];
                            // overlapping trick #165910
                            var xOffset = 100.0;
                           for (var index = 0; index < page.Contents.Elements.Count; index++)
                            {
                                var stream = page.Contents.Elements.GetDictionary(index).Stream;
                                var x = GetMinXOffsetDraft(stream.ToString());   
                                if (xOffset > x){                            {
                                    xOffset = x;
                                }
                            }
                            xOffset *= 0.6; // magic number :)
                            // blank page trick #165910
                            if (page.CropBox.IsEmpty && !page.MediaBox.IsEmpty){
                              page.CropBox = page.MediaBox;
                            }
                            // Get an XGraphics object for drawing beneath the existing content
                            var gfx = XGraphics.FromPdfPage(page, XGraphicsPdfPageOptions.Prepend);                   
                            var tf = new XTextFormatter(gfx);                       
                            var xFont = new XFont("Arial", 10, XFontStyle.Regular);   
                            // Get watermark text size
                            var wmSize = gfx.MeasureString(watermarkText, xFont);   
                            // Middle Y coordinate
                            var wmY = (gfx.PageSize.Height - wmSize.Width) / 2;   
                            var coords = new XPoint(page.CropBox.Location.X + (xOffset < coordinates.X ? xOffset : coordinates.X),
                                page.CropBox.Location.Y + (coordinates.Y > wmY ? coordinates.Y :wmY));   
                            // Define a rotation transformation at the center of the page
                            gfx.TranslateTransform(coordinates.X, coordinates.Y);
                            gfx.RotateTransform(90);
                            gfx.TranslateTransform(-coordinates.X, -coordinates.Y);   
                            // Create brush
                            var brushColor = Color.Red;
                            var brush1= new XSolidBrush(XColor.FromArgb(brushColor.A, brushColor.R, brushColor.G, brushColor.B));
                            brush1.Overprint = false;
                            XBrush brush =
                                new XSolidBrush(XColor.FromArgb(brushColor.A, brushColor.R, brushColor.G, brushColor.B));                       
                            var rect = new XRect(coordinates.X, coordinates.Y, gfx.PageSize.Height -coordinates.Y,
                                coordinates.X);   
                            tf.DrawString(watermarkText, xFont, brush, rect);
                            byte[] outputBytes = null;
                            using (var outStream = new MemoryStream())
                            {
                                pdfNewDoc.Save(outStream, false);
                                outputBytes = outStream.ToArray();
                            }
                            File.WriteAllBytes("c:\\test\\test-"+DateTime.Now.ToString("ddmmyyyyhhmmss") +".pdf", outputBytes);
                        }
                }


Attachments:
black-bkg-watermark.PNG
black-bkg-watermark.PNG [ 206.71 KiB | Viewed 5974 times ]
Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 10, 2021 11:57 am 
Offline
PDFsharp Guru
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 1007
Location: CCAA
Hi!

Which version of PDFsharp do you use?
Why don't you use the Issue Submission Template?

Maybe it makes a difference to wrap the watermark code with "gfx.Save()" and "gfx.Restore()".

P.S.: Also discussed on SO:
https://stackoverflow.com/q/67919243/162529

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


Top
 Profile  
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: No registered users and 6 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:  
cron
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group