Пример #1
0
        public void WithoutUpdateFields()
        {
            //ExStart
            //ExFor:SaveOptions.UpdateFields
            //ExSummary:Shows how to update fields before saving into a PDF document.
            Document doc = DocumentHelper.CreateDocumentFillWithDummyText();

            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions
            {
                UpdateFields = false
            };

            doc.Save(MyDir + @"\Artifacts\UpdateFields_False.pdf", pdfSaveOptions);
            //ExEnd

            Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(MyDir + @"\Artifacts\UpdateFields_False.pdf");

            // Get text fragment by search String
            TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Page  of");

            pdfDocument.Pages.Accept(textFragmentAbsorber);

            // Assert that fields are not updated
            Assert.AreEqual("Page  of", textFragmentAbsorber.TextFragments[1].Text);
        }
        public void WithoutUpdateFields()
        {
            //ExStart
            //ExFor:PdfSaveOptions.Clone
            //ExFor:SaveOptions.UpdateFields
            //ExSummary:Shows how to update fields before saving into a PDF document.
            Document doc = DocumentHelper.CreateDocumentFillWithDummyText();

            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions
            {
                UpdateFields = false
            };

            // PdfSaveOptions objects can be cloned
            Assert.AreNotSame(pdfSaveOptions, pdfSaveOptions.Clone());

            doc.Save(ArtifactsDir + "UpdateFields_False.pdf", pdfSaveOptions);
            //ExEnd
#if !(__MOBILE__ || MAC)
            Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(ArtifactsDir + "UpdateFields_False.pdf");

            // Get text fragment by search String
            Aspose.Pdf.Text.TextFragmentAbsorber textFragmentAbsorber = new Aspose.Pdf.Text.TextFragmentAbsorber("Page  of");
            pdfDocument.Pages.Accept(textFragmentAbsorber);

            // Assert that fields are not updated
            Assert.AreEqual("Page  of", textFragmentAbsorber.TextFragments[1].Text);
#endif
        }
Пример #3
0
        public void EscapeUri(string uri, string result, bool isEscaped)
        {
            //ExStart
            //ExFor:PdfSaveOptions.EscapeUri
            //ExSummary: Shows how to escape hyperlinks or not in the document.
            DocumentBuilder builder = new DocumentBuilder();

            builder.InsertHyperlink("Testlink", uri, false);

            // Set this property to false if you are sure that hyperlinks in document's model are already escaped
            PdfSaveOptions options = new PdfSaveOptions();

            options.EscapeUri = isEscaped;

            builder.Document.Save(MyDir + @"\Artifacts\PdfSaveOptions.EscapedUri Out.pdf", options);
            //ExEnd

            Aspose.Pdf.Document pdfDocument =
                new Aspose.Pdf.Document(MyDir + @"\Artifacts\PdfSaveOptions.EscapedUri Out.pdf");

            // get first page
            Page page = pdfDocument.Pages[1];
            // get the first link annotation
            LinkAnnotation linkAnnot = (LinkAnnotation)page.Annotations[1];

            GoToURIAction action  = (GoToURIAction)linkAnnot.Action;
            string        uriText = action.URI;

            Assert.AreEqual(result, uriText);
            //ExEnd
        }
        public void EscapeUri(string uri, string result, bool isEscaped)
        {
            //ExStart
            //ExFor:PdfSaveOptions.EscapeUri
            //ExFor:PdfSaveOptions.OpenHyperlinksInNewWindow
            //ExSummary: Shows how to escape hyperlinks or not in the document.
            DocumentBuilder builder = new DocumentBuilder();

            builder.InsertHyperlink("Testlink", uri, false);

            // Set this property to false if you are sure that hyperlinks in document's model are already escaped
            PdfSaveOptions options = new PdfSaveOptions();

            options.EscapeUri = isEscaped;
            options.OpenHyperlinksInNewWindow = true;

            builder.Document.Save(ArtifactsDir + "PdfSaveOptions.EscapedUri.pdf", options);
            //ExEnd

#if !(__MOBILE__ || MAC)
            Aspose.Pdf.Document pdfDocument =
                new Aspose.Pdf.Document(ArtifactsDir + "PdfSaveOptions.EscapedUri.pdf");

            // Get first page
            Aspose.Pdf.Page page = pdfDocument.Pages[1];
            // Get the first link annotation
            LinkAnnotation linkAnnot = (LinkAnnotation)page.Annotations[1];

            JavascriptAction action  = (JavascriptAction)linkAnnot.Action;
            string           uriText = action.Script;

            Assert.AreEqual(result, uriText);
#endif
        }
        public void BackgroundShape()
        {
            //ExStart
            //ExFor:DocumentBase.BackgroundShape
            //ExSummary:Shows how to set a background shape for every page of a document.
            Document doc = new Document();

            Assert.IsNull(doc.BackgroundShape);

            // The only shape type that we can use as a background is a rectangle.
            Shape shapeRectangle = new Shape(doc, ShapeType.Rectangle);

            // There are two ways of using this shape as a page background.
            // 1 -  A flat color:
            shapeRectangle.FillColor = System.Drawing.Color.LightBlue;
            doc.BackgroundShape      = shapeRectangle;

            doc.Save(ArtifactsDir + "DocumentBase.BackgroundShape.FlatColor.docx");

            // 2 -  An image:
            shapeRectangle = new Shape(doc, ShapeType.Rectangle);
            shapeRectangle.ImageData.SetImage(ImageDir + "Transparent background logo.png");

            // Adjust the image's appearance to make it more suitable as a watermark.
            shapeRectangle.ImageData.Contrast   = 0.2;
            shapeRectangle.ImageData.Brightness = 0.7;

            doc.BackgroundShape = shapeRectangle;

            Assert.IsTrue(doc.BackgroundShape.HasImage);

            // Microsoft Word does not support shapes with images as backgrounds,
            // but we can still see these backgrounds in other save formats such as .pdf.
            doc.Save(ArtifactsDir + "DocumentBase.BackgroundShape.Image.pdf");
            //ExEnd

            doc = new Document(ArtifactsDir + "DocumentBase.BackgroundShape.FlatColor.docx");

            Assert.AreEqual(System.Drawing.Color.LightBlue.ToArgb(), doc.BackgroundShape.FillColor.ToArgb());
            Assert.Throws <ArgumentException>(() =>
            {
                doc.BackgroundShape = new Shape(doc, ShapeType.Triangle);
            });

#if NET48 || NET5_0 || JAVA
            Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(ArtifactsDir + "DocumentBase.BackgroundShape.Image.pdf");
            XImage pdfDocImage = pdfDocument.Pages[1].Resources.Images[1];

            Assert.AreEqual(400, pdfDocImage.Width);
            Assert.AreEqual(400, pdfDocImage.Height);
            Assert.AreEqual(ColorType.Rgb, pdfDocImage.GetColorType());
#endif
        }
