<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jack9012 </title>
    <description>The latest articles on DEV Community by Jack9012  (@jack_du_64a902eb1614b3933).</description>
    <link>https://dev.to/jack_du_64a902eb1614b3933</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4114752%2F72b3d9cb-c234-4065-824d-7f5c0a1a3fc0.png</url>
      <title>DEV Community: Jack9012 </title>
      <link>https://dev.to/jack_du_64a902eb1614b3933</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jack_du_64a902eb1614b3933"/>
    <language>en</language>
    <item>
      <title>How to Add Watermarks to Excel in C#: Two Practical Approaches</title>
      <dc:creator>Jack9012 </dc:creator>
      <pubDate>Tue, 29 Sep 2026 06:11:43 +0000</pubDate>
      <link>https://dev.to/jack_du_64a902eb1614b3933/how-to-add-watermarks-to-excel-in-c-two-practical-approaches-23e8</link>
      <guid>https://dev.to/jack_du_64a902eb1614b3933/how-to-add-watermarks-to-excel-in-c-two-practical-approaches-23e8</guid>
      <description>&lt;p&gt;In scenarios such as report distribution, contract archiving, and confidential data sharing, adding a watermark to an Excel document is a simple and practical means of copyright identification. A watermark can be composed of either an image or text. Watermarks with wording such as "Confidential," "Internal Use Only," and "Do Not Distribute" are widely used across business contexts worldwide.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs3fr72cfv10z59zrxokd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs3fr72cfv10z59zrxokd.png" alt="Add Watermarks to Excel in C#" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This article will demonstrate how to use a free .NET Excel library to add watermarks to Excel documents in C#, compare two implementation approaches—header image watermarks and background image watermarks—and supplement this with a piece of helper code for generating text watermark images, so that it can be used directly even when no ready-made watermark image is available.&lt;/p&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;The library can be brought into the project via NuGet.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;NuGet Package Manager Console&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  Install-Package FreeSpire.XLS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;.NET CLI&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  dotnet add package FreeSpire.XLS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installation is complete, import the namespace &lt;code&gt;Spire.Xls&lt;/code&gt; in the code to use it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Comparison of the Two Watermark Solutions
&lt;/h2&gt;

&lt;p&gt;An Excel watermark is not a true "watermark" object. It is essentially simulated through a header image or a worksheet background image, so the two solutions differ significantly in terms of printing, view, positioning, and other aspects.&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Comparison Item&lt;/th&gt;
&lt;th&gt;Header Image Watermark&lt;/th&gt;
&lt;th&gt;Background Image Watermark&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Print output&lt;/td&gt;
&lt;td&gt;Printed along with the document&lt;/td&gt;
&lt;td&gt;Does not appear in the print result&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;View visibility&lt;/td&gt;
&lt;td&gt;Visible only in "Page Layout" or "Page Break Preview" mode, not in Normal view&lt;/td&gt;
&lt;td&gt;Visible in all view modes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Position control&lt;/td&gt;
&lt;td&gt;Header area margins need to be adjusted manually to center the image&lt;/td&gt;
&lt;td&gt;Automatically fills the entire worksheet data area&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Visual consistency&lt;/td&gt;
&lt;td&gt;Affected by pagination; adjacent pages may be misaligned&lt;/td&gt;
&lt;td&gt;The entire worksheet displays only the same image&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;
  
  
  Header Image Watermark
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Advantages&lt;/strong&gt; : The watermark appears in the document's final print output, making it suitable for scenarios that must be printed and archived.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Disadvantages&lt;/strong&gt; : It is not visible in Excel's "Normal" view and can only be seen in "Page Layout" or "Page Break Preview"; to position the image exactly in the center of the page, the top and left margins of the image must be adjusted carefully.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Background Image Watermark
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Advantages&lt;/strong&gt; : The watermark image covers the entire worksheet, producing a consistent effect.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Disadvantages&lt;/strong&gt; : The watermark does not appear in the print output.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Solution 1: Add an Image Watermark Through the Header
&lt;/h2&gt;

&lt;p&gt;The following example iterates through all worksheets in the document and sets the same watermark image in the center position of the header.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Xls&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Drawing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;AddWatermarkToExcelUsingHeader&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Create a Workbook object&lt;/span&gt;
            &lt;span class="n"&gt;Workbook&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Workbook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="c1"&gt;// Load the Excel document&lt;/span&gt;
            &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\Administrator\Desktop\Input.xlsx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Load the watermark image&lt;/span&gt;
            &lt;span class="n"&gt;Image&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\Administrator\Desktop\confidential.png"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Iterate through all worksheets in the document&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Worksheets&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// Get the specified worksheet&lt;/span&gt;
                &lt;span class="n"&gt;Worksheet&lt;/span&gt; &lt;span class="n"&gt;worksheet&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Worksheets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

                &lt;span class="c1"&gt;// Insert an image placeholder in the center of the header&lt;/span&gt;
                &lt;span class="n"&gt;worksheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CenterHeader&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;"&amp;amp;G"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

                &lt;span class="c1"&gt;// Set the image in the center of the header&lt;/span&gt;
                &lt;span class="n"&gt;worksheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;CenterHeaderImage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="c1"&gt;// Save the resulting file&lt;/span&gt;
            &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AddWatermark.xlsx"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ExcelVersion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Version2016&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Release resources&lt;/span&gt;
            &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: &lt;code&gt;&amp;amp;G&lt;/code&gt; is Excel's own header image placeholder. All text (such as the word "Confidential") should be baked directly into the image, rather than written into the &lt;code&gt;CenterHeader&lt;/code&gt; string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Solution 2: Set the Image as a Worksheet Background
&lt;/h2&gt;

&lt;p&gt;The following example uses an image as a worksheet background watermark, which is suitable for screen-only viewing scenarios that require strong visibility.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Xls&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Drawing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;AddWatermarkToExcelUsingBackground&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Create a Workbook object&lt;/span&gt;
            &lt;span class="n"&gt;Workbook&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Workbook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="c1"&gt;// Load the Excel document&lt;/span&gt;
            &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\Administrator\Desktop\Input.xlsx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Load the watermark image&lt;/span&gt;
            &lt;span class="n"&gt;Bitmap&lt;/span&gt; &lt;span class="n"&gt;bitmapImage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Bitmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;@"C:\Users\Administrator\Desktop\sample.png"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

            &lt;span class="c1"&gt;// Iterate through all worksheets in the document&lt;/span&gt;
            &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Worksheets&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// Get the specified worksheet&lt;/span&gt;
                &lt;span class="n"&gt;Worksheet&lt;/span&gt; &lt;span class="n"&gt;worksheet&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Worksheets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

                &lt;span class="c1"&gt;// Set the image as the background of the worksheet&lt;/span&gt;
                &lt;span class="n"&gt;worksheet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BackgroundImage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bitmapImage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;

            &lt;span class="c1"&gt;// Save the resulting file&lt;/span&gt;
            &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"AddWatermark.xlsx"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ExcelVersion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Version2016&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Release resources&lt;/span&gt;
            &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Generation of Text Watermarks
&lt;/h2&gt;

