标签: pdfsharp

PDFsharp页面大小和设置保证金问题c#

我正在使用PDFsharp lib将图像转换为pdf.我需要设置保证金和页面大小,所以我从这个论坛得到一个技巧来设置页面大小和页边距.从这里我得到了我用过的代码但是两个区域的错误.这是我得到的代码.

page = document.AddPage();
//page.Size = PdfSharp.PageSize.A4;
XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
if(page.Orientation == PageOrientation.Landscape)
{
   page.Width  = size.Height;
   page.Height = size.Width;
}
else
{
   page.Width  = size.Width;
   page.Height = size.Height;
}

// default unit in points 1 inch = 72 points
page.TrimMargins.Top = 5;
page.TrimMargins.Right = 5;
page.TrimMargins.Bottom = 5;
page.TrimMargins.Left = 5;
Run Code Online (Sandbox Code Playgroud)

这条线路出错了

XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
Run Code Online (Sandbox Code Playgroud)

所以我需要改变它

System.Drawing.Size size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
Run Code Online (Sandbox Code Playgroud)

现在我的程序编译,但当我设置边距然后我得到错误称为 PdfSharp不包含TrimMargins的定义

这些下面的行不能编译设置保证金.

    pdfPage.TrimMargins.Top = 5;
    pdfPage.TrimMargins.Right = 5;
    pdfPage.TrimMargins.Bottom = 5;
    pdfPage.TrimMargins.Left = 5;
Run Code Online (Sandbox Code Playgroud)

我使用的是pdf …

c# pdfsharp

5
推荐指数
1
解决办法
3万
查看次数

MigraDoc页脚位置

我试图将图像作为每页的页脚放置,可以使用以下方法将页脚移到页面底部:

doc.DefaultPageSetup.FooterDistance = "0cm"
Run Code Online (Sandbox Code Playgroud)

但是我似乎找不到任何方法可以缩短页面两侧的距离。有没有办法做到这一点或某种“技巧”来实现这一目标。

我能找到的唯一方法是删除页面设置边距,但是我需要它们将页面固定在一起。

有谁知道解决这个问题的方法?

c# vb.net pdfsharp migradoc

5
推荐指数
1
解决办法
3749
查看次数

How to position and center text in PDFsharp?

I need the "Job Setup Sheet" centered under the Heading. How should that be done?

Here is what I tried:

// Create an empty page
PdfPage page = document.AddPage();
page.Size = PageSize.Letter;
// Get an XGraphics object for drawing
XGraphics gfx = XGraphics.FromPdfPage(page);

//XPdfFontOptions options = new XPdfFontOptions(PdfFontEncoding.Unicode,
                                                PdfFontEmbedding.Always);

// Create a font
XFont HeadingFont = new XFont("Times New Roman", 20, XFontStyle.Bold);
XFont BodyFont = new XFont("Times New Roman", 12);
// Draw the text
gfx.DrawString("Texas Exterior Systems", HeadingFont, XBrushes.Black,
  new …
Run Code Online (Sandbox Code Playgroud)

c# pdfsharp

5
推荐指数
1
解决办法
8046
查看次数

如何使用PdfSharp库测量文本长度

我需要测量PDF中的文本长度,如果长度超过一定量,则换行.我已经在使用PdfSharp库了.

我已经使用以下代码来确定文本的长度.

public static Size MeasureString(string s, Font font)
{
    SizeF result;
    using (var image = new Bitmap(1, 1))
    {
          using (var g = Graphics.FromImage(image))
          {
              result = g.MeasureString(s, font);
          }
     }
     return result.ToSize();
}
Run Code Online (Sandbox Code Playgroud)

据我所知,我很依赖于分辨率和dpi转换Height和类的Width属性Size到毫米.但根据PdfSharp的团队在这篇文章中回答"PDF文件是没有DPI的矢量文件".

所以我对使用这个库测量文本长度的正确方法感到困惑.

pdf pdfsharp

5
推荐指数
1
解决办法
2759
查看次数

HTML到PDF导致横向

我正在使用PDFSharp从html源生成PDF.

PdfGenerator.GeneratePdf(html, PageSize.A4);
Run Code Online (Sandbox Code Playgroud)

生成pdf效果很好,但我不知道如何将页面方向更改为横向?

pdfsharp xhtmlrenderer

5
推荐指数
1
解决办法
2810
查看次数

使用PdfSharp从HTML代码段创建PDF,HTML中包含外部CSS类

我想知道我是否可以通过任何方式

使用PdfSharp从HTML代码段创建PDF having external CSS classes included in the HTML。

我找到了一种使用HTML内容生成pdf文件的方法,但是我的css类没有应用到PDF。

这是我为简单的html字符串执行的操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PdfSharp.Drawing;
using PdfSharp.Pdf;
using System.Drawing;
using TheArtOfDev.HtmlRenderer.PdfSharp;
using PdfSharp;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        PdfDocument pdf = PdfGenerator.GeneratePdf("<h1>tests</h1><p>test para</p>", PageSize.A4,20,null,null,null);
        pdf.Save(@"C\testPDF.pdf");

    }
}
Run Code Online (Sandbox Code Playgroud)

