PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Sun Jul 14, 2024 11:01 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: duplicates pdf files
PostPosted: Fri Dec 12, 2014 6:30 am 
Offline

Joined: Fri Dec 12, 2014 6:19 am
Posts: 5
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: duplicates pdf files
PostPosted: Fri Dec 12, 2014 9:08 pm 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 343
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.

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
 Post subject: Re: duplicates pdf files
PostPosted: Wed Dec 17, 2014 12:59 am 
Offline

Joined: Fri Dec 12, 2014 6:19 am
Posts: 5
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 470 times
Top
 Profile  
Reply with quote  
 Post subject: Re: duplicates pdf files
PostPosted: Thu Dec 18, 2014 4:07 am 
Offline

Joined: Fri Dec 12, 2014 6:19 am
Posts: 5
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


Top
 Profile  
Reply with quote  
 Post subject: Re: duplicates pdf files
PostPosted: Mon Jan 05, 2015 1:37 am 
Offline

Joined: Fri Dec 12, 2014 6:19 am
Posts: 5
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: duplicates pdf files
PostPosted: Mon Jan 05, 2015 5:37 pm 
Offline
PDFsharp Expert
User avatar

Joined: Wed Dec 09, 2009 8:59 am
Posts: 343
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.

_________________
Öhmesh Volta ("() => true")
PDFsharp Team Holiday Substitute


Top
 Profile  
Reply with quote  
 Post subject: Re: duplicates pdf files
PostPosted: Thu Jan 08, 2015 7:27 am 
Offline

Joined: Fri Dec 12, 2014 6:19 am
Posts: 5
Our guess:

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


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

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 18 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