有没有办法用C#.net核心编辑pdf?
我想知道是否有任何方法可以使用c#.net core编辑pdf,只需替换一些单词提供了两种替换页面上静态文本的选项:有没有办法用C#.net核心编辑pdf?,.net,pdf,.net-core,.net-standard,.net,Pdf,.net Core,.net Standard,我想知道是否有任何方法可以使用c#.net core编辑pdf,只需替换一些单词提供了两种替换页面上静态文本的选项: 复制页面对象并更改特定的文本块。基本样本: 在drawText方法中,您可以检查PdfTextData.Text值并绘制另一个文本(替换target.DrawString(td.GetCharacterCodes());调用) 例如,此代码将第一页上出现的所有“have”替换为“has”: 使用系统诊断; 命名空间BitMiracle.Docotic.Pdf.Samples {
drawText
方法中,您可以检查PdfTextData.Text值并绘制另一个文本(替换target.DrawString(td.GetCharacterCodes());
调用)
例如,此代码将第一页上出现的所有“have”替换为“has”:
使用系统诊断;
命名空间BitMiracle.Docotic.Pdf.Samples
{
公共静态类CopyPageObjects
{
公共静态void Main()
{
//注:
//在试用模式下使用时,库会施加一些限制。
//请访问http://bitmiracle.com/pdf-library/trial-restrictions.aspx
//了解更多信息。
常量字符串PathToFile=“CopyPageObjects.pdf”;
使用(var pdf=新的PdfDocument(@“C:\Work\bitmracle\Docotic\QPdf\Futura\u font.pdf”))
{
使用(PdfDocument copy=pdf.CopyPages(0,1))
{
PdfPage sourcePage=copy.Pages[0];
PdfPage copyPage=copy.AddPage();
copyPage.Rotation=sourcePage.Rotation;
copyPage.MediaBox=sourcePage.MediaBox;
if(sourcePage.CropBox!=sourcePage.MediaBox)
copyPage.CropBox=sourcePage.CropBox;
PdfCanvas target=copyPage.Canvas;
foreach(sourcePage.GetObjects()中的PdfPageObject obj)
{
target.SaveState();
setClipRegion(目标,对象ClipRegion);
if(obj.Type==PdfPageObjectType.Path)
{
PdfPath path=(PdfPath)obj;
目标转换(路径转换矩阵);
if(path.PaintMode==PdfDrawMode.Fill | | path.PaintMode==PdfDrawMode.FillAndStroke)
挫折(target.Brush,path.Brush);
if(path.PaintMode==PdfDrawMode.Stroke | | path.PaintMode==PdfDrawMode.FillAndStroke)
setPen(target.Pen,path.Pen);
追加路径(目标,路径);
绘制路径(目标、路径);
}
else if(obj.Type==PdfPageObjectType.Image)
{
PDFPaintedImageImage=(PDFPaintedImageOBJ);
target.TranslateTransform(image.Position.X,image.Position.Y);
目标.变换(图像.变换矩阵);
挫折(target.Brush,image.Brush);
target.DrawImage(image.image,0,0,0);
}
else if(obj.Type==PdfPageObjectType.Text)
{
PdfTextData text=(PdfTextData)obj;
drawText(目标,文本);
}
target.RestoreState();
}
复制。删除页面(0);
复制.保存(路径文件);
}
}
进程启动(PathToFile);
}
私有静态void setClipRegion(PdfCanvas画布、PdfClipRegion clipRegion)
{
if(clipRegion.IntersectedPaths.Count==0)
返回;
PdfMatrix transformationBefore=canvas.TransformationMatrix;
尝试
{
foreach(clipRegion.intersectedpath中的PdfPath clipPath)
{
canvas.ResetTransform();
canvas.Transform(clipPath.TransformationMatrix);
appendPath(画布、clipPath);
SetClip(clipPath.ClipMode.Value);
}
}
最后
{
canvas.ResetTransform();
canvas.Transform(transformationBefore);
}
}
专用静态无效收进(PdfBrush dst、PdfBrushInfo src)
{
PdfColor color=src.color;
如果(颜色!=null)
颜色=颜色;
dst.Opacity=src.Opacity;
var模式=src.模式;
if(模式!=null)
模式=模式;
}
专用静态无效设置笔(PdfPen dst、PdfPenInfo src)
{
PdfColor color=src.color;
如果(颜色!=null)
颜色=颜色;
var模式=src.模式;
if(模式!=null)
模式=模式;
dst.DashPattern=src.DashPattern;
dst.EndCap=src.EndCap;
dst.LineJoin=src.LineJoin;
dst.MiterLimit=src.MiterLimit;
dst.Opacity=src.Opacity;
dst.宽度=src.宽度;
}
专用静态路径(PdfCanvas目标,PdfPath路径)
{
foreach(path.subpath中的PdfSubpath子路径)
{
foreach(子路径段中的PdfPathSegment段)
{
开关(段类型)
{
案例PdfPathSegmentType。要点:
target.CurrentPosition=((PdfPointSegment)段).Value;
打破
案例PdfPathSegmentType.行:
PdfLineSegment行=(PdfLineSegment)段;
target.CurrentPosition=line.Start;
target.AppendLineTo(line.End);
打破
案例Pd
using System.IO;
using SautinSoft.Document;
using System.Linq;
using System.Text.RegularExpressions;
namespace Sample
{
class Sample
{
static void Main(string[] args)
{
FindAndReplace();
}
/// <summary>
/// Find and replace a text using ContentRange.
/// </summary>
/// <remarks>
/// Details: https://sautinsoft.com/products/document/examples/find-replace-content-net-csharp-vb.php
/// </remarks>
public static void FindAndReplace()
{
// Path to a loadable document.
string loadPath = @"..\..\critique.pdf";
// Load a document intoDocumentCore.
DocumentCore dc = DocumentCore.Load(loadPath);
Regex regex = new Regex(@"bean", RegexOptions.IgnoreCase);
//Find "Bean" and Replace everywhere on "Joker :-)"
// Please note, Reverse() makes sure that action replace not affects to Find().
foreach (ContentRange item in dc.Content.Find(regex).Reverse())
{
item.Replace("Joker");
}
// Save our document into PDF format.
string savePath = "Replaced.pdf";
dc.Save(savePath, new PdfSaveOptions());
// Open the result for demonstration purposes.
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(loadPath) { UseShellExecute = true });
System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(savePath) { UseShellExecute = true });
}
}
}