和它是如何生成的: 在此处输入图片说明

但是我想要包含这样的HTML字符串:

<h1>tests</h1><p class='para1'>test para</p>
Run Code Online (Sandbox Code Playgroud)

有什么办法可以做到这一点?使用不同的库不是问题,唯一的问题是所用的库应该是开源的。

html css c# asp.net pdfsharp

5
推荐指数
1
解决办法
8723
查看次数

iTextSharp处理PDF压缩吗?

iTextSharp可以压缩PDF文件吗?我正在寻找可用于开发以压缩PDF文件的PDF库.基本上,我有一个包含许多PDF文件的文件夹列表,大小从1MB到10MB不等,这些文件夹的数量每天都在增长,所以为了节省磁盘空间,我希望能够在PDF文件中读取一次它已被处理,压缩,然后将其保存到指定的文件夹位置.

如果iTextSharp不支持压缩,是否有人可以获得其他.NET PDF库的建议?购买图书馆不是问题.我查看了许多免费的内容,例如PDFSharp,我认为它在制作PDF方面非常好,但无法渲染或压缩它们.

我在Chris Haas的stackoverflow上读到了一个很好的答案:

PdfStamper是一个帮助器类,它最终使用另一个名为PdfStamperImp的类来完成大部分工作.PdfStamperImp派生自PdfWriter,当你使用stamper.Writer时,实际上你正在回到这个实现类.PdfStamper上的许多属性也直接传递给实现类.所以这两个电话实际上做同样的事情.

stamper.SetFullCompression();
stamper.Writer.SetFullCompression();

另一个混淆点是SetFullCompression和CompressionLevel实际上并不相关."完全压缩"表示在PDF 1.5中添加的称为"对象流"的功能,该功能允许将PDF对象组合在一起以潜在地允许更大的压缩.实际上并没有要求我们所谓的"压缩"实际发生,但实际上我认为它总会发生.(可能一个超级简单的文档可能会变大,启用此功能,不确定并且不想测试.)

CompressionLevel实际上是您通常认为的压缩,0到9之间的数字或-1表示默认值(目前等于六我认为).这个属性实际上是PdfStream类的一部分,许多类最终都是从这个类派生出来的.但是,此设置不会"涓滴".由于您是通过GetPageContent()和SetPageContent()从其他位置导入流,因此特定流具有与Writer的压缩设置无关的压缩设置.实际上有第三个参数可以传递给SetPageContent()来设置你想要的特定压缩级别.

reader.SetPageContent(1, reader.GetPageContent(1), PdfStream.BEST_COMPRESSION);

/sf/answers/1541960591/

任何帮助或建议将不胜感激.

谢谢.

c# compression pdf pdfsharp itextsharp

5
推荐指数
1
解决办法
1万
查看次数

PDFsharp水印

我正在创建一个应用程序,在用户选择的PDF上创建水印,我似乎无法在所选PDF上显示水印,但我也没有错误.任何帮助,将不胜感激.

我使用的是PDFsharp版本1.50.4000

