0

I have a function which is cropping the specific part of the pdf file and adding it into the new Pdf file but the main problem that i am getting is that it is showing the cropped part of the page into the bottom (footer) of the newly created pdf file. Here is the code..

public static void CropPdfFile(string sourceFilePath, string outputFilePath)
{
    // Allows PdfReader to read a pdf document without the owner's password
    PdfReader.unethicalreading = true;

    // Reads the PDF document
    using (PdfReader pdfReader = new PdfReader(sourceFilePath))
    {
        // Set which part of the source document will be copied.
        // PdfRectangel(bottom-left-x, bottom-left-y, upper-right-x, upper-right-y)

        PdfRectangle rect = new PdfRectangle(0f, 9049.172f, 594.0195f, 700.3f);

        using (var output = new FileStream(outputFilePath, FileMode.CreateNew, FileAccess.Write))
        {
            // Create a new document
            using (Document doc = new Document())
            {

                // Make a copy of the document
                PdfSmartCopy smartCopy = new PdfSmartCopy(doc, output);

                // Open the newly created document
                doc.Open();              

                // Loop through all pages of the source document
                for (int i = 4; i <= pdfReader.NumberOfPages; i++)
                {
                    // Get a page
                    var page = pdfReader.GetPageN(i);

                    // Apply the rectangle filter we created
                      page.Put(PdfName.CROPBOX, rect);
                      page.Put(PdfName.MEDIABOX, rect);

                    // Copy the content and insert into the new document

                      smartCopy.SetLinearPageMode();

                    var copiedPage = smartCopy.GetImportedPage(pdfReader, i);
                    smartCopy.AddPage(copiedPage);

                }

                // Close the output document
                doc.Close();
            }
        }
    }

Please help me to solve this..

user3664608
  • 333
  • 1
  • 3
  • 12
  • *showing the cropped part of the page into the bottom (footer) of the newly created pdf file* - could you please illustrate what you expect and what you get instead? Right now your description sounds like your rectangle coordinates merely are wrong for your task (lower left corner at 0f, 9049.172f looks a bit weird). – mkl May 31 '14 at 08:47
  • @mkl Yes,I am trying to get(crop) the top of the pdf page and add it into the top of the Newly created PDF ..With the present code cropping part is OK but its is getting added into the bottom of the newly created PDF.Actaully i am unaware of the PDF coordinates also .So what i should do get the thing done ... – user3664608 May 31 '14 at 08:51

1 Answers1

0

The size of a PDF page (expressed in user units) depends on the value of the mediabox. For instance: The media box of an A4 page is usually defined like this [0 0 595 842]

In this case, the origin of the coordinate system (0, 0) coincides with the lower-left corner. The coordinate of the upper right corner is (595, 842).

Another possible value for an A4 page would be [0 842 595 1684]. Same width (595 user units), same height (1684 - 842 = 842 user units), but the lower-left corner now has the coordinate (0, 842) and the coordinate of the upper-right corner is (595, 1684).

You write that you create a PdfRectangle using these parameters: (bottom-left-x, bottom-left-y, upper-right-x, upper-right-y). However, you're using these hard-coded values: 0f, 9049.172f, 594.0195f, 700.3f.

Your lower-left-y (9049.172) is at a higher position than your upper-right-y (700.3). This doesn't really make sense. Hence: you should consider changing that value to something that does make sense. What value that should be, is a question only you can answer since only you know the value of the MediaBox of the file you want to crop.

In your comment, you explain that your PDF is an A4 page. You can check this by using the PdfReader method named getPageSize(). If you want to crop the page so that you only see the header of your document, you need to use something like this:

PdfRectangle rect = new PdfRectangle(0f, 842 - x, 595, 842);

Where x is the height of the header. For instance, if the header is 100 user units, then you'd need:

PdfRectangle rect = new PdfRectangle(0f, 742, 595, 842);

It is unclear why you're always talking about 100px. If you want to convert pixels to points, please read Convert Pixels to Points

Using the formula mentioned there 100 pixels equals 75 points.

Community
  • 1
  • 1
Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • When i used [0 0 595 842] i saw the whole page got cropped ,If i am right then this shows that the page is of A4 size ,.Actually sir i am a web developer so have no idea of the coordinate system (only that much studied in high school).So if i need to crop 100% width 100px; height from the upper part of the page .What will be the coordinate system.. – user3664608 Jun 02 '14 at 03:47
  • I don't understand what is meant with "100% width 100px". The unit `px` stands for "pixel". PDFs don't measure in pixels but in user units. Also 100% is a relative measurement, whereas 100px is an absolute size. It's as if I'm speaking "Dutch" and you're speaking "Portuguese". We understand some of each others words, but we have no idea what we're saying to each other. – Bruno Lowagie Jun 02 '14 at 06:25
  • Ok .I got your point.I have one doubt which part in my dimension is placing the cropped part to the lower end of the page? – user3664608 Jun 02 '14 at 07:04
  • ` PdfRectangle rect = new PdfRectangle(0, 0, 595, 842);` This line copies whole pdf page.So it tells us that Page is of A4 size .Now,please pardon me sir as i am a webdeveloper if i want to crop 100 px; please convert 100px of web into approximate of pdf user units ,What would be the resultant coordinates? – user3664608 Jun 02 '14 at 07:21
  • I still don't understand your question. You seem to assume that a PDF file has a "resolution". That assumption is wrong. PDF is a vector format. Either you don't understand your assignment or your assignment doesn't make sense. Are you sure you want to crop (make part if the PDF invisible) or do you want to zoom? – Bruno Lowagie Jun 02 '14 at 07:46
  • Your question could improve if you provided a drawing showing what you want to do. – Bruno Lowagie Jun 02 '14 at 07:48
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54906/discussion-between-user3664608-and-bruno-lowagie). – user3664608 Jun 02 '14 at 07:57
  • Sir i have posted the image .Please have a look – user3664608 Jun 02 '14 at 08:18
  • I am waiting sir .Thank You – user3664608 Jun 02 '14 at 09:08
  • what will be the value of x? – user3664608 Jun 02 '14 at 09:12
  • 1
    Only you know that. Suppose that I ask you: "how many fingers am I showing?" Can you answer that question? No, you can't, you'd have to guess. Well, the same is true in the case of your question: you have the PDF and I can only guess. You are asking me to pick a number between 0 and 842. Don't you understand that??? – Bruno Lowagie Jun 02 '14 at 09:24