Esempio n. 1
0
    def do_pdf(self,event):
        from com.itextpdf.text.pdf import PdfWriter
        from com.itextpdf.text import Document
        network=self.get_current_network()
        if network is None: name='Nengo'
        else: name=network.name
        fileChooser=JFileChooser()
        fileChooser.setDialogTitle('Save layout as PDF')
        fileChooser.setSelectedFile(java.io.File('%s.pdf'%name))
        if fileChooser.showSaveDialog(self.ng)==JFileChooser.APPROVE_OPTION:
            f=fileChooser.getSelectedFile()

            universe=self.ng.universe
        
            doc=Document()
            writer=PdfWriter.getInstance(doc,java.io.FileOutputStream(f))
            doc.open()
            cb=writer.getDirectContent()
            w=universe.size.width
            h=universe.size.height
            pw=550
            ph=800
            tp=cb.createTemplate(pw,ph)
            g2=tp.createGraphicsShapes(pw,ph)
            at = java.awt.geom.AffineTransform()        
            s=min(float(pw)/w,float(ph)/h)        
            at.scale(s,s)
            g2.transform(at)
            #self.view.area.pdftemplate=tp,s
            universe.paint(g2)
            #self.view.area.pdftemplate=None
            g2.dispose()

            cb.addTemplate(tp,20,0)
            doc.close()
Esempio n. 2
0
    def save_pdf(self, event):
        from com.itextpdf.text.pdf import PdfWriter
        from com.itextpdf.text import Document

        fileChooser = JFileChooser()
        fileChooser.setSelectedFile(java.io.File('%s.pdf' % self.view.network.name))
        if fileChooser.showSaveDialog(self) == JFileChooser.APPROVE_OPTION:
            f = fileChooser.getSelectedFile()

            doc = Document()
            writer = PdfWriter.getInstance(doc, java.io.FileOutputStream(f))
            doc.open()
            cb = writer.getDirectContent()
            w = self.view.area.size.width
            h = self.view.area.size.height
            pw = 550
            ph = 800
            tp = cb.createTemplate(pw, ph)
            g2 = tp.createGraphicsShapes(pw, ph)
            at = java.awt.geom.AffineTransform()
            s = min(float(pw) / w, float(ph) / h)
            at.scale(s, s)
            g2.transform(at)
            self.view.area.pdftemplate = tp, s
            self.view.area.paint(g2)
            self.view.area.pdftemplate = None
            g2.dispose()

            cb.addTemplate(tp, 20, 0)
            doc.close()
Esempio n. 3
0
 def save(self, parent, format):
     chooser = JFileChooser()
     # filter = FileNameExtensionFilter(
     #     format + " files",
     #     array([format, format.lower()], String))
     # chooser.setFileFilter(filter);
     returnVal = chooser.showSaveDialog(parent)
     if returnVal == JFileChooser.APPROVE_OPTION:
         fileName = chooser.getSelectedFile().getPath()
         if not fileName.upper().endswith('.' + format):
             fileName += '.' + format.lower()
         file = File(fileName)
     else:
         return
     if format == 'PNG':
         ChartUtils.saveChartAsPNG(file, self.chart, self.exportX,
                                   self.exportY)
     elif format == 'SVG':
         domImpl = GenericDOMImplementation.getDOMImplementation()
         doc = domImpl.createDocument(None, "svg", None)
         svgGen = SVGGraphics2D(doc)
         svgGen.getGeneratorContext().setPrecision(6)
         self.chart.draw(
             svgGen, Rectangle2D.Double(0, 0, self.exportX, self.exportY),
             None)
         out = OutputStreamWriter(FileOutputStream(file), "UTF-8")
         svgGen.stream(out, True)  #True is for useCSS
         out.close()
     elif format == 'PDF':
         mapper = DefaultFontMapper()
         pageSize = Rectangle(self.exportX, self.exportY)
         doc = TextDocument(pageSize, 50, 50, 50, 50)
         out = BufferedOutputStream(FileOutputStream(file))
         writer = PdfWriter.getInstance(doc, out)
         doc.open()
         cb = writer.getDirectContent()
         tp = cb.createTemplate(self.exportX, self.exportY)
         g2 = tp.createGraphics(self.exportX, self.exportY, mapper)
         r2D = Rectangle2D.Double(0, 0, self.exportX, self.exportY)
         self.chart.draw(g2, r2D)
         g2.dispose()
         cb.addTemplate(tp, 0, 0)
         doc.close()
Esempio n. 4
0
    def do_pdf(self, event):
        from com.itextpdf.text.pdf import PdfWriter
        from com.itextpdf.text import Document
        network = self.get_current_network()
        if network is None: name = 'Nengo'
        else: name = network.name
        fileChooser = JFileChooser()
        fileChooser.setDialogTitle('Save layout as PDF')
        fileChooser.setSelectedFile(java.io.File('%s.pdf' % name))
        if fileChooser.showSaveDialog(self.ng) == JFileChooser.APPROVE_OPTION:
            f = fileChooser.getSelectedFile()

            universe = self.ng.universe

            doc = Document()
            writer = PdfWriter.getInstance(doc, java.io.FileOutputStream(f))
            doc.open()
            cb = writer.getDirectContent()
            w = universe.size.width
            h = universe.size.height
            pw = 550
            ph = 800
            tp = cb.createTemplate(pw, ph)
            g2 = tp.createGraphicsShapes(pw, ph)
            at = java.awt.geom.AffineTransform()
            s = min(float(pw) / w, float(ph) / h)
            at.scale(s, s)
            g2.transform(at)
            #self.view.area.pdftemplate=tp,s
            universe.paint(g2)
            #self.view.area.pdftemplate=None
            g2.dispose()

            cb.addTemplate(tp, 20, 0)
            doc.close()
