예제 #1
0
 /// <summary>Creates form XObject from page content.</summary>
 /// <remarks>
 /// Creates form XObject from page content.
 /// The page shall be from the document, to which FormXObject will be added.
 /// </remarks>
 /// <param name="page"/>
 public PdfFormXObject(PdfPage page)
     : this(page.GetCropBox())
 {
     GetPdfObject().GetOutputStream().WriteBytes(page.GetContentBytes());
     resources = new PdfResources((PdfDictionary)page.GetResources().GetPdfObject().Clone());
     GetPdfObject().Put(PdfName.Resources, resources.GetPdfObject());
 }
예제 #2
0
        public override void CheckIsoConformance(Object obj, IsoKey key, PdfResources resources)
        {
            CanvasGraphicsState gState;
            PdfDictionary       currentColorSpaces = null;

            if (resources != null)
            {
                currentColorSpaces = resources.GetPdfObject().GetAsDictionary(PdfName.ColorSpace);
            }
            switch (key)
            {
            case IsoKey.CANVAS_STACK: {
                checker.CheckCanvasStack((char)obj);
                break;
            }

            case IsoKey.PDF_OBJECT: {
                checker.CheckPdfObject((PdfObject)obj);
                break;
            }

            case IsoKey.RENDERING_INTENT: {
                checker.CheckRenderingIntent((PdfName)obj);
                break;
            }

            case IsoKey.INLINE_IMAGE: {
                checker.CheckInlineImage((PdfStream)obj, currentColorSpaces);
                break;
            }

            case IsoKey.EXTENDED_GRAPHICS_STATE: {
                gState = (CanvasGraphicsState)obj;
                checker.CheckExtGState(gState);
                break;
            }

            case IsoKey.FILL_COLOR: {
                gState = (CanvasGraphicsState)obj;
                checker.CheckColor(gState.GetFillColor(), currentColorSpaces, true);
                break;
            }

            case IsoKey.PAGE: {
                checker.CheckSinglePage((PdfPage)obj);
                break;
            }

            case IsoKey.STROKE_COLOR: {
                gState = (CanvasGraphicsState)obj;
                checker.CheckColor(gState.GetStrokeColor(), currentColorSpaces, false);
                break;
            }

            case IsoKey.TAG_STRUCTURE_ELEMENT: {
                checker.CheckTagStructureElement((PdfObject)obj);
                break;
            }
            }
        }
        protected void ManipulatePdf(String dest)
        {
            PdfDocument pdf  = new PdfDocument(new PdfWriter(dest));
            PdfFont     font = PdfFontFactory.CreateFont(FONT, null, true);

            // Set the full font to be included and all subset ranges to be removed.
            font.SetSubset(false);

            PdfResources acroResources    = new PdfResources();
            PdfName      fontResourceName = acroResources.AddFont(pdf, font);

            PdfAcroForm.GetAcroForm(pdf, true).SetDefaultResources(acroResources.GetPdfObject());

            Rectangle rect           = new Rectangle(100, 700, 200, 120);
            String    annotationText = "Annotation text";

            /* Set a default appearance string:
             * Tf - a text font operator
             * 24 - a font size (zero value meas that the font shall be auto-sized)
             * fontResourceName - a font value (shall match a resource name in the Font entry
             * of the default resource dictionary)
             * 1 0 0 rg - a color value (red)
             */
            PdfString     daString   = new PdfString(fontResourceName + " 24 Tf 1 0 0 rg");
            PdfAnnotation annotation = new PdfFreeTextAnnotation(rect, new PdfString(annotationText, PdfEncodings.UNICODE_BIG))
                                       .SetDefaultAppearance(daString);

            pdf
            .AddNewPage()
            .AddAnnotation(annotation);

            pdf.Close();
        }
예제 #4
0
        private void TestResourceCreated(PdfDocument doc, int pageNo)
        {
            PdfResources res = doc.GetPage(pageNo).GetResources();

            NUnit.Framework.Assert.AreEqual(1, res.GetPdfObject().Size());
            foreach (PdfName name in res.GetResourceNames())
            {
                PdfObject obj = res.GetResourceObject(PdfName.XObject, name);
                NUnit.Framework.Assert.IsTrue(obj.IsStream());
            }
        }
