ã¯ããã«
ãã¢ããªã±ã¼ã·ã§ã³éçºããã¦ããã¨å¸³ç¥¨ãåºåãããã¨ããè¦æãå¤ãèããã¾ãã帳票ãåºåããæ¹æ³ã¯å¹¾ã¤ãããã¾ããããã®ä¸ã§ãããå©ç¨ããã¦ããã®ãPDFå½¢å¼ã«ããå°å·ã§ããPDFã¯ãAdobe Readerã«ãã£ã¦è¡¨ç¤ºï¼å°å·ã§ãããã©ã¼ãããã§ããããã«æ´å½¢ãããå°å·ç©ãæä¾ãããã¨ãã§ãã¾ããæ¬ç¨¿ã§ã¯ãiTextSharpã¨ãããªã¼ãã³ã½ã¼ã¹ã®ã©ã¤ãã©ãªãå©ç¨ãã¦ã.NETã®ã¢ããªã±ã¼ã·ã§ã³ããPDFãçæããæ¹æ³ãç´¹ä»ãã¾ãã
対象èªè
ãVisual Studioï¼.NETï¼ã«ã¦éçºãè¡ã£ã¦ãã¦ãPDFçæã帳票åºåã«èå³ãããæ¹ã対象ã¨ãã¦ãã¾ãã
å¿ è¦ãªç°å¢
ããã¦ã³ãã¼ããã¡ã¤ã«ã¯Visual Studio 2003ã«ã¦ä½æãã¦ãã¾ããæ¬ç¨¿ã§ã¯ãC#ã§è§£èª¬ãã¦ãã¾ãããVisualBasicã®ã½ã¼ã¹ããµã³ãã«ã«ã¯å«ã¾ãã¦ãã¾ããã¾ããVisual C#ï¼Visual Basicï¼ 2005 Express Editionã§ãåãããã¨ãã§ãã¾ãï¼æåã«ããã¸ã§ã¯ããã¢ããã°ã¬ã¼ãããã¦ã£ã¶ã¼ãã表示ããã¾ãï¼ã
ãã¾ããçæãããPDFã確èªããããã«ãAdobe Readerãã¤ã³ã¹ãã¼ã«ããå¿ è¦ãããã¾ãã
iTextSharpã¨PDF
ãæ¬ç¨¿ã§ã¯ãiTextSharpãå©ç¨ãã¦PDFãçæãã¾ããPDFï¼Portable Document Formatï¼ã¨ã¯ããã¾ãã¾ãªOSã§é²è¦§ã§ããããåºãæ®åãã¦ããææ¸ç¨ã®ãã©ã¼ãããå½¢å¼ã§ããã¾ããé»åç½²åãæå·æè¡ãã¢ã¯ã»ã¹å¶å¾¡ãªã©ã®ã»ãã¥ãªãã£ã®èæ ®ãããã¦ããããã¡ã¤ã«å®¹éãå°ãããã¨ããWebãµã¤ããé»åã¡ã¼ã«æ·»ä»ãªã©å¹ åºãç¨éã§å©ç¨ããã¦ãã¾ãã
iTextSharpã®ç¹é·
ãiTextSharpã¯ãiTextã¨ããJavaã®ã©ã¤ãã©ãªãC#ã«ç§»æ¤ãããã®ã§ãã
ã.NETã§åä½ããiTextã«ã¯ãiTextSharp以å¤ã«ããiText.NETã¨ãã移æ¤çãåå¨ãã¾ããéçºè ãæ¥æ¬äººï¼æ°åããï¼ã®ããå¤å½èªå¦çã«å¼·ããæ¥æ¬èªã®æ å ±ãå å®ãã¦ããã¡ãªãããããã¾ãã
ãããã«å¯¾ãã¦iTextSharpã¯ãVisual J# .NETã®åé å¸å¯è½ããã±ã¼ã¸ãä¸è¦ãªãããC#ãVisual Basic .NETããæ軽ã«ä½¿ããã¨ãã£ãã¡ãªãããããã¾ãã
ãé »ç¹ã«ãã¼ã¸ã§ã³ã¢ããããã¦ãããã両æ¹ã¨ãå°æ¥çã«æå¾ ã§ãã¾ãããæ¬ç¨¿ã§ã¯iTextSharpã«ã¤ãã¦ç´¹ä»ãã¾ãã
iTextSharpãå ¥æããã«ã¯
ãiTextSharpã¯ãSourceForgeã«ã¦éçºããã¦ããããã¦ã³ãã¼ããã¼ã¸ããå ¥æãããã¨ãã§ãã¾ããå·çæç¹ã®ææ°çã¯ã3.1.2ã§ãã
ãæ¬ç¨¿ã®ãã¦ã³ãã¼ããµã³ãã«ã«ã¯ãiTextSharpãæ¢ã«å«ã¾ãã¦ãã¾ãã®ã§ãããããã¦å ¥æããå¿ è¦ã¯ããã¾ãããèªåã®ã½ãªã¥ã¼ã·ã§ã³ã§å©ç¨ãããå ´åã¯ããitextsharp-3.1.2-dll.zipãã¨ãiTextAsian-1.0.dllãããã¦ã³ãã¼ããã解åå¾ãåç §è¨å®ãããã¨è¯ãã§ãããã
ãã¦ã³ãã¼ããã¡ã¤ã«ä¸è¦§
ãiTextSharpã®ãµã¤ãã«ã¯ãã¦ã³ãã¼ããã¡ã¤ã«ãå¤æ°ããã¾ãã®ã§ç°¡åã«æ´çãã¾ãã
é å¸ãã©ã«ã | ãã¡ã¤ã«å | 説æ |
itextsharp | itextsharp-3.1.2-dll.zip | iTextSharpã®DLL |
itextsharp | itextsharp-3.1.2.zip | iTextSharpã®ã½ã¼ã¹ã³ã¼ã |
extars | iTextAsian-1.0.dll | CJKï¼Chinese/Japanese/Koreanï¼ãã©ã³ãã®DLL |
extars | iTextAsian-1.0.zip | CJKï¼Chinese/Japanese/Koreanï¼ãã©ã³ãã®ã½ã¼ã¹ã³ã¼ã |
extars | iTextAsianCmaps-1.0.dll | iTextAsianCmapsã®DLL |
extars | iTextAsianCmaps-1.0.zip | iTextAsianCmapsã®ã½ã¼ã¹ã³ã¼ã |
extars | itext-hyph-xml-1.0.dll | èªåãã¤ããã¼ã·ã§ã³ç¨ã®DLL |
extars | itext-hyph-xml-1.0.zip | èªåãã¤ããã¼ã·ã§ã³ç¨ã®ã½ã¼ã¹ã³ã¼ã |
examples | iTextSharp.tutorial.01.zip | C#ã«ãããã¥ã¼ããªã¢ã« |
examples | iTextSharp.tutorial.VB.NET.01.zip | Visual Basic.NETã«ãããã¥ã¼ããªã¢ã« |
ãå¿ è¦ã«å¿ãã¦ãã¦ã³ãã¼ããã¦ãã ããã
ãã¦ã³ãã¼ããã¡ã¤ã«ã®æ¦è¦
ããµã³ãã«ãã¡ã¤ã«ã¯ãVisual Studio .NET 2003ï¼C#ï¼ã«ããWindowsã¢ããªã±ã¼ã·ã§ã³ã§ããã½ãªã¥ã¼ã·ã§ã³ãã¡ã¤ã«ï¼iTextSharpSample.slnï¼ãããã«ã¯ãªãã¯ãã¦ãã¢ããªã±ã¼ã·ã§ã³ãèµ·åãã¦ãã ãããã¢ããªã±ã¼ã·ã§ã³ã¯æ¬¡ã®ã¡ã¤ã³ãã©ã¼ã ããæ§æããã¦ãã¾ãã
ããããã®ãã¿ã³ãã¯ãªãã¯ããã¨ãå®è¡ãã¡ã¤ã«ã¨åãå ´æã«PDFãçæããã¾ãã横ã«ããï¼»PDFãéãï¼½ãã¿ã³ãã¯ãªãã¯ããã¨ãããããã®PDFãéããã¨ãã§ãã¾ããæ¬ç¨¿ã§ã¯ãç°¡åãªPDFã®çæãã解説ããå¾ã ã«é«åº¦ãªPDFã®çæã«ã¤ãã¦è§£èª¬ãã¦ããã¾ãã
ããªããMSã´ã·ãã¯ãMS Pã´ã·ãã¯ãMS UI GothicãMS Pææã¨ãããã©ã³ãã使ç¨ãã¦ããããããããã®ãã©ã³ããPDFãçæããPCã«åå¨ããªãå ´åã¯ã¨ã©ã¼ã¨ãªãã¾ãã®ã§ã注æãã ããã
ï¼1ï¼æåã®ç°¡åãªãµã³ãã«
ãããã§ã¯ãPDFãåºåããããã°ã©ã ãè¦ã¦ã¿ã¾ãããã
//ããã¥ã¡ã³ããä½æ Document doc = new Document(); //ãã¡ã¤ã«ã®åºåå ãè¨å® PdfWriter.GetInstance(doc, new FileStream("01_Hello.pdf", FileMode.Create)); //ããã¥ã¡ã³ããéã doc.Open(); //ãHello iTextSharpããããã¥ã¡ã³ãã«è¿½å doc.Add(new Paragraph("Hello iTextSharp")); //ããã¥ã¡ã³ããéãã doc.Close();
ããã®çãã³ã¼ããå®è¡ããã¨æ¬¡ã®PDFãã¡ã¤ã«ãçæããã¾ãã
ãç»é¢ã®å·¦ä¸ã«ãHello iTextSharpãã¨ããæåã表示ããã¦ãããã¨ã確èªã§ãã¾ãã
ã³ã¼ãã®èª¬æ
ãã³ã¼ãã®è©³ç´°ãè¦ã¦ããã¾ãããã
iTextSharpé¢é£ã¯ã©ã¹ã®åå空é
ãiTextSharpã®ããã°ã©ã ã§ä½¿ç¨ããã¯ã©ã¹ã¯ã次ã®åå空éã«å«ã¾ãã¦ãã¾ãã
åå空é | ã¯ã©ã¹å | 説æ |
iTextSharp.text | Document | ããã¥ã¡ã³ããã®ãã®ã表ãã¯ã©ã¹ |
iTextSharp.text | Paragraph | æååãè¨å®ããããã®ã¯ã©ã¹ |
iTextSharp.text.pdf | PdfWriter | PDFã®åºåãæ ãã¯ã©ã¹ |
ããã®ããã次ã®using
å¥ã追å ãã¦ããã¾ãããªããSystem.IOåå空éã¯ãPDFã®ãã¡ã¤ã«åºåæï¼FileStream
ã¯ã©ã¹ï¼ã§ä½¿ç¨ãã¾ãã
//iTextSharpé¢é£ã®åå空é using iTextSharp.text; using iTextSharp.text.pdf; //ãã¡ã¤ã«IOé¢é£ã®åå空é using System.IO;
Documentãªãã¸ã§ã¯ããçæ
ãã¾ãDocument
ã¯ã©ã¹ã®ã¤ã³ã¹ã¿ã³ã¹ãä½æãã¾ããDocument
ã¯ã©ã¹ã¯çæããPDFããã¥ã¡ã³ããã®ãã®ã表ãã¾ãã
Document doc = new Document();
ãã¡ã¤ã«ã®åºåå ãè¨å®
ã次ã«ãPdfWriter
ã¯ã©ã¹ã®GetInstance
ã¡ã½ããã使ã£ã¦ãã¡ã¤ã«ã®åºåå
ãè¨å®ãã¾ããPdfWriter
ã¯ã©ã¹ã¯PDFã®åºåãæ
ãã¯ã©ã¹ã§ãããã§ã¯ã01_Hello.pdfãã¨ãããã¡ã¤ã«ã«åºåãã¾ãï¼æ¬ç¨¿ã§ã¯PDFã«åºåããä¾ã®ã¿ãç´¹ä»ãã¾ãããHTMLãRTFãªã©ä»ã®åºåå½¢å¼ã®Writerãé¸ã¶ãã¨ãå¯è½ã§ãï¼ã
PdfWriter.GetInstance(doc, new FileStream("01_Hello.pdf", FileMode.Create));
ãGetInstance
ã¡ã½ããã¯ã第1å¼æ°ã«Document
ã®ã¤ã³ã¹ã¿ã³ã¹ãã第2å¼æ°ã«ä»»æã®Streamï¼ããã§ã¯FileStreamï¼ãè¨å®ãã¾ãããªããGetInstance
ã¡ã½ããã¨ããååã®éãã«PdfWriter
ã®ã¤ã³ã¹ã¿ã³ã¹ãåå¾ãããã¨ãã§ãã¾ãã
ããã¥ã¡ã³ããéãï¼æç« ã®åºåéå§ï¼
ãç¶ãã¦ãDocument
ã®Open
ã¡ã½ãããå¼ã³åºãã¾ããæç« ã®åºåãéå§ããããã«ãã¾ãOpen
ã¡ã½ãããå¼ã³åºãå¿
è¦ãããã¾ãã
doc.Open();
ããã¥ã¡ã³ãã«ã³ã³ãã³ãã追å
ãããã¦ã表示ãããæåãå³ã¨ãã£ãã³ã³ãã³ãã追å ãã¾ããããã§ã¯ããHello iTextSharpãã¨ããæååã追å ããããã«Document
ã®Add
ã¡ã½ãããå¼ã³åºãã¦ãã¾ãã
doc.Add(new Paragraph("Hello iTextSharp"));
ãAdd
ã¡ã½ããã®å¼æ°ã«ãParagraph
ã¨ããã¯ã©ã¹ãæå®ãã¦ãã¾ããParagraph
ã¯ã©ã¹ã¯ããã©ã°ã©ãã¨ããååã®éãã«ã段è½ãã表ãã¯ã©ã¹ã§ã表示ãããæåãæ¸å¼ãè¨å®ãããã¨ãã§ãã¾ãï¼è©³ç´°ã¯å¾è¿°ãã¾ãããParagraph
以å¤ã«ãPhrase
ãChunk
ã¨ããã¯ã©ã¹ãå©ç¨ãããã¨ãã§ãã¾ãï¼ã
ããã¥ã¡ã³ããéããï¼æç« ã®åºåå®äºï¼
ãæå¾ã«ãDocument
ã®Close
ã¡ã½ãããå¼ã³åºãã¾ãããã®ã¡ã½ãããå¼ã³åºããã¨ã«ãã£ã¦ãFileStreamãFlushããPDFãã¡ã¤ã«ã¸ã®åºåãå®äºãã¾ãã
doc.Close();
主è¦ã¯ã©ã¹ã®æ´ç
ãDocument
ã¯ã©ã¹ã¨PdfWriter
ã¯ã©ã¹ã®ã¡ã½ãããã¯ã©ã¹å³ã§æ´çãã¾ããiTextSharpã使ãããªãã«ã¯ã主è¦ã¯ã©ã¹ã§ããDocument
ã¯ã©ã¹ã®å½¹å²ããã£ããã¨ç解ãã¦ããã¨è¯ãã§ãããã
ããªãããã以éã®ã³ã¼ãã«ã¤ãã¦ã¯ãå¿ è¦ãªç®æãä¸å¿ã«èª¬æãã¾ãã®ã§ãå ¨ä½ã®ã½ã¼ã¹ãè¦ããæ¹ã¯ãã¦ã³ãã¼ããµã³ãã«ãè¦ãããã«ãã¦ãã ããã