Esempio n. 5
0
    def save_pdf(self, event):
        from com.itextpdf.text.pdf import PdfWriter
        from com.itextpdf.text import Document

        fileChooser = JFileChooser()
        fileChooser.setSelectedFile(
            java.io.File('%s.pdf' % self.view.network.name))
        if fileChooser.showSaveDialog(self) == JFileChooser.APPROVE_OPTION:
            f = fileChooser.getSelectedFile()

            doc = Document()
            writer = PdfWriter.getInstance(doc, java.io.FileOutputStream(f))
            doc.open()
            cb = writer.getDirectContent()
            w = self.view.area.size.width
            h = self.view.area.size.height
            pw = 550
            ph = 800
            tp = cb.createTemplate(pw, ph)
            g2 = tp.createGraphicsShapes(pw, ph)
            at = java.awt.geom.AffineTransform()
            s = min(float(pw) / w, float(ph) / h)
            at.scale(s, s)
            g2.transform(at)
            self.view.area.pdftemplate = tp, s
            self.view.area.paint(g2)
            self.view.area.pdftemplate = None
            g2.dispose()

            cb.addTemplate(tp, 20, 0)
            doc.close()
def _toPDF(fis) :
    b = ByteArrayOutputStream()
         
    # create a new document
    document = Document();
    # get Instance of the PDFWriter
    pdfWriter = PdfWriter.getInstance(document, b)
          
    # document header attributes
    document.addAuthor("betterThanZero")
    document.addCreationDate()
    document.addProducer()
    document.addCreator("MySampleCode.com")
    document.addTitle("Demo for iText XMLWorker")
    document.setPageSize(PageSize.LETTER);
       
    # open document
    document.open();
          
    # To convert a HTML file from the filesystem
    # String File_To_Convert = "docs/SamplePDF.html";
#    //FileInputStream fis = new FileInputStream(File_To_Convert);
       
    # URL for HTML page
       
    # get the XMLWorkerHelper Instance
    worker = XMLWorkerHelper.getInstance();
    # convert to PDF
    worker.parseXHtml(pdfWriter, document, fis);
          
    # close the document
    document.close();
    # close the writer
    pdfWriter.close();
    return b.toByteArray()
Esempio n. 7
0
for file in sys.argv[1:len(sys.argv) - 1]:
	reader = PdfReader(file)
	reader.consolidateNamedDestinations()
	bookmarks = SimpleBookmark.getBookmark(reader)
	if bookmarks != None:
		if page_offset != 0:
			SimpleBookmark.shiftPageNumbers(bookmarks, \
				page_offset, None)
		all_bookmarks.add(bookmarks)

	page_count = reader.getNumberOfPages()
	page_offset += page_offset

	if copy == None:
		document = Document(reader.getPageSizeWithRotation(1))
		output = FileOutputStream(sys.argv[len(sys.argv) - 1])
		copy = PdfCopy(document, output)
		document.open()

	print "Adding", page_count, "pages from", file

	for k in range(0, page_count):
		copy.addPage(copy.getImportedPage(reader, k + 1))

	if reader.getAcroForm() != None:
		copy.copyAcroForm(reader)

if not all_bookmarks.isEmpty():
	copy.setOutlines(all_bookmarks)
Esempio n. 8
0
for file in sys.argv[1:len(sys.argv) - 1]:
	reader = PdfReader(file)
	reader.consolidateNamedDestinations()
	bookmarks = SimpleBookmark.getBookmark(reader)
	if bookmarks != None:
		if page_offset != 0:
			SimpleBookmark.shiftPageNumbers(bookmarks, \
				page_offset, None)
		all_bookmarks.add(bookmarks)

	page_count = reader.getNumberOfPages()
	page_offset += page_offset

	if copy == None:
		document = Document(reader.getPageSizeWithRotation(1))
		output = FileOutputStream(sys.argv[len(sys.argv) - 1])
		copy = PdfCopy(document, output)
		document.open()

	print "Adding", page_count, "pages from", file

	for k in range(0, page_count):
		copy.addPage(copy.getImportedPage(reader, k + 1))

	if reader.getAcroForm() != None:
		copy.copyAcroForm(reader)

if not all_bookmarks.isEmpty():
	copy.setOutlines(all_bookmarks)
Esempio n. 9
0
def createPDF(fis,map={},paragraph=None) :
    b = ByteArrayOutputStream()
         
    # create a new document
    document = Document();
    # get Instance of the PDFWriter
    pdfWriter = PdfWriter.getInstance(document, b)
    
    # document header attributes
    if map :
      if map.has_key(AUTHOR) : document.addAuthor(map[AUTHOR])
      if map.has_key(PRODUCER) : document.addProducer()
      if map.has_key(CREATOR) : document.addCreator(map[CREATOR])
      if map.has_key(TITLE) : document.addTitle(map[TITLE])
      
    document.addCreationDate()
          
    document.setPageSize(PageSize.LETTER);
       
    # open document
    document.open();
    if paragraph :
        for pa in paragraph :
           document.add(Paragraph(pa))

                 
    # get the XMLWorkerHelper Instance
    worker = XMLWorkerHelper.getInstance();
    # convert to PDF
    worker.parseXHtml(pdfWriter, document, fis);
          
    # close the document
    document.close();
    # close the writer
    pdfWriter.close();
    return b.toByteArray()