本文介绍了ITextSharp pdf调整大小和数据对齐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正在使用ITextSharp将HTML转换为PDF,但我希望生成宽度为5厘米的PDF。我使用了以下代码I am using ITextSharp to convert HTML to PDF but i want the PDF to be generated of size 5cm width. I used the following codevar pgSize = new iTextSharp.text.Rectangle(2.05f, 2.05f);Document doc = new Document(pgSize);但它只是调整pdf的大小,我的数据在pdf或get hide中消失了。 如何在PDF中心对齐数据或调整pdf大小?这是我的代码 public void ConvertHTMLToPDF(string HTMLCode) {but it is just resizing the pdf and my data disappeared in the pdf or get hide.How can i align the data in the center in PDF or resize the pdf? Here is my code public void ConvertHTMLToPDF(string HTMLCode) { try { System.IO.StringWriter stringWrite = new StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); StringReader reader = new StringReader(HTMLCode); var pgSize = new iTextSharp.text.Rectangle(2.05f, 2.05f); Document doc = new Document(pgSize); HTMLWorker parser = new HTMLWorker(doc); PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("~") + "/App_Data/HTMLToPDF.pdf", FileMode.Create)); doc.Open(); foreach (IElement element in HTMLWorker.ParseToList( new StringReader(HTMLCode), null)) { doc.Add(element); } doc.Close(); Response.End(); } catch (Exception ex) { } }推荐答案您正在创建一个尺寸为0.0723厘米×0.0723厘米的PDF。这太小了,无法添加任何内容。如果要创建5厘米x 5厘米的PDF,则需要创建如下文档:You are creating a PDF that measures 0.0723 cm by 0.0723 cm. That is much too small to add any content. If you want to create a PDF of 5 cm by 5 cm, you need to create your document like this:var pgSize = new iTextSharp.text.Rectangle(141.732f, 141.732f);Document doc = new Document(pgSize);至于对齐,应该在HTML中定义,但是你使用的是旧版本的iText和你使用的是已弃用的 HTMLWorker 。As for the alignment, that should be defined in the HTML, but you are using an old version of iText and you are using the deprecated HTMLWorker.你应该升级到iText 7和pdfHTML,如下所述:使用iText将HTML转换为PDFYou should upgrade to iText 7 and pdfHTML as described here: Converting HTML to PDF using iText另外:页面的大小可以在CSS的 @page -rule中定义。请参阅标题后的巨大空格使用飞碟Also: the size of the page can be defined in the @page-rule of the CSS. See Huge white space after header in PDF using Flying Saucer当新版本允许你这样做时,为什么你会使用旧的iText版本让自己变得困难:Why would you make it difficult for yourself by using an old iText version, when the new version allows you to do this:@page { size: 5cm 5cm;} 这篇关于ITextSharp pdf调整大小和数据对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-31 00:35