title: Returning pdf that has been clipped via iText using java/spring
body: <p>I have this spring application where a user uploads a multipart pdf to an endpoint and I want the file to get clipped via iText and then returned to the browser for viewing/download.</p>
  
  <pre><code>@RequestMapping("/upload")
    public String upload(Model model,@RequestParam("files") MultipartFile file) throws IOException {
        StringBuilder fileNames = new StringBuilder();
        File convertFile = new File(file.getOriginalFilename());
        convertFile.createNewFile();
        FileOutputStream fos = new FileOutputStream(convertFile);
        fos.write(file.getBytes());
        fos.close();
  
        System.out.println(fos.toString());
        System.out.println(fos    );
  
        model.addAttribute("msg", "Successfully uploaded files "+fos);
        return "uploadstatusview";
    }
  </code></pre>
  
  <p>I am unsuccesfully attempted to change the type of the multipart file so iText can intake it but it wont work. Please help me out with some implementation ideas beacuase right nowI am so stuck.</p>
  
  <pre><code>protected void manipulatePdf(String dest) throws Exception {
          PdfDocument pdfDoc = new PdfDocument(new PdfReader(SRC), new PdfWriter(DEST));
  
          for (int p = 1; p &lt;= pdfDoc.getNumberOfPages(); p++) {
  
              if (p == 1){
                  PdfPage page = pdfDoc.getPage(p);
  
                  Rectangle media = page.getCropBox();
  
                  if (media == null) {
                      media = page.getMediaBox();
                  }
                  float llx = media.getX() + 0;
                  float lly = media.getY() + 250;
                  float w = media.getWidth() - 250;
                  float h = media.getHeight() - 500;
                  // It's important to write explicit Locale settings, because decimal separator differs in
                  // different regions and in PDF only dot is respected
              String command = String.format(Locale.ENGLISH,
                      // re operator constructs a rectangle
                      // W operator - sets the clipping path
                      // n operator - starts a new path
                      // q, Q - operators save and restore the graphics state stack
                      "\nq %.2f %.2f %.2f %.2f re W n\nq\n", llx, lly, w, h);
              // The content, placed on a content stream before, will be rendered before the other content
              // and, therefore, could be understood as a background (bottom "layer")
              PdfPage pdfPage = pdfDoc.getPage(p);
              new PdfCanvas(pdfPage.newContentStreamBefore(), pdfPage.getResources(), pdfDoc)
                      .writeLiteral(command);
              // The content, placed on a content stream after, will be rendered after the other content
              // and, therefore, could be understood as a foreground (top "layer")
              new PdfCanvas(pdfPage.newContentStreamAfter(), pdfPage.getResources(), pdfDoc)
                      .writeLiteral("\nQ\nQ\n");
              }
  
  </code></pre>