&lt;p&gt;In real-world business scenarios, watermarks are often dynamic text such as "Confidential" or "Internal Use Only," and there is no need for a designer to provide a PNG every time. You can use &lt;code&gt;System.Drawing&lt;/code&gt; to draw a text watermark image with a transparent background in memory, and then reuse it with Solution 1 or Solution 2.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Drawing&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Drawing.Drawing2D&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Drawing.Imaging&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;WatermarkHelper&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// Generates a text watermark image (transparent background, tilted -30°)&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="text"&amp;gt;Watermark text, such as "Confidential"&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="width"&amp;gt;Canvas width&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;param name="height"&amp;gt;Canvas height&amp;lt;/param&amp;gt;&lt;/span&gt;
    &lt;span class="c1"&gt;/// &amp;lt;returns&amp;gt;A Bitmap object with a transparent background&amp;lt;/returns&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="n"&gt;Bitmap&lt;/span&gt; &lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;Bitmap&lt;/span&gt; &lt;span class="n"&gt;bmp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Bitmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PixelFormat&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Format32bppArgb&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Graphics&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Graphics&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FromImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bmp&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Clear&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Transparent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SmoothingMode&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;SmoothingMode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AntiAlias&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
            &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TextRenderingHint&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;System&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Drawing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TextRenderingHint&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;AntiAliasGridFit&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Font&lt;/span&gt; &lt;span class="n"&gt;font&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Font&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Arial"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;FontStyle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Bold&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
            &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;SolidBrush&lt;/span&gt; &lt;span class="n"&gt;brush&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;SolidBrush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;FromArgb&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;Color&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Red&lt;/span&gt;&lt;span class="p"&gt;)))&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="c1"&gt;// Calculate the text size and center it&lt;/span&gt;
                &lt;span class="n"&gt;SizeF&lt;/span&gt; &lt;span class="n"&gt;size&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;MeasureString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;font&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

                &lt;span class="c1"&gt;// Translate to the center of the canvas, then rotate, so that the drawn text is centered&lt;/span&gt;
                &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;TranslateTransform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
                &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;RotateTransform&lt;/span&gt;&lt;span class="p"&gt;(-&lt;/span&gt;&lt;span class="m"&gt;30&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

                &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;DrawString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;font&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;brush&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Width&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2f&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;-&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Height&lt;/span&gt; &lt;span class="p"&gt;/&lt;/span&gt; &lt;span class="m"&gt;2f&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;bmp&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When calling it, simply pass the generated &lt;code&gt;Bitmap&lt;/code&gt; directly to either of the two solutions above:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Generate a text watermark image&lt;/span&gt;
&lt;span class="n"&gt;Bitmap&lt;/span&gt; &lt;span class="n"&gt;watermarkImage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;WatermarkHelper&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Confidential"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;800&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;600&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Use it as a worksheet background watermark&lt;/span&gt;
&lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Worksheets&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;PageSetup&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;BackgroundImage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;watermarkImage&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Font Consistency Recommendations
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;System.Drawing&lt;/code&gt; renders text using fonts installed on the system by default. In different environments (such as Linux servers or containerized deployments), font loss may occur, causing text to render incorrectly. In production environments, it is recommended to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Package font files into the application;&lt;/li&gt;
&lt;li&gt;Load private fonts through &lt;code&gt;PrivateFontCollection&lt;/code&gt; and then pass them to the &lt;code&gt;Font&lt;/code&gt; constructor;&lt;/li&gt;
&lt;li&gt;Or render all potentially used watermark text into PNGs in advance and distribute them as static resources with the project.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Solution Selection Reference
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Scenario&lt;/th&gt;
&lt;th&gt;Recommended Solution&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Reports need to be printed and archived, and documents need to be retained long-term&lt;/td&gt;
&lt;td&gt;Solution 1: Header image watermark&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Screen-only viewing, internal presentations, emphasis on intuitive visibility&lt;/td&gt;
&lt;td&gt;Solution 2: Background image watermark&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;The free .NET Excel library itself does not directly provide a "watermark" object. Instead, it implements watermarks through two mechanisms: header images or worksheet background images. By passing any &lt;code&gt;Image&lt;/code&gt;/&lt;code&gt;Bitmap&lt;/code&gt; to &lt;code&gt;CenterHeaderImage&lt;/code&gt; or &lt;code&gt;BackgroundImage&lt;/code&gt;, watermark addition is complete; combined with a short piece of text drawing code, watermarks such as "Confidential" and "Internal Use Only" can be generated flexibly without relying on external assets.&lt;/p&gt;

&lt;p&gt;In actual projects, it is recommended to encapsulate "image generation + watermark injection" into independent utility methods, exposing only a small number of parameters such as file path, watermark text, and visibility to the outside. This minimizes the cost of reuse when dealing with multiple business lines and multiple templates.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Change Page Margins of a PDF Using Python</title>
      <dc:creator>Jack9012 </dc:creator>
      <pubDate>Wed, 23 Sep 2026 01:31:58 +0000</pubDate>
      <link>https://dev.to/jack_du_64a902eb1614b3933/how-to-change-page-margins-of-a-pdf-using-python-374o</link>
      <guid>https://dev.to/jack_du_64a902eb1614b3933/how-to-change-page-margins-of-a-pdf-using-python-374o</guid>
      <description>&lt;p&gt;In document processing scenarios, we may encounter the need to adjust the blank edges (margins) of PDF files, such as reserving blank space for printing, cropping excess white edges, or unifying document layout formats. There are several PDF processing libraries in the Python ecosystem. This article uses &lt;strong&gt;Free Spire.PDF for Python&lt;/strong&gt; to implement pure-code, watermark-free, and lightweight PDF margin increase and decrease operations.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh0wh64a8s650p47ho5xc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fh0wh64a8s650p47ho5xc.png" alt="Change PDF Page Margins" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This solution requires no complex dependencies. Through the core logic of creating a new PDF document, reconstructing page sizes, and redrawing pages using templates, it precisely controls the blank edges on all four sides (top, bottom, left, and right) of a PDF, and is suitable for single-page and multi-page PDF documents.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Setup the Environment
&lt;/h2&gt;

&lt;p&gt;First, install the required third-party library. Free Spire.PDF for Python focuses on basic PDF editing, with a concise and easy-to-understand API, making it suitable for rapid development. Run the following pip command to complete the installation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;spire.pdf
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Overview of Implementation Principles
&lt;/h2&gt;

&lt;p&gt;The core logic of the two margin adjustment methods in this article is the same; only the size calculation and drawing parameters differ:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Increase margins&lt;/strong&gt; : Expand the PDF page size, keep the original page content centered, and use the newly added blank area as margins.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Decrease margins&lt;/strong&gt; : Shrink the PDF page size, and crop the blank edges of the original page by offsetting the content drawing coordinates.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;General workflow: Load the original PDF → Calculate the new page size → Iterate through pages to generate templates → Create new pages and redraw content → Save the new file and release resources.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Increase PDF Page Margins in Python
&lt;/h2&gt;

&lt;p&gt;This scenario is suitable for situations where PDF content is too close to the edges, where printing whitespace needs to be added, or where document margin specifications need to be unified. Below is simplified, directly runnable code where you can customize the margin increments for the top, bottom, left, and right sides.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;spire.pdf.common&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SizeF&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;spire.pdf&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;PdfDocument&lt;/span&gt;

&lt;span class="c1"&gt;# 1. Load the original PDF document
&lt;/span&gt;&lt;span class="n"&gt;original_pdf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PdfDocument&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;original_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sample.pdf&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# 2. Customize the margins to be added on all four sides
&lt;/span&gt;&lt;span class="n"&gt;margin_top&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;
&lt;span class="n"&gt;margin_bottom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;
&lt;span class="n"&gt;margin_left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;
&lt;span class="n"&gt;margin_right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;

&lt;span class="c1"&gt;# 3. Calculate the new page size based on the first page size (applicable to all pages)
&lt;/span&gt;&lt;span class="n"&gt;first_page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;original_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;new_page_width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first_page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Size&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Width&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;margin_left&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;margin_right&lt;/span&gt;
&lt;span class="n"&gt;new_page_height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first_page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Size&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Height&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;margin_top&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;margin_bottom&lt;/span&gt;
&lt;span class="n"&gt;new_page_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SizeF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_page_width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new_page_height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# 4. Create a new PDF document and redraw all pages
&lt;/span&gt;&lt;span class="n"&gt;new_pdf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PdfDocument&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;page_idx&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# Generate a template of the original page, preserving the complete content
&lt;/span&gt;    &lt;span class="n"&gt;page_template&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;original_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;page_idx&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nc"&gt;CreateTemplate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# Add a new page with the custom size
&lt;/span&gt;    &lt;span class="n"&gt;new_page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_page_size&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Draw the original content at the origin of the new page, automatically generating uniform whitespace
&lt;/span&gt;    &lt;span class="n"&gt;page_template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Draw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# 5. Save the file and release resources
&lt;/span&gt;&lt;span class="n"&gt;new_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;increase_pdf_margins.pdf&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;original_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;new_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PDF margin increase completed!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Core explanation&lt;/strong&gt; : By adding the four margin values to the original page width and height, the page size is expanded. The original content is fully preserved at the top-left corner of the page, and the remaining area automatically forms uniform blank margins.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Decrease PDF Page Margins in Python
&lt;/h2&gt;

&lt;p&gt;This scenario is commonly used to remove excess default blank edges from PDFs, compress the visible document size, and make the content display close to the edges. The core is to shrink the page size while offsetting the content drawing coordinates in the opposite direction to crop excess whitespace.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;spire.pdf.common&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;SizeF&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PdfMargins&lt;/span&gt;
&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;spire.pdf&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;PdfDocument&lt;/span&gt;

&lt;span class="c1"&gt;# 1. Load the original PDF document
&lt;/span&gt;&lt;span class="n"&gt;original_pdf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PdfDocument&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;original_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;sample.pdf&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# 2. Customize the blank margins to be cropped on all four sides
&lt;/span&gt;&lt;span class="n"&gt;reduce_top&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;20.0&lt;/span&gt;
&lt;span class="n"&gt;reduce_bottom&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;20.0&lt;/span&gt;
&lt;span class="n"&gt;reduce_left&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;20.0&lt;/span&gt;
&lt;span class="n"&gt;reduce_right&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;20.0&lt;/span&gt;

&lt;span class="c1"&gt;# 3. Calculate the new page size after cropping
&lt;/span&gt;&lt;span class="n"&gt;first_page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;original_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;new_page_width&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first_page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Size&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Width&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;reduce_left&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;reduce_right&lt;/span&gt;
&lt;span class="n"&gt;new_page_height&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;first_page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Size&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Height&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;reduce_top&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;reduce_bottom&lt;/span&gt;
&lt;span class="n"&gt;new_page_size&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SizeF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_page_width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;new_page_height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# 4. Create a new PDF document, redraw and crop page whitespace
&lt;/span&gt;&lt;span class="n"&gt;new_pdf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PdfDocument&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;page_idx&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nf"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;original_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;page_template&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;original_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pages&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;page_idx&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nc"&gt;CreateTemplate&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="c1"&gt;# Add a new page with no margins
&lt;/span&gt;    &lt;span class="n"&gt;new_page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;new_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_page_size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;PdfMargins&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
    &lt;span class="c1"&gt;# Offset the drawing coordinates in the opposite direction to crop the original blank edges
&lt;/span&gt;    &lt;span class="n"&gt;page_template&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Draw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;new_page&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;reduce_left&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;reduce_top&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# 5. Save the file and release resources
&lt;/span&gt;&lt;span class="n"&gt;new_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;decrease_pdf_margins.pdf&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;original_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;new_pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="nf"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;PDF margin cropping completed!&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Core explanation&lt;/strong&gt; : Subtract the whitespace size to be cropped from the page size, and at the same time offset the content drawing coordinates to the left and upward, so that the original page's edge whitespace extends beyond the new page range, thereby achieving the white edge cropping effect.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Key Notes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Unit description&lt;/strong&gt; : All margin values in the code use the standard PDF unit (points). 1 inch ≈ 72 points, and they can be freely converted and adjusted as needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multi-page adaptation&lt;/strong&gt; : The code uses a globally uniform margin rule, so all pages apply the same increase or decrease parameters, making it suitable for standardized document processing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Resource release&lt;/strong&gt; : The &lt;code&gt;Dispose()&lt;/code&gt; method must be called to release document resources and avoid memory usage buildup.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Parameter threshold&lt;/strong&gt; : When decreasing margins, the cropping value must not exceed the original page whitespace; otherwise, the content may be abnormally cropped.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Summary
&lt;/h2&gt;

&lt;p&gt;With Free Spire.PDF for Python, custom adjustment of PDF margins can be implemented in a minimal and efficient way, without relying on Adobe software or complex PDF parsing algorithms. The two sets of code provided in this article respectively cover the two high-frequency scenarios of &lt;strong&gt;increasing whitespace&lt;/strong&gt; and  &lt;strong&gt;cropping white edges&lt;/strong&gt; . The code is concise, with complete comments, and can be directly embedded into automated document processing scripts, office tools, and batch processing programs.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>python</category>
      <category>pdf</category>
    </item>
    <item>
      <title>How to Extract Text and Images from PowerPoint Using C#</title>
      <dc:creator>Jack9012 </dc:creator>
      <pubDate>Mon, 21 Sep 2026 01:38:20 +0000</pubDate>
      <link>https://dev.to/jack_du_64a902eb1614b3933/how-to-extract-text-and-images-from-powerpoint-using-c-558d</link>
      <guid>https://dev.to/jack_du_64a902eb1614b3933/how-to-extract-text-and-images-from-powerpoint-using-c-558d</guid>
      <description>&lt;p&gt;In development scenarios, we may encounter the need for &lt;strong&gt;PowerPoint document content parsing&lt;/strong&gt;, such as batch-extracting text from courseware and reports for archiving, or batch-exporting embedded images from presentations for asset organization.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fl3u8okaz6cxz96jum495.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fl3u8okaz6cxz96jum495.png" alt="Extract Text and Images from PowerPoint Using C#" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The native .NET framework does not provide a built-in API for directly parsing PowerPoint files. Manually working with the OpenXML format results in redundant code, complex logic, and a large number of format compatibility issues. Free Spire.Presentation for .NET is a lightweight, free PowerPoint parsing library that supports the entire .NET version range and can quickly implement core features such as text reading, image extraction, and document parsing for PowerPoint files, greatly reducing development costs.&lt;/p&gt;

&lt;p&gt;This article is based on .NET 10 syntax conventions and uses Free Spire.Presentation to implement two core features:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Batch-extract all text from every slide in a PowerPoint file and export it to a TXT file&lt;/li&gt;
&lt;li&gt;Batch-extract all embedded images from a PowerPoint file and save them as local PNG files&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Environment Preparation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Development Environment
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Development framework: .NET 6+&lt;/li&gt;
&lt;li&gt;Development tools: Visual Studio 2022 / Rider&lt;/li&gt;
&lt;li&gt;Target files: .pptx presentation documents (compatible with mainstream PowerPoint formats)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Installing the NuGet Dependency
&lt;/h3&gt;

&lt;p&gt;The project needs to reference the Free Spire.Presentation core package. Search for and install it in the NuGet Package Manager, or install it via the command line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight powershell"&gt;&lt;code&gt;&lt;span class="n"&gt;Install-Package&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nx"&gt;FreeSpire.Presentation&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This package is a free, open-source version that meets the development needs of daily non-commercial, batch PowerPoint parsing, with no forced watermark and no marketing bindings.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extracting All Text from a PowerPoint File
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Feature Description
&lt;/h3&gt;

&lt;p&gt;Traverse all slides and all shape components in the PowerPoint file, filter for text container components, read the text content paragraph by paragraph, and finally combine all content and export it to a local TXT document. The code is streamlined using minimalist .NET 10 features such as top-level statements and implicit namespaces, making it more concise and readable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complete Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Presentation&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;System.Text&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Initialize a PowerPoint document instance and load the target file&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Presentation&lt;/span&gt; &lt;span class="n"&gt;presentation&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;presentation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Island.pptx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Initialize the text concatenation container&lt;/span&gt;
&lt;span class="n"&gt;StringBuilder&lt;/span&gt; &lt;span class="n"&gt;textBuilder&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Iterate through all slides&lt;/span&gt;
&lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ISlide&lt;/span&gt; &lt;span class="n"&gt;slide&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;presentation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Slides&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Iterate through all components on a single slide&lt;/span&gt;
    &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;IShape&lt;/span&gt; &lt;span class="n"&gt;shape&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;slide&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Shapes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// Filter for text shape components and read paragraph text&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;shape&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="n"&gt;IAutoShape&lt;/span&gt; &lt;span class="n"&gt;autoShape&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;TextParagraph&lt;/span&gt; &lt;span class="n"&gt;paragraph&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;autoShape&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TextFrame&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Paragraphs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="n"&gt;textBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;AppendLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;paragraph&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Text&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Export the text to a local TXT file&lt;/span&gt;
&lt;span class="n"&gt;File&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteAllText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"ExtractText.txt"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;textBuilder&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ToString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation of the Core Logic
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Automatic resource release&lt;/strong&gt; : Uses the &lt;code&gt;using variable declaration&lt;/code&gt; syntax supported by .NET 10, eliminating the need to manually call Dispose. The PowerPoint file resources are automatically released after the program finishes executing, avoiding file locking and memory leaks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type pattern matching&lt;/strong&gt; : Uses &lt;code&gt;is variable declaration&lt;/code&gt; pattern matching to complete type checking and casting in one step, replacing the traditional redundant approach of first checking the type and then casting. This is the recommended optimal syntax in .NET 10.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Layered traversal logic&lt;/strong&gt; : The PowerPoint text storage hierarchy is &lt;code&gt;Slide -&amp;gt; Shape -&amp;gt; TextParagraph&lt;/code&gt;. Through this three-level traversal, all text on a page (titles, body text, and note text) can be precisely captured.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Batch text export&lt;/strong&gt; : Uses &lt;code&gt;StringBuilder&lt;/code&gt; to efficiently concatenate text, avoiding the performance overhead of frequent string concatenation, and finally writes everything to a local file in one go.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Extracting All Embedded Images from a PowerPoint File
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Feature Description
&lt;/h3&gt;

&lt;p&gt;Directly read the global image resource collection of the PowerPoint document, batch-traverse all embedded images, export them uniformly as PNG image files, and automatically name and archive them, making it suitable for batch asset extraction scenarios.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complete Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Presentation&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Presentation.Collections&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Initialize and load the PowerPoint document&lt;/span&gt;
&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Presentation&lt;/span&gt; &lt;span class="n"&gt;presentation&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="n"&gt;presentation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Template.pptx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Get the collection of all embedded images in the PowerPoint file&lt;/span&gt;
&lt;span class="n"&gt;ImageCollection&lt;/span&gt; &lt;span class="n"&gt;imageCollection&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;presentation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Images&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Iterate through the image collection and export in batches&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;imageCollection&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;++)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Name and save images by index to avoid duplicate file names&lt;/span&gt;
    &lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;savePath&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Combine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Presentation"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;$"Images&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;.png"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Create the storage directory (automatically created if it does not exist)&lt;/span&gt;
    &lt;span class="n"&gt;Directory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;CreateDirectory&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Path&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;GetDirectoryName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savePath&lt;/span&gt;&lt;span class="p"&gt;)!);&lt;/span&gt;

    &lt;span class="c1"&gt;// Save the image locally&lt;/span&gt;
    &lt;span class="n"&gt;imageCollection&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="n"&gt;Image&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Save&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;savePath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation of the Core Logic
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Global image reading&lt;/strong&gt; : Unlike traversing slides page by page, this directly obtains all embedded images in the document through &lt;code&gt;presentation.Images&lt;/code&gt;, eliminating the need to repeatedly traverse pages and resulting in higher execution efficiency.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Automatic directory creation&lt;/strong&gt; : Adds directory checking logic to automatically detect whether the image storage folder exists and create it if it does not, avoiding save failures caused by missing directories and improving code robustness.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;.NET 10 string optimization&lt;/strong&gt; : Uses string interpolation instead of traditional &lt;code&gt;string.Format&lt;/code&gt;, resulting in cleaner syntax and stronger readability. This is the recommended approach in newer versions of .NET.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Safe resource release&lt;/strong&gt; : The using syntax manages the lifecycle of the PowerPoint instance, ensuring that resources are completely released after the file has been read.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Key Considerations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;File format compatibility&lt;/strong&gt; : This component only supports parsing &lt;code&gt;.pptx&lt;/code&gt; files. Older &lt;code&gt;.ppt&lt;/code&gt; formats must be converted before parsing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Path conventions&lt;/strong&gt; : The code uses relative paths by default. You can change them to absolute paths according to business needs to adapt to different runtime environments such as servers and desktops.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Special content compatibility&lt;/strong&gt; : This solution can normally extract ordinary text and images. For WordArt, background fill images, and encrypted content, additional compatibility handling is required.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;.NET version compatibility&lt;/strong&gt; : The code in this article is based on minimalist .NET 10 syntax. If compatibility with lower .NET versions is needed, top-level statements can be changed to the traditional class and Main method style.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;With the FreeSpire.Presentation library, we can quickly implement batch extraction of PowerPoint text and images using minimalist .NET 10 code, completely avoiding the complex logic of native OpenXML parsing.&lt;/p&gt;

