PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Wed Dec 19, 2018 1:58 pm

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Thu Sep 15, 2011 12:43 pm 
Offline

Joined: Thu Sep 15, 2011 12:25 pm
Posts: 2
Hi,

I've got an issue with creating a new PDF file by combining other PDF files.
The data seems to be in the PDF but the page is blank when opening.

We got the following process
1) Get a default information PDF (this is static)
2) Create a PDF file for a TIFF, this creates a multi page PDF (in the zip file org_Pdf)
3) Using the following code, we create 1 big PDF
Code:
using (PdfDocument outputDocument = new PdfDocument())
{
   PdfDocumentRenderer pdfRenderer;

   // loop all small documents
   foreach (PdfDocument file in files)
   {
      if (file.PageCount > 0)
      {
         pdfRenderer = new PdfDocumentRenderer();
         pdfRenderer.DocumentRenderer = new DocumentRenderer(new MigraDoc.DocumentObjectModel.Document());
         pdfRenderer.PdfDocument = file;

         using (MemoryStream ms = new MemoryStream())
         {
            pdfRenderer.Save(ms, false);
            logger.DebugFormat(CultureInfo.InvariantCulture, "Stream contains {0} bytes of data", ms.Length);

            // move to the start of the stream
            ms.Seek(0, SeekOrigin.Begin);
            ms.Flush();

            using (PdfDocument inputDocument = PdfReader.Open(ms, PdfDocumentOpenMode.Import))
            {
               for (int idx = 0; idx < inputDocument.PageCount; idx++)
               {
                  outputDocument.AddPage(inputDocument.Pages[idx]);
                  logger.DebugFormat(CultureInfo.InvariantCulture, "Page {0} of document added to big document on page {1}", idx+1, outputDocument.PageCount);

                  // create extra files to check each PDF page
                  counter++;
                  if (!FileHelper.DirExists(@"c:\temp\singlefiles\"))
                     Directory.CreateDirectory(@"c:\temp\singlefiles\");

                  using(PdfDocument newDoc = new PdfDocument())
                  {
                     newDoc.AddPage(inputDocument.Pages[idx]);
                     newDoc.Save(@"c:\temp\singlefiles\" + counter + ".pdf");
                  }
               }
            }
         }
      }
   }

   // add an extra page if its an odd count but not zero
   if ((outputDocument.PageCount > 0) && (outputDocument.PageCount % 2 != 0))
   {
      outputDocument.AddPage(new PdfPage());
      logger.Info("Extra page added");
   }

   outputDocument.Close();
   return outputDocument;
}


The issue is that the new PDF created has the correct amount of pages, but the 2nd PDF (created from the TIFF) isn't visible.
This is also true for the single page saves (this is file new_pdf.pdf in the zip).

When looking at the 2 files with notepad++ and comparing them, the data seems the same.
But the attributes/options/ref/... in the file aren't.

In debug mode the issue doesn't exists, only when building the process (in 64-bit) this seems to be not working.
Can some1 shine a light on this issue?

(ps: we also tried to change the corflags of the application to 32bit with the Microsoft tool, but still the same issue)


Attachments:
exp pdf.zip [108.01 KiB]
Downloaded 295 times


Last edited by Kenny on Fri Sep 16, 2011 8:08 am, edited 1 time in total.
Top
 Profile  
Reply with quote  
PostPosted: Thu Sep 15, 2011 2:18 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 2897
Location: Cologne, Germany
Hi!

Since you don't provide working code (just a code snippet), I wrote my own code (I added this code snippet to the HelloWorld sample):
Code:
{
  PdfDocument doc = new PdfDocument();

  using (PdfDocument inputDocument = PdfReader.Open(@"C:\Users\thho\Desktop\$$$\$$$PDFsharp\exp pdf\org_pdf.pdf", PdfDocumentOpenMode.Import))
  {
    doc.AddPage(inputDocument.Pages[0]);
  }
  doc.Save(@"C:\Users\thho\Desktop\$$$\$$$PDFsharp\exp pdf\new_pdf_by_thho.pdf");
}

This code works in both Debug and Release build and I get a PDF file that displays the image correctly.

Unless you provide complete code that allows me to replicate the problem, I won't investigate this any further.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Sep 16, 2011 9:37 am 
Offline

Joined: Thu Sep 15, 2011 12:25 pm
Posts: 2
Hi,

I've looked again into this issue and seem to have found the problem.
It wasn't the combine that's creating the issue (or that is not the place I fixed it), but the creating of the PDF from the TIFF.

When creating the PDF from the tiff file, we save the result and add the PDFDocument object to a list, this list we use to create the big PDF.
The file is present, but something is wrong when just using the created PDFDocument as input for another PDF document.
If I reread the file using PDFReader.Open and add that document to the list instead of the doc used to create that file, it works.
Below is the code of a test program I used.
- Input File1 - a PDF file
- Input File2 - a TIF/TIFF file
- Output tempFile - a PDF file (create from the tiff)
- Output newFile - a PDF file containing file1 and tempFile

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PdfSharp.Pdf;
using PdfSharp.Pdf.IO;
using MigraDoc.Rendering;
using System.IO;
using System.Globalization;
using PdfSharp.Drawing;

namespace PDFCombineTest
{
    class Program
    {
        static List<PdfDocument> files;

        static void Main(string[] args)
        {
            files = new List<PdfDocument>();
            files.Add(PdfReader.Open(@"c:\temp\file1.pdf"));
            CreatePDFFromTiff(@"c:\temp\file2.tif");

            PdfDocument newFile = Combine(files);
            newFile.Save(@"c:\temp\newFile.pdf");
        }

        static void CreatePDFFromTiff(string fileName)
        {
            TiffSplitter tiff = new TiffSplitter();
            using (PdfDocument doc = new PdfDocument())
            {
                int pageCount = tiff.getPageCount(fileName);

                for (int i = 0; i < pageCount; i++)
                {
                    PdfPage page = doc.AddPage();
                    page.Size = PdfSharp.PageSize.A4;

                    using (XImage img = XImage.FromGdiPlusImage(tiff.getTiffImage(fileName, i)))
                    {
                        using (XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[i]))
                        {
                            xgr.DrawImage(img, 0, 0, img.PointWidth, img.PointHeight);
                        }
                    }
                }

                // if it's not an odd page count
                if (doc != null && doc.PageCount > 0 && (doc.PageCount % 2 != 0))
                {
                    // add a page
                    doc.AddPage();
                }

                doc.Save(String.Format(CultureInfo.InvariantCulture, @"C:\temp\tempFile.pdf", Guid.NewGuid()));
                //doc.Close();

                files.Add(PdfReader.Open(@"C:\temp\tempFile.pdf")); // works
                //files.Add(doc); // works not
            }
        }

        static PdfDocument Combine(List<PdfDocument> files)
        {
            using (PdfDocument outputDocument = new PdfDocument())
            {
                PdfDocumentRenderer pdfRenderer;

                // loop all small documents
                foreach (PdfDocument file in files)
                {
                    if (file.PageCount > 0)
                    {
                        pdfRenderer = new PdfDocumentRenderer();
                        pdfRenderer.DocumentRenderer = new DocumentRenderer(new MigraDoc.DocumentObjectModel.Document());
                        pdfRenderer.PdfDocument = file;

                        using (MemoryStream ms = new MemoryStream())
                        {
                            pdfRenderer.Save(ms, false);

                            // move to the start of the stream
                            ms.Seek(0, SeekOrigin.Begin);
                            ms.Flush();

                            using (PdfDocument inputDocument = PdfReader.Open(ms, PdfDocumentOpenMode.Import))
                            {
                                for (int idx = 0; idx < inputDocument.PageCount; idx++)
                                {
                                    outputDocument.AddPage(inputDocument.Pages[idx]);
                                }
                            }
                        }
                    }
                }

                // add an extra page if its an odd count but not zero
                if ((outputDocument.PageCount > 0) && (outputDocument.PageCount % 2 != 0))
                {
                    outputDocument.AddPage(new PdfPage());
                }

                outputDocument.Close();
                return outputDocument;
            }
        }
    }
}


Attachments:
File comment: file1 and file2 for testing
test files.zip [101.05 KiB]
Downloaded 303 times
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


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