0

I have written a C# code that merges two pdf files upon downloading together but it rotates some (not all) documents 180 degrees. I have attached the sample file that I am uploading.

Kindly, help me on this.

Sample file

try
    {
        string NewFileName = "InspectionReport" + DateTime.Now.ToString("ddMMMyyyy HHmmss").Replace(" ", "");
        destinationFile = HostingEnvironment.MapPath(@"/Downloads/CNGCertificates/" + NewFileName + ".pdf");

        int f = 0;

        PdfReader reader = new PdfReader(sourceFiles[f]);

        int n = reader.NumberOfPages;

        Document document = new Document(reader.GetPageSizeWithRotation(1));

        PdfWriter writer = PdfWriter.GetInstance(document, new FileStream(destinationFile, FileMode.Create));

        document.Open();
        PdfContentByte cb = writer.DirectContent;
        PdfImportedPage page;
        int rotation;

        while (f < sourceFiles.Length)
        {
            int i = 0;
            while (i < n)
            {
                i++;
                document.SetPageSize(reader.GetPageSizeWithRotation(i));
                document.NewPage();
                page = writer.GetImportedPage(reader, i);
                rotation = reader.GetPageRotation(i);
                if (rotation == 90 || rotation == 270)
                {
                    cb.AddTemplate(page, 0, -1f, 1f, 0, 0, reader.GetPageSizeWithRotation(i).Height);
                }
                else
                {
                    cb.AddTemplate(page, 1f, 0, 0, 1f, 0, 0);
                }

            }
            f++;
            if (f < sourceFiles.Length)
            {
                reader = new PdfReader(sourceFiles[f]);

                n = reader.NumberOfPages;

            }
        }

        document.Close();
    }
Filburt
  • 17,626
  • 12
  • 64
  • 115
Testingapps
  • 15
  • 1
  • 3
  • 1
    Without looking into much detail: You are handling `rotation == 90` and rotation == 270` so maybe this is just a matter of also checking for `rotation == 180`. Did you try stepping through using the debugger to inspect cases where the unwanted rotation occurs? I would expect this being reproducable. – Filburt Sep 01 '20 at 11:26
  • Are you sure the source files are correct? And rotation does not say anything about the actual document. If I put a paper upside down in the scanner the rotation will still be 0. – VDWWD Sep 01 '20 at 11:26
  • @Filburt: i have found that the file being uploaded has 270 degrees orientation. – Testingapps Sep 01 '20 at 12:16

1 Answers1

3

You are handling rotation of 90 and 270 identically. Thus, if source pages with a rotation value of 90 are handled as you expect, source pages with a rotation value of 270 consequentially will end up upside down.

Thus, you have to handle 270 separately, using the affine transformation rotating the other way:

cb.AddTemplate(page, 0, 1f, -1f, 0, reader.GetPageSizeWithRotation(i).Width, 0);

Similarly you'll have to handle the 180 value separately, too, unless you want such pages to end up upside down:

cb.AddTemplate(page, -1f, 0, 0, -1f, reader.GetPageSizeWithRotation(i).Width, reader.GetPageSizeWithRotation(i).Height);

That all being said, you actually shouldn't use a normal PdfWriter for merging at all, you should use PdfCopy. Using PdfCopy you don't have to deal with details like rotation anymore. Furthermore, annotations are copied along and not dropped as with PdfWriter. Look here for example. (If you use a 5.5.x version, you don't have to differentiate between the PdfCopy variants mentioned there anymore, you can use the plain PdfCopy for all use cases. If you use a 4.x version, though, you do have to pick appropriately.)

mkl
  • 90,588
  • 15
  • 125
  • 265
  • Not *the same*. Similarly to the 270° case, the 180° case needs *its own parameters*. I've edited my answer accordingly. – mkl Sep 01 '20 at 14:06