&lt;p&gt;The entire codebase is lightweight, free of redundancy, and does not depend on third-party software. It can be directly applied to business scenarios such as  &lt;strong&gt;batch document parsing, content archiving, asset extraction, and office automation&lt;/strong&gt; , making it an efficient solution for handling basic PowerPoint parsing needs on the .NET platform.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>softwaredevelopment</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Adding Custom Text to PDF Documents with Python</title>
      <dc:creator>Jack9012 </dc:creator>
      <pubDate>Thu, 17 Sep 2026 03:49:30 +0000</pubDate>
      <link>https://dev.to/jack_du_64a902eb1614b3933/adding-custom-text-to-pdf-documents-with-python-4omf</link>
      <guid>https://dev.to/jack_du_64a902eb1614b3933/adding-custom-text-to-pdf-documents-with-python-4omf</guid>
      <description>&lt;p&gt;When processing documents, it is often necessary to append text to existing PDF files for purposes such as supplementary notes, explanatory labels, watermark text, and page number annotations. Python boasts an extensive collection of PDF processing libraries. Among them, &lt;strong&gt;Free Spire.PDF for Python&lt;/strong&gt; stands out with its streamlined APIs, comprehensive style customization, and independence from local Adobe components. It enables efficient PDF text editing with support for personalized effects including custom fonts, colors, transparency, and text rotation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk5fy7jh40t7rm08206mt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fk5fy7jh40t7rm08206mt.png" alt="Python Add Text to PDF" width="800" height="350"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This tutorial provides an objective and complete walkthrough of adding text to PDFs using Free Spire.PDF, covering basic executable code and advanced style customization to meet diverse PDF text editing requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Environment Setup
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1.1 Library Installation
&lt;/h3&gt;

