我在我的 c# 项目中使用 PDFSharp。在我以前的工作场所,我使用了 aspose 的 PDF 组件,在该组件中,ASPOSE 提供了一个 xml 模板功能,我简单地以 xml 格式定义了 pdf 文档模板并将其保存到硬盘驱动器,然后从 c# 代码中打开该模板抽取我的数据,结果它正在编写 pdf。
它安静快速,我能够实现性能。
我可以使用 PDFSharp 或任何其他免费开源组件实现相同的目标吗?
我的意思是我想以 xml 格式定义我的 pdf 文档,并使用任何开放的优秀开源组件,如 pdfshpar 或 Itext 等。
请帮忙
谢谢
我知道如何显示页码以及如何在页脚中对齐它们。但是我的问题是我的页脚包含一些自定义文本,这些文本应该左对齐,页码应该与右角对齐。
string footer = "My custom footer";
Paragraph footerParagraph = section.Footers.Primary.AddParagraph(footer);
footerParagraph.AddTab();
footerParagraph.AddPageField();
Run Code Online (Sandbox Code Playgroud)
上面将为第 1 页生成“我的自定义页脚 1”,我需要页面 nmuber 位于页面的最右上角。我可以添加额外的空格或制表符,但我认为必须有一种干净的方法来实现这一点。谢谢。
我想在表格单元格中的某些文本的右侧浮动图像。
cell.AddParagraph("some text");
cell.AddParagraph("next line other text");
Paragraph p = cell.AddParagraph();
p.Format.Alignment = ParagraphAlignment.Right;
p.Format.RightIndent = 12;
Image image = p.AddImage("image.png");
image.ScaleWidth = 0.07;
Run Code Online (Sandbox Code Playgroud)
此代码将图像放在右侧,但我无法将其向上移动。
p.Format.Linespacing = -10;
image.WrapFormat.DistanceTop = -10;
Run Code Online (Sandbox Code Playgroud)
这些设置不起作用。
我正在尝试在 C# 的 PDFsharp 中将页面宽度和高度设置为 4x6。我正在使用以下内容:
page.Width = "4in"; page.Height = "6in";
仍然默认为 8X11 页面。
我之前使用 PDFsharp 进行了一些文件合并,现在我正在尝试更改多个文件(插入或删除一些页面),但我遇到了问题,即库看不到页面。它说 PageCount == 0 并且我在对象中找不到页面(在调试时)。当然,我不能做我现在的工作。我使用这个非常简单的代码:
var destinationPdf = new PdfDocument(destinationFilePath);
Int32 count = destinationPdf.PageCount;
Run Code Online (Sandbox Code Playgroud)
而且,这是我以前用来将文件合并为一个 PDF 的代码:
public class PdfCreator
{
private PdfDocument document;
public PdfCreator()
{
this.document = new PdfDocument();
}
public void AddImage(String imageFilePath)
{
PdfPage newPage = this.document.AddPage();
XGraphics xGraphics = XGraphics.FromPdfPage(newPage);
XImage image = XImage.FromFile(imageFilePath);
xGraphics.DrawImage(image, 0, 0);
}
public void AddPdfFile(String pdfFilePath)
{
PdfDocument inputDocument = PdfReader.Open(pdfFilePath, PdfDocumentOpenMode.Import);
Int32 count = inputDocument.PageCount;
for (Int32 currentPage = 0; currentPage < count; currentPage++)
{
PdfPage …Run Code Online (Sandbox Code Playgroud) 我只是在 Azure WebApplication 网站上使用 Arial 字体,但是当我到达这一行时:
MainFont = new XFont("Arial", FontSize);
Run Code Online (Sandbox Code Playgroud)
它抛出一个异常读数: Font data could not retrieved.
我原以为 Arial 会安装在服务器上……我还尝试将其更改为 Sans-Serif 以匹配 Microsoft 生成的网站的默认字体……但它仍然失败。
我也尝试将 Arial.ttf 添加到项目中,但没有奏效。
我正在使用PDFSharp在我的网络应用程序中动态生成PDF.Web应用程序在本地工作正常但是,当我将应用程序推送到Azure(作为Web应用程序)时,我得到以下异常:
{"message":"An error has occurred.","exceptionMessage":"Internal error. Font data could not retrieved.","exceptionType":"System.InvalidOperationException","stackTrace":" at PdfSharp.Fonts.OpenType.FontData.CreateGdiFontImage(XFont font, XPdfFontOptions options)\r\n at PdfSharp.Fonts.OpenType.FontData..ctor(XFont font, XPdfFontOptions options)\r\n at PdfSharp.Fonts.OpenType.OpenTypeDescriptor..ctor(XFont font, XPdfFontOptions options)\r\n at PdfSharp.Fonts.OpenType.OpenTypeDescriptor..ctor(XFont font)\r\n at PdfSharp.Fonts.FontDescriptorStock.CreateDescriptor(XFont font)\r\n at PdfSharp.Drawing.XFont.get_Metrics()\r\n at PdfSharp.Drawing.XFont.Initialize()\r\n at PdfSharp.Drawing.XFont..ctor(String familyName, Double emSize)\r\n at Spiro.Services.OrderService.GetOrderLabel(Int32 id, Nullable`1 quantity)\r\n at Spiro.Web.Controllers.WebApi.V1.OrderController.GetOrderLabel(Int32 id, Nullable`1 quantity)\r\n at lambda_method(Closure , Object , Object[] )\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.b__9(Object instance, Object[] methodParameters)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from …Run Code Online (Sandbox Code Playgroud) 我在我的本地机器上使用MigraDoc生成PDF文档.日文字符显示不正确.
我正在使用下面的说明生成带有Unicode的文档.
var document = new MigraDoc.DocumentObjectModel.Document();
//Do stuff...
PdfDocumentRenderer renderer = new PdfDocumentRenderer(true, PdfSharp.Pdf.PdfFontEmbedding.Always);
renderer.Document = document;
Run Code Online (Sandbox Code Playgroud)
这适用于某些非ASCII字符(强调字母),但不适用于日文字符.
从PDFsharp网站上的示例来看,我认为我应该使用PdfDocument(而不是Migradoc文档)对象,向该文档添加页面,并使用Migradoc对象填充页面
http://www.pdfsharp.net/wiki/MixMigraDocAndPdfSharp-sample.ashx
但是,我已经使用Migradoc创建了一个复杂的多页文档结构; 所以我希望能够按原样重复使用它(例如将它包含在一个PdfDocument中)
任何的想法?
谢谢
感谢您花时间阅读我关于 PDFSharp 的奇怪问题。
我当时使用 PDFSharp 库版本 1.50.4000.0(我从 1.3.2 更新并遇到同样的问题)用密码保护 PDF 文件。
该程序非常适合纵向文档,但有时我的文档具有混合方向。
但是,当横向页面位于文档中时,页面总是被剪切。
供您参考的代码:
PdfDocument document = PdfReader.Open(System.IO.Path.Combine("H:/Bloq1/", file.Name), "PasswordHere");
PdfSecuritySettings securitySettings = document.SecuritySettings;
securitySettings.OwnerPassword = "PasswordHere";
securitySettings.PermitAccessibilityExtractContent = false;
securitySettings.PermitAnnotations = false;
securitySettings.PermitAssembleDocument = false;
securitySettings.PermitExtractContent = false;
securitySettings.PermitFormsFill = true;
securitySettings.PermitFullQualityPrint = true;
securitySettings.PermitModifyDocument = false;
securitySettings.PermitPrint = true;
document.Save(System.IO.Path.Combine("H:/Bloq1/", file.Name));
tbx.Text = "Complete";
tbx.Background = Brushes.ForestGreen;
Run Code Online (Sandbox Code Playgroud)
提前感谢您阅读或回答这个问题 =D
*****************************解决了******************** ****************************
我切换到 iTextSharp 并测试了几个文档,效果很好,我将分享代码以供参考:
private void Button_Full(object sender, RoutedEventArgs e)//PROTEGE PDF PERMITIENDO IMPRESION
{
string Password = "password"; …Run Code Online (Sandbox Code Playgroud) 我正在尝试将创建的 PDF 文件保存到用户指定的位置。我正在使用 Visual Studio Community 2019。本质上,我正在截取此表单的屏幕截图:
通过使用 PdfSharp 外部库,我创建了一个 PDF 文件,然后将该 PDF 保存到用户指定的某个文件位置。以下是用户选择其首选文件位置的 UI:
一旦程序尝试将 PDF 文件保存到用户指定的位置,就会出现问题。这是我从 Visual Studio 收到的错误:
System.NotSupportedException:“没有可用于编码 1252 的数据。有关定义自定义编码的信息,请参阅 Encoding.RegisterProvider 方法的文档。”
我在网上查找了这个特定的错误,但我并不真正理解它,也不知道如何处理它,这非常令人困惑。在使用 Visual Basic 方面我还是个初学者。这是尝试执行此操作的代码:
Dim fileLocation As String
fileLocation = folderBrowseBox.Text
GetFormImage(True).Save(fileLocation & "\" & RemoveWhitespace(filename) & "_" & RemoveWhitespace(collectionPeriod) & ".jpg", ImageFormat.Jpeg)
' Create new pdf document and page
Dim doc As New PdfDocument()
Dim oPage As New PdfPage()
' Add the page to the pdf document and add the captured …Run Code Online (Sandbox Code Playgroud) 我需要在标题上将图像右对齐。这是我的代码:
Section section = document.AddSection();
table = section.Headers.Primary.AddTable();
var column = table.AddColumn("5cm");
column.Format.Alignment = ParagraphAlignment.Left;
column = table.AddColumn("8cm");
column.Format.Alignment = ParagraphAlignment.Left;
column = table.AddColumn("4cm");
column.Format.Alignment = ParagraphAlignment.Right;
eRow.Cells[0].AddParagraph("Some text");
eRow.Cells[1].AddParagraph("Some text");
eRow.Cells[2].Format.Alignment = ParagraphAlignment.Right;
image = eRow.Cells[2].Elements.AddImage(imagePath);
image.LockAspectRatio = false;
image.Width = Unit.FromInch(0.16);
image.Height = Unit.FromInch(0.09);
image.RelativeVertical = RelativeVertical.Line;
image.RelativeHorizontal = RelativeHorizontal.Margin;
image.Top = ShapePosition.Top;
image.Left = ShapePosition.Right;
image.WrapFormat.Style = WrapStyle.Through;
Run Code Online (Sandbox Code Playgroud)
但这总是在左边,请帮忙吗?
如何在截面区域中对齐表格?
MigraDoc.DocumentObjectModel.Tables.Table tFirma = section.Footers.Primary.AddTable();
MigraDoc.DocumentObjectModel.Tables.Column cFirma = tFirma.AddColumn(MigraDoc.DocumentObjectModel.Unit.FromCentimeter(3));//Tamaño de la coluna
MigraDoc.DocumentObjectModel.Tables.Row rFirma = tFirma.AddRow();
rFirma.Cells[0].Format.Alignment = MigraDoc.DocumentObjectModel.ParagraphAlignment.Center;
rFirma.Cells[0].AddParagraph("Employee Signature");
Run Code Online (Sandbox Code Playgroud)