public void WaterMarkPDF(string sourceFileName)
    {
        try
        {
            string watermark = "watermark";
            int emSize = 100;
            string file ="test.pdf";

            File.Copy(sourceFileName, file, true);
            File.SetAttributes(file, File.GetAttributes(file) & ~FileAttributes.ReadOnly);

            // Take in pdf from the form
            PdfDocument document = PdfReader.Open(file);

            // change the version cause sometimes newer versions break it
            if (document.Version < 14)
                document.Version = 14;

            XFont font = new XFont("Times New Roman", emSize, XFontStyle.BoldItalic);
            for (int idx = 0; idx < document.Pages.Count; idx++)
            {
                var page = document.Pages[idx];

                // Get …
Run Code Online (Sandbox Code Playgroud)

c# pdfsharp

5
推荐指数
1
解决办法
2067
查看次数

PdfSharpCore图像渲染问题

完全无法使用PdfSharpCore将JPEG图像渲染为PDF。

代码很简单

public byte[] GetPdfContent()
{
    ImageSource.ImageSourceImpl = new ImageSharpImageSource();

    var document = new PdfDocument();
    var logo = XImage.FromFile("logo.jpg");

    // this is test line, I'm saving XImage result and it is identical to the source file!
    File.WriteAllBytes("logo1.jpg", logo.AsJpeg().ToArray());

    var page = document.AddPage();
    var gfx = XGraphics.FromPdfPage(page);
    gfx.DrawImage(logo, 0, 0);

    using (var stream = new MemoryStream())
    {
         document.Save(stream, false);
         return stream.ToArray();
    }
}
Run Code Online (Sandbox Code Playgroud)

我知道ImageSource.ImageSourceImpl必须进行设置,并且已经将其设置为ImageSharp基于最简单的实现:ImageSource.ImageSourceImpl = new ImageSharpImageSource()这确实有效,因为XImage被logo1.jpg正确保存。

但是我的PDF在视觉上是空白的。二进制内容在那里,似乎所有格式属性都可以,但是二进制数据与源有些不同。

这是我的ImageSharp实现保存/加载图像的方式:

public class ImageSharpImageSource : …
Run Code Online (Sandbox Code Playgroud)

c# pdfsharp imagesharp

5
推荐指数
1
解决办法
864
查看次数

ZetPDF-有人知道该产品的背景吗?

问题:

ZetPDF只是变相的PDFSharp吗?

这个产品会留在身边吗?

谁是ZetPDF?


我们目前正在使用PDFSharp从头开始创建PDF。我们需要能够绘制特定区域,从嵌入式资源中添加图像,将pdf嵌入式资源用作新PDF的模板,添加条形码(即特定字体或图像),在确切位置添加内容,换行文字以及也可以进行文本流pdf(migradoc)。

PdfSharp可以完成所有这些事情,但是我想看看周围有没有其他选择,因为我对缺乏进展或PdfSharp的更新为零(而且它仍然是beta版)感到不安。我也想看看是否有支持.NET Core的产品。

因此,我最近发现了ZetPDF。 https://zetpdf.com/

它看起来与PdfSharp非常相似。

关于它的信息似乎很少。这个项目会继续进行并保持最新状态吗?有使用许可的费用,但是非常合理,但是当没有有关生产该产品的公司/团体的信息时,我为购买该产品感到不舒服。

与PdfSharp相似的示例(来自其示例-Unicode示例程序):

// The current implementation of ZetPDF is limited to left-to-right languages.
  // Languages like Arabic cannot yet be created even with Unicode fonts. Also the so called
  // CJK (Chinese, Japanese, Korean) support in PDF can also not be addressed with PDF sharp.
  // However, we plan to support as much as possible languages with ZetPDF. If you are a 
  // programmer and a native speaker of one …
Run Code Online (Sandbox Code Playgroud)

.net pdf pdfsharp

5
推荐指数
0
解决办法
553
查看次数

标签 统计

pdfsharp ×10

c# ×7

pdf ×3

.net ×1

asp.net ×1

compression ×1

css ×1

html ×1

imagesharp ×1

itextsharp ×1

migradoc ×1

vb.net ×1

xhtmlrenderer ×1