Esempio n. 1
0
// ---------------------------------------------------------------------------    
    /**
     * Creates a PDF document.
     */
    public byte[] CreatePdf() {
      // step 1
      Rectangle rect = new Rectangle(-595, -842, 595, 842);
      using (MemoryStream ms = new MemoryStream()) {
        using (Document document = new Document(rect)) {
          // step 2
          PdfWriter writer = PdfWriter.GetInstance(document, ms);
          // step 3
          document.Open();
          // step 4
          PdfContentByte canvas = writer.DirectContent;
          // draw coordinate system
          canvas.MoveTo(-595, 0);
          canvas.LineTo(595, 0);
          canvas.MoveTo(0, -842);
          canvas.LineTo(0, 842);
          canvas.Stroke();
          // read the PDF with the logo
          PdfReader reader = new PdfReader(RESOURCE);
          PdfTemplate template = writer.GetImportedPage(reader, 1); 
          // add it
          canvas.SaveState();
          canvas.AddTemplate(template, 0, 0);
          AffineTransform af = new AffineTransform();
          af.Translate(-595, 0);
          af.Scale(0.5f, 0.5f);
          canvas.Transform(af);
          canvas.AddTemplate(template, 0, 0);
          canvas.ConcatCTM(AffineTransform.GetTranslateInstance(595, 595));
          canvas.AddTemplate(template, 0, 0);
          canvas.RestoreState();

          canvas.SaveState();
          af = new AffineTransform(1f, 0f, 0.4f, 1f, -750f, -650f);
          canvas.AddTemplate(template, af);
          canvas.RestoreState();
          
          canvas.SaveState();
          af = new AffineTransform(0, -1, -1, 0, 650, 0);
          canvas.AddTemplate(template, af);
          af = new AffineTransform(0, -0.2f, -0.5f, 0, 350, 0);
          canvas.AddTemplate(template, af);
          canvas.RestoreState();         
        }
        return ms.ToArray();
      }
    }
Esempio n. 2
0
        private byte[] CreatePdfWithRotatedXObject(String xobjectText)
        {
            MemoryStream baos = new MemoryStream();
            Document doc = new Document();
            PdfWriter writer = PdfWriter.GetInstance(doc, baos);
            writer.CompressionLevel = 0;
            doc.Open();

            doc.Add(new Paragraph("A"));
            doc.Add(new Paragraph("B"));

            bool rotate = true;

            PdfTemplate template = writer.DirectContent.CreateTemplate(20, 100);
            template.SetColorStroke(BaseColor.GREEN);
            template.Rectangle(0, 0, template.Width, template.Height);
            template.Stroke();
            AffineTransform tx = new AffineTransform();
            if(rotate) {
                tx.Translate(0, template.Height);
                tx.Rotate(-90 / 180f * Math.PI);
            }
            template.Transform(tx);
            template.BeginText();
            template.SetFontAndSize(BaseFont.CreateFont(), 12);
            if (rotate)
                template.MoveText(0, template.Width - 12);
            else
                template.MoveText(0, template.Height - 12);
            template.ShowText(xobjectText);

            template.EndText();

            Image xobjectImage = Image.GetInstance(template);
            if (rotate)
                xobjectImage.RotationDegrees = 90;
            doc.Add(xobjectImage);

            doc.Add(new Paragraph("C"));

            doc.Close();

            return baos.ToArray();
        }