예제 #5
0
 public Tiling(Rectangle bbox, float xStep, float yStep, bool colored)
     : base(new PdfStream())
 {
     GetPdfObject().Put(PdfName.Type, PdfName.Pattern);
     GetPdfObject().Put(PdfName.PatternType, new PdfNumber(1));
     GetPdfObject().Put(PdfName.PaintType, new PdfNumber(colored ? PdfPattern.Tiling.PaintType.COLORED : PdfPattern.Tiling.PaintType
                                                         .UNCOLORED));
     GetPdfObject().Put(PdfName.TilingType, new PdfNumber(PdfPattern.Tiling.TilingType.CONSTANT_SPACING));
     GetPdfObject().Put(PdfName.BBox, new PdfArray(bbox));
     GetPdfObject().Put(PdfName.XStep, new PdfNumber(xStep));
     GetPdfObject().Put(PdfName.YStep, new PdfNumber(yStep));
     resources = new PdfResources();
     GetPdfObject().Put(PdfName.Resources, resources.GetPdfObject());
 }
 private IDictionary <int, CleanUpImagesCompareTool.PageImageObjectsPaths> ExtractImagesFromPdf(String pdf,
                                                                                                String outputPath)
 {
     using (PdfReader readerPdf = new PdfReader(pdf)) {
         using (PdfDocument pdfDoc = new PdfDocument(readerPdf)) {
             IDictionary <int, CleanUpImagesCompareTool.PageImageObjectsPaths> imageObjectDatas = new Dictionary <int, CleanUpImagesCompareTool.PageImageObjectsPaths
                                                                                                                  >();
             for (int i = 1; i <= pdfDoc.GetNumberOfPages(); i++)
             {
                 PdfPage page = pdfDoc.GetPage(i);
                 CleanUpImagesCompareTool.PageImageObjectsPaths imageObjectData = new CleanUpImagesCompareTool.PageImageObjectsPaths
                                                                                      (page.GetPdfObject().GetIndirectReference());
                 Stack <LocalPathItem> baseLocalPath = new Stack <LocalPathItem>();
                 PdfResources          pdfResources  = page.GetResources();
                 if (pdfResources.GetPdfObject().IsIndirect())
                 {
                     imageObjectData.AddIndirectReference(pdfResources.GetPdfObject().GetIndirectReference());
                 }
                 else
                 {
                     baseLocalPath.Push(new DictPathItem(PdfName.Resources));
                 }
                 PdfDictionary xObjects = pdfResources.GetResource(PdfName.XObject);
                 if (xObjects == null)
                 {
                     continue;
                 }
                 if (xObjects.IsIndirect())
                 {
                     imageObjectData.AddIndirectReference(xObjects.GetIndirectReference());
                     baseLocalPath.Clear();
                 }
                 else
                 {
                     baseLocalPath.Push(new DictPathItem(PdfName.XObject));
                 }
                 bool isPageToGsExtract = false;
                 foreach (PdfName objectName in xObjects.KeySet())
                 {
                     if (!xObjects.Get(objectName).IsStream() || !PdfName.Image.Equals(xObjects.GetAsStream(objectName).GetAsName
                                                                                           (PdfName.Subtype)))
                     {
                         continue;
                     }
                     PdfImageXObject pdfObject = new PdfImageXObject(xObjects.GetAsStream(objectName));
                     baseLocalPath.Push(new DictPathItem(objectName));
                     if (!useGs)
                     {
                         String extension = pdfObject.IdentifyImageFileExtension();
                         String fileName  = outputPath + objectName + "_" + i + "." + extension;
                         CreateImageFromPdfXObject(fileName, pdfObject);
                     }
                     else
                     {
                         isPageToGsExtract = true;
                     }
                     Stack <LocalPathItem> reversedStack = new Stack <LocalPathItem>();
                     reversedStack.AddAll(baseLocalPath);
                     Stack <LocalPathItem> resultStack = new Stack <LocalPathItem>();
                     resultStack.AddAll(reversedStack);
                     imageObjectData.AddLocalPath(resultStack);
                     baseLocalPath.Pop();
                 }
                 if (useGs && isPageToGsExtract)
                 {
                     String fileName = "Page_" + i;
                     ghostscriptHelper.RunGhostScriptImageGeneration(pdf, outputPath, fileName, i.ToString());
                 }
                 CleanUpImagesCompareTool.ImageRenderListener listener = new CleanUpImagesCompareTool.ImageRenderListener();
                 PdfCanvasProcessor parser = new PdfCanvasProcessor(listener);
                 parser.ProcessPageContent(page);
                 ignoredImagesAreas.Put(i, listener.GetImageRectangles());
                 imageObjectDatas.Put(i, imageObjectData);
             }
             return(imageObjectDatas);
         }
     }
 }