├── Examples ├── 01_Quick guide │ └── HelloWorld.py ├── 02_Text │ ├── AddBorderForText.py │ ├── AddTooltipToText.py │ ├── AddTransparentText.py │ ├── DrawRotatedText.py │ ├── DrawText.py │ ├── DrawTextWithGradient.py │ ├── ExtractHighlightedText.py │ ├── ExtractTextFromParticularPage.py │ ├── ExtractTextFromSpecificArea.py │ ├── FindAndHighlightText.py │ ├── FindTextInDefineArea.py │ ├── Font.py │ ├── GetDetailsOfSearchedText.py │ ├── GetTextSizeBasedOnFont.py │ ├── ReplaceAllSearchedText.py │ ├── ReplaceFirstSearchedText.py │ ├── ReplaceFont.py │ ├── ReplaceTextSecond.py │ ├── SearchTextAndAddHyperlink.py │ ├── SearchTextAndDrawRectangle.py │ ├── SearchWithRegularExpression.py │ ├── SetLineBreak.py │ ├── SuperScriptAndSubScript.py │ ├── TextLayout.py │ └── WrapTextAroundImage.py ├── 03_Images │ ├── AddSVGToPDF.py │ ├── ConvertAllPagesToEMF.py │ ├── ConvertAllPagesToPNG.py │ ├── ConvertImageStreamToPDF.py │ ├── ConvertImageToPDF.py │ ├── ConvertToBMP.py │ ├── DeleteImageByBounds.py │ ├── DeleteImageFirstApproach.py │ ├── DeleteImageSecondApproach.py │ ├── DrawImage.py │ ├── PageToEMF.py │ ├── PageToPNG.py │ ├── ReplaceImageFirstApproach.py │ ├── ReplaceImageSecondApproach.py │ ├── ReplaceImageWithText.py │ └── SetImageSize.py ├── 04_Graphs │ ├── Barcode.py │ ├── DrawContentWithSpotColor.py │ ├── DrawDashedLine.py │ ├── DrawFilledRectangles.py │ ├── DrawLine.py │ ├── DrawRectangles.py │ ├── DrawShape.py │ ├── List.py │ ├── Overlay.py │ ├── SetRectangleTransparency.py │ └── Transparency.py ├── 05_Annotations │ ├── AddFreeTextAnnotation.py │ ├── Annotation.py │ ├── CreatePdf3DAnnotation.py │ ├── CreatePdfLineAnnotation.py │ ├── CreatePdfLinkAnnotation.py │ ├── CreatePdfPolygonAnnotation.py │ ├── CreatePolylineAnnotation.py │ ├── DeleteAllAnnotations.py │ ├── DeleteAnnotation.py │ ├── Extract3DViedoFile.py │ ├── GetAllAnnotationsFromPage.py │ ├── GetParticularAnnotationInfo.py │ ├── InvisibleFreeTextAnnotation.py │ ├── SetFreeTextAnnotationStyle.py │ ├── SetFreeTextAnnotationSubject.py │ ├── TextAnnotationProperties.py │ └── UpdateFreeTextAnnotation.py ├── 06_Attachments │ ├── Attachment.py │ ├── DeleteAllAttachments.py │ ├── GetAllAttachments.py │ ├── GetIndividualAttachment.py │ ├── GetPdfAttachmentInfo.py │ ├── PdfAttachmentRelationship.py │ └── SortFileInPdf.py ├── 07_Bookmarks │ ├── Bookmark.py │ ├── DeleteAllBookmarks.py │ ├── DeleteBookmark.py │ ├── ExpandBookmarks.py │ ├── ExpandSpecificBookmarks.py │ ├── GetBookmarkPageNumber.py │ ├── GetPdfBookmarks.py │ ├── GetPdfChildBookmarks.py │ ├── SetInheritZoom.py │ └── UpdateBookmark.py ├── 08_Forms │ ├── AddTextBoxField.py │ ├── AddTooltipForFormField.py │ ├── AssignIconToButtonField.py │ ├── AutomaticField.py │ ├── ChangeFormFieldLocation.py │ ├── DeleteFormField.py │ ├── DetermineRequiredField.py │ ├── ExtractJavaScript.py │ ├── FillFormField.py │ ├── FillImageInButtonField.py │ ├── FlattenFormField.py │ ├── GetCoordinates.py │ ├── GetFieldValue.py │ ├── GetStyleOfRadioButton.py │ ├── GetValuesFromAllFields.py │ ├── ModifyFormFieldValue.py │ ├── RecognizeRequiredField.py │ ├── RemoveFormField.py │ ├── SelectRadioButtonItem.py │ ├── SetExportValueForCheckbox.py │ └── SetFontForFormField.py ├── 09_StampsAndWatermarks │ ├── AddDateTimeStamp.py │ ├── AddImageStamp.py │ ├── AddTextStamp.py │ ├── AddTilingBackgroundImage.py │ ├── FillStrokeText.py │ ├── HeaderAndFooter │ │ ├── AddDifferentHeaders.py │ │ ├── HeaderAndFooter.py │ │ ├── ImageAndPageNumber.py │ │ ├── ImageAndTextUsingTemplate.py │ │ ├── InlineImageAndPageNumber.py │ │ └── PageNumberInFooter.py │ ├── ImageWatermarkFirst.py │ ├── ImageWatermarkSecond.py │ ├── SetPropertiesForStamp.py │ └── TextWaterMark.py ├── 10_LinksAndActions │ ├── AddPdfLaunchAction.py │ ├── AddTableOfContent.py │ ├── DocumentLinkAnnotation.py │ ├── EmbedSoundFile.py │ ├── ExtractAndUpdateLink.py │ ├── FileLinkAnnotation.py │ ├── GetLinkAnnotation.py │ ├── GoToAction.py │ ├── LaunchFileInNewWindow.py │ ├── Link.py │ ├── RemoveOpenAction.py │ └── SpecifyPageToView.py ├── 11_Conversion │ ├── AddAttachmentsToPDF.py │ ├── ConvertToGrayPdf.py │ ├── ConvertToOFD.py │ ├── ConvertToWordSettingProperties.py │ ├── PDFAToPDF.py │ ├── PDFToHTML │ │ ├── EmbedImages.py │ │ ├── EmbedSVG.py │ │ ├── ToHTMLFilesSplittedByPages.py │ │ └── ToHTMLStream.py │ ├── PdfToExcel.py │ ├── PdfToExcelOptions.py │ ├── RemoveHyperlinks.py │ ├── SVGToPDF.py │ ├── TextToPDF.py │ ├── ToDoc.py │ ├── ToDocx.py │ ├── ToHTML.py │ ├── ToImage.py │ ├── ToLinearizedPdf.py │ ├── ToPCL.py │ ├── ToPDFA.py │ ├── ToPdfA2B.py │ ├── ToPostScript.py │ ├── ToSVG.py │ ├── ToTransparentBackgroundImages.py │ ├── ToXLSX.py │ └── ToXPS.py ├── 12_Page │ ├── DeletePage.py │ ├── Extraction.py │ ├── GetNumberOfPages.py │ ├── GetPageInfo.py │ ├── GetPageLabel.py │ ├── GetPageSize.py │ ├── InsertEmptyPage.py │ ├── InsertEmptyPageAtEnd.py │ ├── PageLable.py │ ├── PageSetup.py │ ├── Pagination.py │ ├── ResetPageSize.py │ ├── RotateExistingPDF.py │ ├── RotateNewPDF.py │ ├── SetPageOrientation.py │ ├── SetTabOrder.py │ ├── SplitAPageIntoMultipage.py │ ├── SplitFileByParticularPage.py │ └── ZoomToPageContents.py └── 13_Document │ ├── AddLayers.py │ ├── Booklet.py │ ├── ChangePdfVersion.py │ ├── CompressDocument.py │ ├── CreateMultilayerPDF.py │ ├── CreatePDFA1WithSpirePDF.py │ ├── CreatePDFPortfolio.py │ ├── CreateTwoColumnPDF.py │ ├── CustomDocumentProperties.py │ ├── DeleteLayer.py │ ├── GetDocumentProperties.py │ ├── GetViewerPreference.py │ ├── GetZoomFactor.py │ ├── InvisibleAllPdfLayers.py │ ├── InvisibleParticularPdfLayers.py │ ├── IsPDFPortfolio.py │ ├── MergeDocuments.py │ ├── MergePdfsByStream.py │ ├── ModifyPageMargins.py │ ├── Properties.py │ ├── RearrangePageOrder.py │ ├── RemovePageMargins.py │ ├── SetExpiryDate.py │ ├── SetZoomFactor.py │ ├── SplitDocument.py │ ├── Template.py │ ├── Transition.py │ └── ViewerPreference.py └── README.md /Examples/01_Quick guide/HelloWorld.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "HelloWorld.pdf" 5 | 6 | #Create a pdf document 7 | doc= PdfDocument() 8 | #Create one page 9 | page = doc.Pages.Add() 10 | s = "Hello, World" 11 | x = 10.0 12 | y = 10.0 13 | font = PdfFont(PdfFontFamily.Helvetica ,30.0) 14 | color = PdfRGBColor(Color.get_Black()) 15 | textBrush = PdfSolidBrush(color) 16 | #Draw the text 17 | page.Canvas.DrawString(s, font, textBrush, x, y) 18 | #Save the document 19 | doc.SaveToFile(outputFile) 20 | doc.Close() 21 | 22 | -------------------------------------------------------------------------------- /Examples/02_Text/AddBorderForText.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/SampleB_1.pdf" 5 | outputFile= "AddBorderForText-result.pdf" 6 | 7 | #Create a pdf document 8 | doc = PdfDocument() 9 | #Load from file 10 | doc.LoadFromFile(inputFile) 11 | #Get the first page 12 | page = doc.Pages[0] 13 | text = "Hello, World2!" 14 | font = PdfTrueTypeFont("Times New Roman", 11.0, PdfFontStyle.Regular,True) 15 | size = font.MeasureString(text) 16 | color = PdfRGBColor(Color.get_Black()) 17 | brush = PdfSolidBrush(color) 18 | x = 60.0 19 | y = 600.0 20 | #Draw the text on page 21 | page.Canvas.DrawString(text, font, brush, x, y) 22 | #Draw border for text 23 | page.Canvas.DrawRectangle(PdfPen(brush, 0.5),RectangleF(x, y, size.Width, size.Height)) 24 | #save to file 25 | doc.SaveToFile(outputFile) 26 | doc.Close() 27 | -------------------------------------------------------------------------------- /Examples/02_Text/AddTooltipToText.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile= "AddTooltipToText-result.pdf" 5 | 6 | #Create a pdf document 7 | doc = PdfDocument() 8 | #Create one page 9 | page = doc.Pages.Add() 10 | page.Canvas.DrawString("Move the mouse cursor over the following text to display a tooltip", PdfTrueTypeFont("Arial", 15.0,PdfFontStyle.Regular, True), PdfBrushes.get_Black(), PointF(10.0, 20.0)) 11 | #Define the text and its style 12 | text1 = "Your Office Development Master" 13 | font1 = PdfTrueTypeFont("Arial",18.0,PdfFontStyle.Regular,True) 14 | sizeF1 = font1.MeasureString(text1) 15 | rec1 = RectangleF(PointF(100.0,100.0), sizeF1) 16 | #Draw text 17 | page.Canvas.DrawString(text1, font1, PdfSolidBrush(PdfRGBColor(Color.get_Blue())), rec1) 18 | #Create invisible button on text position 19 | field1 = PdfButtonField(page, "field1") 20 | #Set the bounds and size of field 21 | field1.Bounds = rec1 22 | #Set tooltip content 23 | field1.ToolTip = "E-iceblue Co. Ltd., a vendor of .NET, Java, C++ and Python development components" 24 | #Set no border for field 25 | field1.BorderWidth = 0.0 26 | #Set backcolor and forcolor for field 27 | field1.BackColor = PdfRGBColor(Color.get_Transparent()) 28 | field1.ForeColor = PdfRGBColor(Color.get_Transparent()) 29 | field1.LayoutMode = PdfButtonLayoutMode.IconOnly 30 | field1.IconLayout.IsFitBounds = True 31 | #Define the text and its style 32 | text2 = "Spire.PDF" 33 | font2 = PdfFont(PdfFontFamily.TimesRoman, 20.0) 34 | sizeF2 = font2.MeasureString(text2) 35 | rec2 = RectangleF(PointF(100.0, 160.0), sizeF2) 36 | #Draw text 37 | page.Canvas.DrawString(text2, font2, PdfBrushes.get_DarkOrange(), rec2) 38 | #Create invisible button on text position 39 | field2 = PdfButtonField(page, "field2") 40 | field2.Bounds = rec2 41 | field2.ToolTip = "A professional PDF library applied to creating," + "writing, editing, handling and reading PDF files" + "without any external dependencies within python application." 42 | field2.BorderWidth = 0.0 43 | field2.BackColor = PdfRGBColor(Color.get_Transparent()) 44 | field2.ForeColor = PdfRGBColor(Color.get_Transparent()) 45 | field2.LayoutMode = PdfButtonLayoutMode.IconOnly 46 | field2.IconLayout.IsFitBounds = True 47 | #Add the buttons to pdf form 48 | doc.AllowCreateForm = True 49 | doc.Form.Fields.Add(field1) 50 | doc.Form.Fields.Add(field2) 51 | #Save the document 52 | doc.SaveToFile(outputFile) 53 | doc.Close() -------------------------------------------------------------------------------- /Examples/02_Text/AddTransparentText.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile= "AddTransparentText-result.pdf" 5 | 6 | doc = PdfDocument() 7 | #Create one A4 page 8 | page = doc.Pages.Add(PdfPageSize.A4(),PdfMargins(0.0)) 9 | page.Canvas.Save() 10 | #Set alpha value 11 | alpha = 0.25 12 | page.Canvas.SetTransparency(alpha, alpha, PdfBlendMode.Normal) 13 | #Create rectangle with specified dimensions 14 | rect = RectangleF(50.0, 50.0, 450.0,page.Size.Height) 15 | #Create transparent text 16 | text = "Spire.PDF for Python is a professional PDF development component that enables developers to"+ " create, read, edit, convert, and save PDF files in Python programs"+ " without depending on any external applications or libraries." 17 | text += "\n\n\n\n\n" 18 | text += "Spire.PDF for Python supports various PDF processing features including"+ "security settings, extracting text/image from PDF, merging/splitting PDF, drawing text/image/shape/barcode to PDF, etc."+ "in python applications without using Adobe Acrobat." 19 | #Create brush from color channel 20 | brush = PdfSolidBrush(PdfRGBColor(Color.FromArgb(30, 0, 255, 0))) 21 | #Draw the text 22 | page.Canvas.DrawString(text, PdfFont(PdfFontFamily.Helvetica, 14.0), brush, rect) 23 | page.Canvas.Restore() 24 | #Save the document 25 | doc.SaveToFile(outputFile) 26 | doc.Close() -------------------------------------------------------------------------------- /Examples/02_Text/DrawRotatedText.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "DrawRotatedText.pdf" 5 | 6 | doc = PdfDocument() 7 | page = doc.Pages.Add() 8 | font = PdfTrueTypeFont("Arial", 12.0,PdfFontStyle.Regular,True) 9 | color = PdfRGBColor(Color.get_Blue()) 10 | brush = PdfSolidBrush(color) 11 | text = "This is a text" 12 | page.Canvas.DrawString(text, font, brush, 20.0, 30.0) 13 | state = page.Canvas.Save() 14 | # Rotate 90 degrees clockwise 15 | point1 = PointF(20.0, 30.0) 16 | page.Canvas.RotateTransform(90.0, point1) 17 | page.Canvas.DrawString(text, font, brush, 20.0, 30.0) 18 | page.Canvas.Restore(state) 19 | # Redrawing a new text requires initializing a new state 20 | state2 = page.Canvas.Save() 21 | point2 = PointF(20.0, 150.0) 22 | page.Canvas.RotateTransform(-90.0, point2) 23 | # Rotate 90 degrees counterclockwise 24 | page.Canvas.DrawString(text, font, brush, 20.0, 150.0) 25 | page.Canvas.Restore(state2) 26 | doc.SaveToFile(outputFile) 27 | doc.Close() -------------------------------------------------------------------------------- /Examples/02_Text/DrawTextWithGradient.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "DrawWithGradient-result.pdf" 5 | 6 | #Create a pdf document 7 | doc = PdfDocument() 8 | #Add a new page 9 | page = doc.Pages.Add() 10 | #Create a rectangle 11 | rect = RectangleF(PointF(0.0, 0.0), SizeF(300.0, 100.0)) 12 | #Create a brush with gradient 13 | brush = PdfLinearGradientBrush(rect, PdfRGBColor(Color.get_Red()), PdfRGBColor(Color.get_Blue()), PdfLinearGradientMode.Horizontal) 14 | #Create a true type font with size 20f, underline style 15 | font = PdfTrueTypeFont("Arial", 20.0, PdfFontStyle.Underline,True) 16 | #Draw text 17 | page.Canvas.DrawString("Welcome to E-iceblue!", font, brush, PointF(0.0, 100.0)) 18 | #Save to file 19 | doc.SaveToFile(outputFile) 20 | doc.Close() 21 | 22 | -------------------------------------------------------------------------------- /Examples/02_Text/ExtractHighlightedText.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | def WriteAllText(fname:str,text:List[str]): 5 | fp = open(fname,"w") 6 | for s in text: 7 | fp.write(s) 8 | fp.close() 9 | 10 | inputFile = "./Demos/Data/ExtractHighlightedText.pdf" 11 | outputFile = "ExtractHighlightedText.txt" 12 | 13 | #Create a pdf document 14 | doc = PdfDocument() 15 | #Load a pdf file 16 | doc.LoadFromFile(inputFile) 17 | page = doc.Pages[0] 18 | textMarkupAnnotation = None 19 | stringBuilder="Extracted hightlighted text:" + '\n' 20 | #Get PdfTextMarkupAnnotationWidget objects 21 | annotations = page.AnnotationsWidget 22 | #Update free text annotation. 23 | if annotations.Count > 0: 24 | for i in range(annotations.Count): 25 | textMarkupAnnotation = annotations.get_Item(i) 26 | if isinstance(textMarkupAnnotation, PdfTextMarkupAnnotationWidget): 27 | #Get the highlighted text 28 | stringBuilder+= page.ExtractText(textMarkupAnnotation.Bounds)+ '\n' 29 | #Get the highlighted color 30 | color = textMarkupAnnotation.TextMarkupColor 31 | WriteAllText(outputFile,stringBuilder) 32 | doc.Close() 33 | -------------------------------------------------------------------------------- /Examples/02_Text/ExtractTextFromParticularPage.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | def WriteAllText(fname:str,text:List[str]): 5 | fp = open(fname,"w") 6 | for s in text: 7 | fp.write(s) 8 | fp.close() 9 | 10 | inputFile = "./Demos/Data/PDFTemplate-Az.pdf" 11 | outputFile = "ExtractTextFromParticularPage_out.txt" 12 | 13 | 14 | doc = PdfDocument() 15 | # Read a pdf file 16 | doc.LoadFromFile(inputFile) 17 | # Get the first page 18 | page = doc.Pages[0] 19 | # Extract text from page keeping white space 20 | text = page.ExtractText(True) 21 | # Write a line of text to the file 22 | WriteAllText(outputFile, text) 23 | doc.Close() -------------------------------------------------------------------------------- /Examples/02_Text/ExtractTextFromSpecificArea.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | def WriteAllText(fname:str,text:List[str]): 5 | fp = open(fname,"w") 6 | for s in text: 7 | fp.write(s) 8 | fp.close() 9 | 10 | inputFile = "./Demos/Data/ExtractTextFromSpecificArea.pdf" 11 | outputFile = "ExtractTextFromSpecificArea_out.txt" 12 | 13 | #Load the PDF file 14 | pdf = PdfDocument() 15 | pdf.LoadFromFile(inputFile) 16 | #Get the first page 17 | page = pdf.Pages[0] 18 | #Extract text from a specific rectangular area within the page 19 | text = page.ExtractText(RectangleF(80.0, 180.0, 500.0, 200.0)) 20 | #Save the text to a .txt file 21 | sb = [] 22 | sb.append(text) 23 | WriteAllText(outputFile, sb) 24 | pdf.Close() -------------------------------------------------------------------------------- /Examples/02_Text/FindAndHighlightText.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/FindAndHighlightText.pdf" 5 | outputFile = "FindAndHighlightText_out.pdf" 6 | 7 | #Load the document from disk 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputFile) 10 | result = None 11 | for i in range(pdf.Pages.Count): 12 | page = pdf.Pages.get_Item(i) 13 | result = page.FindText("science",TextFindParameter.none).Finds 14 | for find in result: 15 | #Highlight searched text 16 | find.ApplyHighLight() 17 | #Save the document 18 | pdf.SaveToFile(outputFile, FileFormat.PDF) 19 | pdf.Close() -------------------------------------------------------------------------------- /Examples/02_Text/FindTextInDefineArea.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/SampleB_1.pdf" 5 | outputFile = "FindTextInDefineArea.pdf" 6 | 7 | # Load document from disk 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | # Define a rectangle 11 | rctg = RectangleF(0.0, 0.0, 300.0, 300.0) 12 | pdfPageBase = doc.Pages.get_Item(0) 13 | findCollection = pdfPageBase.FindText(rctg,"Spire",TextFindParameter.WholeWord) 14 | findCollectionOut = pdfPageBase.FindText(rctg,"PDF",TextFindParameter.WholeWord) 15 | # Find text in the rectangle 16 | for find in findCollection.Finds: 17 | #Highlight searched text 18 | find.ApplyHighLight(Color.get_Green()) 19 | for findOut in findCollectionOut.Finds: 20 | #Highlight searched text 21 | findOut.ApplyHighLight(Color.get_Yellow()) 22 | doc.SaveToFile(outputFile,FileFormat.PDF) 23 | doc.Close() -------------------------------------------------------------------------------- /Examples/02_Text/GetDetailsOfSearchedText.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | def WriteAllText(fname:str,text:List[str]): 5 | fp = open(fname,"w") 6 | for s in text: 7 | fp.write(s) 8 | fp.close() 9 | 10 | inputFile = "./Demos/Data/SearchReplaceTemplate.pdf" 11 | outputFile = "GetDetailsOfSearchedText_out.txt" 12 | 13 | doc = PdfDocument() 14 | # Read a pdf file 15 | doc.LoadFromFile(inputFile) 16 | # Get the first page of pdf file 17 | page = doc.Pages[0] 18 | # Create PdfTextFindCollection object to find all the matched phrases 19 | collection = page.FindText("Spire.PDF for Python", TextFindParameter.IgnoreCase) 20 | # Create a StringBuilder object to put the details of the text searched 21 | builder = [] 22 | for find in collection.Finds: 23 | builder.append("=================================================================================="+"\n") 24 | builder.append("Match Text: " + find.MatchText+"\n") 25 | builder.append("Text: " + find.SearchText+"\n") 26 | builder.append("Size: " + find.Size.ToString()+"\n") 27 | builder.append("Position: " + find.Position.ToString()+"\n") 28 | builder.append("The index of page which is including the searched text : " + str(find.SearchPageIndex)+"\n") 29 | builder.append("The line that contains the searched text : " + find.LineText+"\n") 30 | builder.append("Match Text: " + find.MatchText+"\n") 31 | WriteAllText(outputFile, builder) 32 | doc.Close() 33 | -------------------------------------------------------------------------------- /Examples/02_Text/GetTextSizeBasedOnFont.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | def WriteAllText(fname:str,text:List[str]): 6 | fp = open(fname,"w") 7 | for s in text: 8 | fp.write(s) 9 | fp.close() 10 | 11 | outputFile = "GetTextSizeBasedOnFont.txt" 12 | 13 | text = "Spire.PDF for Python" 14 | # Create an instance for PdfFont 15 | font1 = PdfFont(PdfFontFamily.TimesRoman, 12.0) 16 | # Get text size based on font name and size 17 | sizeF1 = font1.MeasureString(text) 18 | stringBuilder = "1. The width is: " + str(sizeF1.Width) + ", the height is: " + str(sizeF1.Height) 19 | # Create an instance for PdfTrueTypeFont 20 | font2 = PdfTrueTypeFont("Arial", 12.0, PdfFontStyle.Regular, True) 21 | # Get text size based on font name and size 22 | sizeF2 = font2.MeasureString(text) 23 | stringBuilder += "2. The width is: " + str(sizeF2.Width) + ", the height is: " + str(sizeF2.Height) 24 | WriteAllText(outputFile, stringBuilder) -------------------------------------------------------------------------------- /Examples/02_Text/ReplaceAllSearchedText.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile = "./Demos/Data/SearchReplaceTemplate.pdf" 6 | outputFile = "ReplaceAllSearchedText_out.pdf" 7 | 8 | doc = PdfDocument() 9 | # Read a pdf file 10 | doc.LoadFromFile(inputFile) 11 | # Get the first page of pdf file 12 | page = doc.Pages[0] 13 | # Searches "Spire.PDF for .NET" by ignoring case 14 | collection = page.FindText("Spire.PDF for Python",TextFindParameter.IgnoreCase) 15 | newText = "E-iceblue Spire.PDF" 16 | # Creates a brush 17 | brush = PdfSolidBrush( PdfRGBColor(Color.get_DarkBlue())) 18 | # Defines a font 19 | font = PdfTrueTypeFont("Arial", 12.0, PdfFontStyle.Regular,True) 20 | #rec = RectangleF() 21 | for find in collection.Finds: 22 | # Gets the bound of the found text in page 23 | rec = find.Bounds 24 | page.Canvas.DrawRectangle(PdfBrushes.get_White() , rec) 25 | # Draws new text as defined font and color 26 | page.Canvas.DrawString(newText, font, brush, rec) 27 | # This method can directly replace old text with newText,but it just can set the background color, can not set font/forecolor 28 | # find.ApplyRecoverString(newText, Color.get_Gray()) 29 | #Save the document 30 | doc.SaveToFile(outputFile) 31 | doc.Close() 32 | 33 | 34 | -------------------------------------------------------------------------------- /Examples/02_Text/ReplaceFirstSearchedText.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile = "./Demos/Data/SearchReplaceTemplate.pdf" 6 | outputFile = "ReplaceFirstSearchedText_out.pdf" 7 | 8 | doc = PdfDocument() 9 | # Read a pdf file 10 | doc.LoadFromFile(inputFile) 11 | # Get the first page of pdf file 12 | page = doc.Pages[0] 13 | # Searches "Spire.PDF for .NET" by ignoring case 14 | collection = page.FindText("Spire.PDF for Python",TextFindParameter.IgnoreCase) 15 | newText = "Spire.PDF API" 16 | # Gets the first found object 17 | find = collection.Finds[0] 18 | # Creates a brush 19 | brush = PdfSolidBrush(PdfRGBColor(Color.get_DarkBlue())) 20 | # Defines a font 21 | font = PdfTrueTypeFont("Arial", 15.0, PdfFontStyle.Bold,True) 22 | # Gets the bound of the first found text in page 23 | rec = find.Bounds 24 | page.Canvas.DrawRectangle(PdfBrushes.get_White(), rec) 25 | # Draws new text as defined font and color 26 | page.Canvas.DrawString(newText, font, brush, rec) 27 | # This method can directly replace old text with newText,but it just can set the background color, can not set font/forecolor 28 | # find.ApplyRecoverString(newText, Color.get_Gray()) 29 | #Save the document 30 | doc.SaveToFile(outputFile) 31 | doc.Close() -------------------------------------------------------------------------------- /Examples/02_Text/ReplaceFont.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/ReplaceFont.pdf" 5 | outputFile = "ReplaceFont.pdf" 6 | 7 | #Load the document from disk 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Get the fonts used in PDF 11 | fonts = doc.UsedFonts 12 | #Create a new font 13 | newfont = PdfTrueTypeFont("Arial", 13.0, PdfFontStyle.Regular ,True) 14 | for font in fonts: 15 | #Replace the font with new font 16 | font.Replace(newfont) 17 | #Save the document 18 | doc.SaveToFile(outputFile) 19 | doc.Close() -------------------------------------------------------------------------------- /Examples/02_Text/ReplaceTextSecond.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/ReplaceTextInPage.pdf" 5 | outputFile = "ReplaceTextInPage_out.pdf" 6 | # Create a new PdfDocument 7 | doc = PdfDocument() 8 | # Load pdf file from disk 9 | doc.LoadFromFile(inputFile) 10 | # Get the first page of pdf file 11 | page = doc.Pages[0] 12 | # Create a PdfTextReplacer using the first page 13 | replacer = PdfTextReplacer(page) 14 | # Replace all texts in this page 15 | replacer.ReplaceAllText("Spire.PDF","E-iceblue") 16 | # Replace the first found word 17 | replacer.ReplaceText("Adobe Acrobat", "PDF editors") 18 | doc.SaveToFile(outputFile) 19 | doc.Close() -------------------------------------------------------------------------------- /Examples/02_Text/SearchTextAndAddHyperlink.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile ="./Demos/Data/SearchReplaceTemplate.pdf" 5 | outputFile = "SearchTextAndAddHyperlink_out.pdf" 6 | 7 | doc = PdfDocument() 8 | # Read a pdf file 9 | doc.LoadFromFile(inputFile) 10 | # Get the first page of pdf file 11 | page = doc.Pages[0] 12 | # Create PdfTextFindCollection object to find all the matched phrases 13 | collection = page.FindText("e-iceblue", TextFindParameter.IgnoreCase) 14 | # hyperlink url 15 | url = "http://www.e-iceblue.com" 16 | for find in collection.Finds: 17 | # Create a PdfUriAnnotation object to add hyperlink for the searched text 18 | uri = PdfUriAnnotation(find.Bounds) 19 | uri.Uri = url 20 | uri.Border = PdfAnnotationBorder(1.0) 21 | uri.Color = PdfRGBColor(Color.get_Blue()) 22 | page.AnnotationsWidget.Add(uri) 23 | #Save the document 24 | doc.SaveToFile(outputFile) 25 | doc.Close() 26 | -------------------------------------------------------------------------------- /Examples/02_Text/SearchTextAndDrawRectangle.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile ="./Demos/Data/SearchReplaceTemplate.pdf" 6 | outputFile = "SearchTextAndDrawRectangle_out.pdf" 7 | 8 | doc = PdfDocument() 9 | # Read a pdf file 10 | doc.LoadFromFile(inputFile) 11 | # Get the first page of pdf file 12 | page = doc.Pages[0] 13 | # Create PdfTextFindCollection object to find all the matched phrases 14 | collection = page.FindText("Spire.PDF for Python", TextFindParameter.IgnoreCase) 15 | for find in collection.Finds: 16 | # Draw a rectangle with red pen 17 | page.Canvas.DrawRectangle(PdfPen(PdfBrushes.get_Red() ,0.9), find.Bounds) 18 | #Save the document 19 | doc.SaveToFile(outputFile) 20 | doc.Close() -------------------------------------------------------------------------------- /Examples/02_Text/SearchWithRegularExpression.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile ="./Demos/Data/SearchReplaceTemplate.pdf" 5 | outputFile = "ReplaceTextWithRegularExpression_out.pdf" 6 | 7 | doc = PdfDocument() 8 | # Read a pdf file 9 | doc.LoadFromFile(inputFile) 10 | # Get the first page of pdf file 11 | page = doc.Pages[0] 12 | # Create PdfTextFindCollection object to find all the phrases matching the regular expression 13 | collection = page.FindText("\\d{4}",TextFindParameter.Regex) 14 | newText = "New Year" 15 | # Creates a brush 16 | brush = PdfSolidBrush(PdfRGBColor(Color.get_DarkBlue())) 17 | # Defines a font 18 | font = PdfTrueTypeFont("Arial", 7.0, PdfFontStyle.Bold,True) 19 | # Defines text horizontal/vertical center format 20 | centerAlign = PdfStringFormat(PdfTextAlignment.Center,PdfVerticalAlignment.Middle) 21 | for find in collection.Finds: 22 | # Gets the bound of the found text in page 23 | rec = find.Bounds 24 | page.Canvas.DrawRectangle(PdfBrushes.get_GreenYellow() , rec) 25 | # Draws new text as defined font and color 26 | page.Canvas.DrawString(newText, font, brush, rec,centerAlign) 27 | # This method can directly replace old text with newText,but it just can set the background color, can not set font/forecolor 28 | # find.ApplyRecoverString(newText, Color.get_Gray()) 29 | #Save the document 30 | doc.SaveToFile(outputFile) 31 | doc.Close() -------------------------------------------------------------------------------- /Examples/02_Text/SetLineBreak.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "SetLineBreak_out.pdf" 5 | 6 | #Create a pdf document 7 | doc = PdfDocument() 8 | #Create one A4 page 9 | page = doc.Pages.Add(PdfPageSize.A4(), PdfMargins(40.0)) 10 | #Create brush from color channel 11 | brush = PdfSolidBrush(PdfRGBColor(Color.get_Black())) 12 | #Create text 13 | text = "Spire.PDF for Python" + "\n" + "A professional PDF library applied to" + " creating, writing, editing, handling and reading PDF files" + " without any external dependencies within python application." 14 | text += "\n\rSpire.PDF for Java" + "\n" + "A PDF Java API that enables developers to read, " + "write, convert and print PDF documents" + "in Java applications without using Adobe Acrobat." 15 | text += "\n\r" 16 | text += "Welcome to evaluate Spire.PDF!" 17 | #Create rectangle with specified dimensions 18 | rect = RectangleF(50.0, 50.0, page.Size.Width - 150.0, page.Size.Height) 19 | #Draw the text 20 | page.Canvas.DrawString(text, PdfFont(PdfFontFamily.Helvetica, 13.0), brush, rect) 21 | #Save the document 22 | doc.SaveToFile(outputFile) 23 | doc.Close() -------------------------------------------------------------------------------- /Examples/02_Text/SuperScriptAndSubScript.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | def DrawSuperscript(page, text, font, brush): 6 | x = 120.0 7 | y = 100.0 8 | page.Canvas.DrawString(text, font, brush, PointF(x, y)) 9 | 10 | #Measure the string 11 | size = font.MeasureString(text) 12 | 13 | #Set the x coordinate of the superscript text 14 | x += size.Width 15 | 16 | #Instantiate a PdfStringFormat instance 17 | strformat = PdfStringFormat() 18 | 19 | #Set format as superscript 20 | strformat.SubSuperScript = PdfSubSuperScript.SuperScript 21 | 22 | #Draw superscript text with format 23 | text = "Superscript" 24 | page.Canvas.DrawString(text, font, brush, PointF(x, y), strformat) 25 | 26 | def DrawSubscript(page, text, font, brush): 27 | x = 120.0 28 | y = 150.0 29 | page.Canvas.DrawString(text, font, brush, PointF(x, y)) 30 | 31 | #Measure the string 32 | size = font.MeasureString(text) 33 | 34 | #Set the x coordinate of the superscript text 35 | x += size.Width 36 | 37 | #Instantiate a PdfStringFormat instance 38 | strformat = PdfStringFormat() 39 | 40 | #Set format as superscript 41 | strformat.SubSuperScript = PdfSubSuperScript.SubScript 42 | 43 | #Draw superscript text with format 44 | text = "SubScript" 45 | page.Canvas.DrawString(text, font, brush, PointF(x, y), strformat) 46 | 47 | 48 | result = "SuperScriptAndSubScriptInPDF_out.pdf" 49 | #Create a pdf document 50 | doc = PdfDocument() 51 | #Add a page 52 | page = doc.Pages.Add() 53 | 54 | #Set font and brush 55 | font = PdfTrueTypeFont("Arial", 20.0,PdfFontStyle.Regular,True) 56 | brush = PdfSolidBrush(PdfRGBColor(Color.get_Black())) 57 | 58 | text = "Spire.PDF for Python" 59 | 60 | #Draw Superscript 61 | DrawSuperscript(page, text, font, brush) 62 | 63 | #Draw Subscript 64 | DrawSubscript(page, text, font, brush) 65 | 66 | 67 | #Save the document 68 | doc.SaveToFile(result) 69 | doc.Close() 70 | 71 | 72 | -------------------------------------------------------------------------------- /Examples/02_Text/WrapTextAroundImage.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | def ReadText(fname:str): 5 | with open(fname, 'r') as f: 6 | content = f.read() 7 | return content 8 | 9 | inputFile1 = "./Data/PdfImage.png" 10 | inputFile2 = "./Data/text.txt" 11 | output = "PlaceTextAroundImage_out.pdf" 12 | 13 | # Creates a pdf document 14 | doc = PdfDocument() 15 | 16 | # Creates a page 17 | page = doc.Pages.Add() 18 | 19 | #Gets page width 20 | pageWidth = page.Canvas.ClientSize.Width 21 | y = 0.0 22 | 23 | y = y + 8 24 | 25 | # Creates a brush 26 | brush = PdfSolidBrush(PdfRGBColor(Color.get_Black())) 27 | 28 | # Defines a font 29 | font1 = PdfTrueTypeFont("Arial", 20.0, PdfFontStyle.Bold ,True) 30 | 31 | # Defines a text center alignment format 32 | format1 = PdfStringFormat(PdfTextAlignment.Center) 33 | format1.CharacterSpacing = 1.0 34 | 35 | text = "Spire.PDF for Python" 36 | # Draws text at the specified position 37 | page.Canvas.DrawString(text, font1, brush, pageWidth / 2, y, format1) 38 | # Get the size of text 39 | size = font1.MeasureString(text, format1) 40 | y = y + size.Height + 6 41 | 42 | # Loads an image 43 | image = PdfImage.FromFile(inputFile1) 44 | 45 | # Draws image at the specified position 46 | page.Canvas.DrawImage(image, PointF(pageWidth - image.PhysicalDimension.Width, y)) 47 | imageLeftSpace = pageWidth - image.PhysicalDimension.Width - 2 48 | imageBottom = image.PhysicalDimension.Height + y 49 | 50 | format2 = PdfStringFormat() 51 | # Loads the text around the image 52 | text = ReadText(inputFile2) 53 | 54 | font2 = PdfTrueTypeFont("Arial", 16.0 , PdfFontStyle.Regular, True) 55 | 56 | #Set line spacing 57 | format2.LineSpacing = font2.Size * 1.5 58 | 59 | textLayouter = PdfStringLayouter() 60 | imageLeftBlockHeight = imageBottom - y 61 | # Splits the text around into multiple lines based on the draw area 62 | result = textLayouter.Layout(text, font2, format2, SizeF(imageLeftSpace, imageLeftBlockHeight)) 63 | if result.ActualSize.Height < imageLeftBlockHeight: 64 | imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight 65 | result = textLayouter.Layout(text, font2, format2, SizeF(imageLeftSpace, imageLeftBlockHeight)) 66 | # Draws all the lines onto the page 67 | for line in result.Lines: 68 | page.Canvas.DrawString(line.Text, font2, brush, 0.0, y, format2) 69 | y = y + result.LineHeight 70 | 71 | # Draw the rest of the text onto the page 72 | textWidget = PdfTextWidget(result.Remainder, font2, brush) 73 | textLayout = PdfTextLayout() 74 | textLayout.Break = PdfLayoutBreakType.FitPage 75 | textLayout.Layout = PdfLayoutType.Paginate 76 | bounds = RectangleF(PointF(0.0, y), page.Canvas.ClientSize) 77 | textWidget.StringFormat = format2 78 | textWidget.Draw(page, bounds, textLayout) 79 | 80 | #Save the document 81 | doc.SaveToFile(output) -------------------------------------------------------------------------------- /Examples/03_Images/AddSVGToPDF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile1 = "./Demos/Data/SampleB_1.pdf" 6 | inputFile2 = "./Demos/Data/template.svg" 7 | outputFile = "AddSVGToPDF_out.pdf" 8 | 9 | #Create a new PDF document. 10 | existingPDF = PdfDocument() 11 | #Load an existing PDF 12 | existingPDF.LoadFromFile(inputFile1) 13 | #Create a new PDF document. 14 | doc = PdfDocument() 15 | #Load the SVG file 16 | doc.LoadFromSvg(inputFile2) 17 | #Create template 18 | template = doc.Pages[0].CreateTemplate() 19 | #Draw template on existing PDF 20 | existingPDF.Pages[0].Canvas.DrawTemplate(doc.Pages[0].CreateTemplate(), PointF(50.0, 350.0), SizeF(200.0, 200.0)) 21 | #Save the document 22 | existingPDF.SaveToFile(outputFile, FileFormat.PDF) 23 | doc.Close() 24 | existingPDF.Close() -------------------------------------------------------------------------------- /Examples/03_Images/ConvertAllPagesToEMF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/ToImage.pdf" 5 | 6 | #Open pdf document 7 | pdf = PdfDocument() 8 | pdf.LoadFromFile(inputFile) 9 | #Save to images 10 | for i in range(pdf.Pages.Count): 11 | fileName = "ToEMF-img-{0:d}.emf".format(i) 12 | with pdf.SaveAsImage(i,PdfImageType.Bitmap) as imageS: 13 | imageS.Save(fileName) 14 | pdf.Close() -------------------------------------------------------------------------------- /Examples/03_Images/ConvertAllPagesToPNG.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile = "./Demos/Data/ToImage.pdf" 6 | 7 | #Open pdf document 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputFile) 10 | #Save to images 11 | for i in range(pdf.Pages.Count): 12 | fileName = "ToPNG-img-{0:d}.png".format(i) 13 | with pdf.SaveAsImage(i) as imageS: 14 | imageS.Save(fileName) 15 | pdf.Close() 16 | -------------------------------------------------------------------------------- /Examples/03_Images/ConvertImageStreamToPDF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile1 = "./Demos/Data/bg.png" 5 | outputFile = "ConvertImageStreamToPDF.pdf" 6 | 7 | # Create a pdf document with a section and page added. 8 | pdf = PdfDocument() 9 | section = pdf.Sections.Add() 10 | page = section.Pages.Add() 11 | # Create a FileStream object to read the imag file 12 | fileStream = Stream(inputFile1) 13 | # Specify the image source as MemoryStream 14 | image = PdfImage.FromStream(fileStream) 15 | #Set image display location and size in PDF 16 | #Calculate rate 17 | widthFitRate = image.PhysicalDimension.Width / page.Canvas.ClientSize.Width 18 | heightFitRate = image.PhysicalDimension.Height / page.Canvas.ClientSize.Height 19 | fitRate = max(widthFitRate, heightFitRate) 20 | #Calculate the size of image 21 | fitWidth = image.PhysicalDimension.Width / fitRate 22 | fitHeight = image.PhysicalDimension.Height / fitRate 23 | #Draw image 24 | page.Canvas.DrawImage(image, 0.0, 30.0, fitWidth, fitHeight) 25 | pdf.SaveToFile(outputFile) 26 | pdf.Close() -------------------------------------------------------------------------------- /Examples/03_Images/ConvertImageToPDF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | import math 4 | 5 | inputFile1 = "./Demos/Data/bg.png" 6 | outputFile = "ConvertImageToPDF.pdf" 7 | 8 | # Create a pdf document with a section and page added. 9 | pdf = PdfDocument() 10 | section = pdf.Sections.Add() 11 | page = pdf.Pages.Add() 12 | #Load a tiff image from system 13 | image = PdfImage.FromFile(inputFile1) 14 | #Set image display location and size in PDF 15 | #Calculate rate 16 | widthFitRate = math.trunc(image.PhysicalDimension.Width / float(page.Canvas.ClientSize.Width)) 17 | heightFitRate = math.trunc(image.PhysicalDimension.Height / float(page.Canvas.ClientSize.Height)) 18 | fitRate = max(widthFitRate, heightFitRate) 19 | #Calculate the size of image 20 | fitWidth = image.PhysicalDimension.Width / fitRate 21 | fitHeight = image.PhysicalDimension.Height / fitRate 22 | #Draw image 23 | page.Canvas.DrawImage(image, 0.0, 30.0, fitWidth, fitHeight) 24 | pdf.SaveToFile(outputFile) 25 | pdf.Close() -------------------------------------------------------------------------------- /Examples/03_Images/ConvertToBMP.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/ToImage.pdf" 5 | 6 | #Open pdf document 7 | pdf = PdfDocument() 8 | pdf.LoadFromFile(inputfile) 9 | #Save to images 10 | for i in range(pdf.Pages.Count): 11 | fileName = "ToBMP-img-{0:d}.bmp".format(i) 12 | with pdf.SaveAsImage(i) as imageS: 13 | imageS.Save(fileName) 14 | pdf.Close() -------------------------------------------------------------------------------- /Examples/03_Images/DeleteImageByBounds.py: -------------------------------------------------------------------------------- 1 | 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | inputfile = "./Demos/Data/DeleteImageByBounds.pdf" 6 | outputFile = "DeleteImageByBounds.pdf" 7 | 8 | #Open pdf document 9 | pdf = PdfDocument() 10 | pdf.LoadFromFile(inputfile) 11 | #Get the first page 12 | page = pdf.Pages[0] 13 | imageInfo = page.ImagesInfo 14 | for i in range(len(imageInfo)): 15 | if imageInfo[i].Bounds.Contains(49.68, 73.1): 16 | page.DeleteImage(page.ImagesInfo[i]) 17 | if imageInfo[i].Bounds.IntersectsWith(RectangleF(100.0, 500.0, 30.0, 40.0)): 18 | page.DeleteImage(page.ImagesInfo[i]) 19 | #Save the document 20 | pdf.SaveToFile(outputFile) 21 | pdf.Close() -------------------------------------------------------------------------------- /Examples/03_Images/DeleteImageFirstApproach.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/DeleteImage.pdf" 5 | outputFile = "DeleteImage_out.pdf" 6 | 7 | #Open pdf document 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputfile) 10 | #Get the first page 11 | page = pdf.Pages[0] 12 | #Delete the first image on the page 13 | page.DeleteImage(page.ImagesInfo[0]) 14 | #Save the document 15 | pdf.SaveToFile(outputFile) 16 | pdf.Close() 17 | -------------------------------------------------------------------------------- /Examples/03_Images/DeleteImageSecondApproach.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/DeleteImage.pdf" 5 | outputFile = "DeleteImageSecondApproach.pdf" 6 | 7 | #Open pdf document 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputfile) 10 | #Get the first page 11 | page = pdf.Pages[0] 12 | #Delete the first image on the page 13 | page.DeleteImage(0) 14 | #Save the document 15 | pdf.SaveToFile(outputFile) 16 | pdf.Close() 17 | -------------------------------------------------------------------------------- /Examples/03_Images/DrawImage.py: -------------------------------------------------------------------------------- 1 | 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | import math 5 | 6 | inputfile = "./Demos/Data/ChartImage.png" 7 | outputFile = "DrawImage.pdf" 8 | 9 | #Create a pdf document 10 | doc = PdfDocument() 11 | #Create one page 12 | page = doc.Pages.Add() 13 | #Save graphics state 14 | state = page.Canvas.Save() 15 | #Draw the text - transform 16 | font = PdfFont(PdfFontFamily.Helvetica, 18.0) 17 | brush1 = PdfSolidBrush(PdfRGBColor(Color.get_Blue())) 18 | brush2 = PdfSolidBrush(PdfRGBColor(Color.get_CadetBlue())) 19 | strformat = PdfStringFormat(PdfTextAlignment.Center) 20 | page.Canvas.TranslateTransform(math.trunc(page.Canvas.ClientSize.Width / float(2)), 20.0) 21 | page.Canvas.DrawString("Chart image", font, brush1, 0.0, 0.0, strformat) 22 | page.Canvas.ScaleTransform(1, -0.8) 23 | page.Canvas.DrawString("Chart image", font, brush2, 0.0, -2 * 18 * 1.2, strformat) 24 | # Restore graphics 25 | page.Canvas.Restore(state) 26 | 27 | #Load an image 28 | image = PdfImage.FromFile(inputfile) 29 | width = image.Width * 0.75 30 | height = image.Height * 0.75 31 | x = (page.Canvas.ClientSize.Width - width) / 2 32 | #Draw the image 33 | page.Canvas.DrawImage(image, x, 60.0, width, height) 34 | 35 | skewX = 20.0 36 | skewY = 20.0 37 | scaleX = 0.2 38 | scaleY = 0.6 39 | width = float(((image.Width + image.Height * math.tan(math.pi * skewX/ 180)) * scaleX)) 40 | height = float(((image.Height + image.Width * math.tan(math.pi * skewY/ 180)) * scaleY)) 41 | template = PdfTemplate(width, height) 42 | template.Graphics.ScaleTransform(scaleX, scaleY) 43 | template.Graphics.SkewTransform(skewX, skewY) 44 | template.Graphics.DrawImage(image, 0.0, 0.0) 45 | #Save graphics state 46 | state = page.Canvas.Save() 47 | page.Canvas.TranslateTransform(page.Canvas.ClientSize.Width - 50, 260.0) 48 | offset = math.trunc((page.Canvas.ClientSize.Width - 100) / float(12)) 49 | for i in range(0, 12): 50 | page.Canvas.TranslateTransform(-offset, 0.0) 51 | page.Canvas.SetTransparency(i / 12.0) 52 | page.Canvas.DrawTemplate(template, PointF(0.0, 0.0)) 53 | #Restore graphics 54 | page.Canvas.Restore(state) 55 | 56 | #Save the document 57 | doc.SaveToFile(outputFile) 58 | doc.Close() 59 | -------------------------------------------------------------------------------- /Examples/03_Images/PageToEMF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/PageToImage.pdf" 5 | outputFile = "PageToEMF.emf" 6 | 7 | #Open pdf document 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputfile) 10 | #Save to images 11 | with pdf.SaveAsImage(1) as imageS: 12 | imageS.Save(outputFile) 13 | pdf.Close() 14 | 15 | -------------------------------------------------------------------------------- /Examples/03_Images/PageToPNG.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/PageToImage.pdf" 5 | outputFile = "PageToPNG.png" 6 | 7 | #Open pdf document 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputfile) 10 | #Save to images 11 | with pdf.SaveAsImage(1) as imageS: 12 | imageS.Save(outputFile) 13 | pdf.Close() 14 | 15 | -------------------------------------------------------------------------------- /Examples/03_Images/ReplaceImageFirstApproach.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/ReplaceImage.pdf" 5 | inputImg = "./Demos/Data/E-iceblueLogo.png" 6 | outputFile = "ReplaceImageFirstApproach.pdf" 7 | 8 | #Create a pdf document 9 | doc = PdfDocument() 10 | #Load file from disk. 11 | doc.LoadFromFile(inputfile) 12 | #Get the first page. 13 | page = doc.Pages[0] 14 | #Get images of the first page. 15 | imageInfo = page.ImagesInfo 16 | #Replace the first image on the page. 17 | page.ReplaceImage(0, PdfImage.FromFile( inputImg)) 18 | #Save the document 19 | doc.SaveToFile(outputFile) 20 | doc.Close() 21 | -------------------------------------------------------------------------------- /Examples/03_Images/ReplaceImageSecondApproach.py: -------------------------------------------------------------------------------- 1 | 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | inputfile = "./Demos/Data/ReplaceImage.pdf" 6 | inputImg = "./Demos/Data/E-iceblueLogo.png" 7 | outputFile = "ReplaceImageSecondApproach.pdf" 8 | 9 | #Create a pdf document 10 | doc = PdfDocument() 11 | #Load file from disk. 12 | doc.LoadFromFile(inputfile) 13 | #Get the first page. 14 | page = doc.Pages[0] 15 | #Get images of the first page. 16 | imageInfo = page.ImagesInfo 17 | #Replace the first image on the page. 18 | page.ReplaceImage(imageInfo[0].Index, PdfImage.FromFile(inputImg)) 19 | #Save the document 20 | doc.SaveToFile(outputFile) 21 | doc.Close() 22 | -------------------------------------------------------------------------------- /Examples/03_Images/ReplaceImageWithText.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/ReplaceImage.pdf" 5 | outputFile = "ReplaceImageWithText.pdf" 6 | 7 | #Create a pdf document 8 | doc = PdfDocument() 9 | #Load file from disk. 10 | doc.LoadFromFile(inputfile) 11 | #Get the first page. 12 | page = doc.Pages[0] 13 | #Get images of the first page. 14 | imageInfo = page.ImagesInfo 15 | image = PdfImage.FromStream(imageInfo[0].Image) 16 | #Get width and height of image 17 | widthInPixel = image.Width 18 | heightInPixel = image.Height 19 | #Convert unit from Pixel to Point 20 | convertor = PdfUnitConvertor() 21 | width = convertor.ConvertFromPixels(float(widthInPixel), PdfGraphicsUnit.Point) 22 | height = convertor.ConvertFromPixels(float(heightInPixel), PdfGraphicsUnit.Point) 23 | #Get location of image 24 | xPos = imageInfo[0].Bounds.X 25 | yPos = imageInfo[0].Bounds.Y 26 | #Remove the image 27 | page.DeleteImage(0) 28 | #Define a rectangle 29 | rect = RectangleF(PointF(xPos, yPos), SizeF(width, height)) 30 | #Define string format 31 | strformat = PdfStringFormat() 32 | strformat.Alignment= PdfTextAlignment.Center 33 | strformat.LineAlignment= PdfVerticalAlignment.Middle 34 | #Draw a string at the location of the image 35 | page.Canvas.DrawString("ReplacedText", PdfFont(PdfFontFamily.Helvetica, 18.0), PdfBrushes.get_Purple(), rect, strformat) 36 | #Save the document 37 | doc.SaveToFile(outputFile) 38 | doc.Close() 39 | 40 | -------------------------------------------------------------------------------- /Examples/03_Images/SetImageSize.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/ChartImage.png" 5 | outputFile = "SetImageSize_out.pdf" 6 | 7 | #Create a pdf document. 8 | doc = PdfDocument() 9 | #Create one page 10 | page = doc.Pages.Add() 11 | #Load an image 12 | image = PdfImage.FromFile(inputfile) 13 | #Set the width and height of image 14 | width = image.Width * 0.75 15 | height = image.Height * 0.75 16 | #Define a position to draw image 17 | x = (page.Canvas.ClientSize.Width - width) / 2 18 | y = 60.0 19 | #Draw image on page canvas 20 | page.Canvas.DrawImage(image, x, y, width, height) 21 | #Save the document 22 | doc.SaveToFile(outputFile) 23 | doc.Close() 24 | 25 | -------------------------------------------------------------------------------- /Examples/04_Graphs/DrawContentWithSpotColor.py: -------------------------------------------------------------------------------- 1 | 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | inputfile = "./Demos/Data/DrawContentWithSpotColor.pdf" 6 | outputFile = "SpotColor.pdf" 7 | 8 | #Load the document from disk 9 | pdf = PdfDocument() 10 | pdf.LoadFromFile(inputfile) 11 | #Get the first page 12 | page = pdf.Pages[0] 13 | #Initialize an instance of PdfSeparationColorSpace 14 | cs = PdfSeparationColorSpace("MySpotColor", PdfRGBColor(Color.get_DarkViolet())) 15 | #Set tini = 1 for the cs 16 | color = PdfSeparationColor(cs, 1.0) 17 | #Create a brush with spot color 18 | brush = PdfSolidBrush(color) 19 | #Draw a string 20 | page.Canvas.DrawString("Tint=1.0", PdfFont(PdfFontFamily.Helvetica, 10.0), brush, PointF(160.0, 160.0)) 21 | #Draw pie with spot color(DarkViolet) 22 | page.Canvas.DrawPie(brush, 148.0, 200.0, 60.0, 60.0, 360.0, 360.0) 23 | page.Canvas.DrawString("Tint=0.7", PdfFont(PdfFontFamily.Helvetica, 10.0), brush, PointF(230.0, 160.0)) 24 | color = PdfSeparationColor(cs, 0.7) 25 | brush = PdfSolidBrush(color) 26 | page.Canvas.DrawPie(brush, 218.0, 200.0, 60.0, 60.0, 360.0, 360.0) 27 | page.Canvas.DrawString("Tint=0.4", PdfFont(PdfFontFamily.Helvetica, 10.0), brush, PointF(300.0, 160.0)) 28 | color = PdfSeparationColor(cs, 0.4) 29 | brush = PdfSolidBrush(color) 30 | page.Canvas.DrawPie(brush, 288.0, 200.0, 60.0, 60.0, 360.0, 360.0) 31 | page.Canvas.DrawString("Tint=0.1", PdfFont(PdfFontFamily.Helvetica, 10.0), brush, PointF(370.0, 160.0)) 32 | color = PdfSeparationColor(cs, 0.1) 33 | brush = PdfSolidBrush(color) 34 | page.Canvas.DrawPie(brush, 358.0, 200.0, 60.0, 60.0, 360.0, 360.0) 35 | #Draw pie with spot color(Purple) 36 | cs = PdfSeparationColorSpace("MySpotColor",PdfRGBColor(Color.get_Purple())) 37 | color = PdfSeparationColor(cs, 1.0) 38 | brush = PdfSolidBrush(color) 39 | page.Canvas.DrawPie(brush, 148.0, 280.0, 60.0, 60.0, 360.0, 360.0) 40 | color = PdfSeparationColor(cs, 0.7) 41 | brush = PdfSolidBrush(color) 42 | page.Canvas.DrawPie(brush, 218.0, 280.0, 60.0, 60.0, 360.0, 360.0) 43 | color = PdfSeparationColor(cs, 0.4) 44 | brush = PdfSolidBrush(color) 45 | page.Canvas.DrawPie(brush, 288.0, 280.0, 60.0, 60.0, 360.0, 360.0) 46 | color = PdfSeparationColor(cs, 0.1) 47 | brush = PdfSolidBrush(color) 48 | page.Canvas.DrawPie(brush, 358.0, 280.0, 60.0, 60.0, 360.0, 360.0) 49 | #Draw pie with spot color(DarkSlateBlue) 50 | cs = PdfSeparationColorSpace("MySpotColor",PdfRGBColor(Color.get_DarkSlateBlue())) 51 | color = PdfSeparationColor(cs, 1.0) 52 | brush = PdfSolidBrush(color) 53 | page.Canvas.DrawPie(brush, 148.0, 360.0, 60.0, 60.0, 360.0, 360.0) 54 | color = PdfSeparationColor(cs, 0.7) 55 | brush = PdfSolidBrush(color) 56 | page.Canvas.DrawPie(brush, 218.0, 360.0, 60.0, 60.0, 360.0, 360.0) 57 | color = PdfSeparationColor(cs, 0.4) 58 | brush = PdfSolidBrush(color) 59 | page.Canvas.DrawPie(brush, 288.0, 360.0, 60.0, 60.0, 360.0, 360.0) 60 | color = PdfSeparationColor(cs, 0.1) 61 | brush = PdfSolidBrush(color) 62 | page.Canvas.DrawPie(brush, 358.0, 360.0, 60.0, 60.0, 360.0, 360.0) 63 | #Save the document 64 | pdf.SaveToFile(outputFile) 65 | pdf.Close() -------------------------------------------------------------------------------- /Examples/04_Graphs/DrawDashedLine.py: -------------------------------------------------------------------------------- 1 | 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | inputfile = "./Demos/Data/DrawingTemplate.pdf" 6 | outputFile = "DrawDashedLine_out.pdf" 7 | 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputfile) 10 | #Create one page 11 | page = pdf.Pages[0] 12 | #Save graphics state 13 | state = page.Canvas.Save() 14 | #Draw line 15 | #Set location and size 16 | x = 150.0 17 | y = 200.0 18 | width = 300.0 19 | #Create pens 20 | pen = PdfPen(PdfRGBColor(Color.get_Red()), 3.0) 21 | #Set dash style and pattern 22 | pen.DashStyle = PdfDashStyle.Dash 23 | pen.DashPattern = [1, 4, 1] 24 | #Draw a rectangle 25 | #Draw two crossed lines 26 | page.Canvas.DrawLine(pen, x, y, x + width, y) 27 | #Restore graphics 28 | page.Canvas.Restore(state) 29 | #Save the document 30 | pdf.SaveToFile(outputFile) 31 | pdf.Close() -------------------------------------------------------------------------------- /Examples/04_Graphs/DrawFilledRectangles.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/DrawingTemplate.pdf" 5 | outputFile = "DrawFilledRectangles_out.pdf" 6 | 7 | pdf = PdfDocument() 8 | pdf.LoadFromFile(inputfile) 9 | #Create one page 10 | page = pdf.Pages[0] 11 | #save graphics state 12 | state = page.Canvas.Save() 13 | #Set rectangle display location and size 14 | x = 200.0 15 | y = 300.0 16 | width = 200.0 17 | height = 120.0 18 | #Create one page and brush 19 | pen = PdfPen(PdfRGBColor(Color.get_Black()), 1.0) 20 | brush = PdfSolidBrush(PdfRGBColor(Color.get_OrangeRed())) 21 | #Draw a filled rectangle 22 | page.Canvas.DrawRectangle(pen, brush, RectangleF(PointF(x, y), SizeF(width, height))) 23 | #restore graphics 24 | page.Canvas.Restore(state) 25 | #Save the document 26 | pdf.SaveToFile(outputFile) 27 | pdf.Close() -------------------------------------------------------------------------------- /Examples/04_Graphs/DrawLine.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/DrawingTemplate.pdf" 5 | outputFile = "DrawLine_out.pdf" 6 | 7 | pdf = PdfDocument() 8 | pdf.LoadFromFile(inputfile) 9 | #Create one page 10 | page = pdf.Pages[0] 11 | #Save graphics state 12 | state = page.Canvas.Save() 13 | #Draw line 14 | #Set location and size 15 | x = 95.0 16 | y = 95.0 17 | width = 400.0 18 | height = 500.0 19 | #Create pens 20 | pen = PdfPen(PdfRGBColor(Color.get_Black()), 0.1) 21 | pen1 = PdfPen(PdfRGBColor(Color.get_Red()), 0.1) 22 | #Draw a rectangle 23 | page.Canvas.DrawRectangle(pen, x, y, width, height) 24 | #Draw two crossed lines 25 | page.Canvas.DrawLine(pen1, x, y, x + width, y + height) 26 | page.Canvas.DrawLine(pen1, x + width, y, x, y + height) 27 | #Restore graphics 28 | page.Canvas.Restore(state) 29 | #Save the document 30 | pdf.SaveToFile(outputFile) 31 | pdf.Close() -------------------------------------------------------------------------------- /Examples/04_Graphs/DrawRectangles.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/DrawingTemplate.pdf" 5 | outputFile = "DrawRectangles_out.pdf" 6 | 7 | pdf = PdfDocument() 8 | pdf.LoadFromFile(inputfile) 9 | #Create one page 10 | page = pdf.Pages[0] 11 | #Save graphics state 12 | state = page.Canvas.Save() 13 | #Draw rectangles 14 | #Set rectangle display location and size 15 | x = 130.0 16 | y = 100.0 17 | width = 400.0 18 | height = 400.0 19 | #Create one page 20 | pen = PdfPen(PdfRGBColor(Color.get_Black()), 0.1) 21 | page.Canvas.DrawRectangle(pen, RectangleF(PointF(x, y), SizeF(width, height))) 22 | y = y + height - 50 23 | width = 100.0 24 | height = 50.0 25 | #Initialize an instance of PdfSeparationColorSpace 26 | cs = PdfSeparationColorSpace("MyColor", PdfRGBColor(Color.FromArgb(0, 100, 0, 0))) 27 | pen1 = PdfPen(PdfRGBColor(Color.get_Red()), 1.0) 28 | #Create a brush with spot color 29 | brush = PdfSolidBrush(PdfSeparationColor(cs, 0.1)) 30 | page.Canvas.DrawRectangle(pen1, brush, RectangleF(PointF(x, y), SizeF(width, height))) 31 | #add by eric 32 | page.Canvas.DrawLine(pen, 130.0, 200.0,400.0,400.0) 33 | page.Canvas.DrawLine(pen, 130.0, 100.0, 530.0, 500.0) 34 | #Restore graphics 35 | page.Canvas.Restore(state) 36 | #Save the document 37 | pdf.SaveToFile(outputFile) 38 | pdf.Close() -------------------------------------------------------------------------------- /Examples/04_Graphs/List.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "List.pdf" 5 | 6 | #Create a pdf document 7 | doc = PdfDocument() 8 | #Margin 9 | unitCvtr = PdfUnitConvertor() 10 | margin = PdfMargins() 11 | margin.Top = unitCvtr.ConvertUnits(2.54, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) 12 | margin.Bottom = margin.Top 13 | margin.Left = unitCvtr.ConvertUnits(3.17, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) 14 | margin.Right = margin.Left 15 | #Create one page 16 | page = doc.Pages.Add(PdfPageSize.A4(), margin) 17 | y = 10.0 18 | #Title 19 | brush1 = PdfBrushes.get_Black() 20 | font1 = PdfTrueTypeFont("Arial", 16.0, PdfFontStyle.Bold, True) 21 | format1 = PdfStringFormat(PdfTextAlignment.Center) 22 | page.Canvas.DrawString("Categories List", font1, brush1,page.Canvas.ClientSize.Width / float(2), y, format1) 23 | y = y + font1.MeasureString("Categories List", format1).Height 24 | y = y + 5 25 | rctg = RectangleF(PointF(0.0, 0.0), page.Canvas.ClientSize) 26 | brush = PdfLinearGradientBrush(rctg, PdfRGBColor(Color.get_Navy()),PdfRGBColor(Color.get_OrangeRed()), PdfLinearGradientMode.Vertical) 27 | font = PdfFont(PdfFontFamily.Helvetica, 12.0, PdfFontStyle.Bold) 28 | formatted = "Beverages\nCondiments\nConfections\nDairy Products\nGrains/Cereals\nMeat/Poultry\nProduce\nSeafood" 29 | #Create a list 30 | plist = PdfList(formatted) 31 | plist.Font = font 32 | plist.Indent = 8 33 | plist.TextIndent = 5 34 | plist.Brush = brush 35 | #Draw the list on the page 36 | result = PdfLayoutWidget(plist).Draw(page, 0.0, y) 37 | y = result.Bounds.Bottom 38 | #Create another list 39 | sortedList = PdfSortedList(formatted) 40 | sortedList.Font = font 41 | sortedList.Indent = 8 42 | sortedList.TextIndent = 5 43 | sortedList.Brush = brush 44 | #Draw the list on the page 45 | result2 = PdfLayoutWidget(sortedList).Draw(page, 0.0, y) 46 | y = result2.Bounds.Bottom 47 | marker1 = PdfOrderedMarker(PdfNumberStyle.LowerRoman, PdfFont(PdfFontFamily.Helvetica, 12.0)) 48 | list2 = PdfSortedList(formatted) 49 | list2.Font = font 50 | list2.Marker = marker1 51 | list2.Indent = 8.0 52 | list2.TextIndent = 5.0 53 | list2.Brush = brush 54 | result3 = PdfLayoutWidget(list2).Draw(page, 0.0, y) 55 | y = result3.Bounds.Bottom 56 | marker2 = PdfOrderedMarker(PdfNumberStyle.LowerLatin, PdfFont(PdfFontFamily.Helvetica, 12.0)) 57 | list3 = PdfSortedList(formatted) 58 | list3.Font = font 59 | list3.Marker = marker2 60 | list3.Indent = 8.0 61 | list3.TextIndent = 5.0 62 | list3.Brush = brush 63 | PdfLayoutWidget(list3).Draw(page, 0.0, y) 64 | #Save pdf file 65 | doc.SaveToFile(outputFile) 66 | doc.Close() -------------------------------------------------------------------------------- /Examples/04_Graphs/Overlay.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile1 = "./Demos/Data/Overlay1.pdf" 5 | inputfile2 = "./Demos/Data/Overlay2.pdf" 6 | outputFile = "Overlay.pdf" 7 | 8 | #Load two documents from disk 9 | doc1 = PdfDocument() 10 | doc1.LoadFromFile(inputfile1) 11 | doc2 = PdfDocument() 12 | doc2.LoadFromFile(inputfile2) 13 | #Create page template 14 | template = doc1.Pages[0].CreateTemplate() 15 | for i in range(doc2.Pages.Count): 16 | page = doc2.Pages.get_Item(i) 17 | page.Canvas.SetTransparency(0.25,0.25, PdfBlendMode.Overlay) 18 | page.Canvas.DrawTemplate(template, PointF.Empty()) 19 | #Save the document 20 | doc2.SaveToFile(outputFile) 21 | doc1.Close() 22 | doc2.Close() 23 | -------------------------------------------------------------------------------- /Examples/04_Graphs/SetRectangleTransparency.py: -------------------------------------------------------------------------------- 1 | 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | import math 5 | 6 | inputfile = "./Demos/Data/DrawingTemplate.pdf" 7 | outputFile = "SetRectangleTransparency_out.pdf" 8 | 9 | pdf = PdfDocument() 10 | pdf.LoadFromFile(inputfile) 11 | #Create one page 12 | page = pdf.Pages[0] 13 | #Save graphics state 14 | state = page.Canvas.Save() 15 | #Draw rectangles 16 | x = 200.0 17 | y = 300.0 18 | width = 200.0 19 | height = 100.0 20 | pen = PdfPen(PdfRGBColor(Color.get_Black()), 1.0) 21 | brush = PdfSolidBrush(PdfRGBColor(Color.get_Red())) 22 | page.Canvas.SetTransparency(0.5, 0.5) 23 | page.Canvas.DrawRectangle(pen, brush, RectangleF(PointF(x, y), SizeF(width, height))) 24 | x = x + math.trunc(width / float(2)) 25 | y = y - math.trunc(height / float(2)) 26 | page.Canvas.SetTransparency(0.2, 0.2) 27 | page.Canvas.DrawRectangle(pen, brush, RectangleF(PointF(x, y), SizeF(width, height))) 28 | #Restore graphics 29 | page.Canvas.Restore(state) 30 | #Save the document 31 | pdf.SaveToFile(outputFile) 32 | -------------------------------------------------------------------------------- /Examples/04_Graphs/Transparency.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/ChartImage.png" 5 | outputFile = "Transparency.pdf" 6 | 7 | #Create a pdf document 8 | doc = PdfDocument() 9 | #Create one section 10 | section = doc.Sections.Add() 11 | #Load an image 12 | image = PdfImage.FromFile(inputfile) 13 | imageWidth = image.PhysicalDimension.Width / float(2) 14 | imageHeight = image.PhysicalDimension.Height / float(2) 15 | #for mode in Enum.GetValues(type_of(PdfBlendMode)): 16 | for mode in PdfBlendMode.__members__: 17 | page = section.Pages.Add() 18 | pageWidth = page.Canvas.ClientSize.Width 19 | y = 0.0 20 | #Title 21 | y = y + 15 22 | brush = PdfSolidBrush(PdfRGBColor(Color.get_OrangeRed())) 23 | font = PdfTrueTypeFont("Arial", 16.0, PdfFontStyle.Bold,True) 24 | format = PdfStringFormat(PdfTextAlignment.Center) 25 | text = "Transparency Blend Mode: {0:s}".format(mode) 26 | page.Canvas.DrawString(text, font, brush, pageWidth / 2, y, format) 27 | size = font.MeasureString(text, format) 28 | y = y + size.Height + 25 29 | page.Canvas.DrawImage(image, 0.0, y, imageWidth, imageHeight) 30 | page.Canvas.Save() 31 | d = (page.Canvas.ClientSize.Width - imageWidth) / 5 32 | x = d 33 | y = y + d / 2+40 34 | for i in range(0, 5): 35 | alpha = 1.0 / 6 * (5 - i) 36 | page.Canvas.SetTransparency(alpha, alpha, PdfBlendMode[mode]) 37 | page.Canvas.DrawImage(image, x, y, imageWidth, imageHeight) 38 | x = x + d 39 | y = y + d / 2+ 40 40 | page.Canvas.Restore() 41 | #Save the document 42 | doc.SaveToFile(outputFile) 43 | doc.Close() 44 | -------------------------------------------------------------------------------- /Examples/05_Annotations/AddFreeTextAnnotation.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/AddFreeTextAnnotation.pdf" 5 | outputFile = "AddFreeTextAnnotation.pdf" 6 | 7 | #Create a pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile( inputfile) 10 | page = doc.Pages[0] 11 | rect = RectangleF(0.0, 300.0, 100.0, 80.0) 12 | #Add free text annotation 13 | textAnnotation = PdfFreeTextAnnotation(rect) 14 | textAnnotation.Text = "\n Spire.PDF" 15 | border = PdfAnnotationBorder(1) 16 | font = PdfFont(PdfFontFamily.TimesRoman, 20.0) 17 | textAnnotation.Font = font 18 | textAnnotation.Border = border 19 | textAnnotation.BorderColor = PdfRGBColor(Color.get_Gray()) 20 | textAnnotation.LineEndingStyle = PdfLineEndingStyle.Slash 21 | textAnnotation.Color = PdfRGBColor(Color.get_LightBlue()) 22 | textAnnotation.Opacity = 0.8 23 | page.AnnotationsWidget.Add(textAnnotation) 24 | rect = RectangleF(150.0, 200.0, 150.0, 40.0) 25 | textAnnotation = PdfFreeTextAnnotation(rect) 26 | textAnnotation.Text = "\nHigh Fidelity Pdf file Conversion" 27 | border = PdfAnnotationBorder(1.0) 28 | font = PdfFont(PdfFontFamily.Helvetica, 10.0) 29 | textAnnotation.Font = font 30 | textAnnotation.Border = border 31 | textAnnotation.BorderColor = PdfRGBColor(Color.get_LightGoldenrodYellow()) 32 | textAnnotation.LineEndingStyle = PdfLineEndingStyle.RClosedArrow 33 | textAnnotation.Color = PdfRGBColor(Color.get_LightPink()) 34 | textAnnotation.Opacity = 0.8 35 | page.AnnotationsWidget.Add(textAnnotation) 36 | rect = RectangleF(150.0, 280.0, 280.0, 40.0) 37 | textAnnotation = PdfFreeTextAnnotation(rect) 38 | textAnnotation.Text = "\nEasily Manipulate document and Form fields" 39 | border = PdfAnnotationBorder(1.0) 40 | font = PdfFont(PdfFontFamily.Helvetica, 10.0) 41 | textAnnotation.Font = font 42 | textAnnotation.Border = border 43 | textAnnotation.BorderColor = PdfRGBColor(Color.get_Gray()) 44 | textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle 45 | textAnnotation.Color = PdfRGBColor(Color.get_LightSkyBlue()) 46 | textAnnotation.Opacity = 0.8 47 | page.AnnotationsWidget.Add(textAnnotation) 48 | rect = RectangleF(150.0, 360.0, 200.0, 40.0) 49 | textAnnotation = PdfFreeTextAnnotation(rect) 50 | textAnnotation.Text = "\nSecurity features" 51 | border = PdfAnnotationBorder(1) 52 | font = PdfFont(PdfFontFamily.Helvetica, 10.0) 53 | textAnnotation.Font = font 54 | textAnnotation.Border = border 55 | textAnnotation.BorderColor = PdfRGBColor(Color.get_Pink()) 56 | textAnnotation.LineEndingStyle = PdfLineEndingStyle.RClosedArrow 57 | textAnnotation.Color = PdfRGBColor(Color.get_LightGreen()) 58 | textAnnotation.Opacity = 0.8 59 | page.AnnotationsWidget.Add(textAnnotation) 60 | rect = RectangleF(150.0, 440.0, 200.0, 40.0) 61 | textAnnotation = PdfFreeTextAnnotation(rect) 62 | textAnnotation.Text = "\nExtract data from Pdf documents" 63 | border = PdfAnnotationBorder(1) 64 | font = PdfFont(PdfFontFamily.Helvetica, 10.0) 65 | textAnnotation.Font = font 66 | textAnnotation.Border = border 67 | textAnnotation.BorderColor = PdfRGBColor(Color.get_OrangeRed()) 68 | textAnnotation.LineEndingStyle = PdfLineEndingStyle.RClosedArrow 69 | textAnnotation.Color = PdfRGBColor(Color.get_LightGoldenrodYellow()) 70 | textAnnotation.Opacity = 0.8 71 | page.AnnotationsWidget.Add(textAnnotation) 72 | #Save pdf file 73 | doc.SaveToFile(outputFile) 74 | doc.Close() -------------------------------------------------------------------------------- /Examples/05_Annotations/CreatePdf3DAnnotation.py: -------------------------------------------------------------------------------- 1 | 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | inputfile = "./Demos/Data/CreatePdf3DAnnotation.u3d" 6 | outputFile = "CreatePdf3DAnnotation_out.pdf" 7 | 8 | #Create a new Pdf document. 9 | doc = PdfDocument() 10 | #Add a new page to it. 11 | page = doc.Pages.Add() 12 | #Draw a rectangle on the page to define the canvas area for the 3D file. 13 | rt = RectangleF(0.0, 80.0, 200.0, 200.0) 14 | #Initialize a new object of Pdf3DAnnotation, load the .u3d file as 3D annotation. 15 | annotation = Pdf3DAnnotation(rt, inputfile) 16 | annotation.Activation = Pdf3DActivation() 17 | annotation.Activation.ActivationMode = Pdf3DActivationMode.PageOpen 18 | #Define a 3D view mode. 19 | View = Pdf3DView() 20 | View.Background = Pdf3DBackground(PdfRGBColor(Color.get_Purple())) 21 | View.ViewNodeName = "3DAnnotation" 22 | View.RenderMode = Pdf3DRendermode(Pdf3DRenderStyle.Solid) 23 | View.InternalName = "3DAnnotation" 24 | View.LightingScheme = Pdf3DLighting() 25 | View.LightingScheme.Style = Pdf3DLightingStyle.Day 26 | #Set the 3D view mode for the annotation. 27 | annotation.Views.Add(View) 28 | #Add the annotation to Pdf. 29 | page.AnnotationsWidget.Add(annotation) 30 | #Save the document 31 | doc.SaveToFile(outputFile) 32 | doc.Close() 33 | -------------------------------------------------------------------------------- /Examples/05_Annotations/CreatePdfLineAnnotation.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "CreatePdfLineAnnotation_out.pdf" 5 | 6 | #Create a PDF document. 7 | document = PdfDocument() 8 | #Add a new page. 9 | page = document.Pages.Add() 10 | #Create a line annotation. 11 | linePoints = [100, 650, 180, 650] 12 | lineAnnotation = PdfLineAnnotation(linePoints, "This is the first line annotation") 13 | #Set the line border. 14 | lineAnnotation.lineBorder.BorderStyle = PdfBorderStyle.Solid 15 | lineAnnotation.lineBorder.BorderWidth = 1 16 | #Set the line intent. 17 | lineAnnotation.LineIntent = PdfLineIntent.LineDimension 18 | #Set the line style. 19 | lineAnnotation.BeginLineStyle = PdfLineEndingStyle.Butt 20 | lineAnnotation.EndLineStyle = PdfLineEndingStyle.Diamond 21 | #Set the line flag. 22 | lineAnnotation.Flags = PdfAnnotationFlags.Default 23 | #Set the line color. 24 | lineAnnotation.InnerLineColor = PdfRGBColor(Color.get_Green()) 25 | lineAnnotation.BackColor = PdfRGBColor(Color.get_Green()) 26 | #Set the leader line. 27 | lineAnnotation.LeaderLineExt = 0 28 | lineAnnotation.LeaderLine = 0 29 | #Add the line annotation to the page. 30 | page.AnnotationsWidget.Add(lineAnnotation) 31 | linePoints = [100, 550, 280, 550] 32 | lineAnnotation = PdfLineAnnotation(linePoints, "This is the second line annotation") 33 | lineAnnotation.lineBorder.BorderStyle = PdfBorderStyle.Underline 34 | lineAnnotation.lineBorder.BorderWidth =2 35 | lineAnnotation.LineIntent = PdfLineIntent.LineArrow 36 | lineAnnotation.BeginLineStyle = PdfLineEndingStyle.Circle 37 | lineAnnotation.EndLineStyle = PdfLineEndingStyle.Diamond 38 | lineAnnotation.Flags = PdfAnnotationFlags.Default 39 | lineAnnotation.InnerLineColor = PdfRGBColor(Color.get_Pink()) 40 | lineAnnotation.BackColor = PdfRGBColor(Color.get_Pink()) 41 | lineAnnotation.LeaderLineExt = 0 42 | lineAnnotation.LeaderLine = 0 43 | page.AnnotationsWidget.Add(lineAnnotation) 44 | linePoints = [100, 450, 280, 450] 45 | lineAnnotation = PdfLineAnnotation(linePoints, "This is the third line annotation") 46 | lineAnnotation.lineBorder.BorderStyle = PdfBorderStyle.Beveled 47 | lineAnnotation.lineBorder.BorderWidth = 2 48 | lineAnnotation.LineIntent = PdfLineIntent.LineDimension 49 | lineAnnotation.BeginLineStyle = PdfLineEndingStyle.none 50 | lineAnnotation.EndLineStyle = PdfLineEndingStyle.none 51 | lineAnnotation.Flags = PdfAnnotationFlags.Default 52 | lineAnnotation.InnerLineColor = PdfRGBColor(Color.get_Blue()) 53 | lineAnnotation.BackColor = PdfRGBColor(Color.get_Blue()) 54 | lineAnnotation.LeaderLineExt = 1 55 | lineAnnotation.LeaderLine = 1 56 | page.AnnotationsWidget.Add(lineAnnotation) 57 | #Save the document 58 | document.SaveToFile(outputFile) 59 | document.Close() -------------------------------------------------------------------------------- /Examples/05_Annotations/CreatePdfLinkAnnotation.py: -------------------------------------------------------------------------------- 1 | 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | inputfile = "./Demos/Data/Template_Pdf_3.pdf" 6 | outputFile = "CreatePdfLinkAnnotation_out.pdf" 7 | 8 | #Create a new object of PdfDocument. 9 | doc = PdfDocument() 10 | #Add a page to it. 11 | page = doc.Pages.Add() 12 | #Declare two parameters that will be passed to the constructor of PdfFileLinkAnnotation class. 13 | rect = RectangleF(0.0, 40.0, 250.0, 35.0) 14 | #Create a file link annotation based on the two parameters and add the annotation to the new page. 15 | link = PdfFileLinkAnnotation(rect, inputfile) 16 | page.AnnotationsWidget.Add(link) 17 | #Create a free text annotation based on the same RectangleF, specifying the content. 18 | text = PdfFreeTextAnnotation(rect) 19 | text.Text = "Click here! This is a link annotation." 20 | font = PdfFont(PdfFontFamily.Helvetica, 15.0) 21 | text.Font = font 22 | page.AnnotationsWidget.Add(text) 23 | #Save the document 24 | doc.SaveToFile(outputFile) 25 | doc.Close() -------------------------------------------------------------------------------- /Examples/05_Annotations/CreatePdfPolygonAnnotation.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "CreatePdfPolygonAnnotation_out.pdf" 5 | 6 | #Create a Pdf document. 7 | pdf = PdfDocument() 8 | #Add a new page to it. 9 | page = pdf.Pages.Add() 10 | #Initialize an instance of PdfPolygonAnnotation, specifying all vertex coordinates which can form a complete shape. 11 | polygon = PdfPolygonAnnotation(page, [PointF(0.0, 30.0), PointF(30.0, 15.0), PointF(60.0, 30.0), PointF(45.0, 50.0), PointF(15.0, 50.0), PointF(0.0, 30.0)]) 12 | #Set the border color, text, border effect and other properties of polygon annotation. 13 | polygon.Color = PdfRGBColor(Color.get_PaleVioletRed()) 14 | polygon.Text = "This is a polygon annotation" 15 | polygon.Author = "E-ICEBLUE" 16 | polygon.Subject = "polygon annotation demo" 17 | polygon.BorderEffect = PdfBorderEffect.BigCloud 18 | polygon.ModifiedDate = DateTime.get_Now() 19 | #Add the annotation to Pdf page and save the document. 20 | page.AnnotationsWidget.Add(polygon) 21 | #Save the document 22 | pdf.SaveToFile(outputFile) 23 | pdf.Close() 24 | -------------------------------------------------------------------------------- /Examples/05_Annotations/CreatePolylineAnnotation.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "CreatePolylineAnnotation.pdf" 5 | 6 | #Create a pdf document 7 | pdf = PdfDocument() 8 | #Add a new page 9 | page = pdf.Pages.Add() 10 | #Create a polyline annotation 11 | polyline = PdfPolyLineAnnotation(page, [PointF(0.0, 60.0), PointF(30.0, 45.0), PointF(60.0, 90.0), PointF(90.0, 80.0)]) 12 | #Set properties of polyline 13 | polyline.Color = PdfRGBColor(Color.get_PaleVioletRed()) 14 | polyline.Text = "This is a polygon annotation" 15 | polyline.Author = "E-ICEBLUE" 16 | polyline.Subject = "polygon annotation demo" 17 | polyline.Name = "Test Annotation" 18 | polyline.Border = PdfAnnotationBorder(1) 19 | polyline.ModifiedDate = DateTime.get_Now() 20 | #Add the annotation into page 21 | page.AnnotationsWidget.Add(polyline) 22 | #Save pdf 23 | pdf.SaveToFile(outputFile, FileFormat.PDF) 24 | pdf.Close() 25 | -------------------------------------------------------------------------------- /Examples/05_Annotations/DeleteAllAnnotations.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputfile = "./Demos/Data/Template_Pdf_3.pdf" 5 | outputFile = "DeleteAllAnnotations.pdf" 6 | 7 | #Create a new PDF document. 8 | document = PdfDocument() 9 | #Load the file from disk 10 | document.LoadFromFile(inputfile) 11 | #Remove all annotations 12 | document.Pages[0].AnnotationsWidget.Clear() 13 | #Save the document 14 | document.SaveToFile(outputFile) 15 | document.Close() -------------------------------------------------------------------------------- /Examples/05_Annotations/DeleteAnnotation.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/DeleteAnnotation.pdf" 5 | outputFile = "DeleteAnnotation.pdf" 6 | 7 | #Open pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Remove the first annotation 11 | doc.Pages[0].AnnotationsWidget.RemoveAt(0) 12 | #Save pdf document 13 | doc.SaveToFile(outputFile) 14 | doc.Close() 15 | -------------------------------------------------------------------------------- /Examples/05_Annotations/Extract3DViedoFile.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/3D.pdf" 5 | 6 | #Load old PDF from disk. 7 | pdf = PdfDocument() 8 | pdf.LoadFromFile(inputFile) 9 | #Get the first page. 10 | firstPage = pdf.Pages[0] 11 | #Get the annotation collection of the first page 12 | annot = firstPage.AnnotationsWidget 13 | #Define an int variable 14 | count = 0 15 | if annot.Count > 0: 16 | for i in range(annot.Count): 17 | annotation = annot.get_Item(i) 18 | #Get the TextWebLink Annotation 19 | if isinstance(annotation, Pdf3DAnnotation): 20 | annot3D = annot[i] if isinstance(annot[i], Pdf3DAnnotation) else None 21 | #Get the 3D video data 22 | strData = annot3D._3DData 23 | if strData is not None: 24 | #Write the data into .u3d format file 25 | strData.Save("""3d-{0:d}.u3d""".format(count)) 26 | count += 1 27 | pdf.Close() 28 | -------------------------------------------------------------------------------- /Examples/05_Annotations/GetAllAnnotationsFromPage.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_3.pdf" 5 | outputFile = "GetAllAnnotationsFromPage.txt" 6 | 7 | #Create a new PDF document. 8 | pdf = PdfDocument() 9 | #Load the file from disk. 10 | pdf.LoadFromFile(inputFile) 11 | #Get all annotations from the first page. 12 | annotations = pdf.Pages[0].AnnotationsWidget 13 | sb = [] 14 | if annotations.Count > 0: 15 | for i in range(annotations.Count): 16 | annotation = annotations.get_Item(i) 17 | #Get the TextWebLink Annotation 18 | if isinstance(annotation, PdfPopupAnnotationWidget): 19 | continue 20 | sb.append("Annotation information: ") 21 | sb.append("Text: " + annotation.Text) 22 | modifiedDate = annotation.ModifiedDate.ToString() 23 | sb.append("ModifiedDate: " + modifiedDate) 24 | #Save to file. 25 | with open(outputFile, "w") as file: 26 | file.write("\n".join(sb)) 27 | pdf.Close() 28 | -------------------------------------------------------------------------------- /Examples/05_Annotations/GetParticularAnnotationInfo.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_3.pdf" 5 | outputFile = "GetParticularAnnotationInfo.txt" 6 | 7 | #Create a new PDF document. 8 | pdf = PdfDocument() 9 | #Load the file from disk. 10 | pdf.LoadFromFile(inputFile) 11 | #Get the annotation collection from the document. 12 | annotations = pdf.Pages[0].AnnotationsWidget 13 | sb = [] 14 | if annotations.Count > 0: 15 | for i in range(annotations.Count): 16 | annotation = annotations.get_Item(i) 17 | #Get particular annotation information from the document. 18 | if isinstance(annotation, PdfTextAnnotationWidget): 19 | sb.append("Annotation information: ") 20 | sb.append("Text: " + annotation.Text) 21 | modifiedDate = annotation.ModifiedDate.ToString() 22 | sb.append("ModifiedDate: " + modifiedDate) 23 | sb.append("Annotation text: " + annotation.Text) 24 | sb.append("Annotation ModifiedDate: " + annotation.ModifiedDate.ToString()) 25 | sb.append("Annotation author: " + annotation.Author) 26 | sb.append("Annotation Name: " + annotation.Name) 27 | #Save to file. 28 | with open(outputFile, "w") as file: 29 | file.write("\n".join(sb)) 30 | pdf.Close() 31 | -------------------------------------------------------------------------------- /Examples/05_Annotations/InvisibleFreeTextAnnotation.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_4.pdf" 5 | outputFile = "InvisibleFreeTextAnnotation.pdf" 6 | 7 | #Create a new PDF document. 8 | doc = PdfDocument() 9 | #Load the file from disk. 10 | doc.LoadFromFile(inputFile) 11 | #Get the first page of PDF file. 12 | page = doc.Pages[0] 13 | #Add a free text annotation to the page and set it invisible. 14 | rect = RectangleF(100.0, 120.0, 150.0, 30.0) 15 | FreetextAnnotation = PdfFreeTextAnnotation(rect) 16 | FreetextAnnotation.Text = "Invisible Free Text Annotation" 17 | font = PdfFont(PdfFontFamily.TimesRoman, 10.0) 18 | border = PdfAnnotationBorder(1.0) 19 | FreetextAnnotation.Font = font 20 | FreetextAnnotation.Border = border 21 | FreetextAnnotation.BorderColor = PdfRGBColor(Color.get_Purple()) 22 | FreetextAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle 23 | FreetextAnnotation.Color = PdfRGBColor(Color.get_Green()) 24 | FreetextAnnotation.Opacity = 0.8 25 | #Invisible free text annotation. 26 | FreetextAnnotation.pipeFlags(PdfAnnotationFlags.Print.value | PdfAnnotationFlags.NoView.value) 27 | page.AnnotationsWidget.Add(FreetextAnnotation) 28 | #Add a free text annotation show it on the page. 29 | rect = RectangleF(100.0, 180.0, 150.0, 30.0) 30 | FreetextAnnotation = PdfFreeTextAnnotation(rect) 31 | FreetextAnnotation.Text = "Show Free Text Annotation" 32 | FreetextAnnotation.Font = font 33 | FreetextAnnotation.Border = border 34 | FreetextAnnotation.BorderColor = PdfRGBColor(Color.get_LightPink()) 35 | FreetextAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle 36 | FreetextAnnotation.Color = PdfRGBColor(Color.get_LightGreen()) 37 | FreetextAnnotation.Opacity = 0.8 38 | page.AnnotationsWidget.Add(FreetextAnnotation) 39 | #Save the document 40 | doc.SaveToFile(outputFile) 41 | doc.Close() 42 | 43 | -------------------------------------------------------------------------------- /Examples/05_Annotations/SetFreeTextAnnotationStyle.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_4.pdf" 5 | outputFile = "SetFreeTextAnnotationFormatting.pdf" 6 | 7 | #Create a new PDF document. 8 | doc = PdfDocument() 9 | #Load the file from disk. 10 | doc.LoadFromFile(inputFile) 11 | #Get the first page of PDF file. 12 | page = doc.Pages[0] 13 | #Initialize a PdfFreeTextAnnotation. 14 | rect = RectangleF(150.0, 120.0, 150.0, 30.0) 15 | textAnnotation = PdfFreeTextAnnotation(rect) 16 | #Specify content. 17 | textAnnotation.Text = "\nFree Text Annotation Formatting" 18 | #Set free text annotation formatting and add it to page. 19 | font = PdfFont(PdfFontFamily.TimesRoman, 10.0) 20 | border = PdfAnnotationBorder(1.0) 21 | textAnnotation.Font = font 22 | textAnnotation.Border = border 23 | textAnnotation.BorderColor = PdfRGBColor(Color.get_Purple()) 24 | textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle 25 | textAnnotation.Color = PdfRGBColor(Color.get_Green()) 26 | textAnnotation.Opacity = 0.8 27 | page.AnnotationsWidget.Add(textAnnotation) 28 | rect = RectangleF(150.0, 200.0, 150.0, 40.0) 29 | textAnnotation = PdfFreeTextAnnotation(rect) 30 | textAnnotation.Text = "\nFree Text Annotation Formatting" 31 | border = PdfAnnotationBorder(1.0) 32 | font = PdfFont(PdfFontFamily.Helvetica, 10.0) 33 | textAnnotation.Font = font 34 | textAnnotation.Border = border 35 | textAnnotation.BorderColor = PdfRGBColor(Color.get_LightGoldenrodYellow()) 36 | textAnnotation.LineEndingStyle = PdfLineEndingStyle.RClosedArrow 37 | textAnnotation.Color = PdfRGBColor(Color.get_LightPink()) 38 | textAnnotation.Opacity = 0.8 39 | page.AnnotationsWidget.Add(textAnnotation) 40 | rect = RectangleF(150.0, 280.0, 280.0, 40.0) 41 | textAnnotation = PdfFreeTextAnnotation(rect) 42 | textAnnotation.Text = "\noHow to Set Free Text Annotation Formatting in Pdf file" 43 | border = PdfAnnotationBorder(1) 44 | font = PdfFont(PdfFontFamily.Helvetica, 10.0) 45 | textAnnotation.Font = font 46 | textAnnotation.Border = border 47 | textAnnotation.BorderColor = PdfRGBColor(Color.get_Gray()) 48 | textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle 49 | textAnnotation.Color = PdfRGBColor(Color.get_LightSkyBlue()) 50 | textAnnotation.Opacity = 0.8 51 | page.AnnotationsWidget.Add(textAnnotation) 52 | rect = RectangleF(150.0, 360.0, 200.0, 40.0) 53 | textAnnotation = PdfFreeTextAnnotation(rect) 54 | textAnnotation.Text = "\nFree Text Annotation Formatting" 55 | border = PdfAnnotationBorder(1.0) 56 | font = PdfFont(PdfFontFamily.Helvetica, 10.0) 57 | textAnnotation.Font = font 58 | textAnnotation.Border = border 59 | textAnnotation.BorderColor = PdfRGBColor(Color.get_Pink()) 60 | textAnnotation.LineEndingStyle = PdfLineEndingStyle.RClosedArrow 61 | textAnnotation.Color = PdfRGBColor(Color.get_LightGreen()) 62 | textAnnotation.Opacity = 0.8 63 | page.AnnotationsWidget.Add(textAnnotation) 64 | #Save the document 65 | doc.SaveToFile(outputFile) 66 | doc.Close() 67 | -------------------------------------------------------------------------------- /Examples/05_Annotations/SetFreeTextAnnotationSubject.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_4.pdf" 5 | outputFile = "SetFreeTextAnnotationSubject.pdf" 6 | 7 | #Create a new PDF document. 8 | doc = PdfDocument() 9 | #Load the file from disk. 10 | doc.LoadFromFile(inputFile) 11 | #Get the first page of PDF file. 12 | page = doc.Pages[0] 13 | #Initialize a PdfFreeTextAnnotation. 14 | rect = RectangleF(150.0, 120.0, 150.0, 30.0) 15 | textAnnotation = PdfFreeTextAnnotation(rect) 16 | #Specify content. 17 | textAnnotation.Text = "\nSet free text annotation subject" 18 | #Set subject. 19 | textAnnotation.Subject = "SubjectTest" 20 | #Set free text annotation formatting and add it to page. 21 | font = PdfFont(PdfFontFamily.TimesRoman, 10.0) 22 | border = PdfAnnotationBorder(1.0) 23 | textAnnotation.Font = font 24 | textAnnotation.Border = border 25 | textAnnotation.BorderColor = PdfRGBColor(Color.get_Purple()) 26 | textAnnotation.LineEndingStyle = PdfLineEndingStyle.Circle 27 | textAnnotation.Color = PdfRGBColor(Color.get_Green()) 28 | textAnnotation.Opacity = 0.8 29 | page.AnnotationsWidget.Add(textAnnotation) 30 | #Save the document 31 | doc.SaveToFile(outputFile) 32 | doc.Close() 33 | 34 | -------------------------------------------------------------------------------- /Examples/05_Annotations/TextAnnotationProperties.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile = "./Demos/Data/FreeTextAnnotation.pdf" 6 | outputFile = "CopyTextAnnotationProperties.pdf" 7 | 8 | #Load old PDF from disk. 9 | pdf = PdfDocument() 10 | pdf.LoadFromFile(inputFile) 11 | #Get the first page. 12 | firstPage = pdf.Pages[0] 13 | #Create a new PDF document. 14 | newPdf = PdfDocument() 15 | #Traverse the annotations of the first page of old PDF 16 | annot = firstPage.AnnotationsWidget 17 | if annot.Count > 0: 18 | for i in range(annot.Count): 19 | annotation = annot.get_Item(i) 20 | #Get the TextWebLink Annotation 21 | if isinstance(annotation, PdfFreeTextAnnotationWidget): 22 | #Get its bounds and text 23 | rect = annotation.Bounds 24 | text = annotation.Text 25 | #Add new page for newPdf 26 | newPage = newPdf.Pages.Add(firstPage.Size) 27 | #Add annotation with the same settings as the annotation of old PDF 28 | newAnnotation = PdfFreeTextAnnotation(rect) 29 | newAnnotation.Text = text 30 | newAnnotation.CalloutLines = annotation.CalloutLines 31 | newAnnotation.LineEndingStyle = annotation.LineEndingStyle 32 | newAnnotation.AnnotationIntent=PdfAnnotationIntent.FreeTextCallout 33 | newAnnotation.RectangleDifferences = annotation.RectangularDifferenceArray 34 | newAnnotation.Color = annotation.Color 35 | newPage.AnnotationsWidget.Add(newAnnotation) 36 | #Save the file 37 | newPdf.SaveToFile(outputFile) 38 | newPdf.Close() 39 | 40 | -------------------------------------------------------------------------------- /Examples/05_Annotations/UpdateFreeTextAnnotation.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/UpdateFreeTextAnnotation.pdf" 5 | outputFile = "UpdateFreeTextAnnotation.pdf" 6 | 7 | #Create a new PDF document. 8 | pdf = PdfDocument() 9 | #Load the file from disk. 10 | pdf.LoadFromFile(inputFile) 11 | #Get the annotation Collection from the document. 12 | annotations = pdf.Pages[0].AnnotationsWidget 13 | #Update free text annotation. 14 | if annotations.Count > 0: 15 | for i in range(annotations.Count): 16 | annotation = annotations.get_Item(i) 17 | annotation.Color = PdfRGBColor(Color.get_YellowGreen()) 18 | #Save the document 19 | pdf.SaveToFile(outputFile) 20 | pdf.Close() 21 | -------------------------------------------------------------------------------- /Examples/06_Attachments/DeleteAllAttachments.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/DeleteAllAttachments.pdf" 5 | outputFile = "DeleteAllAttachments.pdf" 6 | 7 | #Open pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Get all attachments 11 | attachments = doc.Attachments 12 | #Delete all attachments 13 | attachments.Clear() 14 | #Save pdf document 15 | doc.SaveToFile(outputFile) 16 | doc.Close() 17 | 18 | -------------------------------------------------------------------------------- /Examples/06_Attachments/GetAllAttachments.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_2.pdf" 5 | 6 | #Create a new PDF document. 7 | pdf = PdfDocument() 8 | #Load the file from disk. 9 | pdf.LoadFromFile(inputFile) 10 | #Get a collection of attachments on the PDF document. 11 | collection = pdf.Attachments 12 | #Save all the attachments to the files. 13 | if collection.Count > 0: 14 | for i in range(collection.Count): 15 | attitem = collection.get_Item(i) 16 | attitem.Data.Save(attitem.FileName) 17 | pdf.Close() 18 | 19 | 20 | -------------------------------------------------------------------------------- /Examples/06_Attachments/GetIndividualAttachment.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_2.pdf" 5 | 6 | #Create a new PDF document. 7 | pdf = PdfDocument() 8 | #Load the file from disk. 9 | pdf.LoadFromFile(inputFile) 10 | #Get a collection of attachments on the PDF document. 11 | collection = pdf.Attachments 12 | #Get the second attachment in PDF file. 13 | attachment = collection.get_Item(1) 14 | #Save the second attachment to the file. 15 | attachment.Data.Save(attachment.FileName) 16 | pdf.Close() 17 | 18 | -------------------------------------------------------------------------------- /Examples/06_Attachments/GetPdfAttachmentInfo.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_2.pdf" 5 | outputFile = "GetPdfAttachmentInfo.txt" 6 | 7 | #Create a new PDF document 8 | pdf = PdfDocument() 9 | #Load the file from disk. 10 | pdf.LoadFromFile(inputFile) 11 | #Get a collection of attachments on the PDF document 12 | collection = pdf.Attachments 13 | #Get the first attachment. 14 | attachment = collection.get_Item(0) 15 | #Get the information of the first attachment. 16 | content = [] 17 | content.append("Filename: " + attachment.FileName) 18 | content.append("Description: " + attachment.Description) 19 | content.append("Creation Date: " + attachment.CreationDate.ToString()) 20 | content.append("Modification Date: " + attachment.ModificationDate.ToString()) 21 | #Save to file. 22 | with open(outputFile, "w") as file: 23 | file.write("\n".join(content)) 24 | pdf.Close() -------------------------------------------------------------------------------- /Examples/06_Attachments/PdfAttachmentRelationship.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | outputFile = "PdfAttachmentRelationship.pdf" 6 | inputFile = "./Demos/Data/Attachment.pdf" 7 | input_Img = "./Demos/Data/E-iceblueLogo.png" 8 | 9 | #Create a pdf document 10 | doc= PdfDocument() 11 | doc.LoadFromFile(inputFile) 12 | #Define PdfAttachment 13 | attachment = PdfAttachment(input_Img) 14 | #Add addachment 15 | doc.Attachments.Add(attachment, doc, PdfAttachmentRelationship.Alternative) 16 | #Save the document 17 | doc.SaveToFile(outputFile) 18 | doc.Close() 19 | 20 | -------------------------------------------------------------------------------- /Examples/06_Attachments/SortFileInPdf.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile1 = "./Demos/Data/SampleB_1.pdf" 5 | inputFile2 = "./Demos/Data/SampleB_2.pdf" 6 | inputFile3 = "./Demos/Data/SampleB_3.pdf" 7 | outputFile = "SortFileInPdf.pdf" 8 | 9 | #Create a pdf document 10 | doc = PdfDocument() 11 | doc.Collection.AddCustomField("No", "number", CustomFieldType.NumberField) 12 | doc.Collection.AddFileRelatedField("Desc", "desc", FileRelatedFieldType.Desc) 13 | doc.Collection.Sort(["No", "Desc"], [True, True]) 14 | pdfAttachment = PdfAttachment(inputFile1) 15 | doc.Collection.AddAttachment(pdfAttachment) 16 | pdfAttachment = PdfAttachment(inputFile2) 17 | doc.Collection.AddAttachment(pdfAttachment) 18 | pdfAttachment = PdfAttachment(inputFile3) 19 | doc.Collection.AddAttachment(pdfAttachment) 20 | i = 1 21 | for attachment in doc.Collection.AssociatedFiles: 22 | attachment.SetFieldValue("No", i) 23 | attachment.SetFieldValue("Desc", attachment.FileName) 24 | i += 1 25 | doc.SaveToFile(outputFile, FileFormat.PDF) 26 | doc.Close() 27 | -------------------------------------------------------------------------------- /Examples/07_Bookmarks/Bookmark.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | doc = PdfDocument() 5 | #Set the margin 6 | unitCvtr = PdfUnitConvertor() 7 | margin = PdfMargins() 8 | margin.Top = unitCvtr.ConvertUnits(2.54, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) 9 | margin.Bottom = margin.Top 10 | margin.Left = unitCvtr.ConvertUnits(3.17, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) 11 | margin.Right = margin.Left 12 | section = doc.Sections.Add() 13 | section.PageSettings.Margins = margin 14 | section.PageSettings.Size = PdfPageSize.A4() 15 | #Create one page 16 | page = section.Pages.Add() 17 | y = 10.0 18 | brush1 = PdfBrushes.get_Black() 19 | font1 = PdfTrueTypeFont("Arial", 16.0, PdfFontStyle.Bold, True) 20 | format1 = PdfStringFormat(PdfTextAlignment.Center); 21 | page.Canvas.DrawString("Spire Pdf For Python BookMark Demo", font1, brush1, page.Canvas.ClientSize.Width / float(2), y, format1) 22 | y = y + font1.MeasureString("Spire Pdf For Python BookMark Demo", format1).Height 23 | y = y + 5 24 | font2 = PdfTrueTypeFont("Arial", 11.0, PdfFontStyle.Bold, True) 25 | font3 = PdfTrueTypeFont("Arial", 10.0, PdfFontStyle.Bold, True) 26 | vendors = [["First Parent BookMark", "Spire","Demo BookMark","Spire Pdf","Spire Pdf For Python"],["Second Parent BookMark" ],["Third Parent BookMark", "First Child BookMark","Second Child BookMark","Third Child BookMark","Fourth Child BookMark"]] 27 | for i in range(len(vendors)): 28 | if i > 0: 29 | #Next page 30 | page = section.Pages.Add() 31 | y = 0.0 32 | #Draw vendor 33 | vendorTitle = "{0:d}. {1:s}".format(i + 1, vendors[i][0]) 34 | #drawVendorLayoutResult = self._DrawVendor(page, vendors, i, vendorTitle, y) 35 | font1 = PdfTrueTypeFont("Arial", 11.0, PdfFontStyle.Bold, True); 36 | page.Canvas.DrawString(vendorTitle, font1, PdfBrushes.get_Blue(), 0.0, y); 37 | #Add vendor bookmark 38 | vendorBookmarkDest = PdfDestination(page, PointF(0.0, y)) 39 | vendorBookmark = doc.Bookmarks.Add(vendorTitle) 40 | vendorBookmark.Color = PdfRGBColor(Color.get_SaddleBrown()) 41 | vendorBookmark.DisplayStyle = PdfTextStyle.Bold 42 | vendorBookmark.Action = PdfGoToAction(vendorBookmarkDest) 43 | vendorBookmarkColletion = PdfBookmarkCollection(vendorBookmark) 44 | y = y + font1.MeasureString(vendorTitle).Height + 1 45 | #Get parts of vendor 46 | for j in range(1,len(vendors[i])): 47 | if j > 1: 48 | #Next page 49 | page = section.Pages.Add() 50 | y = 0.0 51 | #Draw part 52 | partTitle = "{0:d}.{1:d}. {2:s}".format(i + 1, j, vendors[i][j]) 53 | font1 = PdfTrueTypeFont("Arial", 11.0, PdfFontStyle.Bold, True) 54 | page.Canvas.DrawString(partTitle, font1, PdfBrushes.get_Brown(), 0.0, y) 55 | #Add part bookmark 56 | partBookmarkDest = PdfDestination(page, PointF(0.0, y)) 57 | partBookmark = vendorBookmarkColletion.Add(partTitle) 58 | partBookmark.Color = PdfRGBColor(Color.get_Coral()) 59 | partBookmark.DisplayStyle = PdfTextStyle.Italic 60 | partBookmark.Action = PdfGoToAction(partBookmarkDest) 61 | #Save pdf file 62 | outputFile = "Bookmark.pdf" 63 | doc.SaveToFile(outputFile) 64 | doc.Close() 65 | 66 | -------------------------------------------------------------------------------- /Examples/07_Bookmarks/DeleteAllBookmarks.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_1.pdf" 5 | 6 | #Create a new Pdf document. 7 | document = PdfDocument() 8 | #Load the file from disk. 9 | document.LoadFromFile(inputFile) 10 | #Remove all bookmarks. 11 | document.Bookmarks.Clear() 12 | result = "DeleteAllBookmarks.pdf" 13 | #Save the document 14 | document.SaveToFile(result) 15 | document.Close() 16 | 17 | -------------------------------------------------------------------------------- /Examples/07_Bookmarks/DeleteBookmark.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/DeleteBookmark.pdf" 5 | outputFile = "DeleteBookmark.pdf" 6 | 7 | #Load a pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Delete the first bookmark 11 | doc.Bookmarks.RemoveAt(0) 12 | #Save the pdf document 13 | doc.SaveToFile(outputFile) 14 | doc.Close() 15 | 16 | -------------------------------------------------------------------------------- /Examples/07_Bookmarks/ExpandBookmarks.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_1.pdf" 5 | outputFile = "ExpandBookmarks_out.pdf" 6 | 7 | #Create a new PDF document. 8 | doc = PdfDocument() 9 | #Load the file from disk. 10 | doc.LoadFromFile(inputFile) 11 | #Set BookMarkExpandOrCollapse as true to expand the bookmarks. 12 | doc.ViewerPreferences.BookMarkExpandOrCollapse = True 13 | #Save the document 14 | doc.SaveToFile(outputFile) 15 | doc.Close() 16 | 17 | -------------------------------------------------------------------------------- /Examples/07_Bookmarks/ExpandSpecificBookmarks.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/ExpandSpecificBookmarks.pdf" 5 | outputFile = "ExpandSpecificBookmarks.pdf" 6 | 7 | #Load old PDF from disk. 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputFile) 10 | #Set BookMarkExpandOrCollapse as "true" for the first bookmarks 11 | pdf.Bookmarks.get_Item(0).ExpandBookmark = True 12 | #Set BookMarkExpandOrCollapse as "false" for the second bookmarks 13 | pdf.Bookmarks.get_Item(1).ExpandBookmark = False 14 | #Save the file 15 | pdf.SaveToFile(outputFile) 16 | pdf.Close() 17 | 18 | -------------------------------------------------------------------------------- /Examples/07_Bookmarks/GetBookmarkPageNumber.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_1.pdf" 5 | outputFile = "GetPdfBookmarkPageNumber.txt" 6 | 7 | #Create a new PDF document. 8 | doc = PdfDocument() 9 | #Load the file from disk. 10 | doc.LoadFromFile(inputFile) 11 | #Get bookmarks collections of the PDF file. 12 | bookmarks = doc.Bookmarks 13 | #Get the page number of the first bookmark. 14 | bookmark = bookmarks[0] 15 | pageNumber = doc.Pages.IndexOf(bookmark.Destination.Page)+1 16 | #Save to file. 17 | showPageNumber = str(pageNumber) 18 | #Save to file. 19 | with open(outputFile, "w") as file: 20 | file.write(showPageNumber) 21 | doc.Close() -------------------------------------------------------------------------------- /Examples/07_Bookmarks/GetPdfBookmarks.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_1.pdf" 5 | outputFile = "GetPdfBookmarks.txt" 6 | 7 | def GetChildBookmark(parentBookmark, content): 8 | if parentBookmark.Count > 0: 9 | #for childBookmark in parentBookmark: 10 | for i in range(parentBookmark.Count): 11 | childBookmark = parentBookmark.get_Item(i) 12 | #Get the title. 13 | content.append(childBookmark.Title) 14 | #Get the text style. 15 | textStyle = str(childBookmark.DisplayStyle) 16 | content.append(textStyle) 17 | cldBk = PdfBookmarkCollection(childBookmark) 18 | GetChildBookmark(cldBk, content) 19 | 20 | def GetBookmarks(bookmarks, result): 21 | #Declare a new StringBuilder content 22 | content = [] 23 | #Get Pdf bookmarks information. 24 | if bookmarks.Count > 0: 25 | content.append("Pdf bookmarks:") 26 | for i in range(bookmarks.Count): 27 | parentBookmark = bookmarks.get_Item(i) 28 | #for parentBookmark in bookmarks: 29 | #Get the title. 30 | content.append(parentBookmark.Title) 31 | #Get the text style. 32 | textStyle = str(parentBookmark.DisplayStyle) 33 | content.append(textStyle) 34 | cldBk = PdfBookmarkCollection(parentBookmark) 35 | GetChildBookmark(cldBk, content) 36 | 37 | #Save to file. 38 | with open(outputFile, "w") as file: 39 | file.write("\n".join(content)) 40 | 41 | 42 | 43 | #Create a new Pdf document. 44 | doc = PdfDocument() 45 | #Load the file from disk. 46 | doc.LoadFromFile(inputFile) 47 | #Get bookmarks collection of the Pdf file. 48 | bookmarks = doc.Bookmarks 49 | #Get Pdf Bookmarks. 50 | GetBookmarks(bookmarks, outputFile) 51 | doc.Close() 52 | 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /Examples/07_Bookmarks/GetPdfChildBookmarks.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_1.pdf" 5 | outputFile = "GetPdfChildBookmarks_out.txt" 6 | 7 | def GetChildBookmarks( bookmarks, result): 8 | #Declare a new StringBuilder content 9 | content = [] 10 | #Get Pdf child Bookmarks information. 11 | for i in range(bookmarks.Count): 12 | parentBookmark = bookmarks.get_Item(i) 13 | cldBk = PdfBookmarkCollection(parentBookmark) 14 | if cldBk.Count > 0: 15 | content.append("Child Bookmarks:") 16 | for j in range(cldBk.Count): 17 | childBookmark = cldBk.get_Item(j) 18 | #Get the title 19 | content.append(childBookmark.Title) 20 | #Get the color. 21 | color = str(childBookmark.Color) 22 | colorR = color.split(' ')[0][1:len(color.split(' ')[0])] 23 | content.append(colorR) 24 | #Get the text style. 25 | textStyle = str(childBookmark.DisplayStyle) 26 | content.append(textStyle) 27 | #Save to file. 28 | with open(result, "w") as file: 29 | file.write("\n".join(content)) 30 | 31 | #Create a new Pdf document. 32 | doc = PdfDocument() 33 | #Load the file from disk. 34 | doc.LoadFromFile(inputFile) 35 | #Get bookmarks collections of the PDF file. 36 | bookmarks = doc.Bookmarks 37 | #Get Pdf child Bookmarks. 38 | GetChildBookmarks(bookmarks, outputFile) 39 | doc.Close() 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /Examples/07_Bookmarks/SetInheritZoom.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_1.pdf" 5 | outputFile = "SetInheritZoom_out.pdf" 6 | 7 | #Create a new PDF document 8 | pdfdoc = PdfDocument() 9 | #load the document from disk 10 | pdfdoc.LoadFromFile(inputFile) 11 | #Get bookmarks collections of the PDF file 12 | bookmarks = pdfdoc.Bookmarks 13 | #Set Zoom level as 0, which the value is inherit zoom 14 | for i in range(bookmarks.Count): 15 | bookMark = bookmarks.get_Item(i) 16 | bookMark.Destination.Zoom = 0.5 17 | #Save the document 18 | pdfdoc.SaveToFile(outputFile) 19 | pdfdoc.Close() 20 | 21 | -------------------------------------------------------------------------------- /Examples/07_Bookmarks/UpdateBookmark.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/UpdateBookmark.pdf" 5 | outputFile = "UpdateBookmark.pdf" 6 | 7 | def EditChildBookmark(parentBookmark): 8 | pBookmark = PdfBookmarkCollection(parentBookmark) 9 | for i in range(pBookmark.Count): 10 | childBookmark = pBookmark.get_Item(i) 11 | childBookmark.Color = PdfRGBColor(Color.get_Blue()) 12 | childBookmark.DisplayStyle = PdfTextStyle.Regular 13 | EditChild2Bookmark(childBookmark) 14 | def EditChild2Bookmark(childBookmark): 15 | cldBookmark = PdfBookmarkCollection(childBookmark) 16 | for i in range(cldBookmark.Count): 17 | child2Bookmark = cldBookmark.get_Item(i) 18 | child2Bookmark.Color = PdfRGBColor(Color.get_LightSalmon()) 19 | child2Bookmark.DisplayStyle = PdfTextStyle.Italic 20 | 21 | 22 | #Load a pdf document 23 | doc = PdfDocument() 24 | doc.LoadFromFile(inputFile) 25 | #Get the first bookmark 26 | bookmark = doc.Bookmarks[0] 27 | #Change the title of the bookmark 28 | bookmark.Title = "Modified BookMark" 29 | #Set the color of the bookmark 30 | bookmark.Color = PdfRGBColor(Color.get_Black()) 31 | #Set the outline text style of the bookmark 32 | bookmark.DisplayStyle = PdfTextStyle.Bold 33 | #Edit child bookmarks of the parent bookmark 34 | EditChildBookmark(bookmark) 35 | #Save the pdf document 36 | doc.SaveToFile(outputFile) 37 | doc.Close() 38 | 39 | 40 | -------------------------------------------------------------------------------- /Examples/08_Forms/AddTextBoxField.py: -------------------------------------------------------------------------------- 1 | 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | inputFile = "./Demos/Data/pdfTemplate_N.pdf" 6 | outputFile = "AddTextBoxField_out.pdf" 7 | 8 | #Open pdf document 9 | pdf = PdfDocument() 10 | pdf.LoadFromFile(inputFile) 11 | #Get the first page 12 | page = pdf.Pages[0] 13 | #As for existing pdf, the property needs to be set as true 14 | pdf.AllowCreateForm = True 15 | #Create a new pdf font 16 | font = PdfFont(PdfFontFamily.Helvetica, 12.0, PdfFontStyle.Bold) 17 | #Create a pdf brush 18 | brush = PdfBrushes.get_Black() 19 | x = 50.0 20 | y = 550.0 21 | tempX = 0 22 | text = "TexBox: " 23 | #Draw a text into page 24 | page.Canvas.DrawString(text, font, brush, x, y) 25 | #Add a textBox field 26 | tempX = font.MeasureString(text).Width + x + 15 27 | textbox = PdfTextBoxField(page, "TextBox") 28 | textbox.Bounds = RectangleF(tempX, y, 100.0, 15.0) 29 | textbox.BorderWidth = 0.75 30 | textbox.BorderStyle = PdfBorderStyle.Solid 31 | pdf.Form.Fields.Add(textbox) 32 | #Save the document 33 | pdf.SaveToFile(outputFile) 34 | pdf.Close() 35 | -------------------------------------------------------------------------------- /Examples/08_Forms/AddTooltipForFormField.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/AddTooltipForFormField.pdf" 5 | outputFile = "AddTooltipForFormField_out.pdf" 6 | 7 | # Open pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | # Get the first page 11 | page = doc.Pages[0] 12 | # As for existing pdf, the property needs to be set as true 13 | doc.AllowCreateForm = True 14 | # Create a new pdf font 15 | font = PdfFont(PdfFontFamily.Helvetica, 12.0, PdfFontStyle.Bold) 16 | # Create a pdf brush 17 | brush = PdfBrushes.get_Black() 18 | x = 50.0 19 | y = 590.0 20 | tempX = 0 21 | text = "E-mail: " 22 | # Draw a text into page 23 | page.Canvas.DrawString(text, font, brush, x, y) 24 | tempX = font.MeasureString(text).Width + x + 15 25 | # Create a pdf textbox field 26 | textbox = PdfTextBoxField(page, "TextBox") 27 | # Set the bounds of textbox field 28 | textbox.Bounds = RectangleF(tempX, y, 100.0, 15.0) 29 | # Set the border width of textbox field 30 | textbox.BorderWidth = 0.75 31 | # Set the border style of textbox field 32 | textbox.BorderStyle = PdfBorderStyle.Solid 33 | # Add the textbox field into pdf document 34 | doc.Form.Fields.Add(textbox) 35 | # Add a tooltip for the textbox field 36 | doc.Form.Fields.get_Item("TextBox").ToolTip = "Please insert a valid email address" 37 | # Save pdf document 38 | doc.SaveToFile(outputFile) 39 | doc.Close() 40 | -------------------------------------------------------------------------------- /Examples/08_Forms/AssignIconToButtonField.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputImage1 = "./Demos/Data/E-iceblueLogo.png" 5 | inputImage2 = "./Demos/Data/PdfImage.png" 6 | inputImage3 = "./Demos/Data/PDFJAVA.png" 7 | outputFile = "AssignIconToButtonField-result.pdf" 8 | 9 | # Create a PDF document 10 | doc = PdfDocument() 11 | page = doc.Pages.Add() 12 | # Create a Button 13 | btn = PdfButtonField(page, "button1") 14 | btn.Bounds = RectangleF(0.0, 50.0, 120.0, 100.0) 15 | btn.HighlightMode = PdfHighlightMode.Push 16 | btn.LayoutMode = PdfButtonLayoutMode.CaptionOverlayIcon 17 | # Set text and icon for Normal appearance 18 | btn.Text = "Normal Text" 19 | btn.Icon = PdfImage.FromFile(inputImage1) 20 | # Set text and icon for Click appearance (Can only be set when highlight mode is Push) 21 | btn.AlternateText = "Alternate Text" 22 | btn.AlternateIcon = PdfImage.FromFile(inputImage2) 23 | # Set text and icon for Rollover appearance (Can only be set when highlight mode is Push) 24 | btn.RolloverText = "Rollover Text" 25 | btn.RolloverIcon = PdfImage.FromFile(inputImage3) 26 | # Set icon layout 27 | btn.IconLayout.Spaces = [0.5, 0.5] 28 | btn.IconLayout.ScaleMode = PdfButtonIconScaleMode.Proportional 29 | btn.IconLayout.ScaleReason = PdfButtonIconScaleReason.Always 30 | btn.IconLayout.IsFitBounds = False 31 | # Add the button to the document 32 | doc.Form.Fields.Add(btn) 33 | # Save the document 34 | doc.SaveToFile(outputFile) 35 | doc.Close() 36 | -------------------------------------------------------------------------------- /Examples/08_Forms/ChangeFormFieldLocation.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/FormField.pdf" 5 | outputFile = "ChangeFormFieldLocation_out.pdf" 6 | 7 | #Open pdf document 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputFile) 10 | pdfform = pdf.Form 11 | formWidget = PdfFormWidget(pdfform) 12 | if formWidget.FieldsWidget.Count > 0: 13 | for i in range(formWidget.FieldsWidget.Count): 14 | field = formWidget.FieldsWidget.get_Item(i) 15 | if isinstance(field, PdfTextBoxFieldWidget): 16 | textbox = field if isinstance(field, PdfTextBoxFieldWidget) else None 17 | #Find the textbox named total 18 | if textbox.Name == "TextBox1": 19 | # Change field location 20 | textbox.Location = PointF(390.0, 525.0) 21 | #Save the document 22 | pdf.SaveToFile(outputFile) 23 | pdf.Close() 24 | 25 | -------------------------------------------------------------------------------- /Examples/08_Forms/DeleteFormField.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/DeleteFormField.pdf" 5 | outputFile = "DeleteFormField.pdf" 6 | 7 | doc = PdfDocument() 8 | doc.LoadFromFile(inputFile) 9 | #Get pdf forms 10 | pdfform = doc.Form 11 | formWidget = PdfFormWidget(pdfform) 12 | #Find the particular form field and delete it 13 | if formWidget.FieldsWidget.Count > 0: 14 | i = 0 15 | while i < formWidget.FieldsWidget.Count: 16 | field = formWidget.FieldsWidget.get_Item(i) 17 | 18 | if isinstance(field, PdfTextBoxFieldWidget): 19 | textbox = field if isinstance(field, PdfTextBoxFieldWidget) else None 20 | if textbox.Name == "password2": 21 | formWidget.FieldsWidget.Remove(textbox) 22 | i = i + 1 23 | #Save the pdf document 24 | doc.SaveToFile(outputFile) 25 | doc.Close() 26 | 27 | -------------------------------------------------------------------------------- /Examples/08_Forms/DetermineRequiredField.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/DetermineRequiredField.pdf" 5 | outputFile = "DetermineRequiredField.pdf" 6 | 7 | doc = PdfDocument() 8 | doc.LoadFromFile(inputFile) 9 | #Get pdf forms 10 | pdfform = doc.Form 11 | formWidget = PdfFormWidget(pdfform) 12 | #Find the particular form field and determine if it marks as required or not 13 | if formWidget.FieldsWidget.Count > 0: 14 | for i in range(formWidget.FieldsWidget.Count): 15 | field = formWidget.FieldsWidget.get_Item(i) 16 | 17 | if isinstance(field, PdfTextBoxFieldWidget): 18 | textbox = field if isinstance(field, PdfTextBoxFieldWidget) else None 19 | if textbox.Name == "username": 20 | textbox.Required = True 21 | if textbox.Name == "password2": 22 | textbox.Required = False 23 | #Save pdf document 24 | doc.SaveToFile(outputFile) 25 | doc.Close() 26 | -------------------------------------------------------------------------------- /Examples/08_Forms/ExtractJavaScript.py: -------------------------------------------------------------------------------- 1 | from io import FileIO 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | inputFile = "./Demos/Data/ExtractJavaScript.pdf" 6 | outputFile = "ExtractJavaScript.txt" 7 | 8 | #Create a pdf document 9 | pdf = PdfDocument() 10 | #Load a pdf document 11 | pdf.LoadFromFile(inputFile) 12 | js = None 13 | #Get pdf forms 14 | pdfform = pdf.Form 15 | formWidget = PdfFormWidget(pdfform) 16 | #Find the FieldsWidget 17 | if formWidget.FieldsWidget.Count > 0: 18 | for i in range(formWidget.FieldsWidget.Count): 19 | field = formWidget.FieldsWidget.get_Item(i) 20 | if isinstance(field, PdfTextBoxFieldWidget): 21 | textbox = field if isinstance(field, PdfTextBoxFieldWidget) else None 22 | #Find the textbox named total 23 | if textbox.Name == "total": 24 | #Get the action 25 | jsa = textbox.Actions.Calculate 26 | if jsa is not None: 27 | #Get JavaScript 28 | js = jsa.Script 29 | #Save the result file 30 | f2=open(outputFile,'w', encoding='UTF-8') 31 | f2.write(js) 32 | f2.close() 33 | pdf.Close() 34 | 35 | -------------------------------------------------------------------------------- /Examples/08_Forms/FillImageInButtonField.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/ButtonField.pdf" 5 | outputFile = "FillImageInButtonField.pdf" 6 | inputImage = "./Demos/Data/E-logo.png" 7 | 8 | #Load old PDF from disk. 9 | pdf = PdfDocument() 10 | pdf.LoadFromFile(inputFile) 11 | #Get pdf forms 12 | pdfform = pdf.Form 13 | formWidget = PdfFormWidget(pdfform) 14 | #Traverse all the forms 15 | if formWidget.FieldsWidget.Count > 0: 16 | for i in range(formWidget.FieldsWidget.Count): 17 | field = formWidget.FieldsWidget.get_Item(i) 18 | if isinstance(field, PdfButtonWidgetFieldWidget): 19 | field = field if isinstance(field, PdfButtonWidgetFieldWidget) else None 20 | if field.Name == "Button1": 21 | #Set "true" to fit bounds 22 | field.IconLayout.IsFitBounds = True 23 | #Fill the annotation rectangle exactly without its original aspect ratio 24 | field.IconLayout.ScaleMode = PdfButtonIconScaleMode.Anamorphic 25 | #Fill an image 26 | field.SetButtonImage(PdfImage.FromFile(inputImage)) 27 | #Save to a file 28 | pdf.SaveToFile(outputFile, FileFormat.PDF) 29 | pdf.Close() 30 | 31 | -------------------------------------------------------------------------------- /Examples/08_Forms/FlattenFormField.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/FlattenFormField.pdf" 5 | outputFile = "FlattenFormField_out.pdf" 6 | 7 | #Open pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Flatten form fields 11 | doc.Form.IsFlatten = True 12 | #Save pdf document 13 | doc.SaveToFile(outputFile) 14 | doc.Close() 15 | 16 | -------------------------------------------------------------------------------- /Examples/08_Forms/GetCoordinates.py: -------------------------------------------------------------------------------- 1 | from io import FileIO 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | 6 | inputFile = "./Demos/Data/TextBoxSampleB.pdf" 7 | outputFile = "GetCoordinates.txt" 8 | 9 | #Create a pdf document 10 | doc = PdfDocument() 11 | #Load from file 12 | doc.LoadFromFile(inputFile) 13 | #Get pdf forms 14 | pdfform = doc.Form 15 | formWidget = PdfFormWidget(pdfform) 16 | #Get textbox 17 | field = formWidget.FieldsWidget.get_Item("Text1") 18 | textbox = PdfTextBoxFieldWidget(field.Ptr) 19 | #Get the location of the textbox 20 | location = textbox.Location 21 | content = [] 22 | content.append("The location of the field named " + textbox.Name + " is\n X:" + str(location.X) + " Y:" + str(location.Y)) 23 | #Save the result file 24 | f2=open(outputFile,'w', encoding='UTF-8') 25 | for item in content: 26 | f2.write("%s\n" % item) 27 | f2.close() 28 | doc.Close() 29 | -------------------------------------------------------------------------------- /Examples/08_Forms/GetFieldValue.py: -------------------------------------------------------------------------------- 1 | from io import FileIO 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | inputFile = "./Demos/Data/TextBoxSampleB_1.pdf" 6 | outputFile = "GetFieldValue.txt" 7 | 8 | #Create a pdf document 9 | doc = PdfDocument() 10 | #Load from file 11 | doc.LoadFromFile(inputFile) 12 | #Get pdf forms 13 | pdfform = doc.Form 14 | formWidget = PdfFormWidget(pdfform) 15 | #Get textbox 16 | field = formWidget.FieldsWidget.get_Item("Text1") 17 | textbox = PdfTextBoxFieldWidget(field.Ptr) 18 | #Get the text of the textbox 19 | text = textbox.Text 20 | content = [] 21 | content.append("The value of the field named " + textbox.Name + " is " + text) 22 | #Save the result file 23 | f2=open(outputFile,'w', encoding='UTF-8') 24 | for item in content: 25 | f2.write("%s\n" % item) 26 | f2.close() 27 | doc.Close() 28 | 29 | -------------------------------------------------------------------------------- /Examples/08_Forms/GetStyleOfRadioButton.py: -------------------------------------------------------------------------------- 1 | from io import FileIO 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | inputFile = "./Demos/Data/GetStyleOfRadioButton.pdf" 6 | outputFile = "GetStyleOfRadioButton_out.txt" 7 | 8 | #Open pdf document 9 | pdf = PdfDocument() 10 | pdf.LoadFromFile(inputFile) 11 | #Get the first page 12 | page = pdf.Pages[0] 13 | builder = [] 14 | num = 0 15 | #Get pdf forms 16 | pdfform = pdf.Form 17 | formWidget = PdfFormWidget(pdfform) 18 | #Traverse all the forms 19 | if formWidget.FieldsWidget.Count > 0: 20 | for i in range(formWidget.FieldsWidget.Count): 21 | field = formWidget.FieldsWidget.get_Item(i) 22 | #Find the radio button field 23 | if isinstance(field, PdfRadioButtonListFieldWidget): 24 | num += 1 25 | radio = field if isinstance(field, PdfRadioButtonListFieldWidget) else None 26 | #Get the button style 27 | buttonStyle = radio.ButtonStyle 28 | builder.append(("The button style of Radio button {0:d} is: "+str(buttonStyle)).format(num)) 29 | #Save the document 30 | f2=open(outputFile,'w', encoding='UTF-8') 31 | for item in builder: 32 | f2.write("%s\n" % item) 33 | f2.close() 34 | pdf.Close() 35 | 36 | 37 | -------------------------------------------------------------------------------- /Examples/08_Forms/GetValuesFromAllFields.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/AllFields.pdf" 5 | outputFile = "GetAllValues.txt" 6 | 7 | #Load a pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Get pdf forms 11 | pdfform = doc.Form 12 | formWidget = PdfFormWidget(pdfform) 13 | sb = [] 14 | #Traverse all the forms 15 | if formWidget.FieldsWidget.Count > 0: 16 | for i in range(formWidget.FieldsWidget.Count): 17 | field = formWidget.FieldsWidget.get_Item(i) 18 | if isinstance(field, PdfTextBoxFieldWidget): 19 | textBoxField = field if isinstance(field, PdfTextBoxFieldWidget) else None 20 | #Get text of textbox 21 | text = textBoxField.Text 22 | sb.append("The text in textbox is " + text + "\r\n") 23 | if isinstance(field, PdfListBoxWidgetFieldWidget): 24 | listBoxField = field if isinstance(field, PdfListBoxWidgetFieldWidget) else None 25 | sb.append("Listbox items are \r\n") 26 | #Get values of listbox 27 | items = listBoxField.Values 28 | for i in range(items.Count): 29 | item = items.get_Item(i) 30 | sb.append(item.Value + "\r\n") 31 | #Get selected value 32 | selectedValue = listBoxField.SelectedValue 33 | sb.append("The selected value in the listbox is " + selectedValue + "\r\n") 34 | if isinstance(field, PdfComboBoxWidgetFieldWidget): 35 | comBoxField = field if isinstance(field, PdfComboBoxWidgetFieldWidget) else None 36 | sb.append("comBoxField items are \r\n") 37 | #Get values of comboBox 38 | items = comBoxField.Values 39 | for i in range(items.Count): 40 | item = items.get_Item(i) 41 | sb.append(item.Value + "\r\n") 42 | #Get selected value 43 | selectedValue = comBoxField.SelectedValue 44 | sb.append("The selected value in the comBoxField is " + selectedValue + "\r\n") 45 | if isinstance(field, PdfRadioButtonListFieldWidget): 46 | radioBtnField = field if isinstance(field, PdfRadioButtonListFieldWidget) else None 47 | #Get value of radio button 48 | value = radioBtnField.Value 49 | sb.append("The text in radioButtonField is " + value + "\r\n") 50 | if isinstance(field, PdfCheckBoxWidgetFieldWidget): 51 | checkBoxField = field if isinstance(field, PdfCheckBoxWidgetFieldWidget) else None 52 | #Get the checked state of the checkbox 53 | state = checkBoxField.Checked 54 | stateValue = "True" if state else "False" 55 | sb.append("If the checkBox is checked: " + stateValue + "\r\n") 56 | #Save the result file 57 | f2=open(outputFile,'w', encoding='UTF-8') 58 | for item in sb: 59 | f2.write(item) 60 | f2.close() 61 | doc.Close() 62 | 63 | 64 | -------------------------------------------------------------------------------- /Examples/08_Forms/ModifyFormFieldValue.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile = "./Demos/Data/FormField.pdf" 6 | outputFile = "ModifyFormFieldValue_out.pdf" 7 | 8 | #Open pdf document 9 | pdf = PdfDocument() 10 | pdf.LoadFromFile(inputFile) 11 | pdfform = pdf.Form 12 | formWidget = PdfFormWidget(pdfform) 13 | if formWidget.FieldsWidget.Count > 0: 14 | for i in range(formWidget.FieldsWidget.Count): 15 | field = formWidget.FieldsWidget.get_Item(i) 16 | if isinstance(field, PdfTextBoxFieldWidget): 17 | textbox = field if isinstance(field, PdfTextBoxFieldWidget) else None 18 | #Find the textbox named total 19 | if textbox.Name == "TextBox1": 20 | textbox.Text = "New value" 21 | #Save the document 22 | pdf.SaveToFile(outputFile) 23 | pdf.Close() 24 | 25 | -------------------------------------------------------------------------------- /Examples/08_Forms/RecognizeRequiredField.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/RecognizeRequiredField.pdf" 5 | outputFile = "RecognizeRequiredField_out.txt" 6 | 7 | #Load a pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | builder = [] 11 | #Get pdf forms 12 | pdfform = doc.Form 13 | formWidget = PdfFormWidget(pdfform) 14 | if formWidget.FieldsWidget.Count > 0: 15 | for i in range(formWidget.FieldsWidget.Count): 16 | field = formWidget.FieldsWidget.get_Item(i) 17 | if isinstance(field, PdfField): 18 | field = field if isinstance(field, PdfField) else None 19 | #Judge if the field is required 20 | if field.Required: 21 | builder.append("The field named: "+ field.Name + " is required") 22 | #Save the document 23 | f2=open(outputFile,'w', encoding='UTF-8') 24 | for item in builder: 25 | f2.write("%s\n" % item) 26 | f2.close() 27 | doc.Close() 28 | -------------------------------------------------------------------------------- /Examples/08_Forms/RemoveFormField.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/RemoveFormField.pdf" 5 | outputFile = "RemoveFormField_out.pdf" 6 | 7 | #Create a PdfDocument 8 | pdf = PdfDocument() 9 | #Load the input file from disk 10 | pdf.LoadFromFile(inputFile) 11 | #Get form from the document 12 | pdfform = pdf.Form 13 | formWidget = PdfFormWidget(pdfform) 14 | if formWidget is not None: 15 | i = 0 16 | while i <= formWidget.FieldsWidget.List.Count - 1: 17 | #Case 1: Remove the first form field 18 | if i == 0: 19 | field = formWidget.FieldsWidget.get_Item(i) 20 | refield = field if isinstance(field, PdfField) else None 21 | formWidget.FieldsWidget.Remove(refield) 22 | break 23 | i += 1 24 | #Save the pdf file 25 | pdf.SaveToFile(outputFile) 26 | pdf.Close() 27 | -------------------------------------------------------------------------------- /Examples/08_Forms/SelectRadioButtonItem.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/RadioButton.pdf" 5 | outputFile = "SelectRadioButtonItem_out.pdf" 6 | 7 | #Load a pdf document 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputFile) 10 | #Get pdf forms 11 | pdfform = pdf.Form 12 | formWidget = PdfFormWidget(pdfform) 13 | #Find the field 14 | if formWidget.FieldsWidget.Count > 0: 15 | for i in range(formWidget.FieldsWidget.Count): 16 | field = formWidget.FieldsWidget.get_Item(i) 17 | if isinstance(field, PdfRadioButtonListFieldWidget): 18 | radioButton = field if isinstance(field, PdfRadioButtonListFieldWidget) else None 19 | if radioButton.Name == "RadioButton": 20 | radioButton.SelectedIndex = 1 21 | #Save the document 22 | pdf.SaveToFile(outputFile) 23 | pdf.Close() 24 | 25 | -------------------------------------------------------------------------------- /Examples/08_Forms/SetExportValueForCheckbox.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile = "./Demos/Data/SetExportValueForCheckbox.pdf" 6 | outputFile = "SetExportValueForCheckbox_out.pdf" 7 | 8 | #Load a pdf document 9 | pdf = PdfDocument() 10 | pdf.LoadFromFile(inputFile) 11 | #Get pdf forms 12 | pdfform = pdf.Form 13 | formWidget = PdfFormWidget(pdfform) 14 | count = 1 15 | #Find the field 16 | if formWidget.FieldsWidget.Count > 0: 17 | for i in range(formWidget.FieldsWidget.Count): 18 | field = formWidget.FieldsWidget.get_Item(i) 19 | if isinstance(field, PdfCheckBoxWidgetFieldWidget): 20 | checkbox = field if isinstance(field, PdfCheckBoxWidgetFieldWidget) else None 21 | #Set export value for checkbox 22 | count+=1 23 | checkbox.SetExportValue("True" + str(count)) 24 | #Save the document 25 | pdf.SaveToFile(outputFile) 26 | pdf.Close() 27 | -------------------------------------------------------------------------------- /Examples/08_Forms/SetFontForFormField.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile = "./Demos/Data/TextBoxSampleB.pdf" 6 | outputFile = "SetFontForFormField.pdf" 7 | 8 | #Create a pdf document 9 | doc = PdfDocument() 10 | #Load from file 11 | doc.LoadFromFile(inputFile) 12 | #Get pdf forms 13 | pdfform = doc.Form 14 | formWidget = PdfFormWidget(pdfform) 15 | #Get textbox 16 | field = formWidget.FieldsWidget.get_Item("Text1") 17 | textbox = PdfTextBoxFieldWidget(field.Ptr) 18 | #Set the font for textbox 19 | textbox.Font = PdfTrueTypeFont("Tahoma", 12.0, PdfFontStyle.Regular, True) 20 | #Set text 21 | textbox.Text = "Hello World" 22 | #Save to file 23 | doc.SaveToFile(outputFile) 24 | doc.Close() 25 | 26 | -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/AddDateTimeStamp.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/pdfTemplate_N.pdf" 5 | outputFile = "AddDateTimeStamp.pdf" 6 | 7 | #Load a Pdf document from disk 8 | document = PdfDocument() 9 | document.LoadFromFile(inputFile) 10 | #Get the first page 11 | page = document.Pages[0] 12 | #Set the font and brush 13 | font = PdfTrueTypeFont("Arial", 12.0, PdfFontStyle.Regular, True) 14 | brush = PdfSolidBrush(PdfRGBColor(Color.get_Blue())) 15 | #Time text 16 | timeString = DateTime.get_Now().ToString("MM/dd/yy hh:mm:ss tt ") 17 | #Create a template and rectangle, draw the string 18 | template = PdfTemplate(140.0, 15.0) 19 | rect = RectangleF(PointF(page.ActualSize.Width - template.Width - 10, page.ActualSize.Height - template.Height - 10), template.Size) 20 | template.Graphics.DrawString(timeString, font, brush, PointF(0.0, 0.0)) 21 | #Create stamp annoation 22 | stamp = PdfRubberStampAnnotation(rect) 23 | apprearance = PdfAppearance(stamp) 24 | apprearance.Normal = template 25 | stamp.Appearance = apprearance 26 | page.AnnotationsWidget.Add(stamp) 27 | #Sabe the document 28 | document.SaveToFile(outputFile, FileFormat.PDF) 29 | document.Close() 30 | 31 | -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/AddImageStamp.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile = "./Demos/Data/AddImageStamp.pdf" 6 | outputFile = "AddImageStamp.pdf" 7 | 8 | #Load a pdf document 9 | document = PdfDocument() 10 | document.LoadFromFile(inputFile) 11 | #Get the first page 12 | page = document.Pages[0] 13 | #Create a rubber stamp annotation 14 | loStamp = PdfRubberStampAnnotation(RectangleF(PointF(0.0, 0.0), SizeF(60.0, 60.0))) 15 | #Create an instance of PdfAppearance 16 | loApprearance = PdfAppearance(loStamp) 17 | image = PdfImage.FromFile("./Demos/Data/image stamp.jpg") 18 | template = PdfTemplate(210.0, 210.0) 19 | #Draw a pdf image into pdf template 20 | template.Graphics.DrawImage(image, 60.0, 60.0) 21 | loApprearance.Normal = template 22 | loStamp.Appearance = loApprearance 23 | #Add the rubber stamp annotation into pdf 24 | page.AnnotationsWidget.Add(loStamp) 25 | #Save pdf document 26 | document.SaveToFile(outputFile) 27 | document.Close() 28 | 29 | -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/AddTextStamp.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/AddTextStamp.pdf" 5 | outputFile = "AddTextStamp_out.pdf" 6 | 7 | #Open a pdf document 8 | document = PdfDocument() 9 | document.LoadFromFile(inputFile) 10 | #Get the first page 11 | page = document.Pages[0] 12 | #Create a pdf template 13 | template = PdfTemplate(125.0, 55.0) 14 | font1 = PdfTrueTypeFont("Elephant", 10.0, PdfFontStyle.Italic, True) 15 | brush = PdfSolidBrush(PdfRGBColor(Color.get_DarkRed())) 16 | pen = PdfPen(brush) 17 | rectangle = RectangleF(PointF(5.0, 5.0), template.Size) 18 | CornerRadius = 20.0 19 | path = PdfPath() 20 | path.AddArc(template.GetBounds().X, template.GetBounds().Y, CornerRadius, CornerRadius, 180.0, 90.0) 21 | path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y, CornerRadius, CornerRadius, 270.0, 90.0) 22 | path.AddArc(template.GetBounds().X + template.Width - CornerRadius, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 0.0, 90.0) 23 | path.AddArc(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, CornerRadius, CornerRadius, 90.0, 90.0) 24 | path.AddLine(template.GetBounds().X, template.GetBounds().Y + template.Height - CornerRadius, template.GetBounds().X, template.GetBounds().Y + CornerRadius / float(2)) 25 | template.Graphics.DrawPath(pen, path) 26 | #Draw stamp text 27 | s1 = "REVISED\n" 28 | s2 = "by E-iceblue at " + DateTime.get_Now().ToString("MM dd, yyyy") 29 | template.Graphics.DrawString(s1, font1, brush, PointF(5.0, 10.0)) 30 | font2 = PdfTrueTypeFont("Lucida Sans Unicode", 9.0, PdfFontStyle.Bold, True) 31 | template.Graphics.DrawString(s2, font2, brush, PointF(2.0, 30.0)) 32 | #Create a rubber stamp 33 | stamp = PdfRubberStampAnnotation(rectangle) 34 | apprearance = PdfAppearance(stamp) 35 | apprearance.Normal = template 36 | stamp.Appearance = apprearance 37 | #Draw stamp into page 38 | page.AnnotationsWidget.Add(stamp) 39 | #Save pdf document 40 | document.SaveToFile(outputFile) 41 | document.Close() 42 | 43 | -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/AddTilingBackgroundImage.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/pdfTemplate_N.pdf" 5 | outputFile = "AddTilingBackgroundImage.pdf" 6 | 7 | #Load document from disk 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputFile) 10 | #Load an image 11 | image = PdfImage.FromFile("./Demos/Data/E-iceblueLogo.png") 12 | for i in range(pdf.Pages.Count): 13 | page = pdf.Pages.get_Item(i) 14 | #Create PdfTilingBrush 15 | brush = PdfTilingBrush(SizeF(page.Canvas.Size.Width / float(3), page.Canvas.Size.Height / float(5))) 16 | #Set the transparency 17 | brush.Graphics.SetTransparency(0.3) 18 | #Draw image on brush graphics 19 | brush.Graphics.DrawImage(image, PointF((brush.Size.Width - image.Width) / float(2), (brush.Size.Height - image.Height) / float(2))) 20 | #use the brush to draw rectangle 21 | page.Canvas.DrawRectangle(brush, RectangleF(PointF(0.0, 0.0), page.Canvas.Size)) 22 | #Save the Pdf document 23 | pdf.SaveToFile(outputFile,FileFormat.PDF) 24 | pdf.Close() 25 | 26 | -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/FillStrokeText.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Sample.pdf" 5 | outputFile = "FillStrokeText.pdf" 6 | 7 | #Create a pdf document and load file from disk 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Get the first page 11 | page = doc.Pages[0] 12 | #Define Pdf pen 13 | pen = PdfPen(PdfRGBColor(Color.get_Gray())) 14 | #Save graphics state 15 | state = page.Canvas.Save() 16 | #Rotate page canvas 17 | page.Canvas.RotateTransform(-20.0) 18 | format = PdfStringFormat() 19 | format.CharacterSpacing = 5.0 20 | #Draw the string on page 21 | page.Canvas.DrawString("E-ICEBLUE", PdfFont(PdfFontFamily.Helvetica, 45.0), pen, 0.0, 500.0,format) 22 | #Restore graphics 23 | page.Canvas.Restore(state) 24 | #Save the Pdf file 25 | doc.SaveToFile(outputFile) 26 | doc.Close() -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/HeaderAndFooter/AddDifferentHeaders.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/MultipagePDF.pdf" 5 | outputFile = "AddingDifferentHeaders_result.pdf" 6 | 7 | #Load the Pdf from disk 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | header1 = "Header 1" 11 | header2 = "Header 2" 12 | #Define style 13 | font = PdfTrueTypeFont("Arial", 15.0, PdfFontStyle.Bold, True) 14 | brush = PdfBrushes.get_Red() 15 | rect = RectangleF(PointF(0.0,20.0),SizeF(doc.PageSettings.Size.Width,50.0)) 16 | format = PdfStringFormat() 17 | format.Alignment= PdfTextAlignment.Center 18 | doc.Pages[0].Canvas.DrawString(header1,font,brush,rect,format) 19 | font = PdfTrueTypeFont("Aleo", 15.0, PdfFontStyle.Regular,True) 20 | brush = PdfBrushes.get_Black() 21 | format.Alignment = PdfTextAlignment.Left 22 | doc.Pages[1].Canvas.DrawString(header2, font, brush, rect, format) 23 | #Save the document 24 | doc.SaveToFile(outputFile, FileFormat.PDF) 25 | doc.Close() 26 | 27 | 28 | -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/HeaderAndFooter/HeaderAndFooter.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/HeaderAndFooter.pdf" 5 | outputFile = "HeaderAndFooter_out.pdf" 6 | 7 | #Open the document from disk 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | brush = PdfBrushes.get_Black() 11 | pen = PdfPen(brush, 0.75) 12 | font = PdfTrueTypeFont("Arial", 10.0, PdfFontStyle.Italic, True) 13 | rightAlign = PdfStringFormat(PdfTextAlignment.Right) 14 | leftAlign = PdfStringFormat(PdfTextAlignment.Left) 15 | rightAlign.MeasureTrailingSpaces = True 16 | rightAlign.MeasureTrailingSpaces = True 17 | margin = doc.PageSettings.Margins 18 | space = font.Height * 0.75 19 | x = 0.0 20 | y = 0.0 21 | width = 0.0 22 | #Create a new pdf document 23 | newPdf = PdfDocument() 24 | newPage = None 25 | for i in range(doc.Pages.Count): 26 | page = doc.Pages.get_Item(i) 27 | #Add new page 28 | newPage = newPdf.Pages.Add(page.Size,PdfMargins(0.0)) 29 | newPage.Canvas.SetTransparency(0.5) 30 | x = margin.Left 31 | width = page.Canvas.ClientSize.Width - margin.Left - margin.Right 32 | y = margin.Top - space 33 | #Draw header line 34 | newPage.Canvas.DrawLine(pen, x, y + 15, x + width, y + 15) 35 | y = y+10 - font.Height 36 | #Draw header image into newPage 37 | newPage.Canvas.SetTransparency(0.5) 38 | headerImage = PdfImage.FromFile("./Demos/Data/Header.png" ) 39 | newPage.Canvas.DrawImage(headerImage, PointF(0.0, 0.0)) 40 | #Draw header text into newPage 41 | newPage.Canvas.DrawString("Demo of Spire.Pdf", font, brush, x + width, y, rightAlign) 42 | #Draw footer image into newPage 43 | footerImage = PdfImage.FromFile("./Demos/Data/Footer.png") 44 | newPage.Canvas.DrawImage(footerImage, PointF(0.0, newPage.Canvas.ClientSize.Height - footerImage.PhysicalDimension.Height)) 45 | brush = PdfBrushes.get_DarkBlue() 46 | font = PdfTrueTypeFont("Arial", 12.0, PdfFontStyle.Bold, True) 47 | y = newPage.Canvas.ClientSize.Height - margin.Bottom - font.Height 48 | #Draw footer text into newPage 49 | newPage.Canvas.DrawString("Created by E-iceblue Co,.Ltd", font, brush, x, y, leftAlign) 50 | newPage.Canvas.SetTransparency(1.0) 51 | #Draw the page into newPage 52 | newTemplate = page.CreateTemplate() 53 | template = PdfGraphicsWidget(newTemplate.Ptr) 54 | template.Draw(newPage.Canvas, PointF(0.0, 0.0)) 55 | #Save the document 56 | newPdf.SaveToFile(outputFile) 57 | newPdf.Close() 58 | 59 | -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/HeaderAndFooter/ImageAndTextUsingTemplate.py: -------------------------------------------------------------------------------- 1 | 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | inputFile = "./Demos/Data/PDFTemplate_HF.pdf" 6 | outputFile = "ImageAndTextUsingTemplate.pdf" 7 | 8 | #Load Pdf document from disk 9 | doc = PdfDocument() 10 | doc.LoadFromFile(inputFile) 11 | #Get the first page 12 | page = doc.Pages[0] 13 | #Get the margins of Pdf 14 | margin = doc.PageSettings.Margins 15 | #Define font and brush 16 | font = PdfTrueTypeFont("Impact",14.0,PdfFontStyle.Regular,True) 17 | brush = PdfSolidBrush(PdfRGBColor(Color.get_Gray())) 18 | #Load an image 19 | image = PdfImage.FromFile("./Demos/Data/E-iceblueLogo.png") 20 | #Specify the image size 21 | imageSize = SizeF(image.Width / float(2),image.Height / float(2)) 22 | #Create a header template 23 | headerTemplate = PdfTemplate(page.ActualSize.Width - margin.Left - margin.Right, imageSize.Height) 24 | #Draw the image in the template 25 | headerTemplate.Graphics.DrawImage(image, PointF(0.0, 0.0),imageSize) 26 | #Create a retangle 27 | rect = headerTemplate.GetBounds() 28 | #string format 29 | format1 = PdfStringFormat(PdfTextAlignment.Right,PdfVerticalAlignment.Middle) 30 | #Draw a string in the template 31 | headerTemplate.Graphics.DrawString("Header", font, brush, rect, format1) 32 | #Create a footer template and draw a text 33 | footerTemplate = PdfTemplate(page.ActualSize.Width - margin.Left - margin.Right, imageSize.Height) 34 | format2 = PdfStringFormat(PdfTextAlignment.Center,PdfVerticalAlignment.Middle) 35 | footerTemplate.Graphics.DrawString("Footer", font, brush, rect, format2) 36 | x = margin.Left 37 | y = 0.0 38 | #Draw the header template on page at specified location 39 | page.Canvas.DrawTemplate(headerTemplate, PointF(x,y)) 40 | #Draw the footer template on page at specified location 41 | y = page.ActualSize.Height - footerTemplate.Height - 10 42 | page.Canvas.DrawTemplate(footerTemplate, PointF(x, y)) 43 | #Save the document 44 | doc.SaveToFile(outputFile, FileFormat.PDF) 45 | doc.Close() 46 | -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/HeaderAndFooter/InlineImageAndPageNumber.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile = "./Demos/Data/PDFTemplate_HF.pdf" 6 | outputFile = "InlineImageAndPageNumber.pdf" 7 | 8 | #Load Pdf from disk 9 | doc = PdfDocument() 10 | doc.LoadFromFile(inputFile) 11 | #Get the first page 12 | page = doc.Pages[0] 13 | text1 = "Spire.Pdf is a robust component by" 14 | text2 = "Technology Co., Ltd." 15 | image = PdfImage.FromFile("./Demos/Data/Top-logo.png") 16 | #Define font and bursh 17 | font = PdfTrueTypeFont("Impact", 10.0, PdfFontStyle.Regular , True) 18 | bursh = PdfBrushes.get_DarkGray() 19 | #Get the size of text 20 | s1 = font.MeasureString(text1) 21 | s2 = font.MeasureString(text2) 22 | x = 10.0 23 | y = 10.0 24 | #Define image size 25 | imgSize = SizeF(image.Width / float(2), image.Height / float(2)) 26 | #Define rectangle and string format 27 | size = SizeF(s1.Width, imgSize.Width) 28 | rect1 = RectangleF(PointF(x, y), size) 29 | format = PdfStringFormat(PdfTextAlignment.Left,PdfVerticalAlignment.Middle) 30 | #Draw the text1 31 | page.Canvas.DrawString(text1, font, bursh, rect1, format) 32 | #Draw the image 33 | x += s1.Width 34 | page.Canvas.DrawImage(image, PointF(x, y), imgSize) 35 | #Draw the text2 36 | x += imgSize.Width 37 | size = SizeF(s2.Width, imgSize.Height) 38 | rect1 = RectangleF(PointF(x, y), size) 39 | page.Canvas.DrawString(text2, font, bursh, rect1, format) 40 | #Save the document 41 | doc.SaveToFile(outputFile) 42 | doc.Close() 43 | 44 | -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/HeaderAndFooter/PageNumberInFooter.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/MultipagePDF.pdf" 5 | outputFile = "PageNumberInFooter.pdf" 6 | 7 | def DrawPageNumber(doc, margin, startNumber, pageCount): 8 | for i in range(doc.Pages.Count): 9 | page = doc.Pages.get_Item(i) 10 | page.Canvas.SetTransparency(0.5) 11 | brush = PdfBrushes.get_Black() 12 | pen = PdfPen(brush, 0.75) 13 | font = PdfTrueTypeFont("Arial", 12.0, PdfFontStyle.Italic, True) 14 | format = PdfStringFormat(PdfTextAlignment.Right) 15 | format.MeasureTrailingSpaces = True 16 | space = font.Height * 0.75 17 | x = margin.Left 18 | width = page.Canvas.ClientSize.Width - margin.Left - margin.Right 19 | y = page.Canvas.ClientSize.Height - margin.Bottom + space 20 | page.Canvas.DrawLine(pen, x, y, x + width, y) 21 | y = y + 1 22 | numberLabel = "{0:d} of {1:d}".format(startNumber, pageCount) 23 | startNumber += 1 24 | page.Canvas.DrawString(numberLabel, font, brush, x + width, y, format) 25 | page.Canvas.SetTransparency(1.0) 26 | 27 | def DrawPageNumber(doc, margin, startNumber, pageCount): 28 | for i in range(doc.Pages.Count): 29 | page = doc.Pages.get_Item(i) 30 | page.Canvas.SetTransparency(0.5) 31 | brush = PdfBrushes.get_Black() 32 | pen = PdfPen(brush, 0.75) 33 | font = PdfTrueTypeFont("Arial", 12.0, PdfFontStyle.Italic, True) 34 | format = PdfStringFormat(PdfTextAlignment.Right) 35 | format.MeasureTrailingSpaces = True 36 | space = font.Height * 0.75 37 | x = margin.Left 38 | width = page.Canvas.ClientSize.Width - margin.Left - margin.Right 39 | y = page.Canvas.ClientSize.Height - margin.Bottom + space 40 | page.Canvas.DrawLine(pen, x, y, x + width, y) 41 | y = y + 1 42 | numberLabel = "{0:d} of {1:d}".format(startNumber, pageCount) 43 | startNumber += 1 44 | page.Canvas.DrawString(numberLabel, font, brush, x + width, y, format) 45 | page.Canvas.SetTransparency(1.0) 46 | 47 | 48 | #Create a pdf document 49 | doc = PdfDocument() 50 | doc.LoadFromFile(inputFile) 51 | #Set the margin 52 | margin = doc.PageSettings.Margins 53 | #Draw Page number 54 | DrawPageNumber(doc, margin, 1, doc.Pages.Count) 55 | #Save the document 56 | doc.SaveToFile(outputFile) 57 | doc.Close() 58 | 59 | 60 | -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/ImageWatermarkFirst.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/ImageWaterMark.pdf" 5 | inputFile_img = "./Demos/Data/Background.png" 6 | outputFile = "ImageWaterMark.pdf" 7 | 8 | #Create a pdf document and load file from disk 9 | doc = PdfDocument() 10 | doc.LoadFromFile(inputFile) 11 | #Get the first page 12 | page = doc.Pages[0] 13 | #Load image 14 | img = Stream(inputFile_img) 15 | #Set background image 16 | page.BackgroundImage = img 17 | #Save pdf file 18 | doc.SaveToFile(outputFile) 19 | doc.Close() -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/ImageWatermarkSecond.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/ImageWaterMark.pdf" 5 | inputFile_img = "./Demos/Data/E-logo.png" 6 | outputFile = "ImageWatermarkSecond.pdf" 7 | 8 | #Load Pdf document from disk 9 | doc = PdfDocument() 10 | doc.LoadFromFile(inputFile) 11 | #Insert an image into the first PDF page at specific position 12 | pdfImage = PdfImage.FromFile(inputFile_img) 13 | page = doc.Pages[0] 14 | position = PointF(160.0, 260.0) 15 | page.Canvas.Save() 16 | page.Canvas.SetTransparency(0.5, 0.5, PdfBlendMode.Multiply) 17 | page.Canvas.DrawImage(pdfImage, position) 18 | page.Canvas.Restore() 19 | #Save the Pdf document 20 | doc.SaveToFile(outputFile,FileFormat.PDF) 21 | doc.Close() -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/SetPropertiesForStamp.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/TextStamp.pdf" 5 | outputFile = "SetPropertiesForStamp.pdf" 6 | 7 | #Load old PDF from disk. 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputFile) 10 | #Get the first page 11 | page = pdf.Pages[0] 12 | #Traverse annotations widget 13 | annot = page.AnnotationsWidget 14 | if annot.Count > 0: 15 | for i in range(annot.Count): 16 | annotation = annot.get_Item(i) 17 | #If it is PdfRubberStampAnnotationWidget 18 | if isinstance(annotation, PdfRubberStampAnnotationWidget): 19 | stamp = annotation if isinstance(annotation, PdfRubberStampAnnotationWidget) else None 20 | stamp.Author = "TestUser" 21 | stamp.Subject = "E-iceblue" 22 | stamp.CreationDate = DateTime.get_Now() 23 | stamp.ModifiedDate = DateTime.get_Now() 24 | #Save to a pdf file 25 | pdf.SaveToFile(outputFile, FileFormat.PDF) 26 | pdf.Close() -------------------------------------------------------------------------------- /Examples/09_StampsAndWatermarks/TextWaterMark.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/ImageWaterMark.pdf" 5 | outputFile = "TextWaterMark.pdf" 6 | 7 | #Create a pdf document and load file from disk 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Get the first page 11 | page = doc.Pages[0] 12 | #Draw text watermark 13 | brush = PdfTilingBrush(SizeF(page.Canvas.ClientSize.Width / float(2), page.Canvas.ClientSize.Height / float(3))) 14 | brush.Graphics.SetTransparency(0.3) 15 | brush.Graphics.Save() 16 | brush.Graphics.TranslateTransform(brush.Size.Width / float(2), brush.Size.Height / float(2)) 17 | brush.Graphics.RotateTransform(-45.0) 18 | brush.Graphics.DrawString("Spire.Pdf Demo", PdfFont(PdfFontFamily.Helvetica, 24.0), PdfBrushes.get_Violet(), 0.0, 0.0, PdfStringFormat(PdfTextAlignment.Center)) 19 | brush.Graphics.Restore() 20 | brush.Graphics.SetTransparency(1.0) 21 | page.Canvas.DrawRectangle(brush, RectangleF(PointF(0.0, 0.0), page.Canvas.ClientSize)) 22 | #Save pdf file 23 | doc.SaveToFile(outputFile) 24 | doc.Close() -------------------------------------------------------------------------------- /Examples/10_LinksAndActions/AddPdfLaunchAction.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/text.txt" 5 | outputFile = "AddPdfLaunchAction.pdf" 6 | 7 | #Create a new PDF document 8 | doc = PdfDocument() 9 | page = doc.Pages.Add() 10 | #Create a PDF Launch Action 11 | launchAction = PdfLaunchAction(inputFile) 12 | #Create a PDF Action Annotation with the PDF Launch Action 13 | text = "Click here to open file" 14 | font = PdfTrueTypeFont("Arial", 13.0,PdfFontStyle.Regular,True) 15 | rect = RectangleF(50.0, 50.0, 230.0, 15.0) 16 | page.Canvas.DrawString(text, font, PdfBrushes.get_ForestGreen(), rect) 17 | annotation = PdfActionAnnotation(rect, launchAction) 18 | #Add the PDF Action Annotation to page 19 | newPage = PdfNewPage(page.Ptr) 20 | newPage.Annotations.Add(annotation) 21 | #Save the document 22 | doc.SaveToFile(outputFile) 23 | doc.Close() -------------------------------------------------------------------------------- /Examples/10_LinksAndActions/AddTableOfContent.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/AddTableOfContent.pdf" 5 | outputFile = "AddTableOfContent_out.pdf" 6 | 7 | #open a pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #get the page count 11 | pageCount = doc.Pages.Count 12 | #insert a blank page into the pdf document 13 | tocPage = doc.Pages.Insert(0) 14 | #set title 15 | title = "Table Of Contents" 16 | titleFont = PdfTrueTypeFont("Arial", 20.0, PdfFontStyle.Bold, True) 17 | centerAlignment = PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle) 18 | location = PointF(tocPage.Canvas.ClientSize.Width / float(2), titleFont.MeasureString(title).Height) 19 | tocPage.Canvas.DrawString(title, titleFont, PdfBrushes.get_CornflowerBlue(), location, centerAlignment) 20 | #draw TOC text 21 | titlesFont = PdfTrueTypeFont("Arial", 14.0,PdfFontStyle.Regular,True) 22 | titles = [None for _ in range(pageCount)] 23 | i = 0 24 | while i < len(titles): 25 | titles[i] = "This is page{0:d}".format(i+1) 26 | i += 1 27 | y = titleFont.MeasureString(title).Height + 10 28 | x = 0.0 29 | newPage = PdfNewPage(tocPage.Ptr) 30 | for i in range(1, pageCount + 1): 31 | text = titles[i-1] 32 | titleSize = titlesFont.MeasureString(text) 33 | navigatedPage = doc.Pages[i] 34 | pageNumText = str((i+1)) 35 | pageNumTextSize = titlesFont.MeasureString(pageNumText) 36 | tocPage.Canvas.DrawString(text, titlesFont, PdfBrushes.get_CadetBlue(), 0.0, y) 37 | dotLocation = titleSize.Width + 2 + x 38 | pageNumlocation = tocPage.Canvas.ClientSize.Width - pageNumTextSize.Width 39 | #for j in arange(dotLocation, pageNumlocation): 40 | while dotLocation < pageNumlocation : 41 | if dotLocation >= pageNumlocation: 42 | break 43 | tocPage.Canvas.DrawString(".", titlesFont, PdfBrushes.get_Gray(), dotLocation, y) 44 | dotLocation += 3 45 | tocPage.Canvas.DrawString(pageNumText, titlesFont, PdfBrushes.get_CadetBlue(), pageNumlocation, y) 46 | #add TOC action 47 | location = PointF(0.0, y) 48 | titleBounds = RectangleF(location, SizeF(tocPage.Canvas.ClientSize.Width, titleSize.Height)) 49 | Dest = PdfDestination(navigatedPage, PointF(-doc.PageSettings.Margins.Top, -doc.PageSettings.Margins.Left)) 50 | gotoAction = PdfGoToAction(Dest) 51 | action = PdfActionAnnotation(titleBounds, gotoAction) 52 | action.Border = PdfAnnotationBorder(0.0) 53 | #newPage = PdfNewPage(tocPage.Ptr) 54 | newPage.Annotations.Add(action) 55 | y += titleSize.Height + 10 56 | #save pdf document 57 | doc.SaveToFile(outputFile) -------------------------------------------------------------------------------- /Examples/10_LinksAndActions/DocumentLinkAnnotation.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "DocumentLinkAnnotation_out.pdf" 5 | 6 | def AddDocumentLinkAnnotation(pdf, AddPage, DestinationPage, y): 7 | #Define a font 8 | font = PdfTrueTypeFont("Arial", 12.0, PdfFontStyle.Regular, True) 9 | #Set the string format 10 | format = PdfStringFormat(PdfTextAlignment.Left) 11 | #Text string 12 | prompt = "Local document Link: " 13 | #Draw text string on page that 14 | pdf.Pages[AddPage].Canvas.DrawString(prompt, font, PdfBrushes.get_DodgerBlue(), 0.0, y) 15 | #Use MeasureString to get the width of string 16 | x = font.MeasureString(prompt, format).Width 17 | #Create a PdfDestination with specific page 18 | dest = PdfDestination(pdf.Pages[DestinationPage]) 19 | #Set the location of destination 20 | dest.Location = PointF(0.0, y) 21 | #Set 50% zoom factor 22 | dest.Zoom = 0.5 23 | #Label string 24 | label = "Click here to link the second page." 25 | #Use MeasureString to get the SizeF of string 26 | size = font.MeasureString(label) 27 | #Create a rectangle 28 | bounds = RectangleF(x, y, size.Width, size.Height) 29 | #Draw label string 30 | pdf.Pages[AddPage].Canvas.DrawString(label, font, PdfBrushes.get_OrangeRed(), x, y) 31 | #Create PdfDocumentLinkAnnotation on the rectangle and link to the destination 32 | annotation = PdfDocumentLinkAnnotation(bounds, dest) 33 | #Set color for annotation 34 | annotation.Color = PdfRGBColor(Color.get_Blue()) 35 | #Add annotation to the page 36 | addPage = pdf.Pages[AddPage] 37 | newPage = PdfNewPage(addPage.Ptr) 38 | newPage.Annotations.Add(annotation) 39 | 40 | #Create a pdf document 41 | doc = PdfDocument() 42 | #Create PdfUnitConvertor to convert the unit 43 | unitCvtr = PdfUnitConvertor() 44 | #Setting for page margin 45 | margin = PdfMargins() 46 | margin.Top = unitCvtr.ConvertUnits(2.54, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) 47 | margin.Bottom = margin.Top 48 | margin.Left = unitCvtr.ConvertUnits(2.0, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) 49 | margin.Right = margin.Left 50 | #Add the first page 51 | page1 = doc.Pages.Add(PdfPageSize.A4(), margin) 52 | #Define a PdfBrush 53 | brush1 = PdfBrushes.get_Black() 54 | #Define a font 55 | font1 = PdfTrueTypeFont("Arial", 12.0, PdfFontStyle.Bold, True) 56 | #Set the string format 57 | format1 = PdfStringFormat(PdfTextAlignment.Left) 58 | #Set the position for drawing 59 | x = 0.0 60 | y = 50.0 61 | #Text string 62 | specification = "The sample demonstrates how to create a local document link in PDF document." 63 | #Draw text string on first page 64 | page1.Canvas.DrawString(specification, font1, brush1, x, y, format1) 65 | #Use MeasureString to get the height of string 66 | y = y + font1.MeasureString(specification, format1).Height +10 67 | #Add the second page 68 | page2 = doc.Pages.Add(PdfPageSize.A4(), margin) 69 | #String text 70 | PageContent = "This is the second page!" 71 | #Draw text string on second page 72 | page2.Canvas.DrawString(PageContent, font1, brush1, x, y, format1) 73 | #Add DocumentLinkAnnotation on the first page and link to the second page 74 | AddDocumentLinkAnnotation(doc, 0, 1, y) 75 | #Save the document 76 | doc.SaveToFile(outputFile) 77 | doc.Close() -------------------------------------------------------------------------------- /Examples/10_LinksAndActions/EmbedSoundFile.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile1 = "./Demos/Data/EmbedSoundFile.pdf" 5 | inputFile2 = "./Demos/Data/Music.wav" 6 | outputFile = "EmbedSoundFile_out.pdf" 7 | 8 | #Create a new PDF document 9 | doc = PdfDocument() 10 | doc.LoadFromFile(inputFile1) 11 | #Add a page 12 | page = doc.Pages[0] 13 | #Create a sound action 14 | soundAction = PdfSoundAction(inputFile2) 15 | soundAction.Sound.Bits = 15 16 | soundAction.Sound.Channels = PdfSoundChannels.Stereo 17 | soundAction.Sound.Encoding = PdfSoundEncoding.Signed 18 | soundAction.Volume = 0.8 19 | soundAction.Repeat = True 20 | # Set the sound action to be executed when the PDF document is opened 21 | doc.AfterOpenAction = soundAction 22 | #Save the document 23 | doc.SaveToFile(outputFile) 24 | doc.Close() -------------------------------------------------------------------------------- /Examples/10_LinksAndActions/ExtractAndUpdateLink.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/LinkAnnotation.pdf" 5 | outputFile = "LinkAnnotation.pdf" 6 | 7 | #Create a pdf document 8 | doc = PdfDocument() 9 | #Load file from disk 10 | doc.LoadFromFile(inputFile) 11 | #Get the first page 12 | page = doc.Pages[0] 13 | #Get the annotation collection 14 | widgetCollection = page.AnnotationsWidget 15 | #Verify whether widgetCollection is null or not 16 | if widgetCollection.Count > 0: 17 | for i in range(widgetCollection.Count): 18 | annotation = widgetCollection.get_Item(i) 19 | #Get the TextWebLink Annotation 20 | if isinstance(annotation, PdfTextWebLinkAnnotationWidget): 21 | #Get the link annotation 22 | link = annotation if isinstance(annotation, PdfTextWebLinkAnnotationWidget) else None 23 | #Change the url 24 | link.Url = "http://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html" 25 | #Save the document 26 | doc.SaveToFile(outputFile) 27 | doc.Close() -------------------------------------------------------------------------------- /Examples/10_LinksAndActions/FileLinkAnnotation.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Sample.pdf" 5 | outputFile = "FileLinkAnnotation.pdf" 6 | 7 | def AddFileLinkAnnotation(page, y): 8 | #Define a font 9 | font = PdfTrueTypeFont("Arial", 12.0,PdfFontStyle.Regular,True) 10 | #Set the string format 11 | format = PdfStringFormat(PdfTextAlignment.Left) 12 | #Text string 13 | prompt = "Launch a File: " 14 | #Draw text string on page canvas 15 | page.Canvas.DrawString(prompt, font, PdfBrushes.get_DodgerBlue(), 0.0, y) 16 | #Use MeasureString to get the width of string 17 | x = font.MeasureString(prompt, format).Width 18 | #String of file name 19 | label = "Sample.pdf" 20 | #Use MeasureString to get the SizeF of string 21 | size = font.MeasureString(label) 22 | #Create a rectangle 23 | bounds = RectangleF(x, y, size.Width, size.Height) 24 | #Draw label string 25 | page.Canvas.DrawString(label, font, PdfBrushes.get_OrangeRed(), x, y) 26 | #Create PdfFileLinkAnnotation on the rectangle and link file "Sample.pdf" 27 | annotation = PdfFileLinkAnnotation(bounds, inputFile) 28 | #Set color for annotation 29 | annotation.Color = PdfRGBColor(Color.get_Blue()) 30 | #Add annotation to the page 31 | newPage = PdfNewPage(page.Ptr) 32 | newPage.Annotations.Add(annotation) 33 | 34 | #Create a pdf document 35 | doc = PdfDocument() 36 | #Create PdfUnitConvertor to convert the unit 37 | unitCvtr = PdfUnitConvertor() 38 | #Setting for page margin 39 | margin = PdfMargins() 40 | margin.Top = unitCvtr.ConvertUnits(2.54, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) 41 | margin.Bottom = margin.Top 42 | margin.Left = unitCvtr.ConvertUnits(3, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) 43 | margin.Right = margin.Left 44 | #Add one page 45 | page = doc.Pages.Add(PdfPageSize.A4(), margin) 46 | #Define a PdfBrush 47 | brush1 = PdfBrushes.get_Black() 48 | #Define a font 49 | font1 = PdfTrueTypeFont("Arial", 13.0, PdfFontStyle.Bold, True) 50 | #Set the string format 51 | format1 = PdfStringFormat(PdfTextAlignment.Left) 52 | #Set the position for drawing 53 | x = 0.0 54 | y = 50.0 55 | #Text string 56 | specification = "The sample demonstrates how to create a file link in PDF document." 57 | #Draw text string on page canvas 58 | page.Canvas.DrawString(specification, font1, brush1, x, y, format1) 59 | #Use MeasureString to get the height of string 60 | y = y + font1.MeasureString(specification, format1).Height + 10 61 | #Add file link annotation 62 | AddFileLinkAnnotation(page, y) 63 | #Save the document 64 | doc.SaveToFile(outputFile) 65 | doc.Close() -------------------------------------------------------------------------------- /Examples/10_LinksAndActions/GetLinkAnnotation.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/LinkAnnotation.pdf" 5 | outputFile = "GetLinkAnnotation.txt" 6 | 7 | #Create a pdf document 8 | doc = PdfDocument() 9 | #Load file from disk 10 | doc.LoadFromFile(inputFile) 11 | #Get the first page 12 | page = doc.Pages[0] 13 | #Get the annotation collection 14 | annotations = page.AnnotationsWidget 15 | #Create StringBuilder to save 16 | content = [] 17 | #Verify whether widgetCollection is not null or not 18 | if annotations.Count > 0: 19 | #traverse the PdfAnnotationCollection 20 | for i in range(annotations.Count): 21 | annotation = annotations.get_Item(i) 22 | #if it is PdfTextWebLinkAnnotationWidget 23 | if isinstance(annotation, PdfTextWebLinkAnnotationWidget): 24 | #Get the Url 25 | WebLinkAnnotation = annotation if isinstance(annotation, PdfTextWebLinkAnnotationWidget) else None 26 | url = WebLinkAnnotation.Url 27 | #Add strings to StringBuilder 28 | content.append("The url of link annotation is "+ url+"\r\n") 29 | content.append("The text of link annotation is " + WebLinkAnnotation.Text) 30 | #Save them to a txt file 31 | f2=open(outputFile,'w', encoding='UTF-8') 32 | for item in content: 33 | f2.write(item) 34 | f2.close() 35 | doc.Close() 36 | 37 | -------------------------------------------------------------------------------- /Examples/10_LinksAndActions/GoToAction.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Sample.pdf" 5 | outputFile = "GoToAction_out.pdf" 6 | 7 | def EmbeddedGoToAction(pdf, page,newPage): 8 | #add a attachment 9 | attachment = PdfAttachment(inputFile) 10 | pdf.Attachments.Add(attachment) 11 | text = "Test embedded go-to action! Click this will open the attached PDF in a new window." 12 | font = PdfTrueTypeFont("Arial", 13.0,PdfFontStyle.Regular,True) 13 | width = 490.0 14 | height = font.Height * 2.2 15 | rect = RectangleF(0.0, 100.0, width, height) 16 | page.Canvas.DrawString(text, font, PdfBrushes.get_Black(), rect) 17 | #create a PdfDestination with specific page, location and 200% zoom factor 18 | dest = PdfDestination(1, PointF(0.0, 842.0), 2.0) 19 | #create GoToE action with dest 20 | action = PdfEmbeddedGoToAction(attachment.FileName, dest, True) 21 | annotation = PdfActionAnnotation(rect, action) 22 | #add the annotation 23 | newPage.Annotations.Add(annotation) 24 | 25 | def JumpToSpecificLocationAction(pdf, page,newPage): 26 | #add a new page 27 | pagetwo = pdf.Pages.Add() 28 | #draw text on the page 29 | pagetwo.Canvas.DrawString("This is Page Two.", PdfFont(PdfFontFamily.Helvetica, 20.0), PdfSolidBrush(PdfRGBColor(Color.get_Black())), 10.0, 10.0) 30 | #create PdfDestination instance and link to PdfGoToAction 31 | pageBottomDest = PdfDestination(pagetwo) 32 | pageBottomDest.Location = PointF(0.0, 5.0) 33 | pageBottomDest.Mode = PdfDestinationMode.Location 34 | pageBottomDest.Zoom = 1.0 35 | action = PdfGoToAction(pageBottomDest) 36 | buttonFont = PdfTrueTypeFont("Arial", 10.0, PdfFontStyle.Bold,True) 37 | buttonWidth = 70.0 38 | buttonHeight = buttonFont.Height * 1.5 39 | format2 = PdfStringFormat(PdfTextAlignment.Center, PdfVerticalAlignment.Middle) 40 | buttonBounds = RectangleF(0.0, 200.0, buttonWidth, buttonHeight) 41 | #create a rectangle and draw text 42 | page.Canvas.DrawRectangle(PdfBrushes.get_DarkGray(), buttonBounds) 43 | page.Canvas.DrawString("To Last Page", buttonFont, PdfBrushes.get_CadetBlue(), buttonBounds, format2) 44 | #add the annotation 45 | annotation = PdfActionAnnotation(buttonBounds, action) 46 | annotation.Border = PdfAnnotationBorder(0.75) 47 | annotation.Color = PdfRGBColor(Color.get_LightGray()) 48 | newPage.Annotations.Add(annotation) 49 | 50 | #create a pdf document 51 | pdf = PdfDocument() 52 | page = pdf.Pages.Add() 53 | newPage = (PdfNewPage)(page.Ptr) 54 | #add a GoToE in pdf 55 | EmbeddedGoToAction(pdf, page,newPage) 56 | #creat a action that could jump to specific location 57 | JumpToSpecificLocationAction(pdf, page,newPage) 58 | #save the fila 59 | pdf.SaveToFile(outputFile) 60 | pdf.Close() 61 | 62 | -------------------------------------------------------------------------------- /Examples/10_LinksAndActions/LaunchFileInNewWindow.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile1 = "./Demos/Data/DocumentsLinks.pdf" 5 | inputFile2 = "./Demos/Data/Sample.pdf" 6 | outputFile = "LaunchFileInNewWindow.pdf" 7 | 8 | #Load old PDF from disk. 9 | pdf = PdfDocument() 10 | pdf.LoadFromFile(inputFile1) 11 | #Define the variables 12 | finds = None 13 | test = ["Spire.PDF"] 14 | #Traverse the pages 15 | for i in range(pdf.Pages.Count): 16 | page = pdf.Pages.get_Item(i) 17 | i = 0 18 | while i < len(test): 19 | #Find the defined string 20 | finds = page.FindText(test[i], TextFindParameter.WholeWord).Finds 21 | #Traverse the finds 22 | for find in finds: 23 | launchAction = PdfLaunchAction(inputFile2, PdfFilePathType.Relative) 24 | #Set open document in a new window 25 | launchAction.IsNewWindow = True 26 | #Add annotation 27 | rect = RectangleF(find.Position.X, find.Position.Y, find.Size.Width, find.Size.Height) 28 | annotation = PdfActionAnnotation(rect, launchAction) 29 | pageWidget = PdfPageWidget(page) 30 | pageWidget.AnnotationsWidget.Add(annotation) 31 | i += 1 32 | #Save the file 33 | pdf.SaveToFile(outputFile) 34 | pdf.Close() -------------------------------------------------------------------------------- /Examples/10_LinksAndActions/RemoveOpenAction.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/OpenAction.pdf" 5 | outputFile = "RemoveOpenAction.pdf" 6 | 7 | #Create a pdf document 8 | document = PdfDocument() 9 | #Load an existing pdf from disk 10 | document.LoadFromFile(inputFile) 11 | #Remove action 12 | document.AfterOpenAction = None 13 | #Save the document 14 | document.SaveToFile(outputFile) 15 | document.Close() -------------------------------------------------------------------------------- /Examples/10_LinksAndActions/SpecifyPageToView.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Sample.pdf" 5 | outputFile = "SpecifyPageToView.pdf" 6 | 7 | #Create a pdf document 8 | doc = PdfDocument() 9 | #Load file from disk. 10 | doc.LoadFromFile(inputFile) 11 | #Create a PdfDestination with specific page, location and 50% zoom factor 12 | dest = PdfDestination(2, PointF(0.0, 100.0), 0.5) 13 | #Create GoToAction with dest 14 | action = PdfGoToAction(dest) 15 | #Set open action 16 | doc.AfterOpenAction = action 17 | #Save the document 18 | doc.SaveToFile(outputFile) 19 | doc.Close() -------------------------------------------------------------------------------- /Examples/11_Conversion/AddAttachmentsToPDF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile1 = "./Demos/Data/SampleB_2.pdf" 5 | inputFileImg = "./Demos/Data/E-logo.png" 6 | inputFile2 = "./Demos/Data/Sample.pdf" 7 | outputFile = "AddAttachmentsToPDF.pdf" 8 | 9 | # Open pdf document 10 | doc = PdfDocument() 11 | doc.LoadFromFile(inputFile1) 12 | # Load files and add in attachments 13 | data = Stream(inputFileImg) 14 | attach1 = PdfAttachment("attachment1.png", data) 15 | data2 = Stream(inputFile2) 16 | attach2 = PdfAttachment("attachment2.pdf", data2) 17 | doc.Attachments.Add(attach1) 18 | doc.Attachments.Add(attach2) 19 | doc.SaveToFile(outputFile,FileFormat.PDF) 20 | doc.Close() 21 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ConvertToGrayPdf.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Sample.pdf" 5 | outputFile = "ConvertToGrayPdf.pdf" 6 | 7 | #Create a PdfGrayConverter with an pdf file 8 | converter = PdfGrayConverter(inputFile) 9 | #Convert the file to gray pdf 10 | converter.ToGrayPdf(outputFile) -------------------------------------------------------------------------------- /Examples/11_Conversion/ConvertToOFD.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Sample.pdf" 5 | outputFile = "ConvertToOFD.ofd" 6 | 7 | #Open pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Convert pdf to ofd 11 | doc.SaveToFile(outputFile, FileFormat.OFD) 12 | doc.Close() 13 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ConvertToWordSettingProperties.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Sample.pdf" 5 | outputFile = "ConvertToWordSettingProperties-result.docx" 6 | 7 | #Create PdfToDocConverter 8 | converter = PdfToDocConverter(inputFile) 9 | #Set properties for the .docx document 10 | converter.DocxOptions.Title = "PDFTODOCX" 11 | converter.DocxOptions.Subject = "Set document properties." 12 | converter.DocxOptions.Tags = "Test Tags" 13 | converter.DocxOptions.Categories = "PDF" 14 | converter.DocxOptions.Commments = "This document is just for testing the properties" 15 | converter.DocxOptions.Authors = "E-iceblue Support Team" 16 | converter.DocxOptions.LastSavedBy = "E-iceblue Support Team" 17 | converter.DocxOptions.Revision = 8 18 | converter.DocxOptions.Version = "csharp V4.0" 19 | converter.DocxOptions.ProgramName = "Spire.Pdf for .NET" 20 | converter.DocxOptions.Company = "E-iceblue" 21 | converter.DocxOptions.Manager = "E-iceblue" 22 | #Convert to docx file 23 | converter.SaveToDocx(outputFile) -------------------------------------------------------------------------------- /Examples/11_Conversion/PDFAToPDF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/SamplePDFA.pdf" 5 | outputFile = "PDFAToPdf-result.pdf" 6 | 7 | #Open pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Create a new pdf and draw content on new file 11 | newDoc = PdfNewDocument() 12 | newDoc.CompressionLevel = PdfCompressionLevel.none 13 | for i in range(doc.Pages.Count): 14 | page = doc.Pages.get_Item(i) 15 | size = page.Size 16 | p = newDoc.Pages.Add(size, PdfMargins(0.0)) 17 | page.CreateTemplate().Draw(p, 0.0, 0.0) 18 | #save file 19 | fileStream = Stream(outputFile) 20 | newDoc.Save(fileStream) 21 | fileStream.Close() 22 | newDoc.Close(True) -------------------------------------------------------------------------------- /Examples/11_Conversion/PDFToHTML/EmbedImages.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Sample.pdf" 5 | outputFile = "ToHTMLWithEmbedImages_out.html" 6 | 7 | #Open pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Set the convertion option to embed image in html 11 | doc.ConvertOptions.SetPdfToHtmlOptions(True, True) 12 | #Convert to html file 13 | doc.SaveToFile(outputFile, FileFormat.HTML) 14 | doc.Close() -------------------------------------------------------------------------------- /Examples/11_Conversion/PDFToHTML/EmbedSVG.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Sample.pdf" 5 | outputFile = "ToHTMLWithEmbedingSVG_out.html" 6 | 7 | #Open pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Set the convertion option to embed image in html 11 | doc.ConvertOptions.SetPdfToHtmlOptions(True) 12 | #Convert to html file 13 | doc.SaveToFile(outputFile, FileFormat.HTML) 14 | doc.Close() -------------------------------------------------------------------------------- /Examples/11_Conversion/PDFToHTML/ToHTMLFilesSplittedByPages.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | import io 4 | 5 | inputFile = "./Demos/Data/Sample.pdf" 6 | outputFile = "ToHTMLFilesSplittedByPages.html" 7 | 8 | #Open pdf document 9 | doc = PdfDocument() 10 | doc.LoadFromFile(inputFile) 11 | #Set the convertion option to embed image in html 12 | doc.ConvertOptions.SetPdfToHtmlOptions(False, True, 1) 13 | #Convert to html file 14 | doc.SaveToFile(outputFile, FileFormat.HTML) 15 | doc.Close() -------------------------------------------------------------------------------- /Examples/11_Conversion/PDFToHTML/ToHTMLStream.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | import io 4 | 5 | inputFile = "./Demos/Data/Sample.pdf" 6 | outputFile = "oHTMLStream.html" 7 | 8 | #Open pdf document 9 | doc = PdfDocument() 10 | doc.LoadFromFile(inputFile) 11 | #Save to HTML stream 12 | fileStream = Stream(outputFile) 13 | doc.SaveToStream(fileStream, FileFormat.HTML) 14 | fileStream.Close() 15 | -------------------------------------------------------------------------------- /Examples/11_Conversion/PdfToExcel.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile = "./Demos/Data/PdfToExcel.pdf" 6 | outputFile = "PdfToExcel.xlsx" 7 | 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputFile) 10 | # Save the pdf file to excel 11 | pdf.SaveToFile(outputFile, FileFormat.XLSX) 12 | pdf.Close() 13 | -------------------------------------------------------------------------------- /Examples/11_Conversion/PdfToExcelOptions.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | outputFile = "PdfToExcelOptions.xlsx" 6 | inputFile = "./Demos/Data/PdfToXlsxOptions.pdf" 7 | 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputFile) 10 | pdf.ConvertOptions.SetPdfToXlsxOptions( 11 | XlsxLineLayoutOptions(False, True, True)) 12 | # Save the pdf file to excel 13 | pdf.SaveToFile(outputFile, FileFormat.XLSX) 14 | pdf.Close() 15 | -------------------------------------------------------------------------------- /Examples/11_Conversion/RemoveHyperlinks.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | outputFile = "RemoveHyperlinks.pdf" 6 | inputFile = "./Demos/Data/RemoveHyperlinks.pdf" 7 | 8 | # Load an existing PDF file 9 | document = PdfDocument() 10 | document.LoadFromFile(inputFile) 11 | # Get the first page 12 | page = document.Pages[0] 13 | # Get the annotation collection 14 | widgetCollection = page.AnnotationsWidget 15 | # Verify whether widgetCollection is null or not 16 | if widgetCollection.Count > 0: 17 | for i in range(widgetCollection.Count - 1, -1, -1): 18 | annotation = widgetCollection.get_Item(i) 19 | # Get the TextWebLink Annotation 20 | if isinstance(annotation, PdfTextWebLinkAnnotationWidget): 21 | link = annotation if isinstance( 22 | annotation, PdfTextWebLinkAnnotationWidget) else None 23 | # Remove the TextWebLink annotation 24 | widgetCollection.Remove(link) 25 | document.SaveToFile(outputFile) 26 | document.Close() 27 | -------------------------------------------------------------------------------- /Examples/11_Conversion/SVGToPDF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "SvgToPdf.pdf" 5 | inputFile = "./Demos/Data/template.svg" 6 | 7 | # Create a new PDF document. 8 | doc = PdfDocument() 9 | # Load the file from disk. 10 | doc.LoadFromSvg(inputFile) 11 | # Save the document 12 | doc.SaveToFile(outputFile) 13 | doc.Close() 14 | -------------------------------------------------------------------------------- /Examples/11_Conversion/TextToPDF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | def ReadFromTxt(fname: str) -> str: 5 | with open(fname, 'r') as f: 6 | text = f.read() 7 | return text 8 | 9 | outputFile = "TextToPdf.pdf" 10 | inputFile = "./Demos/Data/TextToPdf.txt" 11 | 12 | # Get text from .txt file 13 | text = ReadFromTxt(inputFile) 14 | # Create a pdf document 15 | doc = PdfDocument() 16 | section = doc.Sections.Add() 17 | page = section.Pages.Add() 18 | # Create a PdfFont 19 | font = PdfFont(PdfFontFamily.Helvetica, 11.0) 20 | # Set string format 21 | strformat = PdfStringFormat() 22 | strformat.LineSpacing = 20.0 23 | brush = PdfBrushes.get_Black() 24 | # Set text layout 25 | textLayout = PdfTextLayout() 26 | textLayout.Break = PdfLayoutBreakType.FitPage 27 | textLayout.Layout = PdfLayoutType.Paginate 28 | bounds = RectangleF(PointF(10.0, 20.0), page.Canvas.ClientSize) 29 | textWidget = PdfTextWidget(text, font, brush) 30 | textWidget.StringFormat = strformat 31 | textWidget.Draw(page, bounds, textLayout) 32 | # Save to file 33 | doc.SaveToFile(outputFile, FileFormat.PDF) 34 | doc.Close() 35 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ToDoc.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "ToDoc.doc" 5 | inputFile = "./Demos/Data/ToDoc.pdf" 6 | 7 | # Load a pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | # Convert to doc file. 11 | doc.SaveToFile(outputFile, FileFormat.DOC) 12 | doc.Close() 13 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ToDocx.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "ToDocx.docx" 5 | inputFile = "./Demos/Data/ToDocx.pdf" 6 | 7 | # Load a pdf document 8 | inputfile = inputFile 9 | doc = PdfDocument() 10 | doc.LoadFromFile(inputfile) 11 | # Convert to doc file. 12 | doc.SaveToFile(outputFile, FileFormat.DOCX) 13 | doc.Close() 14 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ToHTML.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "ToHTML.html" 5 | inputFile = "./Demos/Data/ToHTML.pdf" 6 | 7 | # Load a pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | # Convert to doc file. 11 | doc.SaveToFile(outputFile, FileFormat.HTML) 12 | doc.Close() 13 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ToImage.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "output" 5 | inputFile = "./Demos/Data/ToImage.pdf" 6 | 7 | # Open pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | # Save to images 11 | for i in range(doc.Pages.Count): 12 | fileName = outputFile + "\\ToImage-img-{0:d}.png".format(i) 13 | with doc.SaveAsImage(i) as imageS: 14 | imageS.Save(fileName) 15 | doc.Close() 16 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ToLinearizedPdf.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "ToLinearizedPdf.pdf" 5 | inputFile = "./Demos/Data/PDFTemplate_N.pdf" 6 | 7 | converter = PdfToLinearizedPdfConverter(inputFile) 8 | converter.ToLinearizedPdf(outputFile) 9 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ToPCL.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "ToPCL.pcl" 5 | inputFile = "./Demos/Data/ToPCL.pdf" 6 | 7 | # Load a pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | # Convert to PCL file. 11 | doc.SaveToFile(outputFile, FileFormat.PCL) 12 | doc.Close() 13 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ToPDFA.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "ToPDFA.pdf" 5 | inputFile = "./Demos/Data/ToPDFA.pdf" 6 | 7 | # Convert to PDFA file 8 | converter = PdfStandardsConverter(inputFile) 9 | converter.ToPdfA1B(outputFile) 10 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ToPdfA2B.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "ToPdfA2B.pdf" 5 | inputFile = "./Demos/Data/ToPdfA2B.pdf" 6 | 7 | # Convert to PDFA file 8 | converter = PdfStandardsConverter(inputFile) 9 | # also supports ToPdfA1B ToPdfA1A ToPdfA2A ToPdfA3A ToPdfA3B ToPdfX1A2001 10 | converter.ToPdfA2B(outputFile) 11 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ToPostScript.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "ToPostScript.ps" 5 | inputFile = "./Demos/Data/ToPostScript.pdf" 6 | 7 | # Load a pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | # Convert to PostScript file. 11 | doc.SaveToFile(outputFile, FileFormat.POSTSCRIPT) 12 | doc.Close() 13 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ToSVG.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "ToSVG.svg" 5 | inputFile = "./Demos/Data/ToSVG.pdf" 6 | 7 | # Load a pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | # Convert to SVG file. 11 | doc.SaveToFile(outputFile, FileFormat.SVG) 12 | doc.Close() 13 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ToTransparentBackgroundImages.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "ToTransparentBackgroundImages.png" 5 | inputFile = "./Demos/Data/ToTransparentBackgroundImages.pdf" 6 | 7 | # Open pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | # Save to images 11 | doc.ConvertOptions.SetPdfToImageOptions(0) 12 | with doc.SaveAsImage(0, PdfImageType.Bitmap) as imageS: 13 | imageS.Save(outputFile) 14 | doc.Close() 15 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ToXLSX.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "ToXLSX.xlsx" 5 | inputFile = "./Demos/Data/ChartSample.pdf" 6 | 7 | pdf = PdfDocument() 8 | pdf.LoadFromFile(inputFile) 9 | # Save the pdf file to excel 10 | pdf.SaveToFile(outputFile, FileFormat.XLSX) 11 | pdf.Close() 12 | -------------------------------------------------------------------------------- /Examples/11_Conversion/ToXPS.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "ToXPS.xps" 5 | inputFile = "./Demos/Data/ToXPS.pdf" 6 | 7 | pdf = PdfDocument() 8 | pdf.LoadFromFile(inputFile) 9 | # Save the pdf file to xps 10 | pdf.SaveToFile(outputFile, FileFormat.XPS) 11 | pdf.Close() -------------------------------------------------------------------------------- /Examples/12_Page/DeletePage.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "DeletePage.pdf" 5 | inputFile = "./Demos/Data/DeletePage.pdf" 6 | 7 | # Load the document from disk 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | # Delete the fifth page 11 | doc.Pages.RemoveAt(4) 12 | # Save the document 13 | doc.SaveToFile(outputFile) 14 | doc.Close() 15 | -------------------------------------------------------------------------------- /Examples/12_Page/Extraction.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | def AppendAllText(fname: str, text: List[str]): 6 | fp = open(fname, "w") 7 | for s in text: 8 | fp.write(s + "\n") 9 | fp.close() 10 | 11 | 12 | outputFile = "output" 13 | inputFile = "./Demos/Data/Extraction.pdf" 14 | 15 | # create a pdf document. 16 | doc = PdfDocument() 17 | doc.LoadFromFile(inputFile) 18 | sbuffer = [] 19 | images = [] 20 | for i in range(doc.Pages.Count): 21 | page = doc.Pages.get_Item(i) 22 | sbuffer.append(page.ExtractText()) 23 | for image in page.ExtractImages(): 24 | images.append(image) 25 | # save text 26 | fileName = outputFile + "\Extraction.txt" 27 | AppendAllText(fileName, sbuffer) 28 | # save image 29 | index = 0 30 | for image in images: 31 | imageFileName = outputFile + "\Image-{0:d}.png".format(index) 32 | index += 1 33 | image.Save(imageFileName, ImageFormat.get_Png()) 34 | doc.Close() 35 | -------------------------------------------------------------------------------- /Examples/12_Page/GetNumberOfPages.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | def AppendText(fname: str, text: str): 5 | fp = open(fname, "w") 6 | fp.write(text + "\n") 7 | fp.close() 8 | 9 | outputFile = "GetNumberOfPages.txt" 10 | inputFile = "./Demos/Data/DeletePage.pdf" 11 | 12 | # Load the document from disk 13 | doc = PdfDocument() 14 | doc.LoadFromFile(inputFile) 15 | count = doc.Pages.Count 16 | AppendText( 17 | outputFile, "The page count of the pdf document is " + str(count)) 18 | doc.Close() 19 | -------------------------------------------------------------------------------- /Examples/12_Page/GetPageInfo.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | def AppendAllText(fname: str, text: List[str]): 5 | fp = open(fname, "w") 6 | for s in text: 7 | fp.write(s + "\n") 8 | fp.close() 9 | 10 | outputFile = "GetPageInfo.txt" 11 | inputFile = "./Demos/Data/GetPageInfo.pdf" 12 | 13 | # Create a pdf document 14 | doc = PdfDocument() 15 | # Load an existing pdf from disk 16 | doc.LoadFromFile(inputFile) 17 | # Get the first page of the loaded PDF file 18 | page = doc.Pages[0] 19 | # Get the size of page MediaBox based on "point" 20 | MediaBoxWidth = page.MediaBox.Width 21 | MediaBoxHeight = page.MediaBox.Height 22 | MediaBoxX = page.MediaBox.X 23 | MediaBoxY = page.MediaBox.Y 24 | # Get the size of page BleedBox based on "point" 25 | BleedBoxWidth = page.BleedBox.Width 26 | BleedBoxHeight = page.BleedBox.Height 27 | BleedBoxX = page.BleedBox.X 28 | BleedBoxY = page.BleedBox.Y 29 | # Get the size of page CropBox based on "point" 30 | CropBoxWidth = page.CropBox.Width 31 | CropBoxHeight = page.CropBox.Height 32 | CropBoxX = page.CropBox.X 33 | CropBoxY = page.CropBox.Y 34 | # Get the size of page ArtBox based on "point" 35 | ArtBoxWidth = page.ArtBox.Width 36 | ArtBoxHeight = page.ArtBox.Height 37 | ArtBoxX = page.ArtBox.X 38 | ArtBoxY = page.ArtBox.Y 39 | # Get the size of page TrimBox based on "point" 40 | TrimBoxWidth = page.TrimBox.Width 41 | TrimBoxHeight = page.TrimBox.Height 42 | TrimBoxX = page.TrimBox.X 43 | TrimBoxY = page.TrimBox.Y 44 | # Get the actual size of page 45 | actualSizeW = page.ActualSize.Width 46 | actualSizeH = page.ActualSize.Height 47 | # Gets the rotation angle of the current page 48 | rotationAngle = page.Rotation 49 | rotation = str(rotationAngle) 50 | # Create StringBuilder to save 51 | content = [] 52 | # Add page information string to StringBuilder 53 | content.append("MediaBox width: " + str(MediaBoxWidth) + "pt, height: " + str(MediaBoxHeight) + 54 | "pt, RectangleF X: " + str(MediaBoxX) + "pt, RectangleF Y: " + str(MediaBoxY) + "pt.") 55 | content.append("BleedBox width: " + str(BleedBoxWidth) + "pt, height: " + str(BleedBoxHeight) + 56 | "pt, RectangleF X: " + str(BleedBoxX) + "pt, RectangleF Y: " + str(BleedBoxY) + "pt.") 57 | content.append("CropBox width: " + str(CropBoxWidth) + "pt, height: " + str(CropBoxHeight) + 58 | "pt, RectangleF X: " + str(CropBoxX) + "pt, RectangleF Y: " + str(CropBoxY) + "pt.") 59 | content.append("ArtBox width: " + str(ArtBoxWidth) + "pt, height: " + str(ArtBoxHeight) + 60 | "pt, RectangleF X: " + str(ArtBoxX) + "pt, RectangleF Y: " + str(ArtBoxY) + "pt.") 61 | content.append("TrimBox width: " + str(TrimBoxWidth) + "pt, height: " + str(TrimBoxHeight) + 62 | "pt, RectangleF X: " + str(TrimBoxX) + "pt, RectangleF Y: " + str(TrimBoxY) + "pt.") 63 | content.append( 64 | "The actual size of the current page width: " + str(actualSizeW)) 65 | content.append( 66 | "The actual size of the current page height: " + str(actualSizeH)) 67 | content.append("The rotation angle of the current page: " + rotation) 68 | # Save them to a txt file 69 | AppendAllText(outputFile, content) 70 | doc.Close() 71 | -------------------------------------------------------------------------------- /Examples/12_Page/GetPageLabel.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | def AppendAllText(fname: str, text: List[str]): 5 | fp = open(fname, "w") 6 | for s in text: 7 | fp.write(s + "\n") 8 | fp.close() 9 | 10 | outputFile = "GetPageLabel.txt" 11 | inputFile = "./Demos/Data/PageLabel.pdf" 12 | 13 | # Create a PdfDocument instance 14 | pdf = PdfDocument() 15 | # Load the PDF file 16 | pdf.LoadFromFile(inputFile) 17 | # Create a StringBuilder instance 18 | sb = [] 19 | # Get the labels of the pages in the PDF file 20 | for i in range(pdf.Pages.Count): 21 | page = pdf.Pages.get_Item(i) 22 | sb.append("The page label of page "+str(i+1) + 23 | " is \""+page.PageLabel+"\"") 24 | # Save them to a txt file 25 | AppendAllText(outputFile, sb) 26 | pdf.Close() -------------------------------------------------------------------------------- /Examples/12_Page/GetPageSize.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | def AppendAllText(fname: str, text: List[str]): 5 | fp = open(fname, "w") 6 | for s in text: 7 | fp.write(s + "\n") 8 | fp.close() 9 | 10 | outputFile = "GetPageSize.txt" 11 | inputFile = "./Demos/Data/Sample.pdf" 12 | 13 | # Create a pdf document 14 | doc = PdfDocument() 15 | # Load an existing pdf from disk 16 | doc.LoadFromFile(inputFile) 17 | # Get the first page of the loaded PDF file 18 | page = doc.Pages[0] 19 | # Get the width of page based on "point" 20 | pointWidth = page.Size.Width 21 | # Get the height of page 22 | pointHeight = page.Size.Height 23 | # Create PdfUnitConvertor to convert the unit 24 | unitCvtr = PdfUnitConvertor() 25 | # Convert the size with "pixel" 26 | pixelWidth = unitCvtr.ConvertUnits( 27 | pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel) 28 | pixelHeight = unitCvtr.ConvertUnits( 29 | pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Pixel) 30 | # Convert the size with "inch" 31 | inchWidth = unitCvtr.ConvertUnits( 32 | pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch) 33 | inchHeight = unitCvtr.ConvertUnits( 34 | pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Inch) 35 | # Convert the size with "centimeter" 36 | centimeterWidth = unitCvtr.ConvertUnits( 37 | pointWidth, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter) 38 | centimeterHeight = unitCvtr.ConvertUnits( 39 | pointHeight, PdfGraphicsUnit.Point, PdfGraphicsUnit.Centimeter) 40 | # Create StringBuilder to save 41 | content = [] 42 | # Add pointSize string to StringBuilder 43 | content.append("The page size of the file is (width: " + 44 | str(pointWidth) + "pt, height: " + str(pointHeight) + "pt).") 45 | content.append("The page size of the file is (width: " + 46 | str(pixelWidth) + "pixel, height: " + str(pixelHeight) + "pixel).") 47 | content.append("The page size of the file is (width: " + 48 | str(inchWidth) + "inch, height: " + str(inchHeight) + "inch).") 49 | content.append("The page size of the file is (width: " + 50 | str(centimeterWidth) + "cm, height: " + str(centimeterHeight) + "cm.)") 51 | # Save them to a txt file 52 | AppendAllText(outputFile, content) 53 | doc.Close() 54 | -------------------------------------------------------------------------------- /Examples/12_Page/InsertEmptyPage.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "InsertEmptyPage.pdf" 5 | inputFile = "./Demos/Data/Sample.pdf" 6 | 7 | # Create a pdf document 8 | doc = PdfDocument() 9 | # Load an existing pdf from disk 10 | doc.LoadFromFile(inputFile) 11 | # insert a blank page as the second page 12 | doc.Pages.Insert(1) 13 | # Save the document 14 | doc.SaveToFile(outputFile) 15 | doc.Close() 16 | -------------------------------------------------------------------------------- /Examples/12_Page/InsertEmptyPageAtEnd.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "InsertEmptyPageAtEnd.pdf" 5 | inputFile = "./Demos/Data/MultipagePDF.pdf" 6 | 7 | # Load Pdf document from disk 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | # Add an empty page at the end 11 | doc.Pages.Add(PdfPageSize.A4(), PdfMargins(0.0, 0.0)) 12 | # Save the Pdf document 13 | doc.SaveToFile(outputFile, FileFormat.PDF) 14 | doc.Close() 15 | -------------------------------------------------------------------------------- /Examples/12_Page/PageLable.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "PageLable_1.pdf" 5 | inputFile = "./Demos/Data/Sample.pdf" 6 | 7 | # Load the document from disk 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | doc.PageLabels = PdfPageLabels() 11 | doc.PageLabels.AddRange( 12 | 0, PdfPageLabels.Decimal_Arabic_Numerals_Style(), "label test") 13 | # Save the document 14 | doc.SaveToFile(outputFile) 15 | doc.Close() 16 | -------------------------------------------------------------------------------- /Examples/12_Page/ResetPageSize.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | outputFile = "ResetPageSize.pdf" 6 | inputFile = "./Demos/Data/ResetPageSize.pdf" 7 | 8 | # Load the document from disk 9 | originalDoc = PdfDocument() 10 | originalDoc.LoadFromFile(inputFile) 11 | # Set the margins 12 | margins = PdfMargins(0.0) 13 | # Create a new pdf document 14 | with PdfDocument() as newDoc: 15 | scale = 0.8 16 | for i in range(originalDoc.Pages.Count): 17 | page = originalDoc.Pages.get_Item(i) 18 | # Use same scale to set width and height 19 | width = page.Size.Width * scale 20 | height = page.Size.Height * scale 21 | # Add new page with expected width and height 22 | newPage = newDoc.Pages.Add(SizeF(width, height), margins) 23 | newPage.Canvas.ScaleTransform(scale, scale) 24 | # Copy content of original page into new page 25 | newPage.Canvas.DrawTemplate(page.CreateTemplate(), PointF.Empty()) 26 | # save the document 27 | newDoc.SaveToFile(outputFile) 28 | newDoc.Close() 29 | -------------------------------------------------------------------------------- /Examples/12_Page/RotateExistingPDF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "RotateExistingPDF.pdf" 5 | inputFile = "./Demos/Data/Sample.pdf" 6 | 7 | # Create a pdf document 8 | doc = PdfDocument() 9 | # Load an existing pdf from disk 10 | doc.LoadFromFile(inputFile) 11 | # Get the first page of the loaded PDF file 12 | page = doc.Pages[0] 13 | # Get the original rotation angle 14 | rotation = int(page.Rotation.value) 15 | # Set the angle 16 | rotation += int(PdfPageRotateAngle.RotateAngle270.value) 17 | # Rotate the PDF page based on 18 | page.Rotation = PdfPageRotateAngle(rotation) 19 | # Save the document 20 | doc.SaveToFile(outputFile) 21 | doc.Close() 22 | -------------------------------------------------------------------------------- /Examples/12_Page/RotateNewPDF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "RotateNewPDF.pdf" 5 | 6 | # Create a pdf document 7 | doc = PdfDocument() 8 | # Create PdfUnitConvertor to convert the unit 9 | unitCvtr = PdfUnitConvertor() 10 | # Setting for page margin 11 | margin = PdfMargins() 12 | margin.Top = unitCvtr.ConvertUnits( 13 | 2.54, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) 14 | margin.Bottom = margin.Top 15 | margin.Left = unitCvtr.ConvertUnits( 16 | 2.0, PdfGraphicsUnit.Centimeter, PdfGraphicsUnit.Point) 17 | margin.Right = margin.Left 18 | # Create PdfSection 19 | section = doc.Sections.Add() 20 | # Set "A4" for Pdf page 21 | section.PageSettings.Size = PdfPageSize.A4() 22 | # Set page margin 23 | section.PageSettings.Margins = margin 24 | # Set rotating angle 25 | section.PageSettings.Rotate = PdfPageRotateAngle.RotateAngle90 26 | # Add the page 27 | page = section.Pages.Add() 28 | # Define a PdfBrush 29 | brush = PdfBrushes.get_Black() 30 | # Define a font 31 | font = PdfTrueTypeFont("Arial", 13.0, PdfFontStyle.Bold, True) 32 | # Set the string format 33 | format = PdfStringFormat(PdfTextAlignment.Left) 34 | # Set the position for drawing 35 | x = 0.0 36 | y = 50.0 37 | # Text string 38 | specification = "The sample demonstrates how to rotate page when creating a PDF document." 39 | # Draw text string on page canvas 40 | page.Canvas.DrawString(specification, font, brush, x, y, format) 41 | # Save the document 42 | doc.SaveToFile(outputFile) 43 | doc.Close() 44 | -------------------------------------------------------------------------------- /Examples/12_Page/SetPageOrientation.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "SetPageOrientation.pdf" 5 | inputFile = "./Demos/Data/scenery.jpg" 6 | 7 | #Create a pdf document 8 | doc = PdfDocument() 9 | #Add a section 10 | section = doc.Sections.Add() 11 | #Load a image 12 | image = PdfImage.FromFile(inputFile) 13 | #Check whether the width of the image file is greater than default page width or not 14 | if image.PhysicalDimension.Width > section.PageSettings.Size.Width: 15 | #Set the orientation as landscape 16 | section.PageSettings.Orientation = PdfPageOrientation.Landscape 17 | else: 18 | section.PageSettings.Orientation = PdfPageOrientation.Portrait 19 | #Add a new page with orientation Landscape 20 | page = section.Pages.Add() 21 | #Draw the image on the page 22 | page.Canvas.DrawImage(image,PointF(0.0,0.0)) 23 | #Save to file 24 | doc.SaveToFile(outputFile) 25 | doc.Close() 26 | -------------------------------------------------------------------------------- /Examples/12_Page/SetTabOrder.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "SetTabOrder.pdf" 5 | inputFile = "./Demos/Data/SetTabOrder.pdf" 6 | 7 | #Load old PDF from disk. 8 | pdf = PdfDocument() 9 | pdf.LoadFromFile(inputFile) 10 | #Set using document structure 11 | pdf.FileInfo.IncrementalUpdate = False 12 | page = pdf.Pages[0] 13 | page.SetTabOrder(TabOrder.Structure) 14 | #Save the file 15 | pdf.SaveToFile(outputFile) 16 | pdf.Close() 17 | 18 | -------------------------------------------------------------------------------- /Examples/12_Page/SplitAPageIntoMultipage.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "SplitAPageIntoMultipage.pdf" 5 | inputFile = "./Demos/Data/PDFTemplate_N.pdf" 6 | 7 | #Load Pdf document from disk 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Get the first page 11 | page = doc.Pages[0] 12 | #Create a new Pdf 13 | newPdf = PdfDocument() 14 | #Remove all the margins 15 | newPdf.PageSettings.Margins.All = 0.0 16 | #Set the page size of new Pdf 17 | newPdf.PageSettings.Width = page.Size.Width 18 | newPdf.PageSettings.Height = page.Size.Height / float(2) 19 | #Add a new page 20 | newPage = newPdf.Pages.Add() 21 | format = PdfTextLayout() 22 | format.Break = PdfLayoutBreakType.FitPage 23 | format.Layout = PdfLayoutType.Paginate 24 | #Draw the page in the new page 25 | page.CreateTemplate().Draw(newPage, PointF(0.0, 0.0), format) 26 | #Save the Pdf document 27 | newPdf.SaveToFile(outputFile) 28 | newPdf.Close() 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /Examples/12_Page/SplitFileByParticularPage.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "SplitFileByParticularPage.pdf" 5 | inputFile = "./Demos/Data/Sample.pdf" 6 | 7 | #Create a pdf document 8 | oldPdf = PdfDocument() 9 | #Load an existing pdf from disk 10 | oldPdf.LoadFromFile(inputFile) 11 | #Create a new PDF document 12 | newPdf = PdfDocument() 13 | #Initialize a new instance of PdfPageBase class 14 | page = None 15 | #Specify the pages which you want them to be split 16 | for i in range(1, 3): 17 | #Add same size page for newPdf 18 | page = newPdf.Pages.Add(oldPdf.Pages[i].Size, PdfMargins(0.0)) 19 | #Create template of the oldPdf page and draw into newPdf page 20 | oldPdf.Pages[i].CreateTemplate().Draw(page, PointF(0.0, 0.0)) 21 | #Save the document 22 | newPdf.SaveToFile(outputFile) 23 | newPdf.Close() 24 | -------------------------------------------------------------------------------- /Examples/12_Page/ZoomToPageContents.py: -------------------------------------------------------------------------------- 1 | 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | outputFile = "ZoomToPageContents.pdf" 6 | inputFile = "./Demos/Data/PDFTemplate_N.pdf" 7 | 8 | #Load Pdf document from disk 9 | doc = PdfDocument() 10 | doc.LoadFromFile(inputFile) 11 | #Create a newDoc 12 | newDoc = PdfDocument() 13 | for i in range(doc.Pages.Count): 14 | page = doc.Pages.get_Item(i) 15 | #Add new page with 'A3' size 16 | newPage = newDoc.Pages.Add(PdfPageSize.A3(), PdfMargins(0.0,0.0)) 17 | #Zoom content to the new page 18 | newPage.Canvas.ScaleTransform(newPage.ActualSize.Width / float(page.ActualSize.Width), (newPage.ActualSize.Height / float(page.ActualSize.Height))) 19 | #Draw the page to new page 20 | newPage.Canvas.DrawTemplate(page.CreateTemplate(), PointF(0.0, 0.0)) 21 | #Save the Pdf document 22 | newDoc.SaveToFile(outputFile, FileFormat.PDF) 23 | newDoc.Close() 24 | 25 | -------------------------------------------------------------------------------- /Examples/13_Document/AddLayers.py: -------------------------------------------------------------------------------- 1 | 2 | from spire.pdf.common import * 3 | from spire.pdf import * 4 | 5 | outputFile = "AddLayers.pdf.pdf" 6 | inputFile = "./Demos/Data/AddLayers.pdf" 7 | 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | page = doc.Pages[0] 11 | #create a layer named "red line" 12 | layer = doc.Layers.AddLayer("red line",PdfVisibility.On) 13 | pcA = layer.CreateGraphics(page.Canvas) 14 | pcA.DrawLine(PdfPen(PdfBrushes.get_Red(), 2.0), PointF(100.0, 350.0), PointF(300.0, 350.0)) 15 | #create a layer named "blue line" 16 | layer = doc.Layers.AddLayer("blue line") 17 | pcB = layer.CreateGraphics(doc.Pages[0].Canvas) 18 | pcB.DrawLine(PdfPen(PdfBrushes.get_Blue(), 2.0), PointF(100.0, 400.0), PointF(300.0, 400.0)) 19 | #create a layer named "green line" 20 | layer = doc.Layers.AddLayer("green line") 21 | pcC = layer.CreateGraphics(doc.Pages[0].Canvas) 22 | pcC.DrawLine(PdfPen(PdfBrushes.get_Green(), 2.0), PointF(100.0, 450.0), PointF(300.0, 450.0)) 23 | #save the pdf document 24 | doc.SaveToFile(outputFile) 25 | doc.Close() 26 | -------------------------------------------------------------------------------- /Examples/13_Document/Booklet.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "Booklet_out.pdf" 5 | inputFile = "./Demos/Data/Booklet.pdf" 6 | 7 | #Create a pdf document 8 | doc = PdfDocument() 9 | width = PdfPageSize.A4().Width * 2 10 | height = PdfPageSize.A4().Height 11 | doc.CreateBooklet(inputFile, width, height, True) 12 | #Save pdf file 13 | doc.SaveToFile(outputFile) 14 | doc.Close() 15 | -------------------------------------------------------------------------------- /Examples/13_Document/ChangePdfVersion.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "ChangePdfVersion.pdf" 5 | inputFile = "./Demos/Data/ChangePdfVersion.pdf" 6 | 7 | #Open pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Change the pdf version 11 | doc.FileInfo.Version = PdfVersion.Version1_6 12 | doc.SaveToFile(outputFile) 13 | doc.Close() 14 | -------------------------------------------------------------------------------- /Examples/13_Document/CompressDocument.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "CompressDocument.pdf" 5 | inputFile = "./Demos/Data/CompressDocument.pdf" 6 | 7 | #Load the pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Compress the content in document 11 | doc.FileInfo.IncrementalUpdate = False 12 | #Set the compression level to best 13 | doc.CompressionLevel = PdfCompressionLevel.Best 14 | #Disable the incremental update 15 | doc.FileInfo.IncrementalUpdate = False 16 | #Traverse all pages 17 | for i in range(doc.Pages.Count): 18 | page = doc.Pages.get_Item(i) 19 | if page is not None: 20 | if page.ImagesInfo is not None: 21 | for info in page.ImagesInfo: 22 | page.TryCompressImage(info.Index) 23 | #Save the document 24 | doc.SaveToFile(outputFile) 25 | doc.Close() 26 | -------------------------------------------------------------------------------- /Examples/13_Document/CreateMultilayerPDF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "CreateMultilayerPDF.pdf" 5 | inputFile = "./Demos/Data/MultilayerImage.png" 6 | 7 | doc = PdfDocument() 8 | # Creates a page 9 | page = doc.Pages.Add() 10 | #Create text 11 | text = "Welcome to evaluate Spire.PDF for Python !" 12 | format = PdfStringFormat(PdfTextAlignment.Left) 13 | brush = PdfSolidBrush(PdfRGBColor(Color.get_Black())) 14 | # Defines a font 15 | font = PdfTrueTypeFont("Calibri", 15.0, PdfFontStyle.Regular,True) 16 | x = 50.0 17 | y = 50.0 18 | # Draw text layer 19 | page.Canvas.DrawString(text, font, brush, PointF(x, y), format) 20 | size = font.MeasureString("Welcome to evaluate", format) 21 | size2 = font.MeasureString("Spire.PDF for Python", format) 22 | # Loads an image 23 | image = PdfImage.FromFile(inputFile) 24 | # Draw image layer 25 | page.Canvas.DrawImage(image, PointF(x + size.Width, y),size2) 26 | #Save the document 27 | doc.SaveToFile(outputFile) 28 | doc.Close() 29 | -------------------------------------------------------------------------------- /Examples/13_Document/CreatePDFA1WithSpirePDF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile1 = "CreatePDFA1WithSpirePDF_Temp.pdf" 5 | outputFile2 = "CreatePDFA1WithSpirePDF_A1B.pdf" 6 | inputFile = "./Demos/Data/Wikipedia_Science.png" 7 | 8 | doc = PdfNewDocument() 9 | #Create one A4 page 10 | page = doc.Pages.Add(PdfPageSize.A4(),PdfMargins(40.0)) 11 | # Draw content 12 | pageWidth = page.Canvas.ClientSize.Width 13 | y = 0.0 14 | #Title 15 | y = y + 5 16 | brush2 = PdfSolidBrush(PdfRGBColor(Color.get_Black())) 17 | font2 = PdfTrueTypeFont("Arial", 16.0, PdfFontStyle.Bold, True) 18 | format2 = PdfStringFormat(PdfTextAlignment.Center) 19 | format2.CharacterSpacing = 1 20 | text = "Summary of Science" 21 | page.Canvas.DrawString(text, font2, brush2, pageWidth / 2, y, format2) 22 | size = font2.MeasureString(text, format2) 23 | y = y + size.Height + 6 24 | #Icon 25 | image = PdfImage.FromFile(inputFile) 26 | page.Canvas.DrawImage(image, PointF(pageWidth - image.PhysicalDimension.Width, y)) 27 | imageLeftSpace = pageWidth - image.PhysicalDimension.Width - 2 28 | imageBottom = image.PhysicalDimension.Height + y 29 | #Reference content 30 | font3 = PdfTrueTypeFont("Arial", 9.0, PdfFontStyle.Regular,True) 31 | format3 = PdfStringFormat() 32 | format3.ParagraphIndent = font3.Size * 2 33 | format3.MeasureTrailingSpaces = True 34 | format3.LineSpacing = font3.Size * 1.5 35 | text1 = "(All text and picture from " 36 | text2 = "Wikipedia" 37 | text3 = ", the free encyclopedia)" 38 | page.Canvas.DrawString(text1, font3, brush2, 0.0, y, format3) 39 | size = font3.MeasureString(text1, format3) 40 | x1 = size.Width 41 | format3.ParagraphIndent = 0.0 42 | font4 = PdfTrueTypeFont("Arial", 9.0, PdfFontStyle.Underline, True) 43 | brush3 = PdfBrushes.get_Blue() 44 | page.Canvas.DrawString(text2, font4, brush3, x1, y, format3) 45 | size = font4.MeasureString(text2, format3) 46 | x1 = x1 + size.Width 47 | page.Canvas.DrawString(text3, font3, brush2, x1, y, format3) 48 | y = y + size.Height 49 | #Content 50 | format4 = PdfStringFormat() 51 | with open("./Demos/Data/Summary_of_Science.txt",'r') as f: 52 | text = f.read() 53 | font5 = PdfTrueTypeFont("Arial", 10.0, PdfFontStyle.Regular,True) 54 | format4.LineSpacing = font5.Size * 1.5 55 | textLayouter = PdfStringLayouter() 56 | imageLeftBlockHeight = imageBottom - y 57 | result = textLayouter.Layout(text, font5, format4, SizeF(imageLeftSpace, imageLeftBlockHeight)) 58 | if result.ActualSize.Height < imageBottom - y: 59 | imageLeftBlockHeight = imageLeftBlockHeight + result.LineHeight 60 | result = textLayouter.Layout(text, font5, format4, SizeF(imageLeftSpace, imageLeftBlockHeight)) 61 | for line in result.Lines: 62 | page.Canvas.DrawString(line.Text, font5, brush2, 0.0, y, format4) 63 | y = y + result.LineHeight 64 | textWidget = PdfTextWidget(result.Remainder, font5, brush2) 65 | textLayout = PdfTextLayout() 66 | textLayout.Break = PdfLayoutBreakType.FitPage 67 | textLayout.Layout = PdfLayoutType.Paginate 68 | bounds = RectangleF(PointF(0.0, y), page.Canvas.ClientSize) 69 | textWidget.StringFormat = format4 70 | textWidget.Draw(page, bounds, textLayout) 71 | #Save the document 72 | stream = Stream(outputFile1) 73 | doc.Save(stream) 74 | converter = PdfStandardsConverter(stream) 75 | converter.ToPdfA1B(outputFile2) 76 | doc.Close() -------------------------------------------------------------------------------- /Examples/13_Document/CreatePDFPortfolio.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | import glob 4 | 5 | 6 | inputFile = "./Demos/Data/Sample.pdf" 7 | inputFolder="./Demos/Data/CreatePDFPortfolio/*" 8 | outputFile = "CreatePDFPortfolio.pdf" 9 | 10 | #Create folder and sub folder to add file into it 11 | files = glob.glob(inputFolder) 12 | doc = PdfDocument(inputFile) 13 | i = 0 14 | while i < len(files): 15 | doc.Collection.Folders.AddFile(files[i]) 16 | folder = doc.Collection.Folders.CreateSubfolder("SubFolder" + str(i + 1)) 17 | folder.AddFile(files[i]) 18 | i += 1 19 | #Save the document 20 | doc.SaveToFile(outputFile) 21 | doc.Close() 22 | -------------------------------------------------------------------------------- /Examples/13_Document/CreateTwoColumnPDF.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | outputFile = "CreateTwoColumnPDF.pdf" 5 | 6 | # Creates a pdf document 7 | doc = PdfDocument() 8 | # Creates a new page 9 | page = doc.Pages.Add() 10 | s1 = "Spire.PDF for .NET is a professional PDF component applied to creating, writing, " + "editing, handling and reading PDF files without any external dependencies within " + ".NET application. Using this .NET PDF library, you can implement rich capabilities " + "to create PDF files from scratch or process existing PDF documents entirely through " + "C#/VB.NET without installing Adobe Acrobat." 11 | s2 = "Many rich features can be supported by the .NET PDF API, such as security setting " + "(including digital signature), PDF text/attachment/image extract, PDF merge/split " + ", metadata update, section, graph/image drawing and inserting, table creation and " + "processing, and importing data etc.Besides, Spire.PDF for .NET can be applied to easily " + "converting Text, Image and HTML to PDF with C#/VB.NET in high quality." 12 | # Get width and height of page 13 | pageWidth = page.GetClientSize().Width 14 | pageHeight = page.GetClientSize().Height 15 | brush = PdfBrushes.get_Black() 16 | font = PdfFont(PdfFontFamily.TimesRoman, 12.0) 17 | format = PdfStringFormat(PdfTextAlignment.Justify) 18 | # Draw text 19 | page.Canvas.DrawString(s1, font, brush, RectangleF(0.0, 20.0, pageWidth / 2 - 8, pageHeight), format) 20 | page.Canvas.DrawString(s2, font, brush, RectangleF(pageWidth / 2 + 8, 20.0, pageWidth / 2 - 8, pageHeight), format) 21 | #Save the document 22 | doc.SaveToFile(outputFile) 23 | doc.Close() -------------------------------------------------------------------------------- /Examples/13_Document/CustomDocumentProperties.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/CustomDocumentProperties.pdf" 5 | outputFile = "CustomDocumentProperties.pdf" 6 | 7 | doc = PdfDocument() 8 | #Load a pdf file from disk 9 | doc.LoadFromFile(inputFile) 10 | #Custom document properties 11 | doc.DocumentInformation.SetCustomProperty("Company", "E-iceblue") 12 | doc.DocumentInformation.SetCustomProperty("Component", "Spire.PDF for .NET") 13 | doc.DocumentInformation.SetCustomProperty("Name", "Daisy") 14 | doc.DocumentInformation.SetCustomProperty("Team", "SalesTeam") 15 | #Save to file 16 | doc.SaveToFile(outputFile, FileFormat.PDF) 17 | doc.Close() -------------------------------------------------------------------------------- /Examples/13_Document/DeleteLayer.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/DeleteLayer.pdf" 5 | outputFile = "DeleteLayer.pdf" 6 | 7 | #Load the document from disk 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Delete the "red line" layer 11 | doc.Layers.RemoveLayer("red line") 12 | #Save the document 13 | doc.SaveToFile(outputFile) 14 | doc.Close() 15 | 16 | -------------------------------------------------------------------------------- /Examples/13_Document/GetDocumentProperties.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/PDFTemplate-Az.pdf" 5 | outputFile = "GetDocumentProperties.txt" 6 | 7 | doc = PdfDocument() 8 | # Read a pdf file 9 | doc.LoadFromFile(inputFile) 10 | # Get document information 11 | docInfo = doc.DocumentInformation 12 | # Create a StringBuilder object to put the details 13 | builder = [] 14 | builder.append("Author:"+docInfo.Author) 15 | builder.append("Creation Date: "+ docInfo.CreationDate.ToString()) 16 | builder.append("Keywords: "+ docInfo.Keywords) 17 | builder.append("Modify Date: "+ docInfo.ModificationDate.ToString()) 18 | builder.append("Subject: "+ docInfo.Subject) 19 | builder.append("Title: "+ docInfo.Title) 20 | fp = open(outputFile,"w") 21 | for s in builder: 22 | fp.write(s + "\n") 23 | fp.close() 24 | -------------------------------------------------------------------------------- /Examples/13_Document/GetViewerPreference.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile = "./Demos/Data/PDFTemplate-Az.pdf" 6 | outputFile = "GetViewerPreference.txt" 7 | 8 | doc = PdfDocument() 9 | # Read a pdf file 10 | doc.LoadFromFile(inputFile) 11 | viewer = doc.ViewerPreferences 12 | # Create a StringBuilder object to put the details 13 | builder = [] 14 | builder.append("Whether the documents window position is in the center: ") 15 | builder.append("CenterWindow: " + str(viewer.CenterWindow)) 16 | builder.append("Document displaying mode, i.e. show thumbnails, full-screen, show attachment panel: ") 17 | builder.append("PageMode: " + str(viewer.PageMode)) 18 | builder.append("The page layout, i.e. single page, one column: ") 19 | builder.append("PageLayout: " + str(viewer.PageLayout)) 20 | builder.append("Whether window's title bar should display document title: ") 21 | builder.append("DisplayTitle: " + str(viewer.DisplayTitle)) 22 | builder.append("Whether to resize the document's window to fit the size of the firstdisplayed page: ") 23 | builder.append("FitWindow:" + str(viewer.FitWindow)) 24 | builder.append("Whether to hide menu bar of the viewer application: ") 25 | builder.append("HideMenubar: " + str(viewer.HideMenubar)) 26 | builder.append("Whether to hide tool bar of the viewer application: ") 27 | builder.append("HideToolbar: " + str(viewer.HideToolbar)) 28 | builder.append("Whether to hide UI elements like scroll bars and leave only the page contents displayed: ") 29 | builder.append("HideWindowUI: " + str(viewer.HideWindowUI)) 30 | #save 31 | fp = open(outputFile,"w") 32 | for s in builder: 33 | fp.write(s + "\n") 34 | fp.close() -------------------------------------------------------------------------------- /Examples/13_Document/GetZoomFactor.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/GetZoomFactor.pdf" 5 | outputFile = "GetZoomFactor.txt" 6 | 7 | #Create a pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | action = doc.AfterOpenAction 11 | zoomvalue = action.Destination.Zoom 12 | result = "The zoom factor of the document is "+ str(zoomvalue*100) +"%." 13 | #save 14 | fp = open(outputFile,"w") 15 | fp.write(outputFile + "\n") 16 | fp.close() -------------------------------------------------------------------------------- /Examples/13_Document/InvisibleAllPdfLayers.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_5.pdf" 5 | outputFile = "InvisibleAllPdfLayers.pdf" 6 | 7 | #Create a new PDF document. 8 | doc = PdfDocument() 9 | #Load the file from disk. 10 | doc.LoadFromFile(inputFile) 11 | for i in range(doc.Layers.Count): 12 | #Set all the Pdf layers invisible. 13 | doc.Layers.get_Item(i).Visibility = PdfVisibility.Off 14 | #Save the document 15 | doc.SaveToFile(outputFile) 16 | doc.Close() 17 | -------------------------------------------------------------------------------- /Examples/13_Document/InvisibleParticularPdfLayers.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_5.pdf" 5 | outputFile = "InvisibleParticularPdfLayers.pdf" 6 | 7 | #Create a new PDF document. 8 | doc = PdfDocument() 9 | #Load the file from disk. 10 | doc.LoadFromFile(inputFile) 11 | #Set the first layer invisible. 12 | doc.Layers.get_Item(0).Visibility = PdfVisibility.Off 13 | #Set the layer named "blue line" invisible. 14 | doc.Layers.get_Item("blue line").Visibility = PdfVisibility.Off 15 | #Save the document 16 | doc.SaveToFile(outputFile) 17 | doc.Close() 18 | -------------------------------------------------------------------------------- /Examples/13_Document/IsPDFPortfolio.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Template_Pdf_5.pdf" 5 | outputFile = "IsPDFPortfolio.txt" 6 | 7 | #Create a pdf document 8 | doc = PdfDocument() 9 | #Load from file 10 | doc.LoadFromFile(inputFile) 11 | builder = "" 12 | #Judge whether the document is portfolio or not. 13 | value = doc.IsPortfolio 14 | if value: 15 | builder="The document is portfolio" 16 | else: 17 | builder="The document is not portfolio" 18 | #save 19 | fp = open(outputFile,"w") 20 | for s in builder: 21 | fp.write(s) 22 | fp.close() 23 | doc.Close() -------------------------------------------------------------------------------- /Examples/13_Document/MergeDocuments.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile1 = "./Demos/Data/MergePdfsTemplate_1.pdf" 5 | inputFile2 = "./Demos/Data/MergePdfsTemplate_2.pdf" 6 | inputFile3 = "./Demos/Data/MergePdfsTemplate_3.pdf" 7 | outputFile = "MergeDocuments.pdf" 8 | 9 | #Pdf document list 10 | files = [inputFile1, inputFile2, inputFile3] 11 | #Open pdf documents 12 | docs = [None for _ in range(len(files))] 13 | i = 0 14 | while i < len(files): 15 | docs[i] = PdfDocument() 16 | docs[i].LoadFromFile(files[i]) 17 | i += 1 18 | #Append document 19 | docs[0].AppendPage(docs[1]) 20 | #Import page 21 | for i in range(0, docs[2].Pages.Count, 2): 22 | docs[0].InsertPage(docs[2], i) 23 | #Save pdf file 24 | docs[0].SaveToFile(outputFile) 25 | #Close 26 | for doc in docs: 27 | doc.Close() 28 | -------------------------------------------------------------------------------- /Examples/13_Document/MergePdfsByStream.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile1 = "./Demos/Data/MergePdfsTemplate_1.pdf" 5 | inputFile2 = "./Demos/Data/MergePdfsTemplate_2.pdf" 6 | inputFile3 = "./Demos/Data/MergePdfsTemplate_3.pdf" 7 | outputFile = Stream("MergeFilesByStream.pdf") 8 | 9 | #Pdf document streams 10 | stream1 = Stream(inputFile1) 11 | stream2 = Stream(inputFile2) 12 | stream3 = Stream(inputFile3) 13 | #Pdf document streams 14 | streams = [stream1, stream2, stream3] 15 | mergeOp = MergerOptions() 16 | PdfMerger.MergeByStream(streams,outputFile,mergeOp) 17 | -------------------------------------------------------------------------------- /Examples/13_Document/ModifyPageMargins.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | 5 | inputFile = "./Demos/Data/ModifyPageMargins.pdf" 6 | outputFile = "ModifyPageMargins.pdf" 7 | 8 | doc = PdfDocument() 9 | # Read a pdf file 10 | doc.LoadFromFile(inputFile) 11 | # Creates a new pdf document 12 | newDoc = PdfDocument() 13 | # Defines the page margins of the new document 14 | top = 50.0 15 | bottom = 50.0 16 | left = 50.0 17 | right = 50.0 18 | for i in range(doc.Pages.Count): 19 | page = doc.Pages.get_Item(i) 20 | # Adds a new page to the new document and set the page size to be the same as the source document 21 | newPage = newDoc.Pages.Add(page.Size, PdfMargins(0.0)) 22 | # Set the scale of the new document content 23 | newPage.Canvas.ScaleTransform((page.ActualSize.Width - left - right) / page.ActualSize.Width, (page.ActualSize.Height - top - bottom) / page.ActualSize.Height) 24 | # Draws the content of the source page onto the new document page 25 | newPage.Canvas.DrawTemplate(page.CreateTemplate(), PointF(left, top)) 26 | #Save the document 27 | newDoc.SaveToFile(outputFile) 28 | newDoc.Close() 29 | doc.Close() 30 | 31 | -------------------------------------------------------------------------------- /Examples/13_Document/Properties.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/Properties.pdf" 5 | outputFile = "Properties.pdf" 6 | 7 | #Load pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile( inputFile) 10 | #Set document info 11 | doc.DocumentInformation.Author = "E-iceblue" 12 | doc.DocumentInformation.Creator = "E-iceblue" 13 | doc.DocumentInformation.Keywords = "pdf, demo, document information" 14 | doc.DocumentInformation.Producer = "Spire.Pdf" 15 | doc.DocumentInformation.Subject = "Demo of Spire.Pdf" 16 | doc.DocumentInformation.Title = "Document Information" 17 | #File info 18 | doc.FileInfo.CrossReferenceType = PdfCrossReferenceType.CrossReferenceStream 19 | doc.FileInfo.IncrementalUpdate = False 20 | #Save pdf file 21 | doc.SaveToFile(outputFile) 22 | doc.Close() 23 | -------------------------------------------------------------------------------- /Examples/13_Document/RearrangePageOrder.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/SampleB_3.pdf" 5 | outputFile = "RearrangePageOrder.pdf" 6 | 7 | #Create a pdf document 8 | doc = PdfDocument() 9 | #Load from file 10 | doc.LoadFromFile(inputFile) 11 | #Rearrange the page order 12 | doc.Pages.ReArrange([1, 0]) 13 | #Save to file 14 | doc.SaveToFile(outputFile, FileFormat.PDF) 15 | doc.Close() 16 | 17 | -------------------------------------------------------------------------------- /Examples/13_Document/RemovePageMargins.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/PDFTemplate-Az.pdf" 5 | outputFile = "RemovePageMargins.pdf" 6 | 7 | doc = PdfDocument() 8 | # Read a pdf file 9 | doc.LoadFromFile(inputFile) 10 | # Creates a new page 11 | newDoc = PdfDocument() 12 | # Get page margins of source pdf page 13 | margins = doc.PageSettings.Margins 14 | top = margins.Left 15 | bottom = margins.Bottom 16 | left = margins.Left 17 | right = margins.Right 18 | for i in range(doc.Pages.Count): 19 | page = doc.Pages.get_Item(i) 20 | # Adds a new page to the new document 21 | newPage = newDoc.Pages.Add(SizeF(page.Size.Width - left - right, page.Size.Height - top - bottom), PdfMargins(0.0)) 22 | # Draws the content of the source page onto the new document page 23 | newPage.Canvas.DrawTemplate(page.CreateTemplate(), PointF(-left, -top)) 24 | #Save the document 25 | newDoc.SaveToFile(outputFile) 26 | newDoc.Close() 27 | doc.Close() 28 | 29 | -------------------------------------------------------------------------------- /Examples/13_Document/SetExpiryDate.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/PDFTemplate-Az.pdf" 5 | outputFile = "SetExpiryDate.pdf" 6 | 7 | doc = PdfDocument() 8 | # Read a pdf file 9 | doc.LoadFromFile(inputFile) 10 | javaScript = "var rightNow = new Date();" + "var endDate = new Date('October 20, 2015 23:59:59');" + "if(rightNow.getTime() > endDate)" + "app.alert('This document has expired, please contact us for a new one.',1);" + "this.closeDoc();" 11 | js = PdfJavaScriptAction(javaScript) 12 | doc.AfterOpenAction = js 13 | #Save the document 14 | doc.SaveToFile(outputFile) 15 | doc.Close() 16 | 17 | -------------------------------------------------------------------------------- /Examples/13_Document/SetZoomFactor.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/SetZoomFactor.pdf" 5 | outputFile = "SetZoomFactor.pdf" 6 | 7 | #Open a pdf document 8 | doc = PdfDocument() 9 | doc.LoadFromFile(inputFile) 10 | #Get the first page 11 | page = doc.Pages[0] 12 | #Set pdf destination 13 | dest = PdfDestination(page) 14 | dest.Mode = PdfDestinationMode.Location 15 | dest.Location = PointF(-40.0, -40.0) 16 | dest.Zoom = 0.6 17 | #Set action 18 | gotoAction = PdfGoToAction(dest) 19 | doc.AfterOpenAction = gotoAction 20 | #Save pdf document 21 | doc.SaveToFile(outputFile) 22 | doc.Close() 23 | -------------------------------------------------------------------------------- /Examples/13_Document/SplitDocument.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/SplitDocument.pdf" 5 | 6 | #Open pdf document 7 | doc = PdfDocument() 8 | doc.LoadFromFile(inputFile) 9 | pattern = "SplitDocument-{0}.pdf" 10 | #Split document 11 | doc.Split(pattern) 12 | lastPageFileName = ("SplitDocument-{0:d}.pdf").format(doc.Pages.Count - 1) 13 | doc.Close() 14 | -------------------------------------------------------------------------------- /Examples/13_Document/ViewerPreference.py: -------------------------------------------------------------------------------- 1 | from spire.pdf.common import * 2 | from spire.pdf import * 3 | 4 | inputFile = "./Demos/Data/ViewerPreference.pdf" 5 | outputFile = "ViewerPreference.pdf" 6 | 7 | #Load pdf document 8 | doc = PdfDocument(inputFile) 9 | #Set view reference 10 | doc.ViewerPreferences.CenterWindow = True 11 | doc.ViewerPreferences.DisplayTitle = False 12 | doc.ViewerPreferences.FitWindow = False 13 | doc.ViewerPreferences.HideMenubar = True 14 | doc.ViewerPreferences.HideToolbar = True 15 | doc.ViewerPreferences.PageLayout = PdfPageLayout.SinglePage 16 | #Save pdf file 17 | doc.SaveToFile(outputFile) 18 | doc.Close() --------------------------------------------------------------------------------