public class PatchedLicense
{public static DateTime NewParseExact(string s, string format, IFormatProvider provider){return new DateTime(2099, 12, 30);}public static void Run(){MethodHookManager.Instance.AddHook(new MethodHook(typeof(DateTime).GetMethod(nameof(DateTime.ParseExact), BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(string), typeof(string), typeof(IFormatProvider) }, null),typeof(PatchedLicense).GetMethod(nameof(NewParseExact), BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(string), typeof(string), typeof(IFormatProvider) }, null)));MethodHookManager.Instance.StartHook();ActiveAspose();MethodHookManager.Instance.StopHook();}private static void ActiveAspose(){var licenseText = @"<License><Data><LicensedTo>KMD A/S</LicensedTo><EmailTo>iit-software@kmd.dk</EmailTo><LicenseType>Site OEM</LicenseType><LicenseNote>Up To 10 Developers And Unlimited Deployment Locations</LicenseNote><OrderID>220815085749</OrderID><UserID>324045</UserID><OEM>This is a redistributable license</OEM><Products><Product>Aspose.Total for .NET</Product></Products><EditionType>Enterprise</EditionType><SerialNumber>acea5afd-3c7d-4052-8991-f1e8522f63b4</SerialNumber><SubscriptionExpiry>20230818</SubscriptionExpiry><LicenseVersion>3.0</LicenseVersion><LicenseInstructions>https://purchase.aspose.com/policies/use-license</LicenseInstructions></Data><Signature>d6CNxPzdmeo0I8EJmarUMRizSisbxluOwz5BdYKprWEyJbqjvs93//lCgP0tNzxIzvniD9T7PefYeEtlkQoVKV9fo3pdjfh2QrWFxJZuRby9yzfTqK7Ahghj81URDTpneve+RAL3Z63bwkCNH0anWR0Z1I6Bdug5L8QZpduoS5k=</Signature></License>";var stream = new MemoryStream(Encoding.UTF8.GetBytes(licenseText));var license = new Aspose.Pdf.License();license.SetLicense(stream);}
}
public class PdfTools
{public static void Resolve(){// 输入与输出路径string inputPath = @"E:\zhaxg.pdf";string outputPath = @"E:\zhaxg_out.pdf";// 打开 PDF 文档using (var document = new Document(inputPath)){// 创建文本吸收器,查找目标文字var oldText = "河北99贸易有限公司";var absorber = new TextFragmentAbsorber(oldText);// 设置搜索选项(不区分大小写、全局搜索)absorber.TextSearchOptions = new TextSearchOptions(true);// 扫描所有页面document.Pages.Accept(absorber);// 获取搜索到的文字片段集合var fragments = absorber.TextFragments;Console.WriteLine($"找到 {fragments.Count} 个匹配项。");// 遍历每个匹配片段并替换foreach (TextFragment fragment in fragments){fragment.Text = "北京99软件技术有限公司";}// 搜索图片var img_absorber = new Aspose.Pdf.ImagePlacementAbsorber();document.Pages.Accept(img_absorber);var image = img_absorber.ImagePlacements.FirstOrDefault(x => x.Image.Height == 250);// 删除原来的二维码document.Pages[1].Resources.Images.Delete(((Aspose.Pdf.Operators.Do)image.Operator).Name);// 生成新的二维码var qrText = "https://www.baidu.com/";var qrGenerator = new QRCodeGenerator();var qrCodeData = qrGenerator.CreateQrCode(qrText, QRCodeGenerator.ECCLevel.Q);// 使用 PngByteQRCode 生成 PNG 字节数组var qrCode = new PngByteQRCode(qrCodeData);var qrBytes = qrCode.GetGraphic(pixelsPerModule: 2,darkColor: System.Drawing.Color.Black,lightColor: System.Drawing.Color.White);File.WriteAllBytes(@"E:\zhaxg.png", qrBytes);using var qrStream = new MemoryStream(qrBytes);document.Pages[1].AddImage(qrStream, image.Rectangle, 250, 250, true);// 保存结果document.Save(outputPath);Console.WriteLine("替换完成,文件已保存到: " + outputPath);}}
}