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

duplicates pdf files
https://forum.pdfsharp.net/viewtopic.php?f=2&t=3011
Page 1 of 1

Author:  sunsnow [ Fri Dec 12, 2014 6:30 am ]
Post subject:  duplicates pdf files

Hi all,

I am using a method:

Code:
private void MergeMultiplePDFIntoSinglePDF(string outputFilePath, StudentsFile[] pdfFiles)
        {
            var outputPdfDocument = new PdfDocument();

            var format = new XStringFormat();
            format.Alignment = XStringAlignment.Center;
            format.LineAlignment = XLineAlignment.Far;
            var font = new XFont("Verdana", 10, XFontStyle.Bold);

            foreach (var pdfFile in pdfFiles)
            {
                PdfDocument inputPdfDocument = PdfReader.Open(pdfFile.File, PdfDocumentOpenMode.Import);
                outputPdfDocument.Version = inputPdfDocument.Version;
                foreach (PdfPage page in inputPdfDocument.Pages)
                {
                    var newPage = outputPdfDocument.Pages.Add(page);

                    // Write student name on each page
                    XGraphics gfx = XGraphics.FromPdfPage(newPage);
                    XRect box = newPage.MediaBox.ToXRect();
                    box.Inflate(0, -10);
                    gfx.DrawString(String.Format("{0}", pdfFile.Student),
                      font, XBrushes.Red, box, format);
                }
            }
           
            outputPdfDocument.Save(outputFilePath);
        }


I send an array of different pdf files but in result I have a first pdf file cloned several times. :(
I tried many things: AddPage, InsertPage in different ways...
I made sure that files are different and almost broke my head :(

Pdf Sharp + MigrDoc
Version: 1.32.43334.0 (got from NuGet)

Thank you.

Author:  () => true [ Fri Dec 12, 2014 9:08 pm ]
Post subject:  Re: duplicates pdf files

Hi!

Often the error is not in the code snippet shown here.

Are you sure "pdfFiles" is filled correctly?

Maybe you can provide an SSCCE that allows to replicate the problem - especially if you think it is a bug in PDFsharp.

Author:  sunsnow [ Wed Dec 17, 2014 12:59 am ]
Post subject:  Re: duplicates pdf files

Hi,

this is a full program (it requires PdfSharp - I got it from NuGet):

Code:
using System;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;

namespace PdfSharpTest
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] files = new string[2];
            files[0] = @"C:\temp\PDF\completed_Smith_Anne-Marie3.pdf";
            files[1] = @"C:\temp\PDF\completed_Stewart_Emma3.pdf";

            MergeMultiplePdfIntoSinglePdf(files);
        }

        private static void MergeMultiplePdfIntoSinglePdf( string[] pdfFiles, string outputFilePath = @"D:\test.pdf")
        {
            var outputPdfDocument = new PdfDocument();

            var format = new XStringFormat();
            format.Alignment = XStringAlignment.Center;
            format.LineAlignment = XLineAlignment.Far;
            var font = new XFont("Verdana", 10, XFontStyle.Bold);

            foreach (var pdfFile in pdfFiles)
            {
                PdfDocument inputPdfDocument = PdfReader.Open(pdfFile, PdfDocumentOpenMode.Import);
                outputPdfDocument.Version = inputPdfDocument.Version;
                foreach (PdfPage page in inputPdfDocument.Pages)
                {
                    var newPage = outputPdfDocument.Pages.Add(page);

                    // Write student name on each page
                    XGraphics gfx = XGraphics.FromPdfPage(newPage);
                    XRect box = newPage.MediaBox.ToXRect();
                    box.Inflate(0, -10);
                    gfx.DrawString(String.Format("{0}", pdfFile),
                      font, XBrushes.Red, box, format);
                }
            }

            outputPdfDocument.Save(outputFilePath);
        }
    }


}


In addition, the combined file is empty for the second file.
I attached combined files (only first 2 pages have data - I had to delete the rest)

Thank you.

Attachments:
File comment: an applications with fake data
completed_Stewart_Emma3.zip [168.63 KiB]
Downloaded 467 times
File comment: an applications with fake data
completed_Smith_Anne-Marie3.zip [169.28 KiB]
Downloaded 471 times

Author:  sunsnow [ Thu Dec 18, 2014 4:07 am ]
Post subject:  Re: duplicates pdf files

I noticed one thing:

the code works fine with files that were not created using pdf sharp.

if you try it with file what were created using pdf sharp (see attachments) - it doesn't work

Author:  sunsnow [ Mon Jan 05, 2015 1:37 am ]
Post subject:  Re: duplicates pdf files

I tried Dynamic Pdf (http://www.dynamicpdf.com/) to merge files and works fine.
To be honest, I would like to use PdfSharp - it is has already integrated into my project and it is a headache to ask my company for any money.

Author:  () => true [ Mon Jan 05, 2015 5:37 pm ]
Post subject:  Re: duplicates pdf files

sunsnow wrote:
To be honest, I would like to use PdfSharp
Nice to hear that.
I was able to replicate the problem, but I cannot fix it.
I don't have feedback from Stefan when I plans to fix it.

Author:  sunsnow [ Thu Jan 08, 2015 7:27 am ]
Post subject:  Re: duplicates pdf files

Our guess:

these files have controls with the same id.
Is it a reason?

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