问题描述
我使用PDFsharp lib中的图像转换为PDF格式。我需要设置保证金和放大器;页面大小,所以我从这个论坛设置页面大小和保证金了一招。从这里我得到了我使用,但得到错误有两个区号。这里是代码,我得到了
页= document.AddPage();
//page.Size = PdfSharp.PageSize.A4;
XSize大小= PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
如果(page.Orientation == PageOrientation.Landscape)
{
page.Width = size.Height;
page.Height = size.Width;
}
,否则
{
page.Width = size.Width;
page.Height = size.Height;在点
}
//默认单元1英寸= 72点
page.TrimMargins.Top = 5;
page.TrimMargins.Right = 5;
page.TrimMargins.Bottom = 5;
page.TrimMargins.Left = 5;
我得到了一个错误此行
XSize大小= PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
所以我需要将其更改为
System.Drawing.Size大小= PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
现在我的程序编译,但是当我设置保证金,然后我得到的错误称为
PdfSharp不包含TrimMargins
这下面一行设置保证金不能编译。
的定义
pdfPage.TrimMargins.Top = 5;
pdfPage.TrimMargins.Right = 5;
pdfPage.TrimMargins.Bottom = 5;
pdfPage.TrimMargins.Left = 5;
我使用的是锋利的PDF库版本的 1.0.898.0
所以,指导我怎么可以设置保证金。
这是我完整的代码来生成图像文件pdf
公共静态字符串GeneratePdfFromImage(字符串源)
{
串destinaton = source.Replace(GIF, PDF格式);
PdfDocument DOC =新PdfDocument();
PdfPage pdfPage =新PdfPage();
System.Drawing.Size大小= PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
pdfPage.Orientation = PageOrientation.Portrait;
pdfPage.Width = size.Width;
pdfPage.Height = size.Height;
pdfPage.TrimMargins.Top = 5;
pdfPage.TrimMargins.Right = 5;
pdfPage.TrimMargins.Bottom = 5;
pdfPage.TrimMargins.Left = 5;
doc.Pages.Add(pdfPage);
XGraphics XGR = XGraphics.FromPdfPage(doc.Pages [0]);
XImage IMG = XImage.FromFile(源);
尝试
{
xgr.DrawImage(IMG,0,0);
doc.Save(destinaton);
doc.Close();
}
赶上(异常前)
{
destinaton =;
}
返回destinaton;
}
您不能设置页边距与PDFsharp - 这是给你预留当你画的项目在页面上边距
您复制的代码是从MigraDoc。 MigraDoc附带PDFsharp,但工作在更高的层次,你不跟几页内容,而不是你处理的部分,在这里你可以设置页边距。
请参阅网站对于PDFsharp和MigraDoc进一步的信息:结果
结果
有还展示了如何设置页面大小PDFsharp样本。
当您使用PDFsharp,你可以在任何地方在页面上绘制图像,你还可以指定的大小图像。
I am converting an image to pdf using PDFsharp lib. I need to set margin & page size so I got a trick from this forum to set page size and margin. From here I got code which I used but getting error for two area. Here is code which I got.
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;
I got an error for this line
XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
so i need to change it to
System.Drawing.Size size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
Now my program compiles but when I set margin then I am getting error calledPdfSharp does not contain a definition for TrimMargins
these below line does not compile for setting margin.
pdfPage.TrimMargins.Top = 5;
pdfPage.TrimMargins.Right = 5;
pdfPage.TrimMargins.Bottom = 5;
pdfPage.TrimMargins.Left = 5;
I am using the pdf sharp library version 1.0.898.0
So guide me how can I set margin.
Here is my full code to generate pdf from image file
public static string GeneratePdfFromImage(string source)
{
string destinaton = source.Replace("gif", "pdf");
PdfDocument doc = new PdfDocument();
PdfPage pdfPage = new PdfPage();
System.Drawing.Size size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
pdfPage.Orientation = PageOrientation.Portrait;
pdfPage.Width = size.Width;
pdfPage.Height = size.Height;
pdfPage.TrimMargins.Top = 5;
pdfPage.TrimMargins.Right = 5;
pdfPage.TrimMargins.Bottom = 5;
pdfPage.TrimMargins.Left = 5;
doc.Pages.Add(pdfPage);
XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
XImage img = XImage.FromFile(source);
try
{
xgr.DrawImage(img, 0, 0);
doc.Save(destinaton);
doc.Close();
}
catch (Exception ex)
{
destinaton = "";
}
return destinaton;
}
You cannot set margins with PDFsharp - it's up to you to reserve margins on the page when you draw items.
The code you copied is from MigraDoc. MigraDoc is included with PDFsharp, but works on a higher level where you do not deal with pages, instead you deal with sections and here you can set margins.
See the website for PDFsharp and MigraDoc for further information:
http://pdfsharp.net/
There also is a PDFsharp sample that shows how to set the page size.
When you use PDFsharp, you can draw images anywhere on the page and you also specify the size of the image.
这篇关于PDFsharp页面大小和设置保证金的问题C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!