我当前的代码写了一个qr代码,但它只用qr代码写我的文件。我不知道如何调整qr代码的大小放在文档的一个角落而不是占用整个页面。还不确定RasterImage.Create
是否意味着它只用qr创建一个新文件并丢弃我的原始文件?
代码: - 将PDF转换为Bmp以添加QR,然后保存回PDF
public void PDFFileExample()
{
RasterCodecs codecs1 = new RasterCodecs();
codecs1.Options.Pdf.InitialPath = @"C:\LEADTOOLS 18\Bin\Dotnet4\Win32";
codecs1.Dispose();
RasterCodecs codecs2 = new RasterCodecs();
codecs2.ThrowExceptionsOnInvalidImages = true;
System.Diagnostics.Debug.Assert(codecs2.Options.Pdf.InitialPath == @"C:\LEADTOOLS 18\Bin\Dotnet4\Win32");
string pdfFile = @"C:\QRCodeTesting\bottomRight.pdf";
string destFileName1 = @"C:\QRCodeTesting\bottomRightOutputTemp.pdf";
string destFileName2 = @"C:\QRCodeTesting\bottomRightOutput.bmp";
RasterCodecs codecs = new RasterCodecs();
if (codecs.Options.Pdf.IsEngineInstalled)
{
// Resulting image pixel depth.
codecs.Options.Pdf.Load.DisplayDepth = 24;
codecs.Options.Pdf.Load.GraphicsAlpha = 4;
codecs.Options.Pdf.Load.Password = "";
// Type of font anti-aliasing to use.
codecs.Options.Pdf.Load.TextAlpha = 1;
codecs.Options.Pdf.Load.UseLibFonts = true;
// Horizontal,vertical display resolution in dots per inch.
codecs.Options.RasterizeDocument.Load.XResolution = 150;
codecs.Options.RasterizeDocument.Load.YResolution = 150;
using (RasterImage image = codecs.Load(pdfFile, 0, CodecsLoadByteOrder.BgrOrGray, 1, 1))
{
// Set the PDF version to be v1.4
codecs.Options.Pdf.Save.Version = CodecsRasterPdfVersion.V14;
try
{
// Save the image back as PDF
codecs.Save(image, destFileName1, RasterImageFormat.RasPdf, 24);
}
catch (RasterException ex)
{
if (ex.Code == RasterExceptionCode.FileFormat)
MessageBox.Show(string.Format("Image in file {0} is loaded", destFileName1));
else
{
MessageBox.Show(string.Format("Could not load the file {0}.{1}{2}", destFileName1, Environment.NewLine, ex.Message));
}
}
}
// And load it back before saving it as BMP
using (RasterImage image = codecs.Load(destFileName1))
{
codecs.Save(image, destFileName2, RasterImageFormat.Bmp, image.BitsPerPixel);
writeQRTag(destFileName2);
}
}
else
{
MessageBox.Show("PDF Engine is not found!");
}
// Clean up
codecs.Dispose();
}
QRCode编写方法
private void writeQRTag(string imageFileName)
{
BarcodeEngine engine = new BarcodeEngine();
// Create the image to write the barcodes to
int resolution = 300;
using (RasterImage image = RasterImage.Create((int)(8.5 * resolution), (int)(11.0 * resolution), 1, resolution, RasterColor.FromKnownColor(RasterKnownColor.Red)))
{
// Write two QR barcodes
WriteQRCode(engine.Writer, image, QRBarcodeSymbolModel.Model1AutoSize, "QR Data 1", true);
// Save the image
using (RasterCodecs codecs = new RasterCodecs())
{
codecs.Save(image, imageFileName, RasterImageFormat.CcittGroup4, 1);
}
}
}
答案 0 :(得分:2)
这是来自LEADTOOLS支持的Maen。
我检查了您的代码并注意到以下内容:
1)当您调用RasterImage.Create()方法时,它将创建一个包含空红色图像的新RasterImage对象,您随后将其传递给writeQRTag()函数,然后使用给定的文件名进行保存。 保存时,红色将替换为黑色,因为您使用的文件格式仅支持黑色和白色。由于您正在使用新图像,旧图像将丢失(覆盖)。
如果要在原始文件的图像上写入条形码,则不应创建新图像。相反,您需要使用codecs.Load()使用已加载的图像并在其上写入条形码。
2)代码执行多个加载和保存操作。通常,除非您的应用程序需要不同的文件格式(PDF,BMP和TIFF),否则您不需要这样做。
3)您创建了我们的RasterCodecs对象的不同实例,但实际上只使用其中一个。代码中不需要4个RasterCodecs对象中的3个。
如果您仍然遇到使用我们工具包的代码的问题,可以通过电子邮件将详细信息发送给support@leadtools.com,我们会尽力为您提供帮助。