&lt;p&gt;First, install the Free Spire.PDF dependency via pip by running the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;spire-pdf-free
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This library supports Python 3.7 and above, and is cross-platform compatible with Windows, macOS and Linux. After installation, you may directly call its APIs to edit PDF files.&lt;/p&gt;

&lt;h3&gt;
  
  
  1.2 Overview of Core Modules
&lt;/h3&gt;

&lt;p&gt;Below is a breakdown of key modules used in this practical project and their core functionalities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;PdfDocument&lt;/strong&gt; : The core class for PDF file operations, responsible for loading, saving and closing documents&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Font-related classes&lt;/strong&gt; : &lt;code&gt;PdfCjkStandardFont&lt;/code&gt;, &lt;code&gt;PdfTrueTypeFont&lt;/code&gt;, &lt;code&gt;PdfFont&lt;/code&gt; — designed for CJK fonts, custom system fonts, and built-in standard PDF fonts respectively&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PdfSolidBrush &amp;amp; PdfRGBColor&lt;/strong&gt; : Used to configure text drawing colors and brush styles&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;PointF&lt;/strong&gt; : Defines the coordinate position for rendering text on PDF pages&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  2. Basic Implementation: Insert Plain Text into a PDF
&lt;/h2&gt;

&lt;p&gt;This basic workflow loads an existing PDF file and inserts custom text at a specified coordinate on a target page, alongside basic font and color configuration. Below is fully runnable code with detailed comments.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;spire.pdf&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;PdfDocument&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PdfFontStyle&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PdfSolidBrush&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PdfRGBColor&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PointF&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PdfCjkStandardFont&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PdfCjkFontFamily&lt;/span&gt;

