PDF

Parent Previous Next

This adapter only enables the use of the two .NET libraries PDFSharp and Migradoc.

Further information on the libraries can be found here: http://www.pdfsharp.net/



Sample 1

This sample generates a PDF with a link as text and a few other properties


#IYOPRO.Backend

# Include wrappers for PDF libs and import namespaces

# -------------------------------------------------------------

import clr

clr.AddReferenceToFileAndPath(Session.GetAdapterPath("IYOPRO.PDF"))

clr.AddReference("MigraDoc.DocumentObjectModel-WPF")

clr.AddReference("MigraDoc.Rendering-WPF")

from MigraDoc.DocumentObjectModel import Document, TabAlignment, StyleNames, Orientation, HyperlinkType, TextFormat, Color, Shapes

from MigraDoc.Rendering import PdfDocumentRenderer

clr.AddReference("PdfSharp-WPF")

from PdfSharp.Pdf import PdfFontEmbedding


# To save / read the PDF in a stream

# -------------------------------------------------------------

import System

from System.IO import MemoryStream


# Define the file name for the generated PDF

# -------------------------------------------------------------

PdfName='IYOPRO-Sample.pdf'


# Create PDF document container and set properties

# -------------------------------------------------------------

pdf = Document()


pdf.Info.Title = "Dynamic PDF"

pdf.Info.Subject = "Sample script for PDF creation"

pdf.Info.Author = "IYOPRO Support"

pdf.Info.Keywords = "IronPython, Migradoc, PDFSharp, PDF"


# Create text styles

# -------------------------------------------------------------

style = pdf.Styles["Normal"];

style.Font.Name = "Verdana";


# Set content including page layout

# -------------------------------------------------------------

section   = pdf.AddSection()

pageSetup = pdf.DefaultPageSetup.Clone();

pageSetup.Orientation = Orientation.Landscape;

section.PageSetup = pageSetup


paragraph = section.AddParagraph()

paragraph.Format.Font.Color = Color.FromCmyk(66, 39, 0, 7);

paragraph = paragraph.AddHyperlink("http://www.pdfsharp.net/PDFsharpFeatures.ashx",HyperlinkType.Web);

paragraph.AddFormattedText("More at http://www.pdfsharp.net/PDFsharpFeatures.ashx", TextFormat.Underline);

 

# Render PDF and set other properties

# -------------------------------------------------------------

pdf.UseCmykColor = True;

pdfRenderer = PdfDocumentRenderer(True, PdfFontEmbedding.Always)

pdfRenderer.Document = pdf

pdfRenderer.RenderDocument()

pdfRenderer.PdfDocument.Info.Author = "Support"

pdfRenderer.PdfDocument.Info.CreationDate = System.DateTime.Now

pdfRenderer.PdfDocument.Info.Creator = "Iron Python"

pdfRenderer.PdfDocument.Info.Keywords = "Test, IYOPRO"

pdfRenderer.PdfDocument.Info.ModificationDate = System.DateTime.Now

pdfRenderer.PdfDocument.Info.Subject = "An example"

pdfRenderer.PdfDocument.Info.Title = "My generated PDF"


pdfRenderer.PdfDocument.SecuritySettings.PermitAnnotations = False

pdfRenderer.PdfDocument.SecuritySettings.PermitExtractContent = False

pdfRenderer.PdfDocument.SecuritySettings.PermitFormsFill = False

pdfRenderer.PdfDocument.SecuritySettings.PermitModifyDocument = False


# Save PDF

# -------------------------------------------------------------

with MemoryStream() as mem:

   pdfRenderer.Save(mem,False)

   ProcessInstance.WriteFileFromMemoryStream(PdfName,mem)

 

# Make file name(s) available for attachments

attachments =[ PdfName ]    



Sample 2

This sample concatenates PDFs that were previously uploaded to a form


#IYOPRO.Backend

# Include wrappers for PDF libs and import namespaces

# -------------------------------------------------------------

import clr

clr.AddReferenceToFileAndPath(Session.GetAdapterPath("IYOPRO.PDF"))

clr.AddReference("PdfSharp-WPF")

from PdfSharp.Pdf import PdfDocument

from PdfSharp.Pdf.IO import PdfReader,PdfDocumentOpenMode

# For marking

from PdfSharp.Drawing import XFont, XFontStyle, XGraphics, XBrushes, XRect, XStringFormat


# To save / read the PDF in a stream

# -------------------------------------------------------------

from System.IO import MemoryStream


# Define the file name for the generated PDF

# -------------------------------------------------------------

PdfName='IYOPRO-Concat-Sample.pdf'


# Generate output PDF

# -------------------------------------------------------------

outputDocument = PdfDocument()


# For marking

font = XFont("Verdana", 10, XFontStyle.Bold)


# Process uploaded files

# -------------------------------------------------------------

files = Form["Uploads"]

numfiles = len(files)

for fidx, file in enumerate(files):

   ProcessInstance.Info("Processing "+file)

   # MemoryStream des Files erzeugen

   with ProcessInstance.ReadFileToStream(file) as instream:

      inputDocument = PdfReader.Open(instream, PdfDocumentOpenMode.Import)

      numpages = inputDocument.Pages.Count

      for pidx in xrange(numpages):

         inpage = inputDocument.Pages[pidx]

         outpage = outputDocument.AddPage(inpage)

         # Markierung

         gfx = XGraphics.FromPdfPage(outpage)

         info = "Seite "+unicode(pidx+1)+" / "+unicode(numpages)+" von Dokument "+unicode(fidx+1)+" / "+unicode(numfiles)

         ProcessInstance.Info("Adding "+info)

         gfx.DrawString(info, font, XBrushes.Black, XRect(0.0, outpage.Height.Value-30, outpage.Width.Value, outpage.Height.Value), XStringFormat.TopLeft)


# Save PDF

# -------------------------------------------------------------

with MemoryStream() as mem:

   outputDocument.Save(mem,False)

   ProcessInstance.WriteFileFromMemoryStream(PdfName,mem)


# Make file names available for attachments

# -------------------------------------------------------------

attachments =[ PdfName ]