获取 PDF 的原始大小
Get original Size of PDF
我想获得与使用 Adobe Acrobat 将 pdf 导出为 png 时相同的图像质量。
但不知何故这对我不起作用。如果我在 Adobe Acrobat 工具的帮助下将 pdf 导出为 png,我得到的尺寸是:
宽度:11264 像素
Height:15940像素
如您所见,我向您提供了我用来阅读 pdf 并为每页创建图像的方法。我的可能性是使用 .Render 方法,它需要一个 int page(index) float dpiX, float dpiY, bool forPrinting
但是有些怎么对保存的图片没有影响呢?
using (var document = PdfiumViewer.PdfDocument.Load(file))
{
//This int is used to get the page count for each document
int pagecount = document.PageCount;
//With the int pagecount we can create as may screenshots as there are pages in the document
for (int index = 0; index < pagecount; index++)
{
//render the image created
var image = document.Render(index,8448,11955, true);
//savde the created screenshot temporerlay as a png + number (count)
image.Save(@"C:\Users\chnikos\Desktop\Test\output" + index.ToString("000") + ".png",ImageFormat.Png);
application.Selection.InlineShapes.AddPicture(@"C:\Users\chnikos\Desktop\Test\output" + index.ToString("000") + ".png");
}
}
这可能是一个更好的例子:
Library files
using System.Drawing.Imaging;
string Desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
//This one is with Spire.Pdf
void PDF_To_Image(string pdf)
{
var doc = new Spire.Pdf.PdfDocument();
doc.LoadFromFile(pdf);
//This int is used to get the page count for each document
int pagecount = doc.Pages.Count;
//With the int pagecount we can create as many screenshots as there are pages in the document
for (int index = 0; index < pagecount; index++)
{
//render the image created
var dpiX = 75;
var dpiY = 75;
var image = doc.SaveAsImage(index, Spire.Pdf.Graphics.PdfImageType.Bitmap, dpiX, dpiY);
//save the created screenshot temporerlay as a png + number (count)
image.Save(Desktop + @"\Test\output\" + index.ToString("000") + ".png", ImageFormat.Png);
}
}
//This one is with Aspose.Pdf & xps2img.
//Aspose.Pdf converts the Pdf document to Xps format..
//xps2img creates images from the xps file..
void PDF_To_Image2(string pdf)
{
var doc = new Aspose.Pdf.Document(pdf);
var saveOptions = new Aspose.Pdf.XpsSaveOptions();
doc.Save("Preview.xps", saveOptions);
xps2img.Parameters pp = new xps2img.Parameters();
pp.Dpi = 300;
pp.ImageType = xps2img.ImageType.Png;
pp.RequiredSize = new System.Drawing.Size((int)doc.PageInfo.Width, (int)doc.PageInfo.Height);
pp.ImageOptions = xps2img.ImageOptions.Default;
var img3 = xps2img.Xps2Image.ToBitmap("Preview.xps", pp).ToList();
//This int is used to get the page count for each document
int pagecount = img3.Count;
//With the int pagecount we can create as many screenshots as there are pages in the document
for (int index = 0; index < pagecount; index++)
{
img3[index].Save(Desktop + @"\Test\Output\" + index.ToString("000") + ".png", ImageFormat.Png);
}
}
使用 PDFSharp 和 Migradocs
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Create a font
XFont font = new XFont("Times", 25, XFontStyle.Bold);
PageSize[] pageSizes = (PageSize[])Enum.GetValues(typeof(PageSize));
foreach (PageSize pageSize in pageSizes)
{
if (pageSize == PageSize.Undefined)
continue;
// One page in Portrait...
PdfPage page = document.AddPage();
page.Size = pageSize;
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.DrawString(pageSize.ToString(), font, XBrushes.DarkRed,
new XRect(0, 0, page.Width, page.Height),
XStringFormat.Center);
// ... and one in Landscape orientation.
page = document.AddPage();
page.Size = pageSize;
page.Orientation = PageOrientation.Landscape;
gfx = XGraphics.FromPdfPage(page);
gfx.DrawString(pageSize.ToString() + " (landscape)", font,
XBrushes.DarkRed, new XRect(0, 0, page.Width, page.Height),
XStringFormat.Center);
}
// Save the document...
string filename = "PageSizes.pdf";
document.Save(filename);
// ...and start a viewer.
Process.Start(filename);
try
{
using (var document = PdfiumViewer.PdfDocument.Load(@"C:\Users\ohernandez\Pictures\img\descarga.pdf"))
{
for (int index = 0; index < document.PageCount; index++)
{
var image = document.Render(index, 300, 300, PdfRenderFlags.CorrectFromDpi);
image.Save(@"C:\Users\ohernandez\Pictures\img\output.Jpeg" + index.ToString("000") + ".Jpeg", ImageFormat.Jpeg);
}
// var image = document.Render(0, 300, 300, true);
// image.Save(@"C:\Users\ohernandez\Pictures\img\output.png", ImageFormat.Png);
}
}
catch (Exception ex)
{
// handle exception here;
}
我想获得与使用 Adobe Acrobat 将 pdf 导出为 png 时相同的图像质量。
但不知何故这对我不起作用。如果我在 Adobe Acrobat 工具的帮助下将 pdf 导出为 png,我得到的尺寸是: 宽度:11264 像素 Height:15940像素
如您所见,我向您提供了我用来阅读 pdf 并为每页创建图像的方法。我的可能性是使用 .Render 方法,它需要一个 int page(index) float dpiX, float dpiY, bool forPrinting
但是有些怎么对保存的图片没有影响呢?
using (var document = PdfiumViewer.PdfDocument.Load(file))
{
//This int is used to get the page count for each document
int pagecount = document.PageCount;
//With the int pagecount we can create as may screenshots as there are pages in the document
for (int index = 0; index < pagecount; index++)
{
//render the image created
var image = document.Render(index,8448,11955, true);
//savde the created screenshot temporerlay as a png + number (count)
image.Save(@"C:\Users\chnikos\Desktop\Test\output" + index.ToString("000") + ".png",ImageFormat.Png);
application.Selection.InlineShapes.AddPicture(@"C:\Users\chnikos\Desktop\Test\output" + index.ToString("000") + ".png");
}
}
这可能是一个更好的例子: Library files
using System.Drawing.Imaging;
string Desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
//This one is with Spire.Pdf
void PDF_To_Image(string pdf)
{
var doc = new Spire.Pdf.PdfDocument();
doc.LoadFromFile(pdf);
//This int is used to get the page count for each document
int pagecount = doc.Pages.Count;
//With the int pagecount we can create as many screenshots as there are pages in the document
for (int index = 0; index < pagecount; index++)
{
//render the image created
var dpiX = 75;
var dpiY = 75;
var image = doc.SaveAsImage(index, Spire.Pdf.Graphics.PdfImageType.Bitmap, dpiX, dpiY);
//save the created screenshot temporerlay as a png + number (count)
image.Save(Desktop + @"\Test\output\" + index.ToString("000") + ".png", ImageFormat.Png);
}
}
//This one is with Aspose.Pdf & xps2img.
//Aspose.Pdf converts the Pdf document to Xps format..
//xps2img creates images from the xps file..
void PDF_To_Image2(string pdf)
{
var doc = new Aspose.Pdf.Document(pdf);
var saveOptions = new Aspose.Pdf.XpsSaveOptions();
doc.Save("Preview.xps", saveOptions);
xps2img.Parameters pp = new xps2img.Parameters();
pp.Dpi = 300;
pp.ImageType = xps2img.ImageType.Png;
pp.RequiredSize = new System.Drawing.Size((int)doc.PageInfo.Width, (int)doc.PageInfo.Height);
pp.ImageOptions = xps2img.ImageOptions.Default;
var img3 = xps2img.Xps2Image.ToBitmap("Preview.xps", pp).ToList();
//This int is used to get the page count for each document
int pagecount = img3.Count;
//With the int pagecount we can create as many screenshots as there are pages in the document
for (int index = 0; index < pagecount; index++)
{
img3[index].Save(Desktop + @"\Test\Output\" + index.ToString("000") + ".png", ImageFormat.Png);
}
}
使用 PDFSharp 和 Migradocs
// Create a new PDF document
PdfDocument document = new PdfDocument();
// Create a font
XFont font = new XFont("Times", 25, XFontStyle.Bold);
PageSize[] pageSizes = (PageSize[])Enum.GetValues(typeof(PageSize));
foreach (PageSize pageSize in pageSizes)
{
if (pageSize == PageSize.Undefined)
continue;
// One page in Portrait...
PdfPage page = document.AddPage();
page.Size = pageSize;
XGraphics gfx = XGraphics.FromPdfPage(page);
gfx.DrawString(pageSize.ToString(), font, XBrushes.DarkRed,
new XRect(0, 0, page.Width, page.Height),
XStringFormat.Center);
// ... and one in Landscape orientation.
page = document.AddPage();
page.Size = pageSize;
page.Orientation = PageOrientation.Landscape;
gfx = XGraphics.FromPdfPage(page);
gfx.DrawString(pageSize.ToString() + " (landscape)", font,
XBrushes.DarkRed, new XRect(0, 0, page.Width, page.Height),
XStringFormat.Center);
}
// Save the document...
string filename = "PageSizes.pdf";
document.Save(filename);
// ...and start a viewer.
Process.Start(filename);
try
{
using (var document = PdfiumViewer.PdfDocument.Load(@"C:\Users\ohernandez\Pictures\img\descarga.pdf"))
{
for (int index = 0; index < document.PageCount; index++)
{
var image = document.Render(index, 300, 300, PdfRenderFlags.CorrectFromDpi);
image.Save(@"C:\Users\ohernandez\Pictures\img\output.Jpeg" + index.ToString("000") + ".Jpeg", ImageFormat.Jpeg);
}
// var image = document.Render(0, 300, 300, true);
// image.Save(@"C:\Users\ohernandez\Pictures\img\output.png", ImageFormat.Png);
}
}
catch (Exception ex)
{
// handle exception here;
}