在旋转的同时iTextSharp的preserving评论位置和放大器;方向 [英] Rotating in Itextsharp while preserving comment location & orientation

查看:380
本文介绍了在旋转的同时iTextSharp的preserving评论位置和放大器;方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总之,我使用iTextSharp的导入特定页面的PDF文件,可能旋转,调整大小或改变该网页,然后导出到一个新的PDF文件。为此,我利用iText的该PDFWriter类。我遇到的问题是,使用作家班的时候,它似乎并没有被包括出现在源页面上(对我来说这是一个注释)在输出页面可见注释。有趣的是它确实有没有问题嵌入式OCR。

To summarize, I'm using Itextsharp to import specific pages from a PDF, possibly rotate, resize or otherwise alter that page, and exporting it into a new PDF. To this end I'm using the PDFWriter class from Itext. The problem I'm running into is that when using the writer class, it doesn't appear to be including the visible annotations that appear on the source page (in my case it's a comment) in the output page. Interestingly it does include the embedded OCR without issue.

此外,使用iText的PDFcopy类时,它的工作,就像我希望它(复制意见适当地源),不幸的是PDFcopy不会有很多的方便功能,为物联网的种种,我需要做的在网页(例如,调整大小页)。

Additionally, when using the Itext PDFcopy class it does work just as I want it to (copying comments properly to source), unfortunately PDFcopy doesn't have a lot of easily accessible functionality for the sorts of things I need to do with the page (e.g. resizing pages).

于是我找了两个解决方案之一:

So I am looking for one of two solutions:

- preferably我想继续使用作家班,但需要它来复制/复制任何可见的注释等从源头上的网页,然后它们的输出。

-Preferably I'd like to continue to use the writer class, but need it to copy/duplicate any visible annotations etc. from the source pages and include them in the output.

- 需要一些例如code。有一个pdfcopy.Setpagesize功能(不工作,我怀疑意味着我做错了),但我根本不知道如何正确地缩放源页面需要进行调整的时候了。

-Need some example code of using the PDFCopy class to resize a page. There is a pdfcopy.Setpagesize function (which doesn't work, which I suspect means I'm doing it wrong), but I have basically no idea how to properly scale the source page when it needs to be resized.

下面是一些伪code给你,我在使用PDFWriter类的术语是在一个想法:

Here's some pseudo code to give you an idea of where I'm at in terms of using the PDFWriter class:

      '...

Dim MS As New MemoryStream()
Dim document As New Document
Dim WriterPDF As PdfWriter = PdfWriter.GetInstance(document, MS)
Dim reader As PdfReader = Nothing
Dim cb As PdfContentByte = WriterPDF.DirectContent
reader = New PdfReader(New MemoryStream(File.ReadAllBytes(FilePathList.Item(ItemNum))))

For Each PageItem As Integer In PageNumList
   Dim page As PdfImportedPage = WriterPDF.GetImportedPage(reader, PageItem)
   Dim PageSizeStandard As Rectangle = PageSize.LETTER
  document.SetPageSize(PageSizeStandard)

   Dim tm = New System.Drawing.Drawing2D.Matrix()

        'code to resize, rotate etc... tm.scale, tm.rotate, etc.

   cb.AddTemplate(page, tm)
   document.NewPage()

next

作为替代方案,PDFCopy code,我使用的旋转包括:

As the alternative, the PDFCopy code I'm using for rotation involves:

 Dim MasterCopy As PdfCopy = New PdfCopy(document, New FileStream(outputPath, FileMode.Create))
      '...
 Dim PageDict As PdfDictionary = reader.GetPageN(PageItem)
' can get current rotation with this... 
' Dim Rot As PdfNumber = PageDict.Get(PdfName.ROTATE)

      '...
Dim RotatedPageSizeHeight As Single = reader.GetPageSizeWithRotation(PageItem).Height
Dim RotatedPageSizeWidth As Single = reader.GetPageSizeWithRotation(PageItem).Width

If RotatedPageSizeWidth > RotatedPageSizeHeight Then
     PageDict.Put(PdfName.ROTATE, New PdfNumber(90))
     'there is a Pdfname.Size, but no idea if that's even what I need or how to use it.
     'with the writer class I use a matrix to scale the page, works fine. 
End If

      '... 
MasterCopy.AddPage(page)

抱歉伪code是有点支离破碎,尽量保持其简单。请让我知道如果我可以提供任何其他信息。而在此先感谢!

Sorry the pseudo code is a bit fragmented, trying to keep it brief. Please let me know if I can provide any additional information. And thanks in advance!

推荐答案

我创建了一个小样本,实现什么MKL提示:的 ScaleRotate

I've created a small sample that implements what mkl suggests: ScaleRotate.

PdfReader reader = new PdfReader(src);
int n = reader.getNumberOfPages();
PdfDictionary page;
for (int p = 1; p <= n; p++) {
    page = reader.getPageN(p);
    if (page.getAsNumber(PdfName.USERUNIT) == null)
        page.put(PdfName.USERUNIT, new PdfNumber(2.5f));
    page.remove(PdfName.ROTATE);
}
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(dest));
stamper.close();
reader.close();