&lt;span class="c1"&gt;# Initialize a PDF document object and load the local PDF file
&lt;/span&gt;&lt;span class="n"&gt;pdf&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PdfDocument&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;input.pdf&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;  

&lt;span class="c1"&gt;# Retrieve the first page of the PDF (index starts at 0)
&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pages&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get_Item&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 

&lt;span class="c1"&gt;# Configure CJK font: Monotype Hei Medium, size 16, bold weight
&lt;/span&gt;&lt;span class="n"&gt;font&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PdfCjkStandardFont&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PdfCjkFontFamily&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;MonotypeHeiMedium&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;16.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PdfFontStyle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Bold&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Create a brush and set text color to light orange
&lt;/span&gt;&lt;span class="n"&gt;brush&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PdfSolidBrush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PdfRGBColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;255&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;180&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; 

&lt;span class="c1"&gt;# Define coordinates for text rendering (unit: point)
&lt;/span&gt;&lt;span class="n"&gt;location&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PointF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;72.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;300.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Draw custom text at the specified position
&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DrawString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;This is newly added text.&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;font&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;brush&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;location&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Save the modified PDF file
&lt;/span&gt;&lt;span class="n"&gt;pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;AddedText.pdf&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# Close the document to release occupied resources
&lt;/span&gt;&lt;span class="n"&gt;pdf&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Close&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Core Workflow of the Code
&lt;/h3&gt;

&lt;p&gt;Load document → Select target page → Configure font and color styles → Set rendering coordinates → Draw text → Save file and release resources. The workflow is concise and free of redundant logic.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Advanced Style Customization
&lt;/h2&gt;

&lt;p&gt;Spire.PDF offers abundant text styling extensions to accommodate various layout scenarios, including multiple font types, custom colors, text transparency, and rotated text. Practical implementations of each feature are covered below.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.1 Multi-Font &amp;amp; Custom Color Configuration
&lt;/h3&gt;

&lt;p&gt;Three mainstream font initialization methods are available for CJK text, third-party system fonts, and built-in PDF standard fonts. Custom RGB colors of any shade are also supported.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Method 1: Load system TrueType font (Calibri), italic, size 16, embed font into PDF
&lt;/span&gt;&lt;span class="n"&gt;font1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PdfTrueTypeFont&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Calibri&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;16.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PdfFontStyle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Italic&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="bp"&gt;True&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Method 2: Use PDF built-in Times Roman standard font, italic, size 16
&lt;/span&gt;&lt;span class="n"&gt;font2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PdfFont&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;PdfFontFamily&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;TimesRoman&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;16.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;PdfFontStyle&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Italic&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Create a brush with forest green text color
&lt;/span&gt;&lt;span class="n"&gt;brush&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;PdfSolidBrush&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PdfRGBColor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;139&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Enabling &lt;strong&gt;font embedding&lt;/strong&gt; ensures consistent font rendering across all devices when opening the PDF, making it ideal for documents shared cross-platform.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.2 Adjust Text Transparency
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;SetTransparency&lt;/code&gt; canvas method creates semi-transparent text, commonly used for subtle watermarks and light annotations. The transparency value ranges from 0.0 (fully transparent) to 1.0 (fully opaque).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Set text to 40% opacity (transparency = 0.4)
&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;SetTransparency&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.4&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Render semi-transparent text
&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DrawString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Semi-transparent text&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;font&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;brush&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;PointF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;100.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;100.0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.3 Create Rotated Text
&lt;/h3&gt;

&lt;p&gt;You may rotate the canvas coordinate system to render text at any tilt angle, perfect for diagonal watermarks and marginal notes. Negative values rotate counterclockwise, while positive values rotate clockwise.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Rotate canvas 45 degrees counterclockwise
&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;RotateTransform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="c1"&gt;# Draw rotated text
&lt;/span&gt;&lt;span class="n"&gt;page&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;DrawString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;Rotated text&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;font&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;brush&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;PointF&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;100.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;100.0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Note: Canvas rotation applies globally. Reset canvas transformations after drawing if you need to revert to the default orientation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  4. Key Development Notes
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Resource Release&lt;/strong&gt; : Always call the &lt;code&gt;Close()&lt;/code&gt; method after completing PDF operations to free file handles and avoid file locks or memory leaks.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Coordinate System&lt;/strong&gt; : PDF coordinates use points as units, with the top-left corner of a page serving as the origin. Adjust rendering coordinates based on page dimensions as needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;CJK Text Compatibility&lt;/strong&gt; : Prioritize &lt;code&gt;PdfCjkStandardFont&lt;/code&gt; or system Chinese fonts for Chinese text to prevent garbled characters or missing font errors.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Combined Styling&lt;/strong&gt; : Transparency, rotation and font styles can be combined freely to build complex text display effects.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  5. Conclusion
&lt;/h2&gt;

&lt;p&gt;Spire.PDF for Python delivers lightweight yet powerful PDF text editing capabilities, enabling developers to implement core features including &lt;strong&gt;basic text insertion, multi-font compatibility, custom color schemes, transparent text and rotated text&lt;/strong&gt; with minimal code. Compared with traditional PDF libraries, its key advantages lie in intuitive APIs, full styling support, and native Chinese &amp;amp; English compatibility. It is well-suited for use cases such as automated PDF editing, batch document processing and watermark generation.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>python</category>
      <category>software</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Convert Excel to Markdown and Vice Versa in C#</title>
      <dc:creator>Jack9012 </dc:creator>
      <pubDate>Tue, 15 Sep 2026 01:54:16 +0000</pubDate>
      <link>https://dev.to/jack_du_64a902eb1614b3933/how-to-convert-excel-to-markdown-and-vice-versa-in-c-3bim</link>
      <guid>https://dev.to/jack_du_64a902eb1614b3933/how-to-convert-excel-to-markdown-and-vice-versa-in-c-3bim</guid>
      <description>&lt;p&gt;In document development and interface data synchronization workflows, bidirectional format conversion between Excel spreadsheets and Markdown tables is frequently required. Excel excels at data editing, statistical recording and archiving, while Markdown is ideal for document display, knowledge bases and README documentation. Enabling two-way conversion between them drastically boosts office and development efficiency.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0eefap6fhdlstd9pd14r.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F0eefap6fhdlstd9pd14r.png" alt="Convert Excel to Markdown and Markdown to Excel" width="800" height="333"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This tutorial leverages the Spire.XLS for .NET library to provide a complete, step-by-step pure C# implementation for &lt;strong&gt;Excel-to-Markdown&lt;/strong&gt; and &lt;strong&gt;Markdown-to-Excel&lt;/strong&gt; conversion. The code is concise with no redundant logic, compatible with the full range of platforms including .NET Framework, .NET Core, and .NET 5/6/7/8.&lt;/p&gt;

&lt;p&gt;All code snippets in this tutorial contain no extraneous third-party dependencies or promotional logic and can be directly deployed into production projects.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Development Environment Setup
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1.1 Project Environment Requirements
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Development Tool: Visual Studio 2019 / 2022&lt;/li&gt;
&lt;li&gt;Runtime Platform: .NET Core 3.1 and above / .NET 5 and above / .NET Framework 4.0 and above&lt;/li&gt;
&lt;li&gt;Core Dependency: Spire.XLS for .NET&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1.2 Install the NuGet Dependency
&lt;/h3&gt;

