DLL Download
1. PDF Create
import clr clr.AddReferenceToFileAndPath("itextsharp.dll") import System import System.Text from System.IO.File import OpenRead from System.IO.File import OpenWrite from System.IO import FileStream from System.IO import FileMode from System.IO import FileAccess from System.IO import FileShare from iTextSharp.text import Document from iTextSharp.text import Image from iTextSharp.text import PageSize from iTextSharp.text import Font from iTextSharp.text import Paragraph from iTextSharp.text.pdf import BaseFont from iTextSharp.text.pdf import PdfPTable from iTextSharp.text.pdf import PdfWriter class PDF(): def __init__(self,filename,font="Consolas"): self.doc = Document(PageSize.A4) #self.bf = BaseFont.CreateFont("malgun.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED); #self.bf = BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED); #self.font = Font(self.bf, 12, Font.BOLD|Font.UNDERLINE, Color.BLACK); #Font.Normal #self.ms = MemoryStream() self.fs = FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None); self.writer = PdfWriter.GetInstance(self.doc, self.fs) self.rootOutline = self.writer.RootOutline self.doc.Open() def save(self): self.doc.Close() def newPage(self): self.doc.NewPage() def printText(self,text): #self.doc.Add(Paragraph(text, self.font)); return self.doc.Add(Paragraph(text)); def printImage(self,filename,scale=None): image = Image.GetInstance(filename); if scale: image.ScalePercent(scale) return self.doc.Add(image); def printTable(self,table_data,widths=None): #table [[x,x,x],[y,y,y]] table = PdfPTable(len(table_data[0])) if widths: table.setWidths(widths) #table.setTotalWidth(width); for row in table_data: for col in row: table.AddCell(col) # col : string, image self.doc.Add(table) table_data = [ [ 'Name', 'Age', 'Sex' ], [ 'Tom', '20', 'male' ], [ 'Jane', '18', 'female' ], ] if __name__ == "__main__": pdf = PDF("a.pdf") pdf.printText("Hello PDF") pdf.printImage("Lenna.png",50) pdf.printTable(table_data) pdf.save()
2. PDF Image Extract
import clr clr.AddReferenceToFileAndPath("itextsharp.dll") import System import System.Text from System.IO import * from System.IO.File import * from iTextSharp.text import * from iTextSharp.text.pdf import * if __name__ == "__main__": pdf = Documemt.load("a.pdf") pages = pdf.getDocumentCatalog().getAllPages() iter = pages.iterator() i = 0 while iter.hasNext(): page = iter.next() resources = page.getResources() pageImages = resources.getImages() if pageImages: imageIter = pageImages.keySet().iterator() while imageIter.hasNext(): key = imageIter.next() image = pageImages.get(key) fs = FileStream("%03d.pdf" % i, FileMode.Create, FileAccess.Write, FileShare.None) image.write2OutputStream(fs) i += 1
#-*- coding: utf-8 -*- import clr clr.AddReferenceToFileAndPath(r"C:/Lib/Dll/itextsharp.dll") import System import System.Text from System.IO.File import OpenRead from System.IO.File import OpenWrite from System.IO import FileStream from System.IO import FileMode from System.IO import FileAccess from System.IO import FileShare #from System.Drawing import Color from iTextSharp.text import Document from iTextSharp.text import Image from iTextSharp.text import PageSize from iTextSharp.text import Font from iTextSharp.text import Paragraph from iTextSharp.text.pdf import BaseFont from iTextSharp.text.pdf import PdfPTable from iTextSharp.text.pdf import PdfWriter from iTextSharp.text import BaseColor class PDF(): def __init__(self,filename): #,font="Consolas" self.doc = Document(PageSize.A4) self.font = None #self.ms = MemoryStream() self.fs = FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.None); self.writer = PdfWriter.GetInstance(self.doc, self.fs) self.rootOutline = self.writer.RootOutline self.doc.Open() def save(self): self.doc.Close() def setFont(self,fontfile,size=12): #"malgun.ttf" style = Font.NORMAL #Font.BOLD|Font.UNDERLINE, Font.Normal color = BaseColor.BLACK #self.bf = BaseFont.CreateFont(BaseFont.TIMES_BOLD, BaseFont.WINANSI, BaseFont.EMBEDDED); self.bf = BaseFont.CreateFont(fontfile, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); self.font = Font(self.bf, size, style, color); def newPage(self): self.doc.NewPage() def printText(self,text): if self.font: return self.doc.Add(Paragraph(text, self.font)); else: return self.doc.Add(Paragraph(text)); if __name__ == "__main__": pdf = PDF("a.pdf") pdf.setFont(r"C:/Lib/Font/malgun.ttf") pdf.printText("Hello PDF 한글") pdf.save()
댓글 없음:
댓글 쓰기