Пример #6
0
        public void WithUpdateFields()
        {
            Document doc = DocumentHelper.CreateDocumentFillWithDummyText();

            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions {
                UpdateFields = true
            };

            doc.Save(MyDir + @"\Artifacts\UpdateFields_False.pdf", pdfSaveOptions);

            Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(MyDir + @"\Artifacts\UpdateFields_False.pdf");

            // Get text fragment by search String from PDF document
            TextFragmentAbsorber textFragmentAbsorber = new TextFragmentAbsorber("Page 1 of 2");

            pdfDocument.Pages.Accept(textFragmentAbsorber);

            // Assert that fields are updated
            Assert.AreEqual("Page 1 of 2", textFragmentAbsorber.TextFragments[1].Text);
        }
Пример #7
0
        public void WindowsBarPdfTitle()
        {
            //ExStart
            //ExFor:PdfSaveOptions.DisplayDocTitle
            //ExSummary:Shows how to display title of the document as title bar.
            Document doc = new Document(MyDir + "Rendering.doc");

            doc.BuiltInDocumentProperties.Title = "Windows bar pdf title";

            PdfSaveOptions pdfSaveOptions = new PdfSaveOptions {
                DisplayDocTitle = true
            };

            doc.Save(MyDir + @"\Artifacts\PdfTitle.pdf", pdfSaveOptions);
            //ExEnd

            Aspose.Pdf.Document pdfDocument = new Aspose.Pdf.Document(MyDir + @"\Artifacts\PdfTitle.pdf");

            Assert.IsTrue(pdfDocument.DisplayDocTitle);
            Assert.AreEqual("Windows bar pdf title", pdfDocument.Info.Title);
        }
