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

Strange behaviour when generating PDFs in chain
https://forum.pdfsharp.net/viewtopic.php?f=2&t=2899
Page 1 of 1

Author:  Peter [ Fri Aug 15, 2014 4:40 pm ]
Post subject:  Strange behaviour when generating PDFs in chain

Hi,

I encounter a strange behaviour when generating a new PDF from an existing one.
My requirements are that I must generate 3 PDFs in chain using the previous ones.

Here is a simple code from a console project that demonstrates what is happening:
Code:
        static void Main(string[] args)
        {
            string folder = "c:\\Test";
            //red, yellow, green, blue
            string[] colors = { "#ff0000", "#ffff00", "#00ff00", "#0000ff" };

            for (int i = 0; i < colors.Length; i++)
            {
                string first = Path.Combine(folder, "pdfOne.pdf");
                string second = Path.Combine(folder, "pdfTwo.pdf");
                string third = Path.Combine(folder, "pdfThree.pdf");

                CreateFirstPDF(first, colors[i]);
                CreatePDF(second, first);
                CreatePDF(third, second);
            }
        }

        static void CreateFirstPDF(string destination, string colorHEX)
        {
            PdfDocument document = new PdfDocument();
            PdfPage page = document.AddPage();
            XGraphics gfx = XGraphics.FromPdfPage(page);
            XColor color = XColor.FromArgb(ColorTranslator.FromHtml(colorHEX));
            gfx.Clear(color);
            document.Save(destination);
            document.Close();
        }

        static void CreatePDF(string destination, string basePDF)
        {
            PdfDocument document = new PdfDocument();
            PdfPage page = document.AddPage();
            XGraphics gfx = XGraphics.FromPdfPage(page);

            XPdfForm basePDFForm = XPdfForm.FromFile(basePDF);
            gfx.DrawImage(basePDFForm, new XRect(0, 0, page.Width, page.Height));

            document.Save(destination);
            document.Close();
        }

Somehow, when the loop runs the first time, everything works perfect but on the following times, the third PDF is different from the second PDF.
The three times that a I run this code I ended up with the following results:
pdfOne.pdf => blue
pdfTwo.pdf => blue
pdfThree.pdf => red

I even tried deleting all the files in the directory on each iteration but the results were the same.
Is there something that I'm doing wrong?

Thanks,
Peter

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