&lt;p&gt;Spire.XLS is a professional Excel manipulation library that supports conversion between Excel and multiple formats such as Markdown, HTML and CSV. It operates independently without requiring Microsoft Office components to be installed.&lt;/p&gt;

&lt;p&gt;In Visual Studio, right-click your project → Manage NuGet Packages, then search for and install the package:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Spire.Xls&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Installation via the NuGet Package Manager Console is also supported.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Excel to Markdown Implementation
&lt;/h2&gt;

&lt;h3&gt;
  
  
  2.1 Fully Runnable Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Xls&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;ExcelToMarkdownDemo&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Initialize workbook object&lt;/span&gt;
            &lt;span class="n"&gt;Workbook&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Workbook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="c1"&gt;// Load local Excel file (supports .xls and .xlsx formats)&lt;/span&gt;
            &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Input.xlsx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Export Excel content to a Markdown file&lt;/span&gt;
            &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToMarkdown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"output.md"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Release resources to prevent memory leaks&lt;/span&gt;
            &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.2 Line-by-Line Code Explanation
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Workbook workbook = new Workbook()&lt;/code&gt;: Initializes the core Excel workbook object, which handles all file operations.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;workbook.LoadFromFile("Input.xlsx")&lt;/code&gt;: Reads a local Excel file; absolute and relative file paths are both supported, with compatibility for legacy .xls and modern .xlsx formats.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;workbook.SaveToMarkdown("output.md")&lt;/code&gt;: Core conversion method that automatically parses worksheet data, generates standard Markdown tables, and saves the result locally.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;workbook.Dispose()&lt;/code&gt;: Releases memory and file resources occupied by the workbook. This mandatory step prevents program memory overflow and persistent file locks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2.3 Conversion Output Overview
&lt;/h3&gt;

&lt;p&gt;Running the code generates an &lt;code&gt;output.md&lt;/code&gt; file automatically. All table headers and data rows from the source Excel file are mapped completely to Markdown table structures, with blank cells filled with appropriate placeholders. The standardized output works seamlessly with mainstream platforms including GitHub, Gitee and Yuque.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Markdown to Excel Full Implementation
&lt;/h2&gt;

&lt;p&gt;This feature supports reverse conversion of standard Markdown table files into Excel files, with built-in page adaptation configurations. The generated Excel files feature clean layouts optimized for printing and data editing.&lt;/p&gt;

&lt;h3&gt;
  
  
  3.1 Fully Runnable Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="nn"&gt;Spire.Xls&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nn"&gt;MarkdownToExcelDemo&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Program&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;Main&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Initialize workbook object&lt;/span&gt;
            &lt;span class="n"&gt;Workbook&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Workbook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

            &lt;span class="c1"&gt;// Load Markdown table file&lt;/span&gt;
            &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromMarkdown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Input.md"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Enable page auto-fit to adjust content to page width&lt;/span&gt;
            &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ConverterSetting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SheetFitToPage&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

            &lt;span class="c1"&gt;// Save as Excel 2016 format, compatible with most office software&lt;/span&gt;
            &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"output.xlsx"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ExcelVersion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Version2016&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// Release occupied resources&lt;/span&gt;
            &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3.2 Core Configuration &amp;amp; Code Breakdown
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;LoadFromMarkdown&lt;/code&gt;: Dedicated Markdown parsing method that accurately recognizes standard Markdown table syntax and maps content to Excel rows and columns automatically. Non-table Markdown content (headings, paragraphs, etc.) is ignored during parsing.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SheetFitToPage = true&lt;/code&gt;: Conversion optimization setting that resizes worksheet content to fit page dimensions, eliminating column overflow and improving readability and print quality.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;ExcelVersion.Version2016&lt;/code&gt;: Specifies the target Excel file version, offering maximum compatibility with Office 2016 and all later releases. This value can be replaced with &lt;code&gt;Version2013&lt;/code&gt;, &lt;code&gt;Version2019&lt;/code&gt;, etc., based on project requirements.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Project Compatibility &amp;amp; Optimization Tips
&lt;/h2&gt;

&lt;h3&gt;
  
  
  4.1 Using Absolute File Paths
&lt;/h3&gt;

&lt;p&gt;Absolute file paths are recommended for development and deployment to avoid missing file errors caused by relative path issues. Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;excelPath&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;@"D:\Files\Input.xlsx"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="n"&gt;mdPath&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="s"&gt;@"D:\Files\output.md"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4.2 Exception Handling Improvements
&lt;/h3&gt;

&lt;p&gt;Add exception handling in production environments to resolve errors such as missing files or invalid file formats:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;try&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Workbook&lt;/span&gt; &lt;span class="n"&gt;workbook&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;Workbook&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;LoadFromFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Input.xlsx"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;SaveToMarkdown&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"output.md"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="n"&gt;workbook&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;Dispose&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;FileNotFoundException&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"File not found: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;WriteLine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;$"Conversion failed: &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;ex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4.3 Multi-Worksheet Support
&lt;/h3&gt;

&lt;p&gt;Spire.XLS natively supports multi-worksheet conversion. Multiple worksheets from an Excel file are converted into segmented tables within the Markdown file in sequence. During reverse conversion, separate Markdown tables are imported into distinct Excel worksheets automatically.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Common Issues &amp;amp; Troubleshooting
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Distorted table formatting after conversion&lt;/strong&gt; : Ensure source files follow standard Excel/Markdown table syntax; remove merged cells and complex nested formatting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;File load failure prompts&lt;/strong&gt; : Verify the file path validity, check if the target file is locked by another process, and confirm the file uses a supported format.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Excessive memory consumption&lt;/strong&gt; : Always call the &lt;code&gt;Dispose()&lt;/code&gt; method to release resources. For batch conversion tasks, instantiate and dispose the workbook object for each individual conversion cycle.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  6. Conclusion
&lt;/h2&gt;

&lt;p&gt;The Spire.XLS for .NET library enables efficient bidirectional conversion between Excel and Markdown with minimal code, without requiring a local Microsoft Office installation. It is lightweight, cross-platform and high-performance. This solution fits a wide range of business scenarios including automated document generation, backend data export, and knowledge base format synchronization. The codebase is concise, stable, and ready for direct integration into production-grade projects.&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How to Parse Excel Data, Images, and Charts with Python</title>
      <dc:creator>Jack9012 </dc:creator>
      <pubDate>Thu, 10 Sep 2026 02:56:15 +0000</pubDate>
      <link>https://dev.to/jack_du_64a902eb1614b3933/how-to-parse-excel-data-images-and-charts-with-python-2f3c</link>
      <guid>https://dev.to/jack_du_64a902eb1614b3933/how-to-parse-excel-data-images-and-charts-with-python-2f3c</guid>
      <description>&lt;p&gt;Reading cell data, embedded images, and chart elements from Excel files is a common requirement in Python-based backend processing, automated report analysis, and data migration. Compared with conventional Excel processing libraries, the component introduced in this article provides a unified way to read text and numeric values, handle different cell data types, extract images, and export charts without requiring Microsoft Office or another desktop Office environment. This makes it suitable for server-side and automated processing scenarios.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff8uajp40ptdxpwxqme7s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ff8uajp40ptdxpwxqme7s.png" alt="Python Parse Excel" width="800" height="400"&gt;&lt;/a&gt;&lt;br&gt;
In this article, we will use practical Python code examples to demonstrate four core Excel parsing capabilities and explain the key implementation details behind each approach.&lt;/p&gt;

&lt;p&gt;The examples are based on  &lt;strong&gt;Free Spire.XLS for Python&lt;/strong&gt; , which provides a lightweight API for reading and writing Excel files. It supports parsing common elements in &lt;code&gt;.xlsx&lt;/code&gt; files and offers a simple API with relatively low resource overhead, making it suitable for automated Excel data processing.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Install the Required Component
&lt;/h2&gt;