给定一个原始的PDF pages.pdf 与旋转页面,裁剪页和注释。我们缩放和旋转一些网页,致使 pages_altered.pdf

Given an original PDF pages.pdf with rotated pages, cropped pages and annotations. We scale and rotate some pages, resulting in pages_altered.pdf.

我们引进 UserUnit 2.5的未缩放但所有网页。如果更改 UserUnit 0.5时,你会看到,它不会在Adobe Reader任何影响。针对PDF ISO标准说,可用于用户单元的范围是实现无关。最初是由Adobe的PDF规范1.7版说:Acrobat 7.0产品支持7.5最大UserUnit的价值。没有谈及的最低值,但经验告诉我们,由Adobe reader支持的最小值是1,这意味着你不能缩小。

We introduce a UserUnit of 2.5 for all pages that aren't scaled yet. If you change the UserUnit to 0.5, you'll see that it won't have any effect in Adobe Reader. The ISO standard for PDF says that the range that can be used for the user unit is implementation-independent. Version 1.7 of the PDF specification originally written by Adobe says: "Acrobat 7.0 supports a maximum UserUnit value of 75,000." Nothing is said about the minimum value, but experience tells us that the minimum value supported by Adobe Reader is 1, meaning you can't scale down.

至于旋转,你可以通过改变 /旋转键在网页词典更改页面的旋转。在这个例子中,我删除了关键的,不断变化的景观(其中的 /旋转值为90)为纵向(所有页<$ C $默认值C> /旋转 0)。你会发现,这并不一定是不旋转第4页第4页有任何影响。它看起来像一个页面横向因为页面的尺寸以这样的方式,该宽度大于所述高度创建

As for the rotation, you can change the rotation of a page by changing the /Rotate key in the page dictionary. In the example, I removed the key, changing all pages shown in landscape (of which the value for /Rotate is 90) into portrait (the default value for /Rotate is 0). You'll notice that this doesn't have any effect on page 4. Page 4 isn't rotated. It looks like a page in landscape because the dimensions of the page are created in such a way that the width is greater than the height.

总结:这是小菜一碟旋转在现有的PDF页面,因此被缩放页面到一个更大的尺寸。如果你想为下限值的网页,你只能使用 PdfWriter (其中丢弃所有注释),你需要分别复制注释后的的转化所有在 /矩形值这些注解。这是一项艰巨的任务。它把我们的客户之一几个星期才能正确地实现这一目标。要ppared花时间等量$ P $如果这就是你想要的。

Summarized: it's a piece of cake to rotate pages in an existing PDF, so is scaling the pages up to a bigger size. If you want to downscale pages, you can only use PdfWriter (which throws away all annotations) and you need to copy the annotations separately after transforming all the /Rect values of these annotations. This is a huge task. It took one of our customers several weeks to achieve this correctly. Be prepared to spend an equal amount of time if that's what you want.

免责声明:在 UserUnit不支持的所有观众值。实现方式可以取决于所使用的浏览器而有所不同。该功能中引入了PDF 1.6,这意味着该功能将无法在仅支持较旧的PDF版本的任何观察者工作

DISCLAIMER: the UserUnit value isn't supported by all viewers. Implementations may vary depending on the viewer that is used. The feature was introduced in PDF 1.6, meaning that the functionality won't work in any viewer supporting only older PDF versions.

这篇关于在旋转的同时iTextSharp的preserving评论位置和放大器;方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