镜像(翻转)打印 PDF 文件

发布于 2024-10-28 03:45:32 字数 213 浏览 3 评论 0原文

我使用 ASP.NET (Framework 3.5) 和 Crystal Reports 在 PDF 文件中生成我大学学生的身份证,但我想将卡打印在透明纸上并将其粘贴到相同尺寸的塑料卡上,因为我需要所有要打印的东西都是镜像的。 我尝试以镜像形式设计水晶报告本身,但找不到以镜像形式编写文本的方法。任何人都可以建议一种方法来完成这项工作,我想要的只是翻转 PDF 文件或 Crystal Report 中的内容。

I generating ID Cards of Students of My College in a PDF file using ASP.NET (Framework 3.5) and Crystal Reports But I Want to print the Cards in a Transparent Sheet and Paste it on a Plastic Card of same size for that i need the everything to be printed mirrored.
I tried designing the crystal reports in mirrored form itself but could not find a way to write text in mirrored form. Can anyone suggest a way to do this work all I want is to Flip the contents in PDF File or in Crystal Report.

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

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

发布评论

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

评论(2

握住你手 2024-11-04 03:45:32

几个想法:

1)将 PDF 渲染为图像(使用 Ghostscript/ImageMagick 或商业 PDF 库((例如 Tif))并镜像图像以进行打印

2)镜像 PDF 本身,可能可以使用 iTextSharp

3)使用报告工具并尝试使用某种反向字体(可能是快速选项)

A couple of Ideas:

1) Render the PDF to an Image (using Ghostscript/ImageMagick or commercial PDF library( (eg Tif) and mirror the image for printing

2) Mirror the PDF Itself, might be possible with iTextSharp

3) Use the reporting tool and try and use some kind of reverse font (coud be quick option)

柠檬色的秋千 2024-11-04 03:45:32

任何允许您导入页面并直接写入 PDF 内容流的 API 都可以让您执行此操作。

在 iText (Java) 中,它看起来像这样:

PdfReader reader = new PdfReader(pdfPath);
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance( doc, new FileOutputStream(outPath) );

for (int pageNum = 1; pageNum <= reader.getNumberOfPages(); ++pageNum) {
  PdfImportedPage page = writer.getImportedPage(reader, pageNum);
  PdfContentByte pageContent = writer.getDirectContent();

  // flip around vertical axis
  pageContent.addTemplate(page, 1f, 0f, 0f, -1f, page.getWidth(), 0f);
  doc.newPage();
}

上面的代码正在执行以下操作:

  • 默认 Document() 页面大小与当前 Document() 页面大小匹配代码>PdfImportedPage
  • 源页面不会旋转。
  • 页面内容中不存在任何注释、可选内容组(层)以及各种其他位。

一些解决方法:

// keep the page size consistent
PdfImportedPage page = writer.getImportedPage(reader, pageNum);
doc.newPage(page.getBoundingBox());

PdfContentByte pageContent = writer.getDirectContent();
pageContent.addTemplate(...);

// to compensate for a page's rotation, you need to either rotate the target page 
// Easy in PdfStamper, virtually impossible with `Document` / `PdfWriter`.
AffineTransform unRotate = AffineTranform.getRotateInstance(degToRad(360 - pageRotation), pageCenterX, pageCenterY)
AffineTransform flip = new AffineTransform(1f, 0f, 0f, -1f, page.getWidth(), 0f);
AffineTransform finalTrans = flip;
finalTrans.concatenate(unRotate);

pageContent.addTemplate(page, finalTrans);

公平警告:我的 2d 矩阵 fu 并不是那么强大。我几乎肯定做错了什么。调试这些东西是真正的 PITA。东西要么“看起来正确”,要么完全搞砸了,完全脱离了页面(因此看不见,所以你不知道它往哪个方向走)。我经常将页面矩形更改为 [-1000 -1000 1000 1000],这样我就可以看到它们都去了哪里。有趣的东西。

至于复制注释之类的……哎呀。 PdfCopy 通过它的 addPage() 方法为您完成所有这些工作,但这并不能让您首先转换页面内容。您对 PdfImportedPage 所做的任何更改都将被忽略。您真的陷入了艰难的道路...手动复制所有繁琐的位并更改它们以补偿翻转的页面...或者将源代码弄乱到 addPage() 以获得结果你想要的。两者都需要对 PDF 有一定的深入了解。

考虑到具体细节,您可能不需要担心它,但值得一提的是,以防情况不同的人有相同的目标。

Any API that lets you import pages and write directly to the PDF content stream will let you do this.

In iText (Java), it'd look something like this:

PdfReader reader = new PdfReader(pdfPath);
Document doc = new Document();
PdfWriter writer = PdfWriter.getInstance( doc, new FileOutputStream(outPath) );

for (int pageNum = 1; pageNum <= reader.getNumberOfPages(); ++pageNum) {
  PdfImportedPage page = writer.getImportedPage(reader, pageNum);
  PdfContentByte pageContent = writer.getDirectContent();

  // flip around vertical axis
  pageContent.addTemplate(page, 1f, 0f, 0f, -1f, page.getWidth(), 0f);
  doc.newPage();
}

The above code is making the following ass-u-me-ptions:

  • The default Document() page size matches the size of the current PdfImportedPage.
  • The source pages aren't rotated.
  • There are no annotations, optional content groups (layers), and various other bits that aren't just represented in the page contents.

Some workarounds:

// keep the page size consistent
PdfImportedPage page = writer.getImportedPage(reader, pageNum);
doc.newPage(page.getBoundingBox());

PdfContentByte pageContent = writer.getDirectContent();
pageContent.addTemplate(...);

// to compensate for a page's rotation, you need to either rotate the target page 
// Easy in PdfStamper, virtually impossible with `Document` / `PdfWriter`.
AffineTransform unRotate = AffineTranform.getRotateInstance(degToRad(360 - pageRotation), pageCenterX, pageCenterY)
AffineTransform flip = new AffineTransform(1f, 0f, 0f, -1f, page.getWidth(), 0f);
AffineTransform finalTrans = flip;
finalTrans.concatenate(unRotate);

pageContent.addTemplate(page, finalTrans);

FAIR WARNING: My 2d matrix-fu isn't all that strong. I'm almost certainly doing something wrong. Debugging these sorts of things is a real PITA. Stuff either "looks right" or is so badly screwed up its off the page entirely (ergo invisible, so you don't know which way it went). I often change the page rectangles by [-1000 -1000 1000 1000] just so I can see where it all went. Fun stuff.

As for copying annotations and such... ouch. PdfCopy does all that for you, via it's addPage() method, but that doesn't let you transform the page content first. Any changes you make to the PdfImportedPage are ignored. You're really stuck with The Hard Way... manually copying all the fiddly bits and changing them to compensate for your flipped page... or messing with the source to addPage() to get the results you want. Both require some in-depth knowledge of PDF.

Given the specifics, you probably don't need to worry about it, but it's worth mentioning in case someone with a different situation comes along with the same goal.

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