&lt;p&gt;Free Spire.XLS for Python can be installed directly with &lt;code&gt;pip&lt;/code&gt; and integrated into a Python environment without additional system-level configuration. The following command installs the package:&lt;/p&gt;

&lt;h3&gt;
  
  
  Install with pip
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install spire.xls.free
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Installation Notes
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The package can be installed directly with &lt;code&gt;pip&lt;/code&gt; without manually configuring additional system dependencies.&lt;/li&gt;
&lt;li&gt;If multiple Python versions are installed on your system, use &lt;code&gt;pip3&lt;/code&gt; or &lt;code&gt;python -m pip&lt;/code&gt; as appropriate to ensure that the package is installed in the environment used by your application.&lt;/li&gt;
&lt;li&gt;Once installation is complete, import the library with &lt;code&gt;import spire.xls&lt;/code&gt;. No manual environment-variable configuration is required.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After installation, you can use the APIs described below to read Excel data, extract embedded images, and export charts.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Read Cell Data from a Worksheet
&lt;/h2&gt;

&lt;p&gt;For common Excel data-processing tasks, you can obtain the worksheet's allocated data range and then iterate through its rows and columns to read cell values in batches. Because this approach only processes the area occupied by data, it avoids unnecessarily scanning the entire worksheet and is suitable for automated table-data extraction.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from spire.xls import *

# Create a workbook and load the Excel file
wb = Workbook()
wb.LoadFromFile("Input.xlsx")

# Get the first worksheet
sheet = wb.Worksheets[0]

# Get the allocated data range
locatedRange = sheet.AllocatedRange

# Iterate through all cells and print their values
for i in range(len(locatedRange.Rows)):
    for j in range(len(locatedRange.Rows[i].Columns)):
        print(locatedRange[i + 1, j + 1].Value + "  ", end='')
    print("")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Key Details
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The &lt;code&gt;AllocatedRange&lt;/code&gt; property automatically identifies the used range of the worksheet, avoiding the overhead of iterating through unused rows and columns.&lt;/li&gt;
&lt;li&gt;Cell indexing starts at 1, which is consistent with Excel's row and column numbering and eliminates the need for additional index conversion.&lt;/li&gt;
&lt;li&gt;The nested loops iterate through the rows and columns of the allocated range and retrieve each cell's value while preserving the original table structure.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  3. Read Different Types of Cell Data
&lt;/h2&gt;

&lt;p&gt;Excel cells can contain text, numbers, formulas, dates, Boolean values, and other types of data. Using the general &lt;code&gt;Value&lt;/code&gt; property alone may not always provide enough control when you need to distinguish between these data types. Free Spire.XLS provides dedicated properties for retrieving specific types of cell content.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Get a cell by row and column
cell = sheet.Range[row, column]

# Read different types of cell data
text = cell.Text                    # Text content
number = cell.NumberValue           # Numeric value
formula = cell.Formula              # Original formula
formulaResult = cell.FormulaValue   # Calculated formula result
date = cell.DateTimeValue            # Date and time value
boolean = cell.BooleanValue          # Boolean value
value = cell.Value                   # General value
value2 = cell.Value2                 # Object-based value
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Key Details
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;For formula cells, &lt;code&gt;Formula&lt;/code&gt; and &lt;code&gt;FormulaValue&lt;/code&gt; allow you to retrieve the original formula and its calculated result separately. This is useful when you need to validate formulas or process their results.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;DateTimeValue&lt;/code&gt; provides the date and time represented by a cell in a form that can be processed directly, avoiding manual string parsing.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Value2&lt;/code&gt;returns the cell value as an object, preserving its underlying data type. For example, date and Boolean cells can be returned as&lt;code&gt;datetime&lt;/code&gt;and&lt;code&gt;bool&lt;/code&gt; objects, respectively.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Using these dedicated properties gives you more precise control over how different types of Excel data are handled in your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Extract Embedded Images from a Worksheet
&lt;/h2&gt;

&lt;p&gt;Excel worksheets often contain embedded images such as screenshots, product images, receipts, and other business-related resources. While some Excel libraries focus primarily on tabular data, Free Spire.XLS provides access to the images embedded in a worksheet.&lt;/p&gt;

&lt;p&gt;You can iterate through the worksheet's picture collection and save each image as a standard PNG file for further processing or local storage.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from spire.xls import *

workbook = Workbook()
workbook.LoadFromFile("Input.xlsx")
sheet = workbook.Worksheets[0]

# Iterate through all pictures in reverse order and save them
for i in range(sheet.Pictures.Count - 1, -1, -1):
    pic = sheet.Pictures[i]
    pic.Picture.Save(
        "ExtractImages\\Image-{0:d}.png".format(i),
        ImageFormat.get_Png()
    )
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Key Details
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The picture collection is traversed in reverse order, which can help avoid index-related issues when working with collections that may change during processing.&lt;/li&gt;
&lt;li&gt;You can customize the output directory and file naming convention. The example saves the extracted images as PNG files.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Pictures.Count&lt;/code&gt; returns the number of pictures contained in the worksheet, allowing you to determine how many embedded images need to be processed.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This approach is particularly useful when Excel files are used as containers for both structured data and image-based content.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Export Excel Charts as Images
&lt;/h2&gt;

&lt;p&gt;Excel files can contain various types of charts, including column charts, line charts, and pie charts. Chart elements cannot be retrieved through ordinary cell-value APIs. Instead, they can be rendered as images and exported for offline use, reporting, or further processing.&lt;/p&gt;

&lt;p&gt;Free Spire.XLS provides the &lt;code&gt;SaveChartAsImage&lt;/code&gt; method, which can be used to obtain chart images from a worksheet.&lt;/p&gt;

&lt;h3&gt;
  
  
  Code Example
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from spire.xls import *

workbook = Workbook()
workbook.LoadFromFile("Input.xlsx")
sheet = workbook.Worksheets[0]

# Convert all charts in the worksheet to image streams
image_streams = workbook.SaveChartAsImage(sheet)

# Save the chart images
for i, image_stream in enumerate(image_streams):
    image_stream.Save(f"Output/chart-{i}.png")

# Release workbook resources
workbook.Dispose()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Key Details
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;SaveChartAsImage&lt;/code&gt; retrieves the charts in the specified worksheet as image streams, so you do not need to locate and process each chart individually.&lt;/li&gt;
&lt;li&gt;Saving the results through image streams provides a convenient way to handle multiple charts and export them as image files.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Dispose()&lt;/code&gt; explicitly releases the resources associated with the workbook. This is especially important when processing a large number of Excel files to reduce the risk of excessive memory usage and file-locking issues.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  6. Summary
&lt;/h2&gt;

&lt;p&gt;In this article, we explored four common Excel parsing tasks with Python:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reading cell data from a worksheet&lt;/li&gt;
&lt;li&gt;Retrieving different types of cell values&lt;/li&gt;
&lt;li&gt;Extracting embedded images&lt;/li&gt;
&lt;li&gt;Exporting Excel charts as images&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The approach does not require Microsoft Office to be installed and can therefore be integrated into server-side scripts, automated data-processing workflows, data synchronization tools, and report-processing systems.&lt;/p&gt;

&lt;p&gt;The main advantage of this approach is that it provides a relatively lightweight API for handling both structured worksheet data and embedded visual elements. With support for multiple cell data types, image extraction, chart export, and explicit resource management, it can be used as part of automated workflows that process Excel files in batches.&lt;/p&gt;