Пример #8
0
        public void GeneratePDF(ReceivedLetter receivedLetter)
        {
            MemoryStream stream = null;

            if (receivedLetter.ReceiveLetterFiles != null &&
                receivedLetter.ReceiveLetterFiles.Count > 0)
            {
                //Render Letter Content With Files
                Aspose.Pdf.Document contentPdf = new Aspose.Pdf.Document();
                foreach (var file in receivedLetter.ReceiveLetterFiles)
                {
                    if (file.File.ContentType.Contains("image"))
                    {
                        Aspose.Pdf.Page page = contentPdf.Pages.Add();
                        Bitmap          b    = new Bitmap(new MemoryStream(file.File.Content));
                        page.PageInfo.Width  = b.Width;
                        page.PageInfo.Height = b.Height;
                        // Set margins so image will fit, etc.
                        page.PageInfo.Margin.Bottom = 0;
                        page.PageInfo.Margin.Top    = 0;
                        page.PageInfo.Margin.Left   = 0;
                        page.PageInfo.Margin.Right  = 0;

                        page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height);

                        Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
                        // Add the image into paragraphs collection of the section
                        page.Paragraphs.Add(image1);
                        // Set the image file stream
                        image1.ImageStream = new MemoryStream(file.File.Content);
                    }
                    else if (file.File.ContentType.Contains("pdf"))
                    {
                        //Append pdf to appendages
                        Aspose.Pdf.Document thisPDF = new Aspose.Pdf.Document(new MemoryStream(file.File.Content));
                        contentPdf.Pages.Add(thisPDF.Pages);
                    }
                    else if (file.File.ContentType.Contains("word"))
                    {
                        //Convert to pdf first
                        Document     thisDoc = new Document(new MemoryStream(file.File.Content));
                        MemoryStream temp    = new MemoryStream();
                        thisDoc.Save(temp, SaveFormat.Pdf);
                        //Second append it to appendages
                        Aspose.Pdf.Document thisPDF = new Aspose.Pdf.Document(temp);
                        contentPdf.Pages.Add(thisPDF.Pages);
                    }
                }
                stream = new MemoryStream();
                contentPdf.Save(stream);
                receivedLetter.LetterFileContent = stream.ToArray();
            }
            else
            {
                receivedLetter.LetterFileContent = new byte[0];
            }

            //Debug
            //Aspose.Pdf.Document letterFile = new Aspose.Pdf.Document(stream);
            //letterFile.Save(Server.MapPath("~/LetterOnly.pdf"));


            //Save letter with Appendage
            if (receivedLetter.Appendages != null &&
                receivedLetter.Appendages.Count > 0)
            {
                Aspose.Pdf.Document appendagePdf = new Aspose.Pdf.Document();
                foreach (var file in receivedLetter.Appendages)
                {
                    if (file.File.ContentType.Contains("image"))
                    {
                        Page   page = appendagePdf.Pages.Add();
                        Bitmap b    = new Bitmap(new MemoryStream(file.File.Content));
                        page.PageInfo.Width  = b.Width;
                        page.PageInfo.Height = b.Height;
                        // Set margins so image will fit, etc.
                        page.PageInfo.Margin.Bottom = 0;
                        page.PageInfo.Margin.Top    = 0;
                        page.PageInfo.Margin.Left   = 0;
                        page.PageInfo.Margin.Right  = 0;

                        page.CropBox = new Aspose.Pdf.Rectangle(0, 0, b.Width, b.Height);

                        Aspose.Pdf.Image image1 = new Aspose.Pdf.Image();
                        // Add the image into paragraphs collection of the section
                        page.Paragraphs.Add(image1);
                        // Set the image file stream
                        image1.ImageStream = new MemoryStream(file.File.Content);
                    }
                    else if (file.File.ContentType.Contains("pdf"))
                    {
                        //Append pdf to appendages
                        Aspose.Pdf.Document thisPDF = new Aspose.Pdf.Document(new MemoryStream(file.File.Content));
                        appendagePdf.Pages.Add(thisPDF.Pages);
                    }
                    else if (file.File.ContentType.Contains("word"))
                    {
                        //Convert to pdf first
                        Document     thisDoc = new Document(new MemoryStream(file.File.Content));
                        MemoryStream temp    = new MemoryStream();
                        thisDoc.Save(temp, SaveFormat.Pdf);
                        //Second append it to appendages
                        Aspose.Pdf.Document thisPDF = new Aspose.Pdf.Document(temp);
                        appendagePdf.Pages.Add(thisPDF.Pages);
                    }
                }

                //Return PDF from letter
                if (stream != null && receivedLetter.LetterFileContent.Length > 0)
                {
                    Aspose.Pdf.Document letterPdf = new Aspose.Pdf.Document(stream);

                    stream = new MemoryStream();
                    appendagePdf.Save(stream);
                    receivedLetter.AppendageContent = stream.ToArray();
                    //Debug
                    //appendagePdf.Save(Server.MapPath("~/AppendagesOnly.pdf"));

                    letterPdf.Pages.Add(appendagePdf.Pages);
                    stream = new MemoryStream();
                    letterPdf.Save(stream);
                    //Debug
                    //letterPdf.Save(Server.MapPath("~/letterWithAppendages.pdf"));

                    receivedLetter.LetterFileAppendageContent = stream.ToArray();
                }
                else
                {
                    stream = new MemoryStream();
                    appendagePdf.Save(stream);
                    receivedLetter.AppendageContent           = stream.ToArray();
                    receivedLetter.LetterFileAppendageContent = stream.ToArray();
                }
            }
            else
            {
                receivedLetter.LetterFileAppendageContent = new byte[0];
                receivedLetter.AppendageContent           = new byte[0];
            }
        }