PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Fri Nov 01, 2024 12:59 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 1 post ] 
Author Message
PostPosted: Fri Aug 15, 2014 4:40 pm 
Offline

Joined: Fri Aug 15, 2014 3:55 pm
Posts: 1
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


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 25 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