如何使用 PDFClown 添加图像?
How to add an Image with PDFClown?
我尝试使用 PDFClown 0.1.2.0 使用 C# 创建一个 pdf 文件,其中包含一个图像。
但我无法让它工作。
我得到的错误是:
An attempt was made to move the file pointer before the beginning of the file.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.SeekCore(Int64 offset, SeekOrigin origin)
at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)
at org.pdfclown.documents.contents.entities.JpegImage.Load()
at org.pdfclown.documents.contents.entities.Image.Get(Stream stream)
at org.pdfclown.documents.contents.entities.Image.Get(String path)
at TestPdf.MainForm.createPDF() in c:\Users\Demo\Documents\SharpDevelop Projects\TestPdf\TestPdf\MainForm.cs:line 43
出现此错误的代码行是:
org.pdfclown.documents.contents.entities.Image image = org.pdfclown.documents.contents.entities.Image.Get("test.jpg");
我用的"test.jpg"是:
我使用的代码是:
public void createPDF()
{
try {
org.pdfclown.files.File file = new org.pdfclown.files.File();
org.pdfclown.documents.Document document = file.Document;
org.pdfclown.documents.Page page = new org.pdfclown.documents.Page(document);
document.Pages.Add(page);
org.pdfclown.documents.contents.composition.PrimitiveComposer composer = new org.pdfclown.documents.contents.composition.PrimitiveComposer(page);
composer.SetFont(new org.pdfclown.documents.contents.fonts.StandardType1Font(document, org.pdfclown.documents.contents.fonts.StandardType1Font.FamilyEnum.Courier, true, false), 32);
composer.ShowText("Hello World!", new System.Drawing.PointF(32, 48));
org.pdfclown.documents.contents.entities.Image image = org.pdfclown.documents.contents.entities.Image.Get("test.jpg");
org.pdfclown.documents.contents.xObjects.XObject imageXObject = image.ToXObject(document);
composer.ShowXObject(imageXObject, new System.Drawing.PointF(32, 80));
composer.Flush();
file.Save("test.pdf", org.pdfclown.files.SerializationModeEnum.Incremental);
System.Diagnostics.Process.Start("explorer", System.IO.Directory.GetParent(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName).ToString());
} catch (System.Exception e) {
System.Windows.Forms.MessageBox.Show(e.Message + "\r\n" + e.StackTrace);
}
}
我做错了什么?
问题是,PdfClown 无法处理以渐进模式保存的 jpg 文件。解决问题最简单的方法是将jpg文件保存为非渐进式。
另一种方法是转换文件:
public void createPDF()
{
try {
org.pdfclown.files.File file = new org.pdfclown.files.File();
org.pdfclown.documents.Document document = file.Document;
org.pdfclown.documents.Page page = new org.pdfclown.documents.Page(document);
document.Pages.Add(page);
org.pdfclown.documents.contents.composition.PrimitiveComposer composer = new org.pdfclown.documents.contents.composition.PrimitiveComposer(page);
composer.SetFont(new org.pdfclown.documents.contents.fonts.StandardType1Font(document, org.pdfclown.documents.contents.fonts.StandardType1Font.FamilyEnum.Courier, true, false), 32);
composer.ShowText("Hello World!", new System.Drawing.PointF(32, 48));
org.pdfclown.documents.contents.entities.Image image = LoadImageFile("test.jpg");
org.pdfclown.documents.contents.xObjects.XObject imageXObject = image.ToXObject(document);
composer.ShowXObject(imageXObject, new System.Drawing.PointF(32, 80));
composer.Flush();
file.Save("test.pdf", org.pdfclown.files.SerializationModeEnum.Incremental);
System.Diagnostics.Process.Start("explorer", System.IO.Directory.GetParent(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName).ToString());
} catch (System.Exception e) {
System.Windows.Forms.MessageBox.Show(e.Message + "\r\n" + e.StackTrace);
}
}
public org.pdfclown.documents.contents.entities.Image LoadImageFile(string path)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
var ep = new System.Drawing.Imaging.EncoderParameters(3);
ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
ep.Param[1] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.ScanMethod, (int)System.Drawing.Imaging.EncoderValue.ScanMethodInterlaced);
ep.Param[2] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.RenderMethod, (int)System.Drawing.Imaging.EncoderValue.RenderNonProgressive);
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
System.Drawing.Imaging.ImageCodecInfo encoder_info = null;
int j;
System.Drawing.Imaging.ImageCodecInfo[] encoders;
encoders = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j) {
if (encoders[j].MimeType.Equals("image/jpeg"))
encoder_info = encoders[j];
}
image.Save(memStream, encoder_info, ep);
memStream.Position = 0;
return org.pdfclown.documents.contents.entities.Image.Get(memStream);
}
方法LoadImageFile
在C#中打开一个图像并将其转换为JPG非渐进式文件。
这个非渐进式jpg文件被保存到pdf文档中。
这个也适用于 png 和 tiff。
我尝试使用 PDFClown 0.1.2.0 使用 C# 创建一个 pdf 文件,其中包含一个图像。
但我无法让它工作。
我得到的错误是:
An attempt was made to move the file pointer before the beginning of the file.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.SeekCore(Int64 offset, SeekOrigin origin)
at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin)
at org.pdfclown.documents.contents.entities.JpegImage.Load()
at org.pdfclown.documents.contents.entities.Image.Get(Stream stream)
at org.pdfclown.documents.contents.entities.Image.Get(String path)
at TestPdf.MainForm.createPDF() in c:\Users\Demo\Documents\SharpDevelop Projects\TestPdf\TestPdf\MainForm.cs:line 43
出现此错误的代码行是:
org.pdfclown.documents.contents.entities.Image image = org.pdfclown.documents.contents.entities.Image.Get("test.jpg");
我用的"test.jpg"是:
我使用的代码是:
public void createPDF()
{
try {
org.pdfclown.files.File file = new org.pdfclown.files.File();
org.pdfclown.documents.Document document = file.Document;
org.pdfclown.documents.Page page = new org.pdfclown.documents.Page(document);
document.Pages.Add(page);
org.pdfclown.documents.contents.composition.PrimitiveComposer composer = new org.pdfclown.documents.contents.composition.PrimitiveComposer(page);
composer.SetFont(new org.pdfclown.documents.contents.fonts.StandardType1Font(document, org.pdfclown.documents.contents.fonts.StandardType1Font.FamilyEnum.Courier, true, false), 32);
composer.ShowText("Hello World!", new System.Drawing.PointF(32, 48));
org.pdfclown.documents.contents.entities.Image image = org.pdfclown.documents.contents.entities.Image.Get("test.jpg");
org.pdfclown.documents.contents.xObjects.XObject imageXObject = image.ToXObject(document);
composer.ShowXObject(imageXObject, new System.Drawing.PointF(32, 80));
composer.Flush();
file.Save("test.pdf", org.pdfclown.files.SerializationModeEnum.Incremental);
System.Diagnostics.Process.Start("explorer", System.IO.Directory.GetParent(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName).ToString());
} catch (System.Exception e) {
System.Windows.Forms.MessageBox.Show(e.Message + "\r\n" + e.StackTrace);
}
}
我做错了什么?
问题是,PdfClown 无法处理以渐进模式保存的 jpg 文件。解决问题最简单的方法是将jpg文件保存为非渐进式。
另一种方法是转换文件:
public void createPDF()
{
try {
org.pdfclown.files.File file = new org.pdfclown.files.File();
org.pdfclown.documents.Document document = file.Document;
org.pdfclown.documents.Page page = new org.pdfclown.documents.Page(document);
document.Pages.Add(page);
org.pdfclown.documents.contents.composition.PrimitiveComposer composer = new org.pdfclown.documents.contents.composition.PrimitiveComposer(page);
composer.SetFont(new org.pdfclown.documents.contents.fonts.StandardType1Font(document, org.pdfclown.documents.contents.fonts.StandardType1Font.FamilyEnum.Courier, true, false), 32);
composer.ShowText("Hello World!", new System.Drawing.PointF(32, 48));
org.pdfclown.documents.contents.entities.Image image = LoadImageFile("test.jpg");
org.pdfclown.documents.contents.xObjects.XObject imageXObject = image.ToXObject(document);
composer.ShowXObject(imageXObject, new System.Drawing.PointF(32, 80));
composer.Flush();
file.Save("test.pdf", org.pdfclown.files.SerializationModeEnum.Incremental);
System.Diagnostics.Process.Start("explorer", System.IO.Directory.GetParent(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName).ToString());
} catch (System.Exception e) {
System.Windows.Forms.MessageBox.Show(e.Message + "\r\n" + e.StackTrace);
}
}
public org.pdfclown.documents.contents.entities.Image LoadImageFile(string path)
{
System.Drawing.Image image = System.Drawing.Image.FromFile(path);
var ep = new System.Drawing.Imaging.EncoderParameters(3);
ep.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 100L);
ep.Param[1] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.ScanMethod, (int)System.Drawing.Imaging.EncoderValue.ScanMethodInterlaced);
ep.Param[2] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.RenderMethod, (int)System.Drawing.Imaging.EncoderValue.RenderNonProgressive);
System.IO.MemoryStream memStream = new System.IO.MemoryStream();
System.Drawing.Imaging.ImageCodecInfo encoder_info = null;
int j;
System.Drawing.Imaging.ImageCodecInfo[] encoders;
encoders = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();
for (j = 0; j < encoders.Length; ++j) {
if (encoders[j].MimeType.Equals("image/jpeg"))
encoder_info = encoders[j];
}
image.Save(memStream, encoder_info, ep);
memStream.Position = 0;
return org.pdfclown.documents.contents.entities.Image.Get(memStream);
}
方法LoadImageFile
在C#中打开一个图像并将其转换为JPG非渐进式文件。
这个非渐进式jpg文件被保存到pdf文档中。
这个也适用于 png 和 tiff。