开发者

iTextSharp self-expanding page width

开发者 https://www.devze.com 2023-02-01 08:05 出处:网络
I\'m writing a web application to generate labels. The label printer that I\'m targeting utilizes 12mm tape and the customer specified that they want to limit the labels to 3.25\". I know that with iT

I'm writing a web application to generate labels. The label printer that I'm targeting utilizes 12mm tape and the customer specified that they want to limit the labels to 3.25". I know that with iTextSharp I can specify the size of the document I want to create, however it appears that I have to specify both a width and height.

Document document = new Document(new iTextSharp开发者_运维百科.text.Rectangle(234f, 33.84f));

234 is 3.25" converted to points, and 33.84 is 12mm converted to points, so this sets the document to the maximum size allowed. Is there any way to set just the height and let the document auto-expand to the amount of content? With that, is there a way to determine if the expanded width of the document exceeds to maximum allowed by the customer? Thanks in advance for any help you can offer.


No, that is not possible in iText. It's not a particularly good idea anywhere else either. As Redman said, PDF is a print-based format. It's not HTML.

There are some tricks you can do to get around this to some extent:

  1. Create your page with the maximum legal height. 200" * 72dpi = 14400 points.
  2. Add a "generic tag" to your paragraphs.
  3. create a tag event handler that tracks where the bottom of your last paragraph was drawn.
  4. Save the PDF.
  5. Open it again with a PDFStamper
  6. Set the bottom of the page to match the location of that last paragraph. Remember that the bottom left starts off at 0,0, and the top right will be
  7. Save the final PDF

This trick will only work if your total output is less that 200" high. If you go over that, you'll still get a second page, and your "where the bottom is" code had better be prepared for it.

PS: I don't see what's wrong with having a number of 12mm x 3.25" pages... won't that perfectly fit the labels they want printed?


As far as I know, because PDF is a print format, the document width and height must be set. This is why reporting tools generally default page sizes to the default printer page size.

You best bet to try and calculate the size of the content and create the PDF accordingly. Depending on the volume and nature of the workflow, you could provide a preview for the customer to check before printing.

0

精彩评论

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

关注公众号