PDFã©ã¤ãã©ãªiTextã使ã
Webã¢ããªã±ã¼ã·ã§ã³ã§PDFå½¢å¼ã§ã®å¸³ç¥¨åºåæ©è½ãå¿
è¦ã«ãªã£ãã®ã§ãPDFã©ã¤ãã©ãªã§ããiTextã調ã¹ã¦ã¿ãã
Javaç¨ã®PDFåºåã³ã³ãã¼ãã³ãiTextãå
ã§ã.NETã§ä½¿ç¨å¯è½ãªPortãããã¤ãããã
- http://www.lowagie.com/iText/docs.html
- æ¬å®¶Javaç
- http://www.ujihara.jp/iTextdotNET/ja/index.html
- iTextãJ#.NETã§æ¸ãç´ãããã®ãVisual J# .NET 1.1再頒布可能パッケージã®ã¤ã³ã¹ãã¼ã«ãå¿ è¦ãC#ã§ä½¿ãã«ã¯vjslibã¸ã®ãªã³ã¯ãå¿ è¦ã
- iText® 5 .NET, a .NET PDF library download | SourceForge.net
- .NET TIPS [ASP.NET]Webフォームから動的にPDF文書を作成するには? - C# VB.NET - @IT
- http://codezine.jp/a/article.aspx?aid=84
- http://codezine.jp/a/article.aspx?aid=462
- http://pinoki.la.coocan.jp/wiki/?Java%2FiText
ä»åã¯ãæ¥æ¬äººã®æ°åæ°ã«ãã£ã¦Portããã¦ãã¦ãæ¥æ¬èªã®æ å ±ãè±å¯ãªiText.NETã使ããã¨ã«ããããµã³ãã«ãªã©è³æãå å®ãã¦ããããé常ã«ã¨ã£ã¤ããããã¨æããããããããããããã
ãããããã¨ã¯ãæ¢åã®PDFãèªã¿è¾¼ãã§ãããã«ï¼¤ï¼¢ããèªã¿è¾¼ãã ããã¹ãã»ç»åã表示ãã帳票PDFããã©ã¦ã¶ã«è¡¨ç¤ºããã
ã¾ãã¯ããã¼ã¸ãµã¤ãºã¨ä¸ä¸å·¦å³ã®ãã¼ã¸ã³ãå ¥åãã¦PDFãä½æãã
Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment; filename=test.pdf"); Document doc = new Document(PageSize.A4, 30, 20, 80, 20); PdfWriter pw = PdfWriter.getInstance(doc, Response.OutputStream); doc.open();
ç¶ãã¦ãæ¢åã®PDFææ¸ããã³ãã¬ã¼ãã¨ãã¦èªã¿è¾¼ã¿
PdfReader pr = new PdfReader(Server.MapPath("template.pdf")); PdfImportedPage page = pw.getImportedPage(pr, 1); // ã¤ã³ãã¼ããããã¼ã¸ãæå®ãã PdfContentByte pcb = pw.getDirectContent(); pcb.addTemplate(page, 0, 0);
ãã©ã³ããè¨å®ãã¡ãªã¿ã«MSã´ã·ãã¯ãªã©Windowså èµã®æ¥æ¬èªãã©ã³ããæå®ããã«ã¯@"c:\windows\fonts\msgothic.ttc,1"ãªã©ãç´æ¥ãã©ã³ããã¡ã¤ã«ã®ãã¹ãæå®ãã¦ããå¿ è¦ãããã
BaseFont bf = BaseFont.createFont(@"c:\windows\fonts\msgothic.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); Font font_main = new Font(bf, 10, Font.NORMAL); // 10pt æ¨æºåä½
ããã¹ãã追å ããã«ã¯ãParagraphãªãã¸ã§ã¯ããä½æãã¦Document.add()ãã¦ããã ããããã ãã§èªåæ¹è¡ä»ãã®æ®µè½ãä½ããã
Paragraph prg = new Paragraph(11.5f, strDescription, font_main); prg.setIndentationLeft(20); // å·¦å´ã®ã¤ã³ãã³ããæå® prg.setAlignment(Element.__Value.ALIGN_LEFT); // ã¢ã©ã¤ã³ã¡ã³ããæå® doc.add(prg); // ããã¥ã¡ã³ãã«è¿½å
ææ¸ã¸ã®ç»åã®è¿½å ããããªæãã§æ¥½ã
ããã ãWindowsã®ç»é¢ã¨PDFã§ã¯ç»é¢è§£å度ã®éããããããã®ã¾ã¾ã®ãµã¤ãºã§ç»é²ãã¦ãã¾ãã¨ç»é¢ãã¼ãã¦ãã¾ãããã®ããImage.scalePercent()ã§ç»åã75%縮å°ãã¦ç»é²ãã¦ãããã¡ãªã¿ã«scalePercent()ã§ç¸®å°ãã¦ã表示ä¸ã®ãµã¤ãºãå°ãããªãã ãã§ãã¼ã¿ã¯ä¸ã®ã¾ã¾å
¥ã£ã¦ãã¿ããããã¨ç»åãµã¤ãºã¯ç¨ç´ã®ãµã¤ãºã«åããã¦èªåçã«å¤ãã£ããã¯ããªãã®ã§ã大ãããµã¤ãºã®ç»åãå¼µãã¨ãã¯é©å®ç¸®å°ãã¦ããå¿
è¦ãããã
Image img = Image.getInstance(strImageFilePath); // PDF(72dpi)ã¨Windows(96dpi)ã®ç»é¢è§£å度ã®éããè£æ£ããã img.scalePercent(72.0f / 96.0f * 100f); img.setAlignment(Element.__Value.ALIGN_CENTER); doc.add(img);
æå¾ã¯ãDocument.Close()ã¨Response.End()ã§ãã©ã¦ã¶ä¸ã«è¡¨ç¤ºãããã
// ããã¥ã¡ã³ããéãã doc.close(); // ã¬ã¹ãã³ã¹ãè¿ã Response.End();
ã¡ãªã¿ã«ãDocument.add()ã§æ¬¡ã
ãªãã¸ã§ã¯ãã追å ãã¦è¡ãã¨ãã©ãã©ãä¸ã¸ä¸ã¸ã¨ä¼¸ã³ã¦è¡ããæå®ãã絶対ä½ç½®ã«ããã¹ãã表示ãããã«ã¯PdfContentByte.beginText()以ä¸ã使ãããã®å ´åãè¡æ«ã®èªåæãè¿ãã¯ä½¿ç¨ã§ããªãã®ã§æ³¨æãã¡ãªã¿ã«PDFã®åº§æ¨ç³»ã¯å·¦ä¸ã(0,0)ã¨ãªããæåããã«æ°ã¥ããããã¿ orz
pcb.beginText(); pcb.setFontAndSize(bf, 10.0f); pcb.setTextMatrix(460, 815); pcb.showText("絶対å¤ã¸ã®è¡¨ç¤º"); pcb.endText();
ããã ãç°¡åã«PDFåºåãåºæ¥ã¦ãã¾ãã®ã¯ãã°ãããã§ããªããã¨ParagraphãDocument.add()ãã¦ããææ³ã ã¨ãï¼ãã¼ã¸ã«åã¾ããããªãå ´åã«åæã«æ¹ãã¼ã¸ããã¦ãã¾ãã®ãã¡ãã£ã¨å°ãããã®åé¿æ¹æ³ãæ¸ããã¨æã£ããã©ç æ°ï¼ï¼¡ï¼¸ãªããä»åº¦ã«ãããã
ãã¨iTextç¨ã®å¸³ç¥¨ã¢ããªã³ã°ãã¼ã«ãããã®ãçºè¦
ããã調ã¹ã¦ã¿ããã