0

I have a small Problem using iTextSharp and C#.

Context: I download PDFs and merge them into one huge.

Problem: On every page the first couple centimeters are just White and the pdf I Import starts after that White chunk.

The end of every page is correct. There is no overlapping or missing objects/text - which you would assume since it has to deal with less space. I think it might get stretched vertically.

So the Import works fine, but it always adds a few centrimeters of White on the top of every page. It feels like a top-margin. But I can't seem to fix it.

Any ideas?

I appreciate your help. Thanks a lot.

public void method()
{

    // needed variables for the pdf-merging part
    fs = new FileStream(Variables.destinationFile, FileMode.Create);
    writer = PdfWriter.GetInstance(doc, fs);
    doc.Open();
    doc.SetPageSize(PageSize.A4);
    doc.SetMargins(0f, 0f, 0f, 0f);
    pdfContent = writer.DirectContent; 

    byte[] result;
    int numPages;


    foreach (Tuple<string, string, int> currentTuple in someArray)

            try
                {
                    result = client.DownloadData(new Uri(adress + currentTuple.Item1 + ".pdf"));

                    // read and add the pages to the output file
                    reader = new PdfReader(result);
                    numPages = reader.NumberOfPages;

                    for (int i = 1; i < numPages + 1; i++)
                    {
                        doc.NewPage();
                        page = writer.GetImportedPage(reader, i);
                        pdfContent.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                    }
                catch (Exception e)
                {
                }   

        }   

        doc.Close();
        writer.Close();
        fs.Close();
}

p.s. why does it always delete my "hi there"? :)

DatRid
  • 1,169
  • 2
  • 21
  • 46
HideAndSeek
  • 374
  • 3
  • 16

1 Answers1

0

You are using the wrong method to merge documents. Your method throws away all interactivity and does not respect page sizes (which explains the problem you are reporting). Please tell me where you got the inspiration for merging documents this way, so that I can go and spank the person responsible for the example you were using ;-)

The correct way of concatenating documents is explained in chapter 6 of my book.

You can find some more examples here:

As you can see, your question has been answered many times before on StackOverflow, in the sense that many people have been using the correct way to merge documents (using PdfCopy) instead of doing it the wrong way (using PdfWriter and AddTemplate()).

In your comment, you say that the method AddPage() doesn't exist in PdfCopy. Let's take a look at the most recent version of that class: PdfCopy.cs

I clearly see:

/**
 * Add an imported page to our output
 * @param iPage an imported page
 * @throws IOException, BadPdfFormatException
 */
public virtual void AddPage(PdfImportedPage iPage) {

Note that recent versions also have an AddDocument() method:

virtual public void AddDocument(PdfReader reader) {

Using this method, you no longer have to loop over all the pages, but you can add all the pages of the PDF being read by PdfReader at once.

If you only want to add a selection of pages, you can use:

virtual public void AddDocument(PdfReader reader, List<int> pagesToKeep) {

Please do not use unofficial versions! The official version can be downloaded here: http://sourceforge.net/projects/itextsharp/files/itextsharp/

iText Group does not take any responsibility regarding old versions of iTextSharp, nor can we be held responsible for forks of our software.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • Thanks for your help! They all seem to use PdfCopy.AddPage(page); Yet in my Situation the Method is never found. It doesn't exists. I don't know what Version I am using. But it is one of the newest. Max 1 month old. – HideAndSeek Dec 01 '14 at 14:25
  • Alright. Layer 8 Problem. Thanks for your help. Fixed it. When google'ing I found most are using PdfWriter :/ – HideAndSeek Dec 01 '14 at 14:41