0

iTextSharp V5.0.2. This code works perfectly 95% of the time. But some landscape pdfs that clients load come in shifted down and to the left. I can see the amount in the Rectangle properties .Left and .Bottom, but I have no idea how to fix this. This is adding a header to the top of each page.

I would like to shift the content back up and over to the left and top for the pages where everything is off so the header is flush with the top of the page

public string Add(string path)
{
        string tempPath = path + "HFtemp";
        PdfStamper pdf = new PdfStamper(new PdfReader(path), new FileStream(tempPath, FileMode.Create));

        for (Int32 nPage = 1; nPage <= pdf.Reader.NumberOfPages; nPage++)
        {
            if ((Header))
            {
                PdfContentByte content = pdf.GetOverContent(nPage);

                Rectangle pageRectangle = pdf.Reader.GetPageSizeWithRotation(nPage);
                float h = pageRectangle.Height;
                float w = pageRectangle.Width;
                //float left = pageRectangle.Left;      //(-) 13.011
                //float bottom = pageRectangle.Bottom;  //(+) 13.011

                float y = h - Header_Height;
                // x, y, w, h
                content.Rectangle(0, y, w, Header_Height);

                content.SetRGBColorFill(Header_Color.R, Header_Color.G, Header_Color.B);
                content.Fill();
                content.SaveState();
                content.RestoreState();
            }

        }
        pdf.Close();
        return tempPath;
    }

This is how the bad documents look. Even though I'm setting x and y to the correct values, the document's x and y are off.

Here's what the bad ones look like

mkl
  • 90,588
  • 15
  • 125
  • 265
Steven B
  • 19
  • 4
  • Are the PDFs generated by an image scanning system? So imagine portraits coming from image system A and landscapes coming from image System B. By system I mean a separate piece of hardware (or software) in the imaging chain, might be the same model or software but two different physical pieces of machinery or original scan images. – Sql Surfer Oct 12 '15 at 02:38
  • The images that are an issue (so far) are coming from an Excel export piece of software. At least that's what I'm seeing in the pdf properties. But I have no control on what the clients upload and where it comes from. Could be from a huge variety of sources. – Steven B Oct 12 '15 at 03:32
  • 1
    *This code works perfectly 95% of the time* - but what exactly do you *expect* it to do? – mkl Oct 12 '15 at 04:15
  • Here is my two bits. Your code is making assumptions that work fine in your "good" case. In your bad case the image densities and pixels per logical inch and other properties do not work with the numeric values you are using. So imagine the Page and content properties being on different measuring systems so the Height width numeric values do not match the rectangle values like you think. Do you need to get an orientation property and flip height and width as well? The difference I am talking about is similar to 600dpi vs 1200 dpi within the pdf. – Sql Surfer Oct 12 '15 at 04:20
  • I see that you get the value of the `/MediaBox` (the page size), but that you are ignoring the `/CropBox` (the visible part of the page). In any case: I don't understand your question. You'll have to provide an example that reproduces and explains the problem. – Bruno Lowagie Oct 12 '15 at 06:07
  • @StevenB Your Edit does not really make things clearer. The code you show definitively does *not* cause *some pages* to be *shifted down and right.* Furthermore the order of operations you apply to the `content` is incorrect: You have to set the color *before* starting to define the path. PDF viewers tend to be lax in this respect, though. – mkl Oct 12 '15 at 14:48
  • @mkl The code is not causing the issue. Some pages come in shifted. I'm trying to find a way to adjust them. As stated it works on 95% of the incoming pages, but some are down and to the right. I can identify these by inspecting the .Left and .Bottom properties. Pages that work have zero for those property values. I don't know enough about iTextSharp to know if I can remove or adjust the shift. – Steven B Oct 12 '15 at 16:13
  • So not iText shifts them down and right, but instead they already are shifted and you would like to fix that using iText? (I ask because the title of your question seems to indicate that it is iText's fault.) Can you share a sample PDF with both correct and shifted pages? – mkl Oct 12 '15 at 19:35
  • According to your image, not the page is shifted down and right but that header line is... – mkl Oct 12 '15 at 20:43

0 Answers0