使用Java旋转PDF中的每个页面

发布于 2025-01-28 00:53:00 字数 1355 浏览 3 评论 0 原文

大家好,我在旋转PDF中的页面时需要介绍一些用例。

  1. 我需要检查每个页面旋转值并将其旋转为0度。

  2. 当我在Adobe中检查几个文档时,它显示了我90度,但它将在0度中。

我需要介绍两个用例,我已经使用Java PDFBox编写了一个代码,该代码将显示旋转程度错误, 如果有人有想法如何找到学位的方面,请帮助我通过它。使用代码或Wiki参考, 我正在从事一个春季启动项目。

 PDDocument document;


 public void getPdfFile(String pdfPath) throws IOException {

    File file = new File(pdfPath);

    document = PDDocument.load(file);

   int pageCount = document.getNumberOfPages();

   for(int i = 0; i < pageCount; i++) {
       PDPage page =  document.getPage(i);

      System.out.println( page.getRotation());
      if(page.getRotation() != 0) {
          page.setRotation(0);
      }

   }
     document.save("/Users/tejasreddy/Desktop/CE/StorePDF/rotated1_rotated.pdf");

     document.close();
}

Hi folks i have a few use cases i need to cover while rotating the page in the PDF .

  1. I need to check each page rotation value and rotate it to 0 degree.

  2. when i check few docs in Adobe it shows me 90 degree but it will be in 0 degree.

I need to cover both the use cases, I have written a code using java PDFBox which will get the degree of rotation is showing wrong,
If any one have idea How to find And what are the Aspects that decide the degree please help me through it . with code or Wiki to refer ,
I am working on a spring boot project.

 PDDocument document;


 public void getPdfFile(String pdfPath) throws IOException {

    File file = new File(pdfPath);

    document = PDDocument.load(file);

   int pageCount = document.getNumberOfPages();

   for(int i = 0; i < pageCount; i++) {
       PDPage page =  document.getPage(i);

      System.out.println( page.getRotation());
      if(page.getRotation() != 0) {
          page.setRotation(0);
      }

   }
     document.save("/Users/tejasreddy/Desktop/CE/StorePDF/rotated1_rotated.pdf");

     document.close();
}

enter image description here

CropBox And Media Box Details

Thanks Tejas.

3: PDF Details

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断桥再见 2025-02-04 00:53:00

对PDF页面旋转属性的含义似乎存在误解,因此我在这里解释这一点。

可见的页面区域具有 cropbox 页面输入的尺寸和坐标范围。 x 在该区域的坐标恰好增加, y 协调增加。在此领域,页面内容流的说明可以以任何方向或方向绘制文本和其他内容。

旋转页面的输入(即“页面旋转”)指示查看器程序显示由该条目以该条目为单位的裁剪盒定义的页面区域90°)。

就是这样。

因此,页面旋转值不一定与页面上任何内容的方向相吻合。 (当然,当创建PDF时,通常会选择裁剪盒和旋转值,以使添加内容并尽可能容易地添加内容。

There appears to be a misunderstanding in respect to the meaning of the PDF page rotation property, so I'll explain that here.

The visible page area has the dimensions and coordinate ranges given by the CropBox entry of the page. The x coordinates in this area increase going right, the y coordinate increase going up. In this area the instructions of the page content streams can draw text and other contents in any direction or orientation.

The Rotate entry of the page (i.e. the "page rotation") instructs a viewer program to display the page area defined by the crop box rotated clockwise by the value of that entry in degree (must be a multiple of 90°).

That's it.

Thus, the page rotation value does not necessarily coincide with the orientation of any content on the page. (Of, course, when creating a PDF one usually chooses the crop box and the rotation value to make both adding content and reading it in a viewer as easy as possible. But this is not technically enforced.)

好久不见√ 2025-02-04 00:53:00

我也有类似的问题。我在Microsoft 365中使用VBA。
我想输入注释,但是如果页面旋转,评论将被旋转并在另一个位置。
所以我拉了页面旋转。 (pg_rot = pag.getrotate)
并将其添加到注释的属性(props.rotate = pg_rot)中,并更改坐标(Recter),

If objAVDoc.Open(pth, "") Then Set pdDoc = objAVDoc.GetPDDoc

Set jso = pdDoc.GetJSObject

If Not jso Is Nothing Then

    Set page = pdDoc.AcquirePage(0)
    
    Pg_rot = page.GetRotate()
    
    ' This has been tested for 0 and 270 degrees
    Select Case Pg_rot
        Case Is = 0
            recter(0) = 410  
            recter(1) = 780 
            recter(2) = 600 
            recter(3) = 730 
        Case Is = 270
           recter(0) = 780  
           recter(1) = 410 
           recter(2) = 730  
           recter(3) = 60          
    End Select
    Set annot = jso.AddAnnot
    
    Set props = annot.getprops
        props.page = 0
        props.rotate = Pg_rot
        props.Type = "FreeText"
        props.rect = recter
        props.author = author
        props.contents = note
        
    annot.setProps props
    
    If pdDoc.Save(PDSaveFull, pth) = False Then
        MsgBox "FAIL   -  " & pth
        pdDoc.Close
    Else
        MsgBox "success"
        pdDoc.Close
End If

我似乎无法将其余的到上面的代码段。 (如果将应用程序设置为note等,结束了)

I had a similar problem. I use vba in Excel, MicroSoft 365.
I wanted to put in an annotation, but if the page was rotated, the comment would be rotated and in a different place.
So I pull in the page rotation. (Pg_rot = page.GetRotate)
And add it to the annotation's properties(props.rotate = Pg_rot), as well as changing the coordinates (recter)

If objAVDoc.Open(pth, "") Then Set pdDoc = objAVDoc.GetPDDoc

Set jso = pdDoc.GetJSObject

If Not jso Is Nothing Then

    Set page = pdDoc.AcquirePage(0)
    
    Pg_rot = page.GetRotate()
    
    ' This has been tested for 0 and 270 degrees
    Select Case Pg_rot
        Case Is = 0
            recter(0) = 410  
            recter(1) = 780 
            recter(2) = 600 
            recter(3) = 730 
        Case Is = 270
           recter(0) = 780  
           recter(1) = 410 
           recter(2) = 730  
           recter(3) = 60          
    End Select
    Set annot = jso.AddAnnot
    
    Set props = annot.getprops
        props.page = 0
        props.rotate = Pg_rot
        props.Type = "FreeText"
        props.rect = recter
        props.author = author
        props.contents = note
        
    annot.setProps props
    
    If pdDoc.Save(PDSaveFull, pth) = False Then
        MsgBox "FAIL   -  " & pth
        pdDoc.Close
    Else
        MsgBox "success"
        pdDoc.Close
End If

I can't seem to the rest to the code snippet above. (End if, set app to nothing etc.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文