</description>
      <category>programming</category>
      <category>python</category>
      <category>msexcel</category>
    </item>
    <item>
      <title>Find and Highlight Text in PDF Files with Python</title>
      <dc:creator>Jack9012 </dc:creator>
      <pubDate>Tue, 08 Sep 2026 02:06:12 +0000</pubDate>
      <link>https://dev.to/jack_du_64a902eb1614b3933/find-and-highlight-text-in-pdf-files-with-python-37m6</link>
      <guid>https://dev.to/jack_du_64a902eb1614b3933/find-and-highlight-text-in-pdf-files-with-python-37m6</guid>
      <description>&lt;p&gt;In document processing, data analysis, content review, and similar workflows, you may often need to search PDF files for specific text and highlight the matching content so that important information can be identified more quickly.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft8tskqbu42gx6b02tfab.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Ft8tskqbu42gx6b02tfab.png" alt="Automate PDF Text Highlighting" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Python provides a wide range of PDF processing libraries that make this kind of automation relatively easy to implement. In this article, we will introduce two practical approaches for finding and highlighting text in PDF files: exact text matching and regular-expression-based matching.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Library and Environment Setup
&lt;/h2&gt;

&lt;p&gt;In this tutorial, we will use &lt;strong&gt;Free Spire.PDF for Python&lt;/strong&gt; to search for text and add highlight annotations to PDF documents.&lt;/p&gt;

&lt;p&gt;The library provides convenient APIs for page traversal, exact text searching, regular expression matching, and custom highlight colors, so there is no need to manually work with low-level PDF structures.&lt;/p&gt;

&lt;p&gt;One limitation to keep in mind is that the free version supports PDF documents with up to  &lt;strong&gt;10 pages&lt;/strong&gt; . This is generally sufficient for lightweight testing, small documents, and simple document-processing tasks.&lt;/p&gt;

&lt;p&gt;Before getting started, install the required package with pip:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install spire.pdf.free
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  2. Scenario 1: Find and Highlight Exact Text Across a PDF
&lt;/h2&gt;

&lt;p&gt;This approach is suitable when you already know the exact keyword or phrase you want to locate. The program can iterate through every page in the PDF and highlight all occurrences of the target text.&lt;/p&gt;

&lt;p&gt;For example, the following code searches for the text &lt;code&gt;"cloud service"&lt;/code&gt; throughout the document and highlights every match.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complete Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from spire.pdf import *
from spire.pdf.common import *

# Create a PdfDocument object and load the PDF file
pdf = PdfDocument()
pdf.LoadFromFile("inpue.pdf")

# Iterate through all pages in the PDF document
for i in range(pdf.Pages.Count):
    page = pdf.Pages.get_Item(i)

    # Create a text finder for the current page
    pdfTextFinder = PdfTextFinder(page)

    # Set the search parameter to find exact matches
    pdfTextFinde.Options.Parameter = TextFindParameter.IgnoreCase

    # Find all occurrences of the target text on the page
    result = pdfTextFinder.Find("cloud service")

    # Highlight all matched text in cyan
    for find in result:
        find.HighLight(Color.get_Cyan())

# Save the processed PDF document
pdf.SaveToFile("output/result.pdf")

# Release document resources
pdf.Close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  How the Code Works
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Load the PDF document&lt;/strong&gt;
A &lt;code&gt;PdfDocument&lt;/code&gt; object is created, and the &lt;code&gt;LoadFromFile()&lt;/code&gt; method is used to open the source PDF.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterate through each page&lt;/strong&gt;
The program loops through all pages in the document to ensure that matching text is not missed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Search for the target text&lt;/strong&gt;
A &lt;code&gt;PdfTextFinder&lt;/code&gt; object is created for each page. The &lt;code&gt;Find()&lt;/code&gt; method performs an exact search and returns all matching text fragments.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Apply highlighting&lt;/strong&gt;
Each matching result is processed with the &lt;code&gt;HighLight()&lt;/code&gt; method. In this example, cyan is used as the highlight color, but it can be replaced with another supported color.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Save and close the document&lt;/strong&gt;
Finally, the modified PDF is saved to a new file, and &lt;code&gt;Close()&lt;/code&gt; is called to release the document resources.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  3. Scenario 2: Highlight Text Using Regular Expressions
&lt;/h2&gt;

&lt;p&gt;In many cases, the text you want to find does not have a fixed value but follows a consistent pattern.&lt;/p&gt;

&lt;p&gt;Typical examples include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Numbers&lt;/li&gt;
&lt;li&gt;Percentages&lt;/li&gt;
&lt;li&gt;Phone numbers&lt;/li&gt;
&lt;li&gt;Dates&lt;/li&gt;
&lt;li&gt;Email addresses&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For these situations, regular expressions provide a more flexible way to search for matching text.&lt;/p&gt;

&lt;p&gt;In the following example, we use a regular expression to find and highlight integers, decimal numbers, and percentages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complete Code
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from spire.pdf import *
from spire.pdf.common import *

# Create a PdfDocument object and load the PDF file
pdf = PdfDocument()
pdf.LoadFromFile("input.pdf")

# Get the first page of the PDF
# Change the page index if you want to process another page
page = pdf.Pages.get_Item(0)

# Create a text finder for the page
pdfTextFinder = PdfTextFinder(page)

# Enable regular expression matching
pdfTextFinder.Options.Parameter = TextFindParameter.Regex

# Regular expression for integers, decimals, and percentages
# Examples: 10, 99.9, 50%
pattern = r'\d+(?:\.\d+)?%?'

# Find all text fragments that match the pattern
result = pdfTextFinder.Find(pattern)

# Highlight the matched text in deep pink
for find in result:
    find.HighLight(Color.get_DeepPink())

# Save the processed PDF document
pdf.SaveToFile("output/result.pdf")

# Release document resources
pdf.Close()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Key Points
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Enable regular expression mode&lt;/strong&gt;
Set &lt;code&gt;Options.Parameter&lt;/code&gt; to &lt;code&gt;TextFindParameter.Regex&lt;/code&gt; to switch from standard text searching to regular-expression matching.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Define the matching pattern&lt;/strong&gt;
The regular expression:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;   \d+(?:\.\d+)?%?
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;matches several common numeric formats, including integers, decimal numbers, and percentages.&lt;/p&gt;

&lt;p&gt;You can replace it with another pattern to search for phone numbers, dates, email addresses, or other structured text.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Process a specific page&lt;/strong&gt;
This example searches only the first page of the PDF. If you need to search the entire document, you can combine this approach with the page loop used in the first example.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Customize the highlight color&lt;/strong&gt;
The highlight color can be changed by using another supported color value. This makes it possible to apply different visual styles for different types of matched content.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  4. Common Issues and Optimization Tips
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. The PDF Cannot Be Saved
&lt;/h3&gt;

&lt;p&gt;Make sure that the output directory already exists before saving the file.&lt;/p&gt;

&lt;p&gt;If the &lt;code&gt;output&lt;/code&gt; folder does not exist, Python may raise a file-path-related error.&lt;/p&gt;

&lt;p&gt;You can create the directory automatically with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os

os.makedirs("output", exist_ok=True)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. The PDF Exceeds the Page Limit
&lt;/h3&gt;

&lt;p&gt;The free version supports PDF documents with up to 10 pages.&lt;/p&gt;

&lt;p&gt;For longer documents, you can split the PDF into smaller files before performing the search and highlight operation.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Improving Search Accuracy
&lt;/h3&gt;

&lt;p&gt;By default, the search operation may match text fragments containing the specified keyword.&lt;/p&gt;

&lt;p&gt;If you need more precise matching, such as whole-word matching or case-sensitive searching, you can configure the corresponding options through the text finder's &lt;code&gt;Options&lt;/code&gt; settings.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Conclusion
&lt;/h2&gt;

&lt;p&gt;This article demonstrated two ways to find and highlight text in PDF documents with Python: exact text matching and regular-expression-based matching.&lt;/p&gt;

&lt;p&gt;Exact matching works well when the target keyword is already known, while regular expressions are more suitable for structured or variable content such as numbers, percentages, dates, and other formatted data.&lt;/p&gt;

&lt;p&gt;Both approaches require relatively little code and can be easily integrated into automated document-processing scripts. For small and short PDF files, they provide a straightforward way to identify important content, prepare documents for further data extraction, and reduce the amount of manual document review required.&lt;/p&gt;

</description>
      <category>automation</category>
      <category>python</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
