开发者

Mirrored (Flipped) Printing A PDF File

开发者 https://www.devze.com 2023-02-20 13:01 出处:网络
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 C开发者_J

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 C开发者_JAVA